In this article, you will build a document processing solution using Form Recognizer, Logic Apps, and Azure OpenAI.
Azure Form Recognizer is an Azure Applied AI service that lets you easily analyze documents and extract text fields, data and tables and identify key-value pairs. In this article, you will learn how to extract common fields from sales receipts using the Azure Form Recognizer client library for Python.
You will learn how to:
- Provision a Form Recognizer resource.
- Use the Form Recognizer resource and the Python SDK to extract information from receipts.
To complete the exercise, you will need to install:
- Python 3,
- Visual Studio code or another code editor.
You will also need an Azure subscription. If you don’t have one, you can sign up for an Azure free account.
What is the Form Recognizer?
Form Recognizer is one of Azure’s cloud-based artificial intelligence services that can be used to build applications for automatic document analysis and information extraction. Form Recognizer uses deep learning models to analyze document data, extract text and values, and map relationships between data fields. Using the Form Recognizer API, you can automate the process of analyzing documents and extracting data at scale with high accuracy.
Azure Form Recognizer provides numerous pre-built models for analyzing common documents, such as invoices, receipts, and business cards, while you can also build custom models to analyze documents specific to your business.
In this article, we will be focusing on analyzing common sales receipts using a pre-trained receipt model.
Create a Form Recognizer Resource
To use the Azure Form Recognizer service, you can either create a Form Recognizer resource or a Cognitive Services resource.
In this exercise, you will create a single Form Recognizer resource. If you plan to use Form Recognizer along with other cognitive services, such as Computer Vision, you can create a Cognitive Services resource, or else you can create a Computer Vision resource.
Sign in to Azure Portal and select Create a resource.
Search for Form Recognizer and then click Create.
Create a Form Recognizer resource with the following settings:
- Subscription: Your Azure subscription.
- Resource group: Select an existing resource group or create a new one.
- Region: Choose any available region, for example, North Europe.
- Name: This would be your custom domain name in your endpoint. Enter a unique name.
- Pricing tier: Free F0.
Select Review + Create and then select the Create button and wait for the deployment to complete.
Once the resource has been deployed, select Go to resource.
Get Endpoint and Keys
Navigate to the Keys and Endpoint page. Save the Key 1 and the Endpoint. You’ll need these values to connect your Python application to the Form Recognizer API.
Set up your application
Install the client library
To analyze documents with Form Recognizer, you’ll need to install the Form Recognizer client library for Python. You can install the latest version of the client library using
Create a configuration file
Create a configuration file (
.env) and save the Azure’s key and endpoint you copied in the previous step.
Create a new Python application
Create a new Python file (form-recognizer.py) and import the following libraries:
1 2 3 4
from azure.ai.formrecognizer import FormRecognizerClient from azure.core.credentials import AzureKeyCredential from dotenv import load_dotenv import os
Add the following code to load the key and endpoint from the configuration file.
1 2 3
load_dotenv() key = os.getenv('KEY') endpoint = os.getenv('ENDPOINT')
Authenticate the client
To interact with the Form Recognizer resource, create a
FormRecognizerClient. Using the methods of the
FormRecognizerClient, you will be able to recognize common fields from receipts.
The following code creates a
FormRecognizerClient using the
endpoint variables you defined above.
In this article, we will be focusing on analyzing and extracting common fields and semantic values from sales receipts.
Analyze the given receipt
Insert the following code after the form recognizer client creation to analyze a receipt from a URL. This code calls the
begin_recognize_receipts_from_url method to extract values from the receipt and then retrieves the result. The input parameter of the above-mentioned method must be the URL of the receipt to be analyzed.
To analyze a local receipt image, use the
begin_recognize_receipts method of the
Display general information
Add the following code to print general information about the given receipt, such as the receipt type and the transaction date.
Display receipt items
The following code displays information about the items (such as item name, price, and quantity along with their confidence score) recognized by the pre-trained receipt model.
Display the total price
Use the following code to print the total tax and the transaction total and subtotal along with the confidence score.
Summary and next steps
In this article, you learned how to analyze sales receipts using the Form Recognizer service and the client library for Python. Here are some additional resources from Microsoft Learn:
If you have finished learning, you can delete the resource group from your Azure subscription:
- In the Azure portal, select Resource groups on the right menu and then select the resource group that you have created.
- Click Delete resource group.