AI allows you to deliver breakthrough experiences in your apps. With Azure Cognitive Services, you can easily customize and deploy the same AI models that power Microsoft’s products, such as Xbox and Bing, using the tools and languages of your choice.
In this blog we will walk through an exercise that you can complete in under an hour and learn how to build an application that can be useful for you, all while exploring a set of Azure services. If you have ever wanted to get your financial transactions in order, look no further. With this exercise, we’ll explore how to quickly take a snap of a receipt from your phone and upload it for categorization, creating expense reports, and to gain insights to your spending. Remember, even though we’ll walk you through each step, you can always explore the sample code and get creative with your own unique solution!
Features of the application:
Prerequisites
Key Azure technologies:
App Architecture Description:
Another visual of the flow of data within the solution architecture is shown below.
Now that we’ve explored the technology and services we’ll be using, let’s dive into building our app!
Implementation
To get started, data from receipts must be extracted; this is done by setting up the Form Recognizer service in Azure and connecting to the service to use the relevant API for receipts. A JSON is returned that contains the information extracted from receipts and is stored in Azure Blob Storage to be used by Azure Cognitive Search. Cognitive Search is then utilized to index the receipt data, and to search for relevant information.
High level overview of steps, along with sample code snippets for illustration:
Name |
A descriptive name for your resource. |
Subscription |
Select the Azure subscription which has been granted access. |
Location |
The location of your cognitive service instance. Different locations may introduce latency, but have no impact on the runtime availability of your resource. |
Pricing Tier |
The cost of your resource depends on the pricing tier you choose and your usage. For more information, see the API pricing details. |
Resource Group |
The Azure resource group that will contain your resource. You can create a new group or add it to a pre-existing group. |
# Analyse script
import json
import time
from requests import get, post
# Endpoint URL
endpoint = r"<endpoint url>"
apim_key = "<subscription key>"
post_url = endpoint + "/formrecognizer/v2.0/prebuilt/receipt/analyze"
source = r"<path to your receipt>"
headers = {
# Request headers
'Content-Type': 'image/jpeg',
'Ocp-Apim-Subscription-Key': apim_key,
}
params = {
"includeTextDetails": True
}
with open(source, "rb") as f:
data_bytes = f.read()
try:
resp = post(url=post_url, data=data_bytes, headers=headers, params=params)
if resp.status_code != 202:
print("POST analyze failed:\n%s" % resp.text)
quit()
print("POST analyze succeeded:\n%s" % resp.headers)
get_url = resp.headers["operation-location"]
except Exception as e:
print("POST analyze failed:\n%s" % str(e))
quit()
|
# Get results.
n_tries = 10
n_try = 0
wait_sec = 6
while n_try < n_tries:
try:
resp = get(url = get_url, headers = {"Ocp-Apim-Subscription-Key": apim_key})
resp_json = json.loads(resp.text)
if resp.status_code != 200:
print("GET Receipt results failed:\n%s" % resp_json)
quit()
status = resp_json["status"]
if status == "succeeded":
print("Receipt Analysis succeeded:\n%s" % resp_json)
quit()
if status == "failed":
print("Analysis failed:\n%s" % resp_json)
quit()
# Analysis still running. Wait and retry.
time.sleep(wait_sec)
n_try += 1
except Exception as e:
msg = "GET analyze results failed:\n%s" % str(e)
print(msg)
quit()
This code uses the operation id and makes another API call.
resulted in the JSON from which we have extracted the following details:
MerchantName: THE MAD HUNTER
TransactionDate: 2020-08-23
TransactionTime: 22:07:00
Total: £107.10
{
"id":"INV001",
"user":"Sujith Kumar",
"createdDateTime":"2020-10-23T17:16:32Z",
"MerchantName":"THE MAD HUNTER",
"TransactionDate":"2020-10-23",
"TransactionTime":"22:07:00",
"currency":"GBP",
"Category":"Entertainment",
"Total":"107.10",
"Items":[ ]
}
We can now save this JSON and build a search service to extract the information we want from it.
Before continuing onto step 8, you must have an Azure Storage Account with Blob storage.
We will use the free Azure service, which means you can create three indexes, three data sources and three indexers. The dashboard will show you how many of each you have left. For this exercise you will create one of each.
Fields have data types and attributes. The check boxes across the top are index attributes controlling how the field is used.
Make sure you choose the following fields:
This object defines an executable process. For now, use the default option (Once) to run the indexer once, immediately.
Soon you should see the newly created indexer in the list, with status indicating "in progress" or success, along with the number of documents indexed.
The main service page provides links to the resources created in your Azure Cognitive Search service. To view the index you just created, click Indexes from the list of links.
Now you should have a search index that you can use to query the receipt data that’s been extracted from the uploaded receipts.
You will get results as verbose JSON documents as shown below:
Now that you have built a query indexer and aimed it at your data you can now use it to build queries programmatically and extract information to answer some of the following questions:
In addition to the services and functionalities used throughout this exercise, there are numerous other ways you can use Azure AI to build in support for all kinds of receipts or invoices. For example, the logo extractor can be used to identify logos of popular restaurants or hotel chains, and the business card model can ingest business contact information just as easily as we saw with receipts.
We encourage you to explore some of the following ideas to enrich your application:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.