How to extract information from a form photo in Node.JS using OCR

Optical character recognition, or OCR, can be amazingly useful when it comes to dealing with heaps of forms, or photographs of said forms. I’m going to show you how to set this in motion with almost no work on your part, but with the potential for excellent customization and accuracy. Let’s dive in.

First comes our installation, which can be done with npm install.

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

Now just call this function.

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.ImageOcrApi();var imageFile = "/path/to/file"; // File | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.var opts = {'bucketID': "bucketID_example", // String | Bucket ID of the Configuration Bucket storing the form templates'bucketSecretKey': "bucketSecretKey_example", // String | Bucket Secret Key of the Configuration Bucket storing the form templates'recognitionMode': "recognitionMode_example", // String | Optional, enable advanced recognition mode by specifying 'Advanced', enable handwriting recognition by specifying 'EnableHandwriting'.  Default is disabled.'preprocessing': "preprocessing_example", // String | Optional, preprocessing mode, default is 'Auto'.  Possible values are None (no preprocessing of the image), and Auto (automatic image enhancement of the image - including automatic unrotation of the image - before OCR is applied; this is recommended).  Set this to 'None' if you do not want to use automatic image unrotation and enhancement.'diagnostics': "diagnostics_example" // String | Optional, diagnostics mode, default is 'false'.  Possible values are 'true' (will set DiagnosticImage to a diagnostic PNG image in the result), and 'false' (no diagnostics are enabled; this is recommended for best performance).};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.imageOcrPhotoRecognizeFormAdvanced(imageFile, opts, callback);

As you can see, the function can be customized with form field identities, and can even recognize handwriting.

Image for post
Image for post

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