(Advanced) Recognize a Photo of a Form, Extract Key Fields using Stored Templates in Python

Cloudmersive
2 min readFeb 16, 2022

--

In a prior article, we covered how you can use the Cloudmersive OCR API to recognize & store key information from a business form with some customization options. In this article, we’re going to cover the Advanced version of this API, which enables you to use template definitions stored in Cloudmersive Configuration. After following the steps below to connect with the API in Python, you can configure stored templates by logging into the Cloudmersive Management Portal and navigating to Settings > API Configuration > Create Bucket.

First, run this command for Python SDK installation:

pip install cloudmersive-ocr-api-client

After that, you can run the below code to call the function & use the API. In the second snippet, you’ll be asked to supply your Cloudmersive API key (if you don’t have a Cloudmersive Account yet, you can easily make a free account by visiting the Cloudmersive website).

from __future__ import print_function
import time
import cloudmersive_ocr_api_client
from cloudmersive_ocr_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_ocr_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_ocr_api_client.ImageOcrApi(cloudmersive_ocr_api_client.ApiClient(configuration))
image_file = '/path/to/inputfile' # file | Image file to perform OCR on. Common file formats such as PNG, JPEG are supported.
bucket_id = 'bucket_id_example' # str | Bucket ID of the Configuration Bucket storing the form templates (optional)
bucket_secret_key = 'bucket_secret_key_example' # str | Bucket Secret Key of the Configuration Bucket storing the form templates (optional)
recognition_mode = 'recognition_mode_example' # str | Optional, enable advanced recognition mode by specifying 'Advanced', enable handwriting recognition by specifying 'EnableHandwriting'. Default is disabled. (optional)
preprocessing = 'preprocessing_example' # str | Optional, preprocessing mode, default is 'Auto'. Possible values are None (no preprocessing of the image), and Auto (automatic image enhancement of the image - including automatic unrotation of the image - before OCR is applied; this is recommended). Set this to 'None' if you do not want to use automatic image unrotation and enhancement. (optional)
diagnostics = 'diagnostics_example' # str | Optional, diagnostics mode, default is 'false'. Possible values are 'true' (will set DiagnosticImage to a diagnostic PNG image in the result), and 'false' (no diagnostics are enabled; this is recommended for best performance). (optional)
try:
# Recognize a photo of a form, extract key fields using stored templates
api_response = api_instance.image_ocr_photo_recognize_form_advanced(image_file, bucket_id=bucket_id, bucket_secret_key=bucket_secret_key, recognition_mode=recognition_mode, preprocessing=preprocessing, diagnostics=diagnostics)
pprint(api_response)
except ApiException as e:
print("Exception when calling ImageOcrApi->image_ocr_photo_recognize_form_advanced: %s\n" % e)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet