How to get the angle of a document page photo in Node.JS

Pre-processing is a very important step when performing optical character recognition on a document. Without it, accuracy can be greatly compromised. Besides binarization (covered in a separate tutorial), the proper angling of your document is crucial to achieve good results. With today’s API, this is a snap, as you will see soon enough.

Let’s install our client to get the ball rolling. This can be done with npm install with the following command.

npm install cloudmersive-ocr-api-client --save

Now preprocessingGetPageAngle can be invoked with this snippet.

var CloudmersiveOcrApiClient = require('cloudmersive-ocr-api-client');var defaultClient = CloudmersiveOcrApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveOcrApiClient.PreprocessingApi();var imageFile = "/path/to/file"; // File | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.preprocessingGetPageAngle(imageFile, callback);

That’s it! Our resulting angle will be returned to us, which we can use to rotate our image to be straight. On to the OCR!

Image for post

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store