Tesseract is an optical character recognition engine for various operating systems. Released under the Apache License, it is a free software.
Originally developed by Hewlett-Packard as proprietary software in the 1980s, it was released as open source in 2005 and development has been sponsored by Google since 2006.
In this article we are going to see how to install Tesseract into a Raspberry Pi or more generally into Linux, and how to test it via command line and with a simple script.
Different languages support tesseract, in this specific article we focus our efforts in Python.
Install Tesseract on Raspberry Pi
The installation in Linux systems is straightforward. Open a shell and type the following command:
Once installed in your system, we need to install the Python wrapper (compatible with Python 2.7 amd 3.6+) called pytesseract:
Another possibility for installing pytesseract is to specify the Python interpreter version you want to use. For example, if you want to install pytesseract into your Python3 installation, you can type:
python3 -m pip install pytesseract
If you need more information about how to install Python packages, refer to how to install Python packets on Windows using Pip.
Command Line Test
Before we dive into the Python script, let’s check how tesseract works with its command line version.
In order to do that, let’s consider as sample text image the following picture:
our goals is to get those words in clear text. Save the picture somewhere in your hard disk, let’s call it picture.png.
Once saved, open a shell in the same folder in which you previously stored your picture and type the following command:
The command is simple, after the name of the tool, we have to put the picture name (if it is in a different folder, we must specify also the path for reaching it) and a file in which tesseract can store what it reads. In this case I used the stdout, so that the result is printed out into your shell.
If everything is ok, you should now see the text correctly printed.
Tesseract comes with a lot of different options that can be used in order to improve the reading performance. Check the documentation out for more information.
Now it is time to test tesseract in Python.
Open the same folder in which you have the picture.png file, and create a new text file.
If you want to do it using the shell, type:
inside this script we are going to write these few Python lines that do the trick:
As you can see, we also use another library called PIL (Pillow) which is widely used when you have to open images. If you haven’t install it yet, type the following command in your shell:
After loading it, we pass the picture to the pytesseract command that will return the text as result.
In this article we have seen how to install Tesseract in your Raspberry Pi, or more generally in your Linux system, and how to use it.
As wrote above, for more details on how to use tesseract, please visit the official documentation.
Keep in mind also that, in order to reach the best result, you must process your image so that, for example, the text is black and the rest is white (not viceversa), the text should have a fixed size (around 12pt) and the noise must be reduced as much as possible.