Object Character Recognition (OCR) refers to recognizing text characters from different types of images such as handwritten PDF files, car number plates, house nameplates, etc. OCR has a variety of applications particularly when you want to digitize text from images. In this article, you will see how to use the Python Pytesseract library for object character recognition.

Table of Contents

  1. Installing Tesseract Object Character Recognition Engine
  2. Importing Python Libraries
  3. Reading Text From Images

Installing Tesseract Object Character Recognition Engine

Behind the scenes, the Python Pytesseract library calls Google’s Tesseract OCR engine for performing different OCR tasks. Therefore, you first have to install Google’s Tesseract OCR Engine.

The executable file for the Tesseract OCR engine can be downloaded from this GitHub link. Download the executable file and open it.

You will see the following dialogue box. Select the language, and click the “OK” button.

 

tesseract installation

 

Click the “Next” button from the following dialogue box.

2. tesseract_install

 

A new dialogue box will appear where you will see the license agreement. Click on the “I Agree” button.

Next, on the “Choose Users” dialogue box select the appropriate option as per your personal preference, and click the “Next” button.

You will see the following dialogue box. Here, you can select the components to install. Keep the default selections and click the “Next” button.

3. tesseract_install

 

In the next dialogue box, as shown below, you will be asked to select the installation folder. Select the installation folder and remember it.  Click the “Next” button.

4. tesseract_install

 

Next, you will be asked to choose a start menu folder as shown below. Click the “Install” button.

5. tesseract_install

 

Once the installation is complete, you will see a dialogue box with the message “Setup was completed successfully”. Click the “Next” button.

Finally, you will see the following dialogue box. Click the “Finish” button to complete the installation.

6. tesseract_install

Importing Python Libraries

Execute the following script on your command terminal to install the Pytesseract and Pillow libraries for Python. The Pillow library will be used to import the images containing text, used as an example in this article.

$ pip install pytesseract
$ pip install Pillow

 

Reading Text From Images

Now we are ready to read text from images.  Open a Python editor and import the Pytesseract and the Pillow libraries that you just installed.

import pytesseract as pts
from PIL import Image

 

Next, you need to set the path to the executable file containing Google’s Tesseract OCR engine, as the value for “tesseract_cmd” variable of the pytesseract class from the pytesseract module.

In our case, the following script does that. Your path for Google’s tesseract OCR engine might be different depending upon the installation folder.

pts.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

 

Let’s now try to read the text from the following nameplate. This is a random nameplate image searchable on Google.

name_plate

The first step is to open the image using the “open()” method of the  Image class from the Pillow library. Next, to read the text from the image, pass the object returned by the “open()” method to the “image_to_string()” method of the Pytesseract library. The “image_to_string()” method returns the text extracted from the image in the form of a string which you can  print on the console as shown in the script below:

image = Image.open(r"D:/Datasets/name_plate.jpeg")
text = pt.image_to_string(image)
print(text)

Output:

ee
DR. Jatin Talwar
M.A.,M.PHIL.,Ph.D
Senior Consultant Dentist

 

D.NO - 293/A1
He NR

From the output above, you can see that most of the text has been successfully extracted, along with some noise.

Let’s see another example. Let’s try to read the text from the following car number plate.

 

number_plate

The process remains the same as you have already seen.

image = Image.open(r"D:/Datasets/car_number.jpg")
text = pt.image_to_string(image)
print(text)

Output:

RT 13 RAV) 

The output shows that the car number plate has also been successfully read.

The performance of OCR also depends upon the quality of the image. Clear images with less noise are more easily and precisely readable compared to noisy images.