Powered by
/src$ make
  • Home
  • About
  • Directory
  • Contact
  • Home
  • About
  • Directory
  • Contact

Simple OCR Guide: Installing and Using Tesseract In Python Code (Ubuntu)

3/19/2018

 

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. 
Picture
(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:
1
2
3
4
5
6
7
8
from PIL import Image
import pytesseract

im = Image.open("ocr_orig.png")

text = pytesseract.image_to_string(im, lang = 'eng')

print(text)
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
References
1. www.pyimagesearch.com/2017/07/03/installing-tesseract-for-ocr/
2. github.com/nikhilkumarsingh/tesseract-python

Comments are closed.

    Author

    Hi, 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.
    Metamask tip button
    License: All code and instructions are provided under the MIT License.

    Discord

    Chat with me.


    Youtube

    Watch my videos.


    Twitter

    Get the latest news.


    Twitch

    See the me code live.


    Github

    My latest projects.

Powered by Create your own unique website with customizable templates.