Skip to content

Commit 3094344

Browse files
authored
[form recognizer] Remove US receipt (#11764)
1 parent e49027c commit 3094344

17 files changed

+414
-646
lines changed

sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
`CustomFormModel` and `CustomFormModelInfo` models.
2323
- `models` property of `CustomFormModel` is renamed to `submodels`
2424
- `CustomFormSubModel` is renamed to `CustomFormSubmodel`
25+
- Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details.
26+
- Removed `USReceiptItem`. To see how to access the individual items on a receipt, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details.
27+
- Removed `ReceiptType` and the `receipt_type` property from `RecognizedReceipt`. See the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details.
2528

2629
**New features**
2730

sdk/formrecognizer/azure-ai-formrecognizer/README.md

+12-16
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ for cell in table.cells:
210210
```
211211

212212
### Recognize Receipts
213-
Recognize data from USA sales receipts using a prebuilt model.
213+
Recognize data from USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields the service returns for a recognized receipt.
214214

215215
```python
216216
from azure.ai.formrecognizer import FormRecognizerClient
@@ -227,21 +227,16 @@ with open("<path to your receipt>", "rb") as fd:
227227
poller = form_recognizer_client.begin_recognize_receipts(receipt)
228228
result = poller.result()
229229

230-
r = result[0]
231-
print("Receipt contained the following values with confidences: ")
232-
print("Receipt Type: {} has confidence: {}".format(r.receipt_type.type, r.receipt_type.confidence))
233-
print("Merchant Name: {} has confidence: {}".format(r.merchant_name.value, r.merchant_name.confidence))
234-
print("Transaction Date: {} has confidence: {}".format(r.transaction_date.value, r.transaction_date.confidence))
235-
print("Receipt items:")
236-
for item in r.receipt_items:
237-
print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence))
238-
print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence))
239-
print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence))
240-
print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence))
241-
print("Subtotal: {} has confidence: {}".format(r.subtotal.value, r.subtotal.confidence))
242-
print("Tax: {} has confidence: {}".format(r.tax.value, r.tax.confidence))
243-
print("Tip: {} has confidence: {}".format(r.tip.value, r.tip.confidence))
244-
print("Total: {} has confidence: {}".format(r.total.value, r.total.confidence))
230+
for receipt in result:
231+
for name, field in receipt.fields.items():
232+
if name == "Items":
233+
print("Receipt Items:")
234+
for idx, items in enumerate(field.value):
235+
print("...Item #{}".format(idx))
236+
for item_name, item in items.value.items():
237+
print("......{}: {} has confidence {}".format(item_name, item.value, item.confidence))
238+
else:
239+
print("{}: {} has confidence {}".format(name, field.value, field.confidence))
245240
```
246241

247242
### Train a model
@@ -439,6 +434,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
439434
[cognitive_authentication_aad]: https://docs.microsoft.com/azure/cognitive-services/authentication#authenticate-with-azure-active-directory
440435
[azure_identity_credentials]: ../../identity/azure-identity#credentials
441436
[default_azure_credential]: ../../identity/azure-identity#defaultazurecredential
437+
[service_recognize_receipt]: https://westus2.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v2-preview/operations/GetAnalyzeReceiptResult
442438

443439
[cla]: https://cla.microsoft.com
444440
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py

-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
TrainingStatus,
1515
CustomFormModelStatus,
1616
FormContentType,
17-
USReceipt,
18-
ReceiptType,
19-
USReceiptItem,
2017
FormTable,
2118
FormTableCell,
2219
TrainingDocumentInfo,
@@ -45,9 +42,6 @@
4542
'CustomFormModelStatus',
4643
'FormContentType',
4744
'FormContent',
48-
'USReceipt',
49-
'ReceiptType',
50-
'USReceiptItem',
5145
'FormTable',
5246
'FormTableCell',
5347
'TrainingDocumentInfo',

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from azure.core.polling.base_polling import LROBasePolling
1818
from ._generated._form_recognizer_client import FormRecognizerClient as FormRecognizer
1919
from ._response_handlers import (
20-
prepare_us_receipt,
20+
prepare_receipt,
2121
prepare_content_result,
2222
prepare_form_result
2323
)
@@ -74,7 +74,7 @@ def __init__(self, endpoint, credential, **kwargs):
7474

7575
def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument
7676
analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response)
77-
return prepare_us_receipt(analyze_result)
77+
return prepare_receipt(analyze_result)
7878

7979
@distributed_trace
8080
def begin_recognize_receipts(self, receipt, **kwargs):

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py

+2-134
Original file line numberDiff line numberDiff line change
@@ -195,82 +195,13 @@ class RecognizedReceipt(RecognizedForm):
195195
:ivar list[~azure.ai.formrecognizer.FormPage] pages:
196196
A list of pages recognized from the input document. Contains lines,
197197
words, tables and page metadata.
198-
:ivar ~azure.ai.formrecognizer.ReceiptType receipt_type:
199-
The reciept type and confidence.
200-
:ivar str receipt_locale: Defaults to "en-US".
201198
"""
202-
def __init__(self, **kwargs):
203-
super(RecognizedReceipt, self).__init__(**kwargs)
204-
self.receipt_type = kwargs.get("receipt_type", None)
205-
self.receipt_locale = kwargs.get("receipt_locale", "en-US")
206199

207200
def __repr__(self):
208-
return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={}, " \
209-
"receipt_type={}, receipt_locale={})".format(
210-
self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages),
211-
repr(self.receipt_type), self.receipt_locale
201+
return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={})".format(
202+
self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages)
212203
)[:1024]
213204

214-
class USReceipt(RecognizedReceipt): # pylint: disable=too-many-instance-attributes
215-
"""Extracted fields found on the US sales receipt. Provides
216-
attributes for accessing common fields present in US sales receipts.
217-
218-
:ivar ~azure.ai.formrecognizer.FormField merchant_address:
219-
The address of the merchant.
220-
:ivar ~azure.ai.formrecognizer.FormField merchant_name:
221-
The name of the merchant.
222-
:ivar ~azure.ai.formrecognizer.FormField merchant_phone_number:
223-
The phone number associated with the merchant.
224-
:ivar list[~azure.ai.formrecognizer.USReceiptItem] receipt_items:
225-
The purchased items found on the receipt.
226-
:ivar ~azure.ai.formrecognizer.FormField subtotal:
227-
The subtotal found on the receipt
228-
:ivar ~azure.ai.formrecognizer.FormField tax:
229-
The tax value found on the receipt.
230-
:ivar ~azure.ai.formrecognizer.FormField tip:
231-
The tip value found on the receipt.
232-
:ivar ~azure.ai.formrecognizer.FormField total:
233-
The total amount found on the receipt.
234-
:ivar ~azure.ai.formrecognizer.FormField transaction_date:
235-
The transaction date of the sale.
236-
:ivar ~azure.ai.formrecognizer.FormField transaction_time:
237-
The transaction time of the sale.
238-
:ivar fields:
239-
A dictionary of the fields found on the receipt.
240-
:vartype fields: dict[str, ~azure.ai.formrecognizer.FormField]
241-
:ivar ~azure.ai.formrecognizer.FormPageRange page_range:
242-
The first and last page number of the input receipt.
243-
:ivar list[~azure.ai.formrecognizer.FormPage] pages:
244-
Contains page metadata such as page width, length, text angle, unit.
245-
If `include_text_content=True` is passed, contains a list
246-
of extracted text lines for each page in the input document.
247-
:ivar str form_type: The type of form.
248-
"""
249-
250-
def __init__(self, **kwargs):
251-
super(USReceipt, self).__init__(**kwargs)
252-
self.merchant_address = kwargs.get("merchant_address", None)
253-
self.merchant_name = kwargs.get("merchant_name", None)
254-
self.merchant_phone_number = kwargs.get("merchant_phone_number", None)
255-
self.receipt_items = kwargs.get("receipt_items", None)
256-
self.subtotal = kwargs.get("subtotal", None)
257-
self.tax = kwargs.get("tax", None)
258-
self.tip = kwargs.get("tip", None)
259-
self.total = kwargs.get("total", None)
260-
self.transaction_date = kwargs.get("transaction_date", None)
261-
self.transaction_time = kwargs.get("transaction_time", None)
262-
263-
def __repr__(self):
264-
return "USReceipt(merchant_address={}, merchant_name={}, merchant_phone_number={}, " \
265-
"receipt_type={}, receipt_items={}, subtotal={}, tax={}, tip={}, total={}, "\
266-
"transaction_date={}, transaction_time={}, fields={}, page_range={}, pages={}, " \
267-
"form_type={}, receipt_locale={})".format(
268-
repr(self.merchant_address), repr(self.merchant_name), repr(self.merchant_phone_number),
269-
repr(self.receipt_type), repr(self.receipt_items), repr(self.subtotal), repr(self.tax),
270-
repr(self.tip), repr(self.total), repr(self.transaction_date), repr(self.transaction_time),
271-
repr(self.fields), repr(self.page_range), repr(self.pages), self.form_type, self.receipt_locale
272-
)[:1024]
273-
274205

275206
class FormField(object):
276207
"""Represents a field recognized in an input form.
@@ -513,69 +444,6 @@ def __repr__(self):
513444
)[:1024]
514445

515446

516-
class ReceiptType(object):
517-
"""The type of the analyzed US receipt and the confidence
518-
value of that type.
519-
520-
:ivar str type: The type of the receipt. For example, "Itemized",
521-
"CreditCard", "Gas", "Parking", "Gas", "Other".
522-
:ivar float confidence:
523-
Measures the degree of certainty of the recognition result. Value is between [0.0, 1.0].
524-
"""
525-
526-
def __init__(self, **kwargs):
527-
self.type = kwargs.get("type", None)
528-
self.confidence = kwargs.get("confidence", None)
529-
530-
@classmethod
531-
def _from_generated(cls, item):
532-
return cls(
533-
type=item.value_string,
534-
confidence=adjust_confidence(item.confidence)) if item else None
535-
536-
def __repr__(self):
537-
return "ReceiptType(type={}, confidence={})".format(self.type, self.confidence)[:1024]
538-
539-
540-
class USReceiptItem(object):
541-
"""A receipt item on a US sales receipt.
542-
Contains the item name, quantity, price, and total price.
543-
544-
:ivar ~azure.ai.formrecognizer.FormField name:
545-
The name of the item.
546-
:ivar ~azure.ai.formrecognizer.FormField quantity:
547-
The quantity associated with this item.
548-
:ivar ~azure.ai.formrecognizer.FormField price:
549-
The price of a single unit of this item.
550-
:ivar ~azure.ai.formrecognizer.FormField total_price:
551-
The total price of this item, taking the quantity into account.
552-
"""
553-
554-
def __init__(self, **kwargs):
555-
self.name = kwargs.get("name", None)
556-
self.quantity = kwargs.get("quantity", None)
557-
self.price = kwargs.get("price", None)
558-
self.total_price = kwargs.get("total_price", None)
559-
560-
@classmethod
561-
def _from_generated(cls, items, read_result):
562-
try:
563-
receipt_item = items.value_array
564-
return [cls(
565-
name=FormField._from_generated("Name", item.value_object.get("Name"), read_result),
566-
quantity=FormField._from_generated("Quantity", item.value_object.get("Quantity"), read_result),
567-
price=FormField._from_generated("Price", item.value_object.get("Price"), read_result),
568-
total_price=FormField._from_generated("TotalPrice", item.value_object.get("TotalPrice"), read_result),
569-
) for item in receipt_item]
570-
except AttributeError:
571-
return []
572-
573-
def __repr__(self):
574-
return "USReceiptItem(name={}, quantity={}, price={}, total_price={})".format(
575-
repr(self.name), repr(self.quantity), repr(self.price), repr(self.total_price)
576-
)[:1024]
577-
578-
579447
class FormTable(object):
580448
"""Information about the extracted table contained on a page.
581449

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py

+6-37
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,33 @@
77
# pylint: disable=protected-access
88

99
from ._models import (
10-
USReceipt,
11-
ReceiptType,
1210
FormField,
13-
USReceiptItem,
1411
FormPage,
1512
FormLine,
1613
FormTable,
1714
FormTableCell,
1815
FormPageRange,
19-
RecognizedForm
16+
RecognizedForm,
17+
RecognizedReceipt
2018
)
2119

2220

23-
def prepare_us_receipt(response):
21+
def prepare_receipt(response):
2422
receipts = []
2523
read_result = response.analyze_result.read_results
2624
document_result = response.analyze_result.document_results
2725
form_page = FormPage._from_generated(read_result)
2826

2927
for page in document_result:
3028
if page.fields is None:
31-
receipt = USReceipt(
29+
receipt = RecognizedReceipt(
3230
page_range=FormPageRange(first_page_number=page.page_range[0], last_page_number=page.page_range[1]),
3331
pages=form_page[page.page_range[0]-1:page.page_range[1]],
3432
form_type=page.doc_type,
3533
)
3634
receipts.append(receipt)
3735
continue
38-
receipt = USReceipt(
39-
merchant_address=FormField._from_generated(
40-
"MerchantAddress", page.fields.get("MerchantAddress"), read_result
41-
),
42-
merchant_name=FormField._from_generated(
43-
"MerchantName", page.fields.get("MerchantName"), read_result
44-
),
45-
merchant_phone_number=FormField._from_generated(
46-
"MerchantPhoneNumber",
47-
page.fields.get("MerchantPhoneNumber"),
48-
read_result,
49-
),
50-
receipt_type=ReceiptType._from_generated(page.fields.get("ReceiptType")),
51-
receipt_items=USReceiptItem._from_generated(
52-
page.fields.get("Items"), read_result
53-
),
54-
subtotal=FormField._from_generated(
55-
"Subtotal", page.fields.get("Subtotal"), read_result
56-
),
57-
tax=FormField._from_generated("Tax", page.fields.get("Tax"), read_result),
58-
tip=FormField._from_generated("Tip", page.fields.get("Tip"), read_result),
59-
total=FormField._from_generated(
60-
"Total", page.fields.get("Total"), read_result
61-
),
62-
transaction_date=FormField._from_generated(
63-
"TransactionDate", page.fields.get("TransactionDate"), read_result
64-
),
65-
transaction_time=FormField._from_generated(
66-
"TransactionTime", page.fields.get("TransactionTime"), read_result
67-
),
36+
receipt = RecognizedReceipt(
6837
page_range=FormPageRange(
6938
first_page_number=page.page_range[0], last_page_number=page.page_range[1]
7039
),
@@ -73,7 +42,7 @@ def prepare_us_receipt(response):
7342
fields={
7443
key: FormField._from_generated(key, value, read_result)
7544
for key, value in page.fields.items()
76-
},
45+
}
7746
)
7847

7948
receipts.append(receipt)

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from azure.core.polling.async_base_polling import AsyncLROBasePolling
1818
from .._generated.aio._form_recognizer_client_async import FormRecognizerClient as FormRecognizer
1919
from .._response_handlers import (
20-
prepare_us_receipt,
20+
prepare_receipt,
2121
prepare_content_result,
2222
prepare_form_result
2323
)
@@ -84,7 +84,7 @@ def __init__(
8484

8585
def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument
8686
analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response)
87-
return prepare_us_receipt(analyze_result)
87+
return prepare_receipt(analyze_result)
8888

8989
@distributed_trace_async
9090
async def recognize_receipts(

0 commit comments

Comments
 (0)