Installing Tesseract for OCR

Today’s blog post is part one in a two part series on installing and using the Tesseract library for Optical Character Recognition (OCR).

OCR is the automatic process of converting typed, handwritten, or printed text to machine-encoded text that we can access and manipulate via a string variable.

Part one of this series will focus on installing and configuring Tesseract on your machine, followed by utilizing the tesseract  command to apply OCR to input images.

In next week’s blog post we’ll discover how to use the Python “bindings” to the Tesseract library to call Tesseract directly from your Python script.

To learn more about Tesseract and how it can be used for OCR, just keep reading.

Looking for the source code to this post?
Jump right to the downloads section.

Installing Tesseract for OCR

Tesseract, originally developed by Hewlett Packard in the 1980s, was open-sourced in 2005. Later, in 2006, Google adopted the project and has been a sponsor ever since.

The Tesseract software works with many natural languages from English (initially) to Punjabi to Yiddish. Since the updates in 2015, it now supports over 100 written languages and has code in place so that it can easily be trained on other languages as well.

Originally a C program, it was ported to C++ in 1998. The software is headless and can be executed via the command line. It does not come with a GUI but there are several other software packages that wrap around Tesseract to provide a GUI interface.

To read more about Tesseract visit the project page and read the Wikipedia article.

In this blog post we will:

  • Install Tesseract on our systems.
  • Validate that the Tesseract install is working correctly.
  • Try Tesseract OCR on some sample input images.

After going through this tutorial you will have the knowledge to run Tesseract on your own images.

Step #1: Install Tesseract

In order to use the Tesseract library, we first need to install it on our system.

For macOS users, we’ll be using Homebrew to install Tesseract:

Figure 1: Installing Tesseract OCR on macOS.

If you’re using the Ubuntu operating system, simply use apt-get  to install Tesseract OCR:

Figure 2: Installing Tesseract OCR on Ubuntu.

For Windows, please consult Tesseract documentation as PyImageSearch does not support or recommend Windows for computer vision development.

Step #2: Validate that Tesseract has been installed

To validate that Tesseract has been successfully installed on your machine, execute the following command:

Figure 3: Validating that Tesseract has been successfully installed on my machine.

You should see the Tesseract version printed to your screen, along with a list of image file format libraries Tesseract is compatible with.

If you instead get the error:

Then Tesseract was not properly installed on your system. Go back to Step #1 and check for errors. Additionally, you may need to update your PATH  variable (for advanced users only).

Step #3: Test out Tesseract OCR

For Tesseract OCR to obtain reasonable results, you’ll want to supply images that are cleanly pre-processed.

When utilizing Tesseract, I recommend:

  • Using as an input image with as high resolution and DPI as possible.
  • Applying thresholding to segment the text from the background.
  • Ensuring the foreground is as clearly segmented from the background as possible (i.e., no pixelations or character deformations).
  • Applying text skew correction to the input image to ensure the text is properly aligned.

Deviations from these recommendations can lead to incorrect OCR results as we’ll find out later in this tutorial.

Now, let’s apply OCR to the following image:

Figure 4: An example image we are going to apply OCR to using Tesseract.

Simply enter the following command in your terminal:

Correct! Tesseract correctly identified, “Testing Tesseract OCR”, and printed it in the terminal.

Next, let’s try this image:

Figure 5: A second example image to apply Optical Character Recognition to using Tesseract.

Enter the following in your terminal, noting the changed input filename:

Figure 6: Tesseract is able to correctly OCR our image.

Success! Tesseract correctly identified the text, “PyImageSearch”, in the image.

Now, let’s try OCR’ing digits as opposed to alphabetic characters:

Figure 7: Using Tesseract to OCR digits in images.

This example uses the command line  digits  switch to only report digits:

Once again, Tesseract correctly identified our string of characters (in this case digits only).

In each of these three situations Tesseract was able to correctly OCR all of our images — and you may even be thinking that Tesseract is the right tool for all OCR uses cases.

However, as we’ll find out in the next section, Tesseract has a number of limitations.

Limitations of Tesseract for OCR

A few weeks ago I was working on a project to recognize the 16-digit numbers on credit cards.

I was easily able to write Python code to localize each of the four groups of 4-digits.

Here is an example 4-digit region of interest:

Figure 8: Localizing a 4-digit grouping of characters on a credit card.

However, when I tried to apply Tesseract to the following image, the results were dissatisfying:

Figure 9: Trying to apply Tesseract to “noisy” images.



Notice how Tesseract reported 5513 , but the image clearly shows 5678 .

Unfortunately, this is a great example of a limitation of Tesseract. While we have segmented the foreground text from background, the pixelated nature of the text “confuses” Tesseract. It’s also likely that Tesseract was not trained on a credit card-like font.

Tesseract is best suited when building document processing pipelines where images are scanned in, pre-processed, and then Optical Character Recognition needs to be applied.

We should note that Tesseract is not an off-the-shelf solution to OCR that will work in all (or even most) image processing and computer vision applications.

In order to accomplish that, you’ll need to apply feature extraction techniques, machine learning, and deep learning.

A great example of applying feature extraction and machine learning to build a handwriting recognition system can be found inside my book, Practical Python and OpenCV.

Summary

Today we learned how to install and configure Tesseract on our machines, the first part in a two part series on using Tesseract for OCR. We then used the  tesseract  binary to apply OCR to input images.

However, we found out that unless our images are cleanly segmented Tesseract will give poor results. In the case of “noisy” input images, we’ll likely obtain better accuracy by training a custom machine learning model to recognize characters in our specific use case.

Tesseract is best suited for situations with high resolution inputs where the foreground text is cleanly segmented from the background.

Next week we’ll learn how to access Tesseract via Python code, so stay tuned.

To be notified when the next blog post on Tesseract goes live, be sure to enter your email address in the form below!

Downloads:

If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 11-page Resource Guide on Computer Vision and Image Search Engines, including exclusive techniques that I don’t post on this blog! Sound good? If so, enter your email address and I’ll send you the code immediately!

, ,

28 Responses to Installing Tesseract for OCR

  1. Mans July 3, 2017 at 11:29 am #

    Thank you for your professionalism and always interesting newsletter.

    • Adrian Rosebrock July 5, 2017 at 6:09 am #

      Thank you Mans, I’m happy to hear you are enjoying the PyImageSearch blog 🙂

      • Raghunandan Palakodety July 18, 2017 at 12:45 am #

        Thank you Dr. Rosebrock, I always follow your articles. I will try to understand how tesseract works now.

  2. Rahul July 3, 2017 at 11:36 am #

    Hello sir, how can we add custom database fonts in tesseract, i followed some on-line sites but no results. Can you please help out on this for my academic project.

    • Adrian Rosebrock July 5, 2017 at 6:08 am #

      Take a look at “Daniel Paull”‘s comment.

  3. Jeff Bass July 3, 2017 at 5:41 pm #

    Thanks for this. While there are free online services for OCR, they are web / gui based and not helpful. The Google Drive OCR option for uploaded documents is also web /gui based. The Google cloud platform OCR does a good job, but it still requires uploading the image to the cloud, subsequently using an API to do the OCR. But Google OCR API is not free and a bit of a pain to use. Tesseract can run locally without uploading anything to the internet. This command line approach worked well for me and I look forward to Part 2 so I can use it from Python.

  4. Anthony The Koala July 3, 2017 at 7:17 pm #

    I am yet to study denoising of images. Nevertheless my question is about denoising a noisy image in order to apply the tesseract package to a denoised image.

    Do you have tutorials in your blog about denoising. So that one can apply denoising techniques on a noisy image then perform OCR using the tesseract?

    Anthony from Sydney NSW

    • Adrian Rosebrock July 5, 2017 at 6:03 am #

      Hi Anthony — next week’s blog post will be an example of how to cleanup images before passing them through Tesseract to increase OCR accuracy.

  5. Greg Pierce July 3, 2017 at 10:01 pm #

    Does this mean that a machine neural network would actually be better than using Tesseract in the average case? I’ve tossed credit cards at these at this point and they seem to perform pretty good.

    • Adrian Rosebrock July 5, 2017 at 6:02 am #

      It really depends on your application and how cleanly segmented your images are. If your images are nice and segmented, Tesseract can do very, very well. If you have a lot of noise and variation in your characters, it might be worth considering training your own neural network.

  6. Mahdi July 4, 2017 at 5:22 am #

    Hi
    Is it possible use for another language?,how?
    Thank you .

    • Adrian Rosebrock July 5, 2017 at 6:00 am #

      Take a look at the “Other Languages” section of the official Tesseract documentation.

  7. Daniel Paull July 4, 2017 at 7:46 am #

    G’day Adrian,

    I’ve recently spent a lot of time getting Tesseract to work nicely for OCR of some documents. I found that disabling the use of dictionaries (since I’m not not parsing prose), using character whitelists and training for specific fonts was needed to get reliable results. As an aside, if you need to train for a specific font, give this website a crack (I have no affiliation with them, but found it useful):

    http://trainyourtesseract.com/

    Noise is a problem for sure. I use morphological operators to fill and smooth, but I still get some problems. Doing your own thresholding is a must as the built in thresholding seems pretty basic and doesn’t do a very good job.

    Once you get it working for a given application, Tesseract can work well. But, it certainly needs a lot of hand holding to get there.

    • Adrian Rosebrock July 5, 2017 at 5:58 am #

      Thank you for sharing, Daniel! I had never used TrainYourTesseract before, I will certainly give it a look.

  8. Thimira Amaratunga July 5, 2017 at 11:08 am #

    Thanks for another great article!
    I haven’t used Tesseract before, but thanks to this article I should be able to 🙂

    Just one thought about the statement “PyImageSearch does not support or recommend Windows for computer vision development”,
    About a year back I would have agreed with you (and I use Linux for most of my development still). But Windows has matured a lot since then, and many computer vision and machine learning tools/libraries does work quite well with Windows now. I think it’s worth a shot giving Windows a chance.
    (Here’s some posts I made on setting up things on Windows: http://www.codesofinterest.com/search/label/Installation)

    Just a thought 🙂

    • Adrian Rosebrock July 7, 2017 at 10:10 am #

      I agree with Windows has matured a lot, especially with the bash inclusion, but I still don’t recommend it for computer vision development. Microsoft is doing some really neat things with their APIs, but Windows itself isn’t conducive to computer vision development. Furthermore, as the writer of PyImageSearch I can only support what I know, which is Unix. The support burden of having to troubleshoot Windows is simply far too high.

  9. Dami July 11, 2017 at 9:52 am #

    Hi Adrian,

    I just recently subscribed to your messages and I have been playing with examples you created. I’m actually pretty new to Python and so far I’m enjoying the ride.

    I have a question i’m hoping you can help me with. I have been testing out the results of running pytesseract with various options. Setting the config to “config=’-psm xx'” works just fine but I can’t get it to read my custom config file which I placed in my tessdata folder (C:\Program Files (x86)\Tesseract-OCR\tessdata\configs). Is there a different folder perhaps which stores the pytesseract config files?

    Please help!

    • Dami July 11, 2017 at 9:54 am #

      Edit : Just wanted to add that using the -c option in the config such as “-c tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyz” also doesn’t seem to work. I still see values returned which are not in that list!

    • Dami July 11, 2017 at 10:32 am #

      Actually…Never mind. Found what I needed. I can’t cancel this comment so…please help me do so.

      Well done with the tutorials though!

      • Adrian Rosebrock July 11, 2017 at 2:08 pm #

        Congrats on resolving the issue Dami!

  10. Petes July 15, 2017 at 1:02 am #

    Adrian,

    Loved this introduction to Tesseract. Looking forward to learning about how to use Tesseract with Python next week. Thanks for providing great content!

    • Adrian Rosebrock July 18, 2017 at 10:10 am #

      Thanks Petes! 🙂

  11. Yuan July 18, 2017 at 8:50 am #

    How to recognize Chinese?

    • Adrian Rosebrock July 18, 2017 at 9:41 am #

      I don’t know if Tesseract recognizes Chinese characters out of the box, but you should consult the documentation regarding the provided languages and how to train your own language classifier if need be.

  12. faizal July 19, 2017 at 12:37 am #

    Hi Adrian,

    Thanks for the nice tutorial. Would it possible to read OCR using webcam video streaming?

    • Adrian Rosebrock July 21, 2017 at 9:00 am #

      Absolutely. You would need to localize the text in each frame first, then pass the text through Tesseract.

Trackbacks/Pingbacks

  1. Using Tesseract OCR with Python - PyImageSearch - July 10, 2017

    […] last week’s blog post we learned how to install the Tesseract binary for Optical Character Recognition […]

  2. Credit card OCR with OpenCV and Python - PyImageSearch - July 17, 2017

    […] a previous blog post, we learned how to install the Tesseract binary and use it for OCR. We then learned how to cleanup images using basic image processing techniques to improve the […]

Leave a Reply