Introduction: OCR
There are times when there's text written inside of image files that we want to extract. Can we do that, programmatically? The answer is yes, that's what OCR is.
It's simple enough to OCR an image using the command line in Ubuntu, but we also want to be able to use OCR in programs. Python is a good language for using OCR, and Tesseract is the OCR tool we'll be using. OCR From the Command Line: Install Tesseract
Let's install Tesseract so that we can use it in our command line. In Ubuntu, it's really simple.
sudo apt-get install tesseract-ocr
To test it, download the following image on your computer.
(Right click and save the image.)
Then in a terminal (inside the directory your picture was downloaded too, with the correct image name), use Tesseract on the image with the following command:
tesseract ocr_orig.png stdout
For me the output is:
Hello World. Using Eggfiggggplg OCR. From gggmgxg.
Why did it get the words Tesseract and srcmake incorrect? Notice the squiggly red lines under the words, in the picture. Often, "noise" in images makes OCR imperfect. That's why cleaning images up is important, before using OCR on them. For this reason, it's often important to be able to use OCR in a program, and not just the command line.
Let's look at writing a python program that uses Tesseract, now. Setup Python Project and Install Libraries
We can use Tesseract from the command line, but how about in Python? (Obviously, make sure that you have python installed. Also, you'll need tesseract installed, from the previous section.) (Also, shout out to nikhilkumarsingh on github for providing this really easy install/code guide.)
Use the following commands to install the python tesseract library, pillow (for processing images in python). We'll also install imagemagick and wand now, for the sake of processing pdf files (and helping with image cleaning, later). sudo apt-get install python3-pip sudo pip3 install pytesseract sudo pip3 install pillow sudo pip3 install wand sudo apt-get install imagemagick sudo apt-get upgrade
Our installation should work, so let's test it with some code.
Some Python OCR Code
We're going to make a simple python file to OCR an image. In the same folder that you have the test image you downloaded from before in, create a file named "main.py". In main.py, add the following code:
Of course, make sure the image name on line 4 is correct.
To run this code, in your terminal (which should be located in the directory with main.py and the ocr_orig.png file):
python3 main.py
You should see the OCR output in your terminal.
Conclusion
We looked at how to OCR an image, both in the command line, and through python code. We chose Tesseract as our library, and we see that sometimes the results get skewed by noise in the image. It's best practice to try to make the text in an image clearer and to clean up anything unnecessary in an image, to make the OCR tool work better.
Going forward, try to look up more advanced image processing tricks to make the OCR work better.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|