Title: Detecting and OCR’ing Digits with Tesseract and Python - PyImageSearch

URL Source: https://pyimagesearch.com/2021/08/30/detecting-and-ocring-digits-with-tesseract-and-python/

Published Time: 2021-08-30T14:00:00+00:00

Markdown Content:
In a [previous tutorial](https://pyimagesearch.com/2021/08/23/your-first-ocr-project-with-tesseract-and-python/), we implemented our very first OCR project. We saw that Tesseract worked well on some images but returned total nonsense for other examples. Part of being a successful OCR practitioner is learning that when you see this garbled, nonsensical output from Tesseract, it means some combination of (1) your image pre-processing techniques and (2) your Tesseract OCR options are incorrect.

![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20393'%3E%3C/svg%3E)

****To learn how to detect and OCR digits with Tesseract and Python, _just keep reading._****

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

Tesseract is a tool, like any other software package. Just like a data scientist can’t simply import millions of customer purchase records into Microsoft Excel and expect Excel to recognize purchase patterns _automatically_, **it’s unrealistic to expect Tesseract to figure out what you need to OCR automatically and correctly output it.**

Instead, it would help if you learned how to configure Tesseract properly for the task at hand. For example, suppose you are tasked with creating a computer vision application to **automatically OCR business cards for their phone numbers.**

How would you go about building such a project? Would you try to OCR the _entire_ business card and then use a combination of regular expressions and post-processing pattern recognition to parse out the digits?

Or would you take a step back and examine the Tesseract OCR engine itself — **is it possible to tell Tesseract to** **_only_** **OCR digits?**

It turns out there is. And that’s what we’ll cover in this tutorial.

**Learning Objectives**
-----------------------

In this tutorial, you will:

1.  Gain hands-on experience OCR’ing digits from input images
2.  Extend our previous OCR script to handle digit recognition
3.  Learn how to configure Tesseract to only OCR digits
4.  Pass in this configuration to Tesseract via the `pytesseract` library

### **Configuring your development environment**

To follow this guide, you need to have the OpenCV library installed on your system.

Luckily, OpenCV is pip-installable:

$ pip install opencv-contrib-python

**If you need help configuring your development environment for OpenCV, I _highly recommend_ that you read my** [**_pip install OpenCV_ guide**](https://pyimagesearch.com/2018/09/19/pip-install-opencv/) — it will have you up and running in a matter of minutes.

### **Having problems configuring your development environment?**

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20500%20334'%3E%3C/svg%3E)](https://pyimagesearch.com/pyimagesearch-university/)

**Figure 1:** Having trouble configuring your dev environment? Want access to pre-configured Jupyter Notebooks running on Google Colab? Be sure to join [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/) — you’ll be up and running with this tutorial in a matter of minutes.

All that said, are you:

*   Short on time?
*   Learning on your employer’s administratively locked system?
*   Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?
*   **Ready to run the code** **_right now_** **on your Windows, macOS, or Linux system?**

Then join [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/) today!

**Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are** **_pre-configured_** **to run on Google Colab’s ecosystem right in your web browser!** No installation required.

And best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux!

**Digit Detection and Recognition with Tesseract**
--------------------------------------------------

In the first part of this tutorial, we’ll review digit detection and recognition, including real-world problems where we may wish to OCR _only_ digits.

From there, we’ll review our project directory structure, and I’ll show you how to perform digit detection and recognition with Tesseract. We’ll wrap up this tutorial with a review of our digit OCR results.

### **What Is Digit Detection and Recognition?**

As the name suggests, digit recognition is the process of OCR’ing and identifying _only_ digits, purposely ignoring other characters. Digit recognition is often applied to real-world OCR projects (a montage of which can be seen in **Figure** **2**), including:

*   Extracting information from business cards
*   Building an intelligent water monitor reader
*   Bank check and credit card OCR

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20433'%3E%3C/svg%3E)

**Figure 2.** A montage of different applications of digit detection and recognition. Water meter image source: [pyimg.co/vlx63](http://pyimg.co/vlx63).

Our goal here is to “cut through the noise” of the non-digit characters. We will instead “laser in” on the digits. Luckily, accomplishing this digit recognition task is relatively easy once we supply the correct parameters to Tesseract.

### **Project Structure**

Let’s review the directory structure for this project:

|-- apple\_support.png
|-- ocr\_digits.py

Our project consists of one testing image (`apple_support.png`) and our `ocr_digits.py` Python script. The script accepts an image and an optional “digits only” setting and reports OCR results accordingly.

### **OCR’ing Digits with Tesseract and OpenCV**

We are now ready to OCR digits with Tesseract. Open a new file, name it `ocr_digits.py`, and insert the following code:

\# import the necessary packages
import pytesseract
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-i", "--image", required=True,
	help="path to input image to be OCR'd")
ap.add\_argument("-d", "--digits", type=int, default=1,
	help="whether or not \*digits only\* OCR will be performed")
args = vars(ap.parse\_args())

As you can see, we’re using the PyTesseract package in conjunction with OpenCV. After our imports are taken care of, we parse two command line arguments:

*   `--image`: Path to the image to be OCR’d
*   `--digits`: A flag indicating whether or not we should OCR _digits only_ (by `default`, the option is set to a `True` Boolean)

Let’s go ahead and load our image and perform OCR:

\# load the input image, convert it from BGR to RGB channel ordering,
# and initialize our Tesseract OCR options as an empty string
image = cv2.imread(args\["image"\])
rgb = cv2.cvtColor(image, cv2.COLOR\_BGR2RGB)
options = ""

# check to see if \*digit only\* OCR should be performed, and if so,
# update our Tesseract OCR options
if args\["digits"\] > 0:
	options = "outputbase digits"

# OCR the input image using Tesseract
text = pytesseract.image\_to\_string(rgb, config=options)
print(text)

Tesseract requires RGB color channel ordering for performing OCR. **Lines 16 and 17** load the input `--image` and swap color channels accordingly.

We then establish our Tesseract `options` (**Lines 18–23**). Configuring Tesseract with options allows for more granular control over Tesseract’s methods under the hood to perform OCR.

For now, our `options` are either empty (**Line 18**) or `outputbase digits`, indicating that we will **only OCR digits** on the input image (**Lines 22 and 23**).

From there, we use the `image_to_string` function call while passing our `rgb` image and our configuration `options` (**Line 26**). Notice that we’re using the `config` parameter and including the digits only setting if the `--digits` command line argument Boolean is `True`.

Finally, we show the OCR `text` results in our terminal (**Line 27**). Let’s see if those results meet our expectations.

### **Digit OCR Results**

We are now ready to OCR digits with Tesseract.

Open a terminal and execute the following command:

$ python ocr\_digits.py --image apple\_support.png
1-800-275-2273

As input to our `ocr_digits.py` script, we’ve supplied a sample business card-like image that contains the text _“Apple Support,”_ along with the corresponding phone number (**Figure 3**). Our script can correctly OCR the phone number, displaying it to our terminal while ignoring the _“Apple Support”_ text.

![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20279'%3E%3C/svg%3E)

**Figure 3.** Business card of Apple support.

One of the problems of using Tesseract via the command line or with the `image_to_string` function is that it becomes quite hard to debug exactly _how_ Tesseract arrived at the final output.

Once we gain some more experience working with the Tesseract OCR engine, we’ll turn our attention to visually debugging and eventually filtering out extraneous characters via confidence/probability scores. For the time being, please pay attention to the options and configurations we’re supplying to Tesseract to accomplish our goals (i.e., digit recognition).

If you instead want to OCR _all characters_ (not just limited to digits), you can set the `--digits` command line argument to any value _≤0:_

$ python ocr\_digits.py --image apple\_support.png --digits 0
a
Apple Support
1-800-275-2273

Notice how the _“Apple Support”_ text is now included with the phone number in the OCR Output. **But what’s up with that** **_“a”_** **in the output? Where is that coming from?**

The _“a”_ in the output is Tesseract confusing the leaf at the _top_ of the Apple logo as an alphabet (**Figure 4**).

![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20700%20305'%3E%3C/svg%3E)

**Figure 4.** The leaf at the _top_ of the Apple logo confuses Tesseract into treating that as an alphabet.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

**Summary**
-----------

In this tutorial, you learned how to configure Tesseract and `pytesseract` to OCR _only_ digits. We then used our Python script to handle OCR’ing the digits.

You’ll want to pay close attention to the `config` and `options` we supply to Tesseract. Frequently, being able to apply OCR successfully to a Tesseract project depends on providing the correct set of configurations.

In our next tutorial, we’ll continue exploring Tesseract options by learning how to whitelist and blacklist a custom set of characters.

**To download the source code to this post (and be notified when future tutorials are published here on PyImageSearch), _simply enter your email address in the form below!_**

![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!