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

Cloudmersive
2 min readMar 14, 2020

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!

--

--

Cloudmersive

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