How to straighten a document image with Node.JS using deep learning

Crooked or skewed document images can be very detrimental on your OCR (optical character recognition) operations. That’s why it is important to first detect and straighten out this issue. Deep Learning is a great assistant to have around for this, but can be difficult to set up from scratch. That’s why we already did it for you. All you need to do, in fact, is call our API in the manner described below.

Use this dependency reference in your package.json to install the client so we can continue.

"dependencies": {
"cloudmersive-ocr-api-client": "^1.2.7"

Now call our function from the API.

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.preprocessingUnrotate(imageFile, callback);

Done! The API will automatically detect the angle of the image and rotate it accordingly so that the text is nice and straight for 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