Skip to content

Commit cd52156

Browse files
✨ add support for Barcode Reader V1
1 parent 6187fbe commit cd52156

File tree

9 files changed

+139
-1
lines changed

9 files changed

+139
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from mindee import Client, documents
2+
3+
# Init a new client
4+
mindee_client = Client(api_key="my-api-key")
5+
6+
# Load a file from disk
7+
input_doc = mindee_client.doc_from_path("/path/to/the/file.ext")
8+
9+
# Parse the Barcode Reader by passing the appropriate type
10+
result = input_doc.parse(documents.TypeBarcodeReaderV1)
11+
12+
# Print a brief summary of the parsed data
13+
print(result.document)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Barcode Reader V1
2+
-----------------
3+
4+
**Sample Code:**
5+
6+
.. literalinclude:: /extras/code_samples/barcode_reader_v1.txt
7+
:language: Python
8+
9+
.. autoclass:: mindee.documents.BarcodeReaderV1
10+
:members:

mindee/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class CommandConfig(Generic[TypeDoc]):
2727
help="Custom document type from API builder",
2828
doc_class=documents.TypeCustomV1,
2929
),
30+
"barcode-reader": CommandConfig(
31+
help="Barcode Reader",
32+
doc_class=documents.TypeBarcodeReaderV1,
33+
),
3034
"invoice": CommandConfig(
3135
help="Invoice",
3236
doc_class=documents.TypeInvoiceV4,

mindee/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ def _standard_doc_config(
374374

375375
def _init_default_endpoints(self) -> None:
376376
configs: List[ConfigSpec] = [
377+
ConfigSpec(
378+
doc_class=documents.BarcodeReaderV1,
379+
url_name="barcode_reader",
380+
version="1",
381+
),
377382
ConfigSpec(
378383
doc_class=documents.InvoiceV3,
379384
url_name="invoices",

mindee/documents/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from mindee.documents import eu, fr, us
2+
from mindee.documents.barcode_reader import BarcodeReaderV1, TypeBarcodeReaderV1
23
from mindee.documents.cropper import CropperV1, TypeCropperV1
34
from mindee.documents.custom import CustomV1, TypeCustomV1
45
from mindee.documents.financial_document import (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .barcode_reader_v1 import BarcodeReaderV1, TypeBarcodeReaderV1
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from typing import List, Optional, TypeVar
2+
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
4+
from mindee.fields.text import TextField
5+
6+
7+
class BarcodeReaderV1(Document):
8+
"""Barcode Reader v1 prediction results."""
9+
10+
codes_1d: List[TextField]
11+
"""List of decoded 1D barcodes."""
12+
codes_2d: List[TextField]
13+
"""List of decoded 2D barcodes."""
14+
15+
def __init__(
16+
self,
17+
api_prediction=None,
18+
input_source=None,
19+
page_n: Optional[int] = None,
20+
):
21+
"""
22+
Barcode Reader v1 prediction results.
23+
24+
:param api_prediction: Raw prediction from HTTP response
25+
:param input_source: Input object
26+
:param page_n: Page number for multi pages pdf input
27+
"""
28+
super().__init__(
29+
input_source=input_source,
30+
document_type="barcode_reader",
31+
api_prediction=api_prediction,
32+
page_n=page_n,
33+
)
34+
self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n)
35+
36+
def _build_from_api_prediction(
37+
self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None
38+
) -> None:
39+
"""
40+
Build the object from the prediction API JSON.
41+
42+
:param api_prediction: Raw prediction from HTTP response
43+
:param page_n: Page number
44+
"""
45+
self.codes_1d = [
46+
TextField(prediction, page_id=page_n)
47+
for prediction in api_prediction["codes_1d"]
48+
]
49+
self.codes_2d = [
50+
TextField(prediction, page_id=page_n)
51+
for prediction in api_prediction["codes_2d"]
52+
]
53+
54+
def __str__(self) -> str:
55+
codes_1d = f"\n { ' ' * 13 }".join(
56+
[str(item) for item in self.codes_1d],
57+
)
58+
codes_2d = f"\n { ' ' * 13 }".join(
59+
[str(item) for item in self.codes_2d],
60+
)
61+
return clean_out_string(
62+
"Barcode Reader V1 Prediction\n"
63+
"============================\n"
64+
f":Filename: {self.filename or ''}\n"
65+
f":Barcodes 1D: {codes_1d}\n"
66+
f":Barcodes 2D: {codes_2d}\n"
67+
)
68+
69+
70+
TypeBarcodeReaderV1 = TypeVar(
71+
"TypeBarcodeReaderV1",
72+
bound=BarcodeReaderV1,
73+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import json
2+
3+
import pytest
4+
5+
from mindee.documents import BarcodeReaderV1
6+
7+
BARCODE_READER_DATA_DIR = "./tests/data/products/barcode_reader"
8+
FILE_PATH_BARCODE_READER_V1_COMPLETE = f"{ BARCODE_READER_DATA_DIR }/response_v1/complete.json"
9+
FILE_PATH_BARCODE_READER_V1_EMPTY = f"{ BARCODE_READER_DATA_DIR }/response_v1/empty.json"
10+
11+
12+
@pytest.fixture
13+
def barcode_reader_v1_doc() -> BarcodeReaderV1:
14+
json_data = json.load(open(FILE_PATH_BARCODE_READER_V1_COMPLETE, encoding="utf-8"))
15+
return BarcodeReaderV1(json_data["document"]["inference"], page_n=None)
16+
17+
18+
@pytest.fixture
19+
def barcode_reader_v1_doc_empty() -> BarcodeReaderV1:
20+
json_data = json.load(open(FILE_PATH_BARCODE_READER_V1_EMPTY, encoding="utf-8"))
21+
return BarcodeReaderV1(json_data["document"]["inference"], page_n=None)
22+
23+
24+
@pytest.fixture
25+
def barcode_reader_v1_page0():
26+
json_data = json.load(open(FILE_PATH_BARCODE_READER_V1_COMPLETE, encoding="utf-8"))
27+
return BarcodeReaderV1(json_data["document"]["inference"]["pages"][0], page_n=0)
28+
29+
30+
def test_empty_doc_constructor(barcode_reader_v1_doc_empty):
31+
assert len(barcode_reader_v1_doc_empty.codes_1d) == 0
32+
assert len(barcode_reader_v1_doc_empty.codes_2d) == 0

tests/documents/us/test_w9_v1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ def test_empty_doc_constructor(w9_v1_doc_empty):
4040
assert w9_v1_doc_empty.signature_position.value is None
4141
assert w9_v1_doc_empty.signature_date_position.value is None
4242
assert w9_v1_doc_empty.tax_classification_llc.value is None
43-

0 commit comments

Comments
 (0)