def test_polling_interval(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): client = FormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential(formrecognizer_test_api_key), polling_interval=7) self.assertEqual(client._client._config.polling_interval, 7) poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg, polling_interval=6) poller.wait() self.assertEqual(poller._polling_method._timeout, 6) poller2 = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) poller2.wait() self.assertEqual(poller2._polling_method._timeout, 7) # goes back to client default
def test_get_form_training_client(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): transport = RequestsTransport() frc = FormRecognizerClient(endpoint=form_recognizer_account, credential=AzureKeyCredential(form_recognizer_account_key), transport=transport) with frc: poller = frc.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result() assert transport.session is not None with frc.get_form_training_client() as ftc: assert transport.session is not None properties = ftc.get_account_properties() poller = frc.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result() assert transport.session is not None
def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) poller = client.begin_recognize_receipts_from_url(self.receipt_url_png) result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') self.assertEqual(receipt.subtotal.value, 1098.99) self.assertEqual(receipt.tax.value, 104.4) self.assertEqual(receipt.total.value, 1203.39) self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page, 1) self.assertEqual(receipt.page_range.last_page, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized')
def test_mock_quota_exceeded_403(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): response = mock.Mock(status_code=403, headers={ "Retry-After": 186688, "Content-Type": "application/json" }, reason="Bad Request") response.text = lambda encoding=None: json.dumps({ "error": { "code": "403", "message": "Out of call volume quota for FormRecognizer F0 pricing tier. " "Please retry after 1 day. To increase your call volume switch to a paid tier." } }) response.content_type = "application/json" transport = mock.Mock(send=lambda request, **kwargs: response) client = FormRecognizerClient( formrecognizer_test_endpoint, AzureKeyCredential(formrecognizer_test_api_key), transport=transport) with pytest.raises(HttpResponseError) as e: poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg) assert e.value.status_code == 403 assert e.value.error.message == 'Out of call volume quota for FormRecognizer F0 pricing tier. Please retry after 1 day. To increase your call volume switch to a paid tier.'
def test_receipt_url_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg, include_text_content=True) result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): if field not in [ "receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale" ]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page) for field, value in receipt.fields.items(): self.assertTextContentHasValues(value.value_data.text_content, receipt.page_range.first_page)
def test_receipt_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) initial_poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg) cont_token = initial_poller.continuation_token() poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg, continuation_token=cont_token) result = poller.result() self.assertIsNotNone(result) initial_poller.wait( ) # necessary so azure-devtools doesn't throw assertion error
def test_receipt_url_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) responses = [] def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) extracted_receipt = prepare_us_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) poller = client.begin_recognize_receipts_from_url( url=self.receipt_url_jpg, include_text_content=True, cls=callback ) result = poller.result() raw_response = responses[0] returned_model = responses[1] receipt = returned_model[0] actual = raw_response.analyze_result.document_results[0].fields read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results # check hardcoded values self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantPhoneNumber"), actual.get("MerchantPhoneNumber"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Subtotal"), actual.get("Subtotal"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Tax"), actual.get("Tax"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Tip"), actual.get("Tip"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Total"), actual.get("Total"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("TransactionDate"), actual.get("TransactionDate"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("TransactionTime"), actual.get("TransactionTime"), read_results) # check page range self.assertEqual(receipt.page_range.first_page, document_results[0].page_range[0]) self.assertEqual(receipt.page_range.last_page, document_results[0].page_range[1]) # check receipt type self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results)
def test_receipt_url_auth_bad_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): client = FormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential("xxxx")) with self.assertRaises(ClientAuthenticationError): poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg)
def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) poller = client.begin_recognize_receipts_from_url(self.multipage_url_pdf, include_text_content=True) result = poller.result() self.assertEqual(len(result), 3) receipt = result[0] self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') self.assertEqual(receipt.subtotal.value, 300.0) self.assertEqual(receipt.total.value, 430.0) self.assertEqual(receipt.page_range.first_page, 1) self.assertEqual(receipt.page_range.last_page, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') receipt = result[2] self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') self.assertEqual(receipt.subtotal.value, 3000.0) self.assertEqual(receipt.total.value, 1000.0) self.assertEqual(receipt.page_range.first_page, 3) self.assertEqual(receipt.page_range.last_page, 3) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized')
def test_active_directory_auth(self): token = self.generate_oauth_token() endpoint = self.get_oauth_endpoint() client = FormRecognizerClient(endpoint, token) poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result() self.assertIsNotNone(result)
def test_receipt_url_auth_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result()
def test_receipt_url_bad_endpoint(self, formrecognizer_test_endpoint, formrecognizer_test_api_key): with self.assertRaises(ServiceRequestError): client = FormRecognizerClient( "http://notreal.azure.com", AzureKeyCredential(formrecognizer_test_api_key)) poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg)
def test_receipt_url_auth_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential("xxxx")) with self.assertRaises(ClientAuthenticationError): poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg)
def test_receipt_url_pass_stream(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) with open(self.receipt_png, "rb") as receipt: with self.assertRaises(HttpResponseError): poller = client.begin_recognize_receipts_from_url(receipt)
def test_receipt_bad_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) with self.assertRaises(HttpResponseError): poller = client.begin_recognize_receipts_from_url( "https://badurl.jpg")
def recognize_receipts_from_url(self): # [START recognize_receipts_from_url] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer import FormRecognizerClient endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] form_recognizer_client = FormRecognizerClient( endpoint=endpoint, credential=AzureKeyCredential(key) ) url = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png" poller = form_recognizer_client.begin_recognize_receipts_from_url(receipt_url=url) receipts = poller.result() for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx+1)) receipt_type = receipt.fields.get("ReceiptType") if receipt_type: print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) merchant_name = receipt.fields.get("MerchantName") if merchant_name: print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) transaction_date = receipt.fields.get("TransactionDate") if transaction_date: print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) if receipt.fields.get("Items"): print("Receipt items:") for idx, item in enumerate(receipt.fields.get("Items").value): print("...Item #{}".format(idx+1)) item_name = item.value.get("Name") if item_name: print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) item_quantity = item.value.get("Quantity") if item_quantity: print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) item_price = item.value.get("Price") if item_price: print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) item_total_price = item.value.get("TotalPrice") if item_total_price: print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) subtotal = receipt.fields.get("Subtotal") if subtotal: print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) tax = receipt.fields.get("Tax") if tax: print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) tip = receipt.fields.get("Tip") if tip: print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) total = receipt.fields.get("Total") if total: print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------")
def authentication_with_api_key_credential_form_recognizer_client(self): # [START create_fr_client_with_key] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer import FormRecognizerClient endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] form_recognizer_client = FormRecognizerClient(endpoint, AzureKeyCredential(key)) # [END create_fr_client_with_key] poller = form_recognizer_client.begin_recognize_receipts_from_url(self.url) receipt = poller.result()
def authentication_with_azure_active_directory_form_recognizer_client(self): """DefaultAzureCredential will use the values from these environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET """ # [START create_fr_client_with_aad] from azure.ai.formrecognizer import FormRecognizerClient from azure.identity import DefaultAzureCredential endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] credential = DefaultAzureCredential() form_recognizer_client = FormRecognizerClient(endpoint, credential) # [END create_fr_client_with_aad] poller = form_recognizer_client.begin_recognize_receipts_from_url(self.url) receipt = poller.result()
def recognize_receipts_from_url(self): # [START recognize_receipts_from_url] from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer import FormRecognizerClient endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"] key = os.environ["AZURE_FORM_RECOGNIZER_KEY"] form_recognizer_client = FormRecognizerClient( endpoint=endpoint, credential=AzureKeyCredential(key)) url = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png" poller = form_recognizer_client.begin_recognize_receipts_from_url( receipt_url=url) receipts = poller.result() for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) print("Receipt Type: {} has confidence: {}".format( receipt.receipt_type.type, receipt.receipt_type.confidence)) print("Merchant Name: {} has confidence: {}".format( receipt.merchant_name.value, receipt.merchant_name.confidence)) print("Transaction Date: {} has confidence: {}".format( receipt.transaction_date.value, receipt.transaction_date.confidence)) print("Receipt items:") for item in receipt.receipt_items: print("...Item Name: {} has confidence: {}".format( item.name.value, item.name.confidence)) print("...Item Quantity: {} has confidence: {}".format( item.quantity.value, item.quantity.confidence)) print("...Individual Item Price: {} has confidence: {}".format( item.price.value, item.price.confidence)) print("...Total Item Price: {} has confidence: {}".format( item.total_price.value, item.total_price.confidence)) print("Subtotal: {} has confidence: {}".format( receipt.subtotal.value, receipt.subtotal.confidence)) print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) print("Total: {} has confidence: {}".format( receipt.total.value, receipt.total.confidence)) print("--------------------------------------")
def recognize_receipts_from_url(self): # This is your client - a one entry point to working with the resource form_recognizer_client = FormRecognizerClient( endpoint = "https://receiptformanalyzer.cognitiveservices.azure.com/", credential=AzureKeyCredential("KEY_CREDENTIAL") ) # This is the receipt we will be recognizing - you can swap it with any other receipt url url = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png" # Here we start the recognition and poll to see if we have results available poller = form_recognizer_client.begin_recognize_receipts_from_url(receipt_url=url) receipts = poller.result() # We know we sent only one receipt so we get the first (and only) item in the list of returned results recognized_receipt = receipts[0].fields # Let's see what we recognized print("Receipt Type: {}".format(recognized_receipt.get("ReceiptType").value)) print("Merchant Name: {}".format(recognized_receipt.get("MerchantName").value)) print("Transaction Date: {}".format(recognized_receipt.get("TransactionDate").value)) print("Receipt items:") for idx, item in enumerate(recognized_receipt.get("Items").value): print("...Item #{}".format(idx+1)) item_name = item.value.get("Name") if item_name: print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) item_quantity = item.value.get("Quantity") if item_quantity: print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) item_price = item.value.get("Price") if item_price: print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) item_total_price = item.value.get("TotalPrice") if item_total_price: print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) print("Subtotal: {}".format(recognized_receipt.get("Subtotal").value)) print("Tax: {}".format(recognized_receipt.get("Tax").value)) print("Total: {}".format(recognized_receipt.get("Total").value)) print("--------------------------------------")
def test_logging_info_fr_client(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) mock_handler = MockHandler() logger = logging.getLogger("azure") logger.addHandler(mock_handler) logger.setLevel(logging.INFO) poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result() for message in mock_handler.messages: if message.levelname == "INFO": # not able to use json.loads here. At INFO level only API key should be REDACTED if message.message.find("Ocp-Apim-Subscription-Key") != -1: assert message.message.find("REDACTED") != -1 else: assert message.message.find("REDACTED") == -1
def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] self.assertEqual( receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') self.assertEqual( receipt.fields.get("MerchantName").value, 'Contoso Contoso') self.assertEqual( receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) self.assertEqual(receipt.fields.get("Tax").value, 1.17) self.assertEqual(receipt.fields.get("Tip").value, 1.63) self.assertEqual(receipt.fields.get("Total").value, 14.5) self.assertEqual( receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) self.assertEqual( receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) receipt_type = receipt.fields.get("ReceiptType") self.assertIsNotNone(receipt_type.confidence) self.assertEqual(receipt_type.value, 'Itemized') self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False)
def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) responses = [] def callback(raw_response, _, headers): analyze_result = client._client._deserialize( AnalyzeOperationResult, raw_response) extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) poller = client.begin_recognize_receipts_from_url( self.multipage_url_pdf, include_text_content=True, cls=callback) result = poller.result() raw_response = responses[0] returned_model = responses[1] actual = raw_response.analyze_result.document_results read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results page_results = raw_response.analyze_result.page_results # check hardcoded values for receipt, actual in zip(returned_model, actual): if not actual.fields: # second page is blank continue # check dict values self.assertFormFieldTransformCorrect( receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect( receipt.fields.get("MerchantName"), actual.fields.get("MerchantName"), read_results) self.assertFormFieldTransformCorrect( receipt.fields.get("MerchantPhoneNumber"), actual.fields.get("MerchantPhoneNumber"), read_results) self.assertFormFieldTransformCorrect( receipt.fields.get("Subtotal"), actual.fields.get("Subtotal"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Tax"), actual.fields.get("Tax"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Tip"), actual.fields.get("Tip"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("Total"), actual.fields.get("Total"), read_results) self.assertFormFieldTransformCorrect( receipt.fields.get("TransactionDate"), actual.fields.get("TransactionDate"), read_results) self.assertFormFieldTransformCorrect( receipt.fields.get("TransactionTime"), actual.fields.get("TransactionTime"), read_results) # check page range self.assertEqual(receipt.page_range.first_page_number, actual.page_range[0]) self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type receipt_type = receipt.fields.get("ReceiptType") self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect( receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results)
poller = form_recognizer_client.begin_recognize_content_from_url(formUrl) page = poller.result() table = page[0].tables[0] # page 1, table 1 print("Table found on page {}:".format(table.page_number)) for cell in table.cells: print("Cell text: {}".format(cell.text)) print("Location: {}".format(cell.bounding_box)) print("Confidence score: {}\n".format(cell.confidence)) # </snippet_getcontent> # <snippet_receipts> receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png" poller = form_recognizer_client.begin_recognize_receipts_from_url(receiptUrl) result = poller.result() for receipt in result: for name, field in receipt.fields.items(): if name == "Items": print("Receipt Items:") for idx, items in enumerate(field.value): print("...Item #{}".format(idx + 1)) for item_name, item in items.value.items(): print("......{}: {} has confidence {}".format( item_name, item.value, item.confidence)) else: print("{}: {} has confidence {}".format(name, field.value, field.confidence)) # </snippet_receipts>
# <snippet_logging> import sys import logging from azure.ai.formrecognizer import FormRecognizerClient from azure.core.credentials import AzureKeyCredential # Create a logger for the 'azure' SDK logger = logging.getLogger('azure') logger.setLevel(logging.DEBUG) # Configure a console output handler = logging.StreamHandler(stream=sys.stdout) logger.addHandler(handler) endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE" credential = AzureKeyCredential("PASTE_YOUR_FORM_RECOGNIZER_SUBSCRIPTION_KEY_HERE") # This client will log detailed information about its HTTP sessions, at DEBUG level form_recognizer_client = FormRecognizerClient(endpoint, credential, logging_enable=True) # </snippet_logging> # <snippet_example> receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png" poller = form_recognizer_client.begin_recognize_receipts_from_url(receiptUrl, logging_enable=True) # </snippet_example>