OCR (Optical Character Recognition) functionality in Node.JS Application
Optical character recognition or optical character reader (OCR) is the electronic or mechanical conversion of images of typed, handwritten or printed text into machine-encoded text, whether from a scanned document, a photo of a document, a scene-photo (for example the text on signs and billboards in a landscape photo) or from subtitle text superimposed on an image (for example: from a television broadcast) — Wikipedia
nowadays in most applications, we can see this OCR functionality, in this article we are going to see how to add optical character recognition functionality to your node js application.
for this purpose, we gonna use this amazing Tesseract.js library. it provides us a very simple and straight forward way for recognition characters from the images.
video tutorial related to this post
okay, let’s build a node.js application from the scratch to use Tesseract.js
first you have to do is open a terminal and navigate to the location that you want, and then type,
npm init -y
this will create a package.json file for you. after that you should install Tesseract using npm.
npm install tesseract.js
then create an index.js file, index js file should look like this.
//index.js file
const Tesseract= require('tesseract.js');
Tesseract.recognize(// this first argument is for the location of an image it can be a //url like below or you can set a local path in your computer
'https://tesseract.projectnaptha.com/img/eng_bw.png',// this second argument is for the laguage 'eng',
{ logger: m => console.log(m) }).then(({ data: { text } }) => {
console.log(text);
})
and then save the index.js file then type
node index
in your console.
what this simple code does is getting below image using URL
and identifying the characters in the image then log the paragraph to a console.
that’s it guys very easy and very straight forward isn’t it?. so we can use this tesseract.js for our applications and get the ocr work done. here is the GitHub link for tesseract library, there are many things we can do using tesseract and it supports over 100 languages. if you want to learn further go there and see the documentation also.
so dear readers I think this simple article will help you a lot. if you have any clarification put a comment below. see you in the next article until then Happy Coding!!!