How to Detect and Unskew a Photo of a Document using Node.js
Document skew can ruin OCR operations, so it’s important to incorporate an “unskew” step into our OCR applications.
Using the below code, we can conveniently take advantage of a free OCR preprocessing API designed to automatically detect the angle of skewed images (especially scanned documents) and apply an appropriate correction. We can use this service right before our OCR operations to ensure a high quality output.
To structure our API call, let’s begin by installing the SDK. We can either run the following command:
npm install cloudmersive-ocr-api-client --save
Or, alternatively, we can add this snippet to our package.json:
"dependencies": {
"cloudmersive-ocr-api-client": "^1.3.3"
}
Before we incorporate the code examples we see below, let’s first grab a free-tier API key from the Cloudmersive website to authorize our request. These will allow up to 800 API calls per month with no commitment, so we can easily get our projects off the ground without adding unnecessary costs:
var CloudmersiveOcrApiClient = require('cloudmersive-ocr-api-client');
var defaultClient = CloudmersiveOcrApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveOcrApiClient.PreprocessingApi();
var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // 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.preprocessingUnskew(imageFile, callback);
That’s all there is to it — no more code required!