|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for |
| 6 | +# license information. |
| 7 | +# -------------------------------------------------------------------------- |
| 8 | + |
| 9 | +""" |
| 10 | +FILE: sample_strongly_typed_recognized_form_async.py |
| 11 | +
|
| 12 | +DESCRIPTION: |
| 13 | + This sample demonstrates how to use the fields in your recognized forms to create an object with |
| 14 | + strongly-typed fields. The pre-trained receipt method will be used to illustrate this sample, but |
| 15 | + note that a similar approach can be used for any custom form as long as you properly update the |
| 16 | + fields' names and types. |
| 17 | +
|
| 18 | + See fields found on a receipt here: |
| 19 | + https://aka.ms/azsdk/python/formrecognizer/receiptfields |
| 20 | +
|
| 21 | +USAGE: |
| 22 | + python sample_strongly_typed_recognized_form_async.py |
| 23 | +
|
| 24 | + Set the environment variables with your own values before running the sample: |
| 25 | + 1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Cognitive Services resource. |
| 26 | + 2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | +import asyncio |
| 31 | +from azure.ai.formrecognizer import FormField |
| 32 | + |
| 33 | + |
| 34 | +class Receipt(object): |
| 35 | + """Creates a strongly-typed Receipt class from the fields returned in a RecognizedForm. |
| 36 | + If a specific field is not found on the receipt, it will return None. |
| 37 | +
|
| 38 | + See fields found on a receipt here: |
| 39 | + https://aka.ms/azsdk/python/formrecognizer/receiptfields |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, form): |
| 43 | + self.receipt_type = form.fields.get("ReceiptType", FormField()) |
| 44 | + self.merchant_name = form.fields.get("MerchantName", FormField()) |
| 45 | + self.merchant_address = form.fields.get("MerchantAddress", FormField()) |
| 46 | + self.merchant_phone_number = form.fields.get("MerchantPhoneNumber", FormField()) |
| 47 | + self.receipt_items = self.convert_to_receipt_item(form.fields.get("Items", FormField())) |
| 48 | + self.subtotal = form.fields.get("Subtotal", FormField()) |
| 49 | + self.tax = form.fields.get("Tax", FormField()) |
| 50 | + self.tip = form.fields.get("Tip", FormField()) |
| 51 | + self.total = form.fields.get("Total", FormField()) |
| 52 | + self.transaction_date = form.fields.get("TransactionDate", FormField()) |
| 53 | + self.transaction_time = form.fields.get("TransactionTime", FormField()) |
| 54 | + |
| 55 | + def convert_to_receipt_item(self, items): |
| 56 | + """Converts Items in a receipt to a list of strongly-typed ReceiptItem |
| 57 | + """ |
| 58 | + if items is None: |
| 59 | + return [] |
| 60 | + return [ReceiptItem(item) for item in items.value] |
| 61 | + |
| 62 | + |
| 63 | +class ReceiptItem(object): |
| 64 | + """Creates a strongly-typed ReceiptItem for every receipt item found in a RecognizedForm |
| 65 | + """ |
| 66 | + |
| 67 | + def __init__(self, item): |
| 68 | + self.name = item.value.get("Name", FormField()) |
| 69 | + self.quantity = item.value.get("Quantity", FormField()) |
| 70 | + self.price = item.value.get("Price", FormField()) |
| 71 | + self.total_price = item.value.get("TotalPrice", FormField()) |
| 72 | + |
| 73 | + |
| 74 | +class StronglyTypedRecognizedFormSampleAsync(object): |
| 75 | + |
| 76 | + async def strongly_typed_receipt_async(self): |
| 77 | + path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/receipt/contoso-allinone.jpg")) |
| 78 | + |
| 79 | + from azure.core.credentials import AzureKeyCredential |
| 80 | + from azure.ai.formrecognizer.aio import FormRecognizerClient |
| 81 | + |
| 82 | + endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] |
| 83 | + key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] |
| 84 | + |
| 85 | + async with FormRecognizerClient( |
| 86 | + endpoint=endpoint, credential=AzureKeyCredential(key) |
| 87 | + ) as form_recognizer_client: |
| 88 | + |
| 89 | + with open(path_to_sample_forms, "rb") as f: |
| 90 | + poller = await form_recognizer_client.begin_recognize_receipts(receipt=f) |
| 91 | + receipts = await poller.result() |
| 92 | + |
| 93 | + for receipt in receipts: |
| 94 | + receipt = Receipt(receipt) |
| 95 | + print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.value, receipt.receipt_type.confidence)) |
| 96 | + print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) |
| 97 | + print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) |
| 98 | + print("Receipt items:") |
| 99 | + for item in receipt.receipt_items: |
| 100 | + print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) |
| 101 | + print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) |
| 102 | + print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) |
| 103 | + print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) |
| 104 | + print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) |
| 105 | + print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) |
| 106 | + print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) |
| 107 | + print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) |
| 108 | + |
| 109 | + |
| 110 | +async def main(): |
| 111 | + sample = StronglyTypedRecognizedFormSampleAsync() |
| 112 | + await sample.strongly_typed_receipt_async() |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + loop = asyncio.get_event_loop() |
| 117 | + loop.run_until_complete(main()) |
0 commit comments