Skip to main content

Putting Form Recognizer on test - Extract labels from flight tickets (Cognitive Services)

Have you ever need to extract content from a document or image? I’m not referring just plain text, but key-values like tuples (e.g. name: Tom, age: 18) without having to do any additional manual steps.

Form Recognizer
At this moment in time, we can find in private preview on Azure – Form Recognizer. It a service part of Cognitive Services that have an ML behind the scene, allowing us to analyse digital content and extract data that it is in key-value pairs or table data from forms.

Overview of the service
The service can automatically extract this information without requiring us to do labelling of data. We need to provide at least 5 samples that are used by Form Recognizer to train the model. Once we have the model trained, we can start to extract content from our digital library.
The API provided by the service can be used to get, delete or train a model and extract the keys and values. There is an SDK that is built on top of it for .NET, but for testing purpose, I used the API together with curl. The output is provided in JSON or XML, that should work in most of the situations.

There is a prebuild model for receipts that works fantastic. I invite you to give it a try to see how well works with any receipts. The custom models are trained by ourself by uploading at least 5 samples. They might not work so good as the prebuilt models, but an out of the box solutions it is pretty amazing. For now, we can’t define and label custom keys or push our trained model.
Form Recognizer can be used directly from Azure in a SaaS model or can be configured to run inside your on-premises as 2 Docker containers. By allowing us to run on the edge devices, Form Recognizer becomes a good candidate for a service that can be integrated into our pipelines inside banks or financial institutes.

If you want to see my slide deck related to Form Recognizer please check the next link - http://vunvulearadu.blogspot.com/2019/12/post-event-global-ai-bootcamp-cluj.html

Flight tickets – the real test
I decided to test Form Recognizer with a form that is not pre-build – flight tickets. The purpose was to see the number of labels that can be identified with success by the service when we are using 5 documents for training.
In general, when you need to train a model, you need hundreds of items. Using just 5 items sounds impressive and I wanted to see how it would behave. I upload 5 older plane tickets that I had from the past and also used another 3 to see how well the labelling is done.

The keys extract from the document are marked with RED and the values with BLACK. The line between them represents the mapping between key and value. The rendering was possible because each key and value retuned by Form Recognize contains the coordinates on the document.

For a plane ticket the model was able to:
  • 38 keys
  • 29 values
  • 29 mappings between keys and values
  • 9 keys without a value
  • 9 values that were marked as keys (the 9 keys from the previous point)
  • 5 labels that were missed (where the key was available, but not well aligned)

After reviewing the result and eliminating the results where the confidence level was under 1 we have the following output:
  • 29 keys
  • 28 values
  • 28 mappings between keys and values
  • 1 key without a value

As we can see from the above result, the confidence level is a good indicator that can help us to eliminate false-positive results. By reviewing the final result that was provided by the Form Recognizer, we could say that we can extract easilty necessary information from the flight ticket using Form Recognizer. 
The errors appear especially for labels without a key or when the value of the key is not in close proximity of the value.

Result review
I am satisfied with the result. There were some narrow cases where the labelling was wrong, but overall the result is excellent. I hope that in the future we will be able to do some model optimization that could improve our overall experience with the service.

Do you want to try by yourself?
Follow the steps specified on Azure - https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/curl-train-extract. The crunking of first 500 pages are free.
Below you can find the scripts that I used for my own needs:

Train the model:
C:\Users\rvunvulea>curl -X POST "https://rvfr.cognitiveservices.azure.com/formrecognizer/v1.0-preview/custom/train" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: 5b156fd6d9214fe782d09404073e077e" --data-ascii "{ \"source\": \""https://rvstoragetrain.blob.core.windows.net/train?sv=2019-02-02&ss=bfqt&srt=sco&sp=rwdlacup&se=2020-01-10T23:06:24Z&st=2019-12-05T15:06:24Z&spr=https&sig=ViAM63qYg44fmc2ntc4Bq%2BOcnYBm%2F1WRc4ZutvLjNxc%3D"\"}"
Output:
{"modelId":"7325d040-7d7a-4e77-ac09-ed740e7f2076","trainingDocuments":[{"documentName":"T1.pdf","pages":3,"errors":[],"status":"success"},{"documentName":"T2.pdf","pages":3,"errors":[],"status":"success"},{"documentName":"T3.pdf","pages":3,"errors":[],"status":"success"},{"documentName":"T4.pdf","pages":3,"errors":[],"status":"success"},{"documentName":"T5.pdf","pages":2,"errors":[],"status":"success"}],"errors":[]}

Model id returned in the previous steps needs to be used when you want to process a page.

Extract labels
curl -X POST "https://rvfr.cognitiveservices.azure.com/formrecognizer/v1.0-preview/custom/models/7325d040-7d7a-4e77-ac09-ed740e7f2076/analyze" -H "Content-Type: multipart/form-data" -F "form=@\"C:\1\T1.pdf\";type=application/pdf" -H "Ocp-Apim-Subscription-Key: 5b156fd6d9214fe782d09404073e077e"
Output: JSON result

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP