A simple, Pillow-friendly, Python wrapper around tesseract-ocr API using Cython


Keywords
Tesseract, tesseract-ocr, OCR, optical, character, recognition, PIL, Pillow, Cython, optical-character-recognition, python-library
License
MIT
Install
pip install tesserocr==2.6.3

Documentation

tesserocr

A simple, Pillow_-friendly, wrapper around the tesseract-ocr API for Optical Character Recognition (OCR).

Github Actions build status

Latest version on PyPi

Supported python versions

tesserocr integrates directly with Tesseract's C++ API using Cython which allows for a simple Pythonic and easy-to-read source code. It enables real concurrent execution when used with Python's threading module by releasing the GIL while processing an image in tesseract.

tesserocr is designed to be Pillow_-friendly but can also be used with image files instead.

Requirements

Requires libtesseract (>=3.04) and libleptonica (>=1.71).

On Debian/Ubuntu:

$ apt-get install tesseract-ocr libtesseract-dev libleptonica-dev pkg-config

You may need to manually compile tesseract for a more recent version. Note that you may need to update your LD_LIBRARY_PATH environment variable to point to the right library versions in case you have multiple tesseract/leptonica installations.

Cython_ (>=0.23) is required for building and optionally Pillow_ to support PIL.Image objects.

Installation

Linux and BSD/MacOS

$ pip install tesserocr

The setup script attempts to detect the include/library dirs (via pkg-config_ if available) but you can override them with your own parameters, e.g.:

$ CPPFLAGS=-I/usr/local/include pip install tesserocr

or

$ python setup.py build_ext -I/usr/local/include

Tested on Linux and BSD/MacOS

Windows

The proposed downloads consist of stand-alone packages containing all the Windows libraries needed for execution. This means that no additional installation of tesseract is required on your system.

The recommended method of installation is via Conda as described below.

Conda

You can use the simonflueckiger channel to install from Conda:

> conda install -c simonflueckiger tesserocr

Or alternatively the conda-forge channel:

> conda install -c conda-forge tesserocr

pip

Download the wheel file corresponding to your Windows platform and Python installation from simonflueckiger/tesserocr-windows_build/releases and install them via:

> pip install <package_name>.whl

Build from source

If you need Windows tessocr package and your Python version is not supported by above mentioned project, you can try to follow step by step instructions for Windows 64bit in Windows.build.md.

tessdata

You may need to point to the tessdata path if it cannot be detected automatically. This can be done by setting the TESSDATA_PREFIX environment variable or by passing the path to PyTessBaseAPI (e.g.: PyTessBaseAPI(path='/usr/share/tessdata')). The path should contain .traineddata files which can be found at https://github.com/tesseract-ocr/tessdata.

Make sure you have the correct version of traineddata for your tesseract --version.

You can list the current supported languages on your system using the get_languages function:

Usage

Initialize and re-use the tesseract API instance to score multiple images:

PyTessBaseAPI exposes several tesseract API methods. Make sure you read their docstrings for more info.

Basic example using available helper functions:

image_to_text and file_to_text can be used with threading to concurrently process multiple images which is highly efficient.

Advanced API Examples

GetComponentImages example:

Orientation and script detection (OSD):

or more simply with OSD_ONLY page segmentation mode:

more human-readable info with tesseract 4+ (demonstrates LSTM engine usage):

Iterator over the classifier choices for a single symbol: