Create Simple Optical Character Recognition (OCR) with Python | by Fahmi Nurfikri | Towards Data Science

When collecting data for the text mining process or looking for other references, we often find sources in the form of images. For example, if we are going to analyze a word in pdf format, the file instead contains an image of text. This certainly makes it difficult for data processing. One solution to this problem is that we can use Optical Character Recognition (OCR).
OCR is a technology for recognizing text in images, such as scanned documents and photos. One of the OCR tools that are often used is Tesseract. Tesseract is an optical character recognition engine for various operating systems. It was originally developed by Hewlett-Packard as proprietary software. Later Google took over development.
Currently Tesseract is running well on the Windows, macOS, and Linux platforms. Tesseract supports Unicode (UTF-8) and supports more than 100 languages. In this article we will start with the Tesseract OCR installation process, and test the extraction of text in images.
The first step is to install the Tesseract. In order to use the Tesseract library, we first need to install it on our system. If you’re using Ubuntu, you can simply use apt-get to install Tesseract OCR:
sudo apt-get install tesseract-ocr
For macOS users, we’ll be using Homebrew to install Tesseract.
brew install tesseract
For Windows, please see Tesseract documentation. Let’s begin by getting pytesseract installed.
$ pip install pytesseract
After installation completed, let’s move forward by applying tesseract with python. First we import the dependencies.
from PIL import Imageimport pytesseractimport numpy as np
I will use a simple image to test the usage of the tesseract. I will use the image below.
Let’s load this image and convert it to text.
filename = 'image_01.png'img1 = np.array(Image.open(filename))text = pytesseract.image_to_string(img1)
Now, let’s see the result.
print(text)
And this is the result.
The results obtained from the tesseract are good enough for simple images. Because in the real world it is difficult to find images that are really simple, so I will add noise to see the performance of the tesseract. I will use the image below.
Then we will do the same process as before.
filename = 'image_02.png'img2 = np.array(Image.open(filename))text = pytesseract.image_to_string(img2)print(text)
This is the result.
The result is ... Nothing. This means that Tesseract cannot read words in images that have noise.
Then we try to use a little image processing to eliminate noise in the image. Here I will use the Open CV library. In this experiment, I’m using normalization, thresholding and image blur.
import numpy as npimport cv2norm_img = np.zeros((img.shape[0], img.shape[1]))img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)[1]img = cv2.GaussianBlur(img, (1, 1), 0)
Then the result will be like this.
The image is clean enough, then we will try again with the same process as before. And this is the result.
Can be seen from the picture above that the results are in accordance with what we expect.
medium.com
The conclusion that can be obtained is that the tesseract is most suitable when building a document processing pipeline where images are scanned and processed. This works best for situations with high-resolution input where foreground text is neatly segmented from the background.