Colab tesseract is not installed or its not in your path. see readme file for more information.

This tutorial explains how to install Tesseract on Linux using both the Debian apt packages manager and the git repositories for other Linux distributions.

Tesseract is the free and probably the best OCR solution in the market. Since 2006 it has been sponsored by Google; previously, it was developed by Hewlett Packard in C and C++ between 1985 and 1998. The system can identify even handwriting; it can learn, increasing its accuracy, and is among the most developed and complete in the market.

If properly trained, it can beat commercial competitors like ABBY; if you are looking for a serious solution for OCR, Tesseract is the most accurate one, but don’t expect massive solutions: it uses a core per process, which means an 8 core processor [hyperthreading accepted] will be able to process 8 or 16 images simultaneously.

Tesseract is a great solution, but before thinking about it, you must know that the last Tesseract’s versions brought big improvements, some of which mean hard work. While training could last for hours or days, recent Tesseract’s versions training may be of days, weeks, or even months, especially if you are looking for a multilingual OCR solution.

Installing Tesseract on Debian and Ubuntu:

To install Tesseract on Debian or Ubuntu Linux distribution, use apt as shown in the screenshot below.

sudo apt install tesseract-ocr -y

This will install Tesseract under /usr/share/tesseract-ocr/4.00/tessdata.

Note: For other Linux distributions, jump to Install Tesseract from Sources.

By default, Tesseract will install the English language pack. To install additional languages, the syntax is the following. In the example below, I will install the Hebrew language pack.

sudo apt install tesseract-ocr-heb

To install all available languages, run:

sudo apt install tesseract-ocr-all -y

For Tesseract to work properly, we will need to use the “convert” command. This command is useful to convert between image formats and resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. This tool is provided by Imagemagick:

sudo apt install imagemagick

Now let’s test Tesseract, find an image containing text and run:

tesseract

Tesseract will extract the text from the image.

When I worked with Tesseract, all we needed was to word count documents. Like with any other program, you can, and must, train it to understand the handwriting.

In advanced text editors, we can define some symbols which can be counted or not, if to count or not numbers, etc., the same with possibility is available on Tesseract.

Optimizing Tesseract:

  • Size Optimization: According to official sources, the optimal pixel size for an image to be processed successfully by Tesseract is 300DPI. We’ll need to process any image using the -r parameter to enforce this DPI. Increasing the DPI will also increase the processing time.
  • Page rotation: If, when scanned, the page isn’t properly positioned and stays 180° or 45°, Tesseract’s accuracy will decrease, so you can use a Python script to detect and fix rotation issues automatically.
  • Border Removal: According to Tesseract’s official man, borders can erroneously be picked as characters, especially dark borders and where there is a gradation variety. Removing borders may be a good step to achieve maximal accuracy with Tesseract.
  • Removing Noise: According to Tesseract sources, noise “is random variation of brightness or color in an image”. We can remove this variation in the binarization step, which means polarizing its colors.

Introduction to Tesseract training process:

Previously this article covered Tesseract’s training process, which evolved to a more manual process that deserves a dedicated article. Therefore this section only covers theoretical information on the training process and instructions to install Tesseract training tools and launch them.

According to Tesseract’s official wiki, we have 3 current options to train our OCR system:

  • “Fine-tune. Starting with an existing trained language, train on your specific additional data. This may work for problems that are close to the existing training data but different in some subtle way, like a particularly unusual font. May work with even a small amount of training data.
  • Cut off the top layer [or some arbitrary number of layers] from the network and retrain a new top layer using the new data. If fine-tuning doesn’t work, this is most likely the next best option. If you start with the most similar-looking script, cutting off the top layer could still work for training a completely new language or script.
  • Retrain from scratch. Unless you have a very representative and sufficiently large training set for your problem, this is a daunting task. If not, you will likely end up with an over-fitted network that does really well on the training data but not on the actual data.

Before continuing to Tesseract training instructions, we will need to install additional libraries:

sudo apt install libicu-dev libicu-dev libcairo2-dev

On Debian-based Linux distributions, install the Tesseract development package, including Tesseract training tools using apt as shown below. If you are not using a Debian-based Linux distribution, read the instructions to install Tesseract training tools from sources.

sudo apt install libtesseract-dev -y

After the installation, you’ll be able to see the training tools under /usr/share/tesseract-ocr/ as shown below.

ls /usr/share/tesseract-ocr

Before starting to train a language, you need to provide Tesseract the content from which to learn.

For this, you need to create the langdata directory and eng subdirectory within Tesseract’s installation main directory. Then create the training text file as shown below.

sudo mkdir /usr/share/tesseract-ocr/langdata/
sudo mkdir /usr/share/tesseract-ocr/langdata/eng/
sudo nano /usr/share/tesseract-ocr/langdata/eng/eng.training_text

Note: Remember to add content to the eng.training_text file.

Once the training text file was added, the syntax to start training a language is the following. The following command is to train the English language defined as “eng”.

./tesstrain.sh --lang eng --langdata_dir /usr/share/tesseract-ocr/langdata --tessdata_dir /usr/share/tesseract-ocr/tessdata

This process may take a long time. Of course, this also depends on your training text files. This is the introduction to the Tesseract training process. We will publish a new article focused on the training process only.

Troubleshooting missing fonts:

In my case, I got an error when trying to train Tesseract. The Arial Bold font was missing. I solved this by running the command below.

sudo apt install ttf-mscorefonts-installer

Install Tesseract from Sources on Linux:

On different Linux distributions, you can get Tesseract using git, as shown below.

git clone //github.com/tesseract-ocr/tesseract.git

Once cloned, go into the tesseract directory by running using cd.

Then run the autogen.sh script as shown below.

The command above creates the installation files; now run the following command to start the installation process.

Run make to start compiling Tesseract.

Then run make install, as shown in the screenshot below.

Execute ldconfig as shown below.

To compile training tools, run the following command.

Then run:

sudo make training-install

Now you can follow the instructions to get started with the training process.

Conclusion:

As you can see, installing Tesseract on Linux is pretty easy, especially on Debian-based Linux distributions. When I used Tesseract, we managed thousands of potential customers uploading handwritten content, images with text, etc. We used 48 core servers, with DatabaseByDesign and then with AWS; we never had a resource problem.

We had an uploader that discriminated between text files like Microsoft Office or Open Office files and images or scanned documents. The uploader determined whatever the OCR or PHP scripts would process an order in the field of text recognition.

In my experience, Tesseract is the best OCR solution available in the market, and it’s open-source.

Thank you for reading this tutorial explaining how to install and configure Tesseract OCR on Linux. Keep following us for additional Linux tips and tutorials.

About the author

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

How to install tesseract package in Python?

Steps of Installation:-.
Install tesseract using windows installer available at: //github.com/UB-Mannheim/tesseract/wiki..
Note the tesseract path from the installation. ... .
pip install pytesseract..
Set the tesseract path in the script before calling "image_to_string":.

How to add tesseract to path Linux?

Install Tesseract from Sources on Linux: Once cloned, go into the tesseract directory by running using cd. Then run the autogen.sh script as shown below. The command above creates the installation files; now run the following command to start the installation process. Run make to start compiling Tesseract.

How to import tesseract in Python?

OPENING A SIMPLE IMAGE.
Import cv2..
Import pytesseract..
Save the test image in the same directory..
Create a variable to store the image using cv2. imread[] function and pass the name of the image as parameter..
To resize the image use cv2. resize[] function and pass the required resolution..
Use cv2. ... .
Add a cv2..

Where is Pytesseract EXE located?

pytesseract. tesseract_cmd = 'C:\Program Files [x86]\Tesseract-OCR\tesseract.exe' to your script.

Chủ Đề