Training machines to understand and record human languages is another significant step toward making artificial intelligence (AI) more human. Powered by deep learning, Tesseract OCR is one such AI engine that enables computers to capture and extract text from scanned documents. This article serves as a comprehensive guide to install, run, and implement Tesseract OCR with Python and OpenCV.
Let’s explore how Tesseract OCR enhances traditional optical character recognition services for building enterprise-grade AI solutions.
Tesseract is an open-source Optical Character Recognition (OCR) engine originally initiated as a research paper by Hewlett Packard and later developed by Google. The latest version, Tesseract 4.0, is available under the Apache 2.0 license and can detect over 100 languages from images and videos.
Tesseract’s compatibility with several programming languages makes it an efficient tool for extracting text from large volumes of documents and images.
With Tesseract, providers of artificial intelligence development services are able to achieve optimum accuracy and efficiency with the following structural advantages-
Tesseract is an example based system working on a set of rules that can be easily modified depending on the requirement.
The OCR engine supports various output formats including plain text, HTML, PDF, TSV, and XML.
The first step begins with color sensing followed by converting the image into binary images. The third is the main step as it extracts the character outline and does OCR to further organize the text into lines and regions. Text recognition is then possible with the adaptive classifier that needs to be trained for producing effective results as shown below.
The OCR engine has its origins in OCRopus’ Python-based LSTM (Long Short Term Memory) which is a class of Recurrent Neural Network (RNN). LSTMs are highly efficient at learning from a long sequence of words and predicting the next word. In the next section, we will decode how to install and run Tesseract OCR with Python and OpenCV.
Though Tesseract can be easily installed on various operating systems, for this post we will focus on Windows with the support of precompiled binaries. The first step is to download the version Tesseract 4.0 or above on your system and run Python-tesseract (PyTesseract) with the following command-
$ pip install pytesseract
Pytesseract is a wrapper for Tesseract OCR that recognizes text from all image types supported by Pillow and Leptonica imaging libraries. It requires Python 2.7 or Python 3.5+ along with PIL or Pillow fork. You can use the following pip to install Pillow, Pytesseract, and Imutils:
OpenCV OCR and text recognition with Tesseract
$ pip install pillow
$ pip install pytesseract
$ pip install imutils
After this step, you can open a Python shell to confirm whether or not you can import OpenCV.
Before implementing Tesseract OCR with Python, we must understand the architectural working of the OpenCV OCR pipeline.
OpenCV (Open Source Computer Vision) is a library of programming functions and algorithms that provides API for real-time computer vision applications. For OCR purposes, we need to use OpenCV’s EAST Text Detector, which employs deep learning algorithms for text recognition.
EAST stands for: Efficient and Accurate Scene Text Detection pipeline.
The detector is a robust mechanism that localizes text even if it is blurred, reflective or obscured. The pipeline can efficiently predict words and text on 720p images or above with a rate of 13 frames per second. The architectural flow of OpenCV OCR with Tesseract is given below-
As shown, the EAST detector gives bounding boxes of text ROIs that are passed into Tesseract’s LSTM to extract the final OCR result. You can install OpenCV-Python on Windows using the following steps:
Step 1: Download and Install Python 2.7.x, Numpy, and Matplotlip at their default location.
Step 2: Download latest OpenCV release and extract the file.
Step 3: Go to OpenCV/build/python/2.7 folder.
Step 4: Copy cv2.pyd to C:/Python27/lib/site-packages.
Step 5: Access Python IDLE and add the following code in Python terminal
>>> import cv2
>> print cv2.__version__
One final set of commands with three important flags, namely -1, –oem, and –psm is required for controlling the language, algorithms, and page segmentation respectively.
Our system is now ready to perform text recognition using Tesseract OCR with Python and OpenCV. Below are some qualitative results of running Tesseract OCR with Python and OpenCV:
We, at Oodles, have experiential knowledge in deploying Tesseract OCR for extracting information from identity cards, invoices, and financial reports. For a sample Aadhar Card, our team was able to extract the following text using Tesseract OCR and OpenCV:
With in-built functionalities for pre-processing the images, OpenCV is also capable of capturing text from the physical world with accuracy and ease.
As the world shifts toward technology-led solutions, our effort is to harness AI technologies for enterprise efficiency. Our team of experts and analysts have hands-on experience in deploying Tesseract OCR for recognizing text from images and video on systems as well as mobile devices.
Our OCR services encompass the following efficiencies-
a) Capture and Extract text from financial documents, identity cards such as Aadhar and PAN cards, health records, and more.
b) Make editable and searchable archives of scanned documents.
c) Combine machine learning for image preprocessing with over 95% accuracy achieved.
d) Automate enterprise operations such as onboarding employees, KYC, report processing, and more.
To embed AI-powered OCR systems into your business model, connect with our AI development team.