How to Get the Angle of a Page for OCR in Python
Since OCR applications typically require a squared, vertical image to effectively read and extract its pixelated text contents, identifying the current rotation angle of that image is a key step in improving the OCR process. The below API solution makes this easy, automatically returning the angle of an input document and allowing subsequent preprocessing steps to correct any discrepancy. You can take advantage of this API for free following steps below.
Your first step is to install the Python SDK. To do so, run the following command:
pip install cloudmersive-ocr-api-client
Next, call the function, starting with the imports:
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.PreprocessingApi(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.
try:
# Get the angle of the page / document / receipt
api_response = api_instance.preprocessing_get_page_angle(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling PreprocessingApi->preprocessing_get_page_angle: %s\n" % e)
Finally, to complete your API call, include a free-tier Cloudmersive API key in the configuration.api_key field above. After that, you’re all set!
You can get a free-tier key by visiting our website and registering a free account (this provides a limit of 800 API calls per month; once this limit is reached, the total resets the following month).