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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: sample_recognize_invoices.py
DESCRIPTION:
This sample demonstrates how to recognize fields from invoices.
See fields found on a invoice here:
https://aka.ms/formrecognizer/invoicefields
USAGE:
python sample_recognize_invoices.py
Set the environment variables with your own values before running the sample:
1) AZURE_FORM_RECOGNIZER_ENDPOINT - the endpoint to your Form Recognizer resource.
2) AZURE_FORM_RECOGNIZER_KEY - your Form Recognizer API key
"""
import os
class RecognizeInvoiceSample(object):
def recognize_invoice(self):
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__),
"..", "..", "./sample_forms/forms/sample_invoice.jpg"))
# [START recognize_invoices]
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)
)
with open(path_to_sample_forms, "rb") as f:
poller = form_recognizer_client.begin_recognize_invoices(invoice=f, locale="en-US")
invoices = poller.result()
for idx, invoice in enumerate(invoices):
print("--------Recognizing invoice #{}--------".format(idx+1))
vendor_name = invoice.fields.get("VendorName")
if vendor_name:
print("Vendor Name: {} has confidence: {}".format(vendor_name.value, vendor_name.confidence))
vendor_address = invoice.fields.get("VendorAddress")
if vendor_address:
print("Vendor Address: {} has confidence: {}".format(vendor_address.value, vendor_address.confidence))
vendor_address_recipient = invoice.fields.get("VendorAddressRecipient")
if vendor_address_recipient:
print("Vendor Address Recipient: {} has confidence: {}".format(vendor_address_recipient.value, vendor_address_recipient.confidence))
customer_name = invoice.fields.get("CustomerName")
if customer_name:
print("Customer Name: {} has confidence: {}".format(customer_name.value, customer_name.confidence))
customer_id = invoice.fields.get("CustomerId")
if customer_id:
print("Customer Id: {} has confidence: {}".format(customer_id.value, customer_id.confidence))
customer_address = invoice.fields.get("CustomerAddress")
if customer_address:
print("Customer Address: {} has confidence: {}".format(customer_address.value, customer_address.confidence))
customer_address_recipient = invoice.fields.get("CustomerAddressRecipient")
if customer_address_recipient:
print("Customer Address Recipient: {} has confidence: {}".format(customer_address_recipient.value, customer_address_recipient.confidence))
invoice_id = invoice.fields.get("InvoiceId")
if invoice_id:
print("Invoice Id: {} has confidence: {}".format(invoice_id.value, invoice_id.confidence))
invoice_date = invoice.fields.get("InvoiceDate")
if invoice_date:
print("Invoice Date: {} has confidence: {}".format(invoice_date.value, invoice_date.confidence))
invoice_total = invoice.fields.get("InvoiceTotal")
if invoice_total:
print("Invoice Total: {} has confidence: {}".format(invoice_total.value, invoice_total.confidence))
due_date = invoice.fields.get("DueDate")
if due_date:
print("Due Date: {} has confidence: {}".format(due_date.value, due_date.confidence))
purchase_order = invoice.fields.get("PurchaseOrder")
if purchase_order:
print("Purchase Order: {} has confidence: {}".format(purchase_order.value, purchase_order.confidence))
billing_address = invoice.fields.get("BillingAddress")
if billing_address:
print("Billing Address: {} has confidence: {}".format(billing_address.value, billing_address.confidence))
billing_address_recipient = invoice.fields.get("BillingAddressRecipient")
if billing_address_recipient:
print("Billing Address Recipient: {} has confidence: {}".format(billing_address_recipient.value, billing_address_recipient.confidence))
shipping_address = invoice.fields.get("ShippingAddress")
if shipping_address:
print("Shipping Address: {} has confidence: {}".format(shipping_address.value, shipping_address.confidence))
shipping_address_recipient = invoice.fields.get("ShippingAddressRecipient")
if shipping_address_recipient:
print("Shipping Address Recipient: {} has confidence: {}".format(shipping_address_recipient.value, shipping_address_recipient.confidence))
print("Invoice items:")
for idx, item in enumerate(invoice.fields.get("Items").value):
print("...Item #{}".format(idx+1))
item_description = item.value.get("Description")
if item_description:
print("......Description: {} has confidence: {}".format(item_description.value, item_description.confidence))
item_quantity = item.value.get("Quantity")
if item_quantity:
print("......Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
unit = item.value.get("Unit")
if unit:
print("......Unit: {} has confidence: {}".format(unit.value, unit.confidence))
unit_price = item.value.get("UnitPrice")
if unit_price:
print("......Unit Price: {} has confidence: {}".format(unit_price.value, unit_price.confidence))
product_code = item.value.get("ProductCode")
if product_code:
print("......Product Code: {} has confidence: {}".format(product_code.value, product_code.confidence))
item_date = item.value.get("Date")
if item_date:
print("......Date: {} has confidence: {}".format(item_date.value, item_date.confidence))
tax = item.value.get("Tax")
if tax:
print("......Tax: {} has confidence: {}".format(tax.value, tax.confidence))
amount = item.value.get("Amount")
if amount:
print("......Amount: {} has confidence: {}".format(amount.value, amount.confidence))
subtotal = invoice.fields.get("SubTotal")
if subtotal:
print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))
total_tax = invoice.fields.get("TotalTax")
if total_tax:
print("Total Tax: {} has confidence: {}".format(total_tax.value, total_tax.confidence))
previous_unpaid_balance = invoice.fields.get("PreviousUnpaidBalance")
if previous_unpaid_balance:
print("Previous Unpaid Balance: {} has confidence: {}".format(previous_unpaid_balance.value, previous_unpaid_balance.confidence))
amount_due = invoice.fields.get("AmountDue")
if amount_due:
print("Amount Due: {} has confidence: {}".format(amount_due.value, amount_due.confidence))
service_start_date = invoice.fields.get("ServiceStartDate")
if service_start_date:
print("Service Start Date: {} has confidence: {}".format(service_start_date.value, service_start_date.confidence))
service_end_date = invoice.fields.get("ServiceEndDate")
if service_end_date:
print("Service End Date: {} has confidence: {}".format(service_end_date.value, service_end_date.confidence))
service_address = invoice.fields.get("ServiceAddress")
if service_address:
print("Service Address: {} has confidence: {}".format(service_address.value, service_address.confidence))
service_address_recipient = invoice.fields.get("ServiceAddressRecipient")
if service_address_recipient:
print("Service Address Recipient: {} has confidence: {}".format(service_address_recipient.value, service_address_recipient.confidence))
remittance_address = invoice.fields.get("RemittanceAddress")
if remittance_address:
print("Remittance Address: {} has confidence: {}".format(remittance_address.value, remittance_address.confidence))
remittance_address_recipient = invoice.fields.get("RemittanceAddressRecipient")
if remittance_address_recipient:
print("Remittance Address Recipient: {} has confidence: {}".format(remittance_address_recipient.value, remittance_address_recipient.confidence))
# [END recognize_invoices]
if __name__ == '__main__':
sample = RecognizeInvoiceSample()
sample.recognize_invoice()
|