Easily track employee expenses (Part 1 – training).
How to get the receipt grand total from an image by using Azure Cognitive API and save it Business Central.

SCENARIO
- The user, by using a mobile device, takes the photo of the receipt and uploads to an azure blob storage;
- The Azure “Form Recognizer” API service automatically recognise the receipt grand total;
- Business Central automatically fills the employee expense grand total;
WHAT IS NEEDED
- A blog storage to store images.
- An Azure Form Recognizer account.
- A docker image to train a custom model.
- A custom Business Central AL codeunit
AZURE COGNITIVE SERVICES
Azure cognitive services offers a variety of APIs:
Among all the available features, “Form Recognizer” allows to detect part of a receipt.
At the moment “Form Recognizer”, just out of beta stage, only applies to UK/USA receipts and isn’t yet able to correctly detect grand totals with EU receipts.
This means it’s necessary to train a custom model to get better results.
https://docs.microsoft.com/en-gb/azure/cognitive-services/form-recognizer/overview#train-with-labels
To create the service is as easy as three mouse clicks: Add Resource, select “Form Recognizer” from list, configure names and location/resource group.

Once the service has been created, under Keys and Endpoints you can find “Key 1” and “Endpoint”, these two strings will be used when configuring Label Tools.

AZURE BLOB STORAGE
I create a blob container on Azure Portal and I upload there all of my receipts by using “Storage Explorer”.
It’s important to enable “Configure cross-domain resource sharing (CORS)” access.
HOW TO CREATE AND TRAIN A CUSTOM MODEL
Since the service by default recognises only US/UK receipts I have to create my own custom model.
To create a custom model I followed official microsoft instructions https://docs.microsoft.com/it-it/azure/cognitive-services/form-recognizer/quickstarts/label-tool
I installed docker, and switched to Linux containers (unfortunately the label tools requires a linux container instead of a Windows container).
Just with two lines are enough to have the container up and running:
First I pull the label tool container:
docker pull mcr.microsoft.com/azure-cognitive-services/custom-form/labeltool
Then I start the container:
docker run -it -p 3000:80 mcr.microsoft.com/azure-cognitive-services/custom-form/labeltool eula=accept
It’s now possibile to open the web console from local browser:

First of all I configured connection with the blob storage, then I configured the form recognizer endpoint and API key from previous chapter.
The web application imports all of the images from blob storage.
I create my own tag as a numeric.

Now the boring part, for each of the image I manually assign the tag to the corresponding text on receipt.


Now it’s time to start the training.

After a couple of minutes I get the result of training session:

The web console allows to test the custom model on receipts by uploading them.

COMING SOON
In next article I will cover how to call the Cognitive API from a Business Central codeunit.
LAST UPDATED
21st of July, 2020