-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformRecognizer.py
More file actions
69 lines (64 loc) · 3.88 KB
/
formRecognizer.py
File metadata and controls
69 lines (64 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#import credentials from config file
import config
#import azure from recognizer libraries
from azure.ai.formrecognizer import FormRecognizerClient
from azure.ai.formrecognizer import FormTrainingClient
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import ResourceNotFoundError
# create client and authenticate with the endpoint and key
endpoint = config.endpoint
key = config.key
#create client and authenticate with the endpoint and key
# I am using the same key for both the training and the recognizer client
form_recognizer_client = FormRecognizerClient(endpoint, AzureKeyCredential(key))
form_training_client = FormTrainingClient(endpoint, AzureKeyCredential(key))
# myReceiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"
myReceiptUrl = "https://docsinvestreusa.s3.amazonaws.com/Comunicaciones/test-receipt.jpg"
#user form recognizer client to recognize image from myReceiptUrl
poller = form_recognizer_client.begin_recognize_receipts_from_url(myReceiptUrl)
result = poller.result()
#loop through results and extract data from receipt
for receipt in result:
for name, field in receipt.fields.items():
# each field is of type FormField
# label_data is populated if you are using a model trained without labels,
# since the service needs to make predictions for labels if not explicitly given to it.
if name == "MerchantName":
if field.value:
print("Merchant Name: {} has confidence: {}".format(field.value, field.confidence))
elif field.label_data:
print("Merchant Name: {} has confidence: {}".format(field.label_data.text, field.confidence))
if name == "TransactionDate":
if field.value:
print("Transaction Date: {} has confidence: {}".format(field.value, field.confidence))
elif field.label_data:
print("Transaction Date: {} has confidence: {}".format(field.label_data.text, field.confidence))
if name == "Items":
if field.value:
print("Receipt Items:")
for idx, item in enumerate(field.value):
print("...Item #{}".format(idx+1))
item_name = item.value.get("Name")
item_quantity = item.value.get("Quantity")
item_price = item.value.get("Price")
item_total_price = item.value.get("TotalPrice")
if item_name:
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
if item_quantity:
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
if item_price:
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
if item_total_price:
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
elif field.label_data:
print("Receipt Items:")
for idx, item in enumerate(field.value):
print("...Item #{}".format(idx+1))
item_name = item.value.get("Name")
item_quantity = item.value.get("Quantity")
item_price = item.value.get("Price")
item_total_price = item.value.get("TotalPrice")
if item_name:
print("......Item Name: {} has confidence: {}".format(item_name.label_data.text, item_name.confidence))
if item_quantity:
print("......Item Quantity: {} has confidence: {}".format(item_quantity.label))