How to straighten a document image with Python using Deep Learning

Cloudmersive
2 min readApr 29, 2020

--

If you want to get the very best results when using optical character recognition, it is necessary to process the image in advance. Besides binarization, the most important preprocessing procedure is straightening out the image. Today we will be using Deep Learning to accomplish this, providing very high accuracy indeed. Don’t worry, though, we will not be setting this up from scratch. Instead of a lengthy AI training session, we will whip out an API that has this capability.

First comes our installation:

pip install cloudmersive-ocr-api-client

And here we have the code needed for our function call from the API:

from __future__ import print_functionimport timeimport cloudmersive_ocr_api_clientfrom cloudmersive_ocr_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_ocr_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_ocr_api_client.PreprocessingApi(cloudmersive_ocr_api_client.ApiClient(configuration))image_file = '/path/to/file' # file | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.try:# Detect and unrotate a document image (advanced)api_response = api_instance.preprocessing_unrotate_advanced(image_file)pprint(api_response)except ApiException as e:print("Exception when calling PreprocessingApi->preprocessing_unrotate_advanced: %s\n" % e)

All done!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet