Skip to main content

Python wrapper for Tesseract OCR and Google Vision OCR

Project description

**************************
gpyocr (Google-Python-OCR)
**************************

.. image:: https://img.shields.io/pypi/v/gpyocr.svg
:target: https://pypi.org/project/gpyocr

.. image:: https://img.shields.io/pypi/l/gpyocr.svg
:target: https://pypi.org/project/gpyocr

.. image:: https://img.shields.io/pypi/pyversions/gpyocr.svg
:target: https://pypi.org/project/gpyocr/

.. image:: https://travis-ci.org/ceccoemi/gpyocr.svg?branch=master
:target: https://travis-ci.org/ceccoemi/gpyocr

.. image:: https://codecov.io/gh/ceccoemi/gpyocr/branch/master/graph/badge.svg
:target: https://codecov.io/gh/ceccoemi/gpyocr



Python wrapper for `Tesseract OCR <https://github.com/tesseract-ocr/tesseract>`_ and `Google Vision OCR <https://cloud.google.com/vision/>`_ to perform OCR on images and get a confidence value of the results.

Both OCR engines are Google's products. Tesseract is an open source software that needs some tweaks to get good results, especially if performed on images with poorly defined text. Google Vision OCR engine is a commercial product with much better performance, allowing you to skip the pre-processing jobs on the images.

Only Python 3.6 is supported.

Usage
#####

The ``gpyocr`` module have two main functions:

- ``tesseract_ocr(image, lang='', psm=None, config='')``: it returns a tuple
(*text*, *confidence*) obtained with Tesseract. The parameters are the same of
the `command-line Tesseract tool <https://github.com/tesseract-ocr/tesseract/wiki/Command-Line-Usage>`_
except for the output file.
- ``google_vision_ocr(image, langs=None)``: it returns a tuple
(*text*, *confidence*) obtained with Google Vision API. The `langs` parameter
is a list of languages to look for during the OCR process. More information
about the supported languages are described on
`this page <https://cloud.google.com/vision/docs/languages>`_


The parameter ``image`` could be:

* a string containing the path to the image file
* a numpy object (OpenCV)
* an Image object (Pillow/PIL)


It is possible to get some information about the Tesseract and Google Vision
versions found in the system with ``get_tesseract_version()`` and
``get_google_vision_version()`` respectively.

The installation of the package also provides a command-line tool, please run
::

.. code-block::

$ gpyocr --help

for more information.


Examples
########

Examples to read the text in the image ``tests/resources/european-test.png``
on this repository.

.. code-block:: python

>>> import gpyocr
>>> gpyocr.tesseract_ocr('tests/resources/european-test.png')
('The (quick) [brown] {fox} ... ', 87.13636363636364)
>>> gpyocr.google_vision_ocr('tests/resources/european-test.png')
('The (quick) [brown] {fox} ... ', 98.00000190734863)
>>> import cv2 # support for OpenCV library
>>> gpyocr.tesseract_ocr(cv2.imread('tests/resources/european-test.png'))
('The (quick) [brown] {fox} ... ', 87.13636363636364)
>>> from PIL import Image # support for Pillow library
>>> gpyocr.tesseract_ocr(Image.open('tests/resources/european-test.png'))
('The (quick) [brown] {fox} ... ', 87.13636363636364)
>>> gpyocr.tesseract_ocr(
'tests/resources/european-test.png'), lang='ita', psm=7,
config='tessedit_char_whitelist=abc'
)
('bc aa cb b c a ... ', 18.5)
>>> gpyocr.google_vision_ocr(
'tests/resources/european-test.png', langs=['en', 'it']
)
('The (quick) [brown] {fox} ... ', 87.13636363636364)

Please see the unit tests for more examples.


Installation
############

``gpyocr`` is a pip package available in the Python Package Index.
To install it in your Python environment run:

.. code-block::

$ pip install gpyocr

If you want to run Tesseract with gpyocr you have to install it in your
system. In order to get the confidence value, gpyocr needs Tesseract >= 3.05.
You could install Tesseract with the bash script `tesseract_installer.sh` that
you find in the repository. If you want Tesseract 3.05 (the suggested version)
then run:

.. code-block::

$ sudo ./tesseract_installer.sh 3.05

If you want to try Tesseract 4.00 (whitelist not working) then run:

.. code-block::

$ sudo ./tesseract_installer.sh 4.00

**Note**: this bash script is tested in Ubuntu 18.04 and CentOS 7; if you
don't have these systems, there may be problems installing some dependencies.
You could install all the dependencies by your self and then run:

.. code-block::

$ sudo ./tesseract_installer.sh 3.05 --tesseract-only


To use Google Cloud Vision API, you have to authenticate with

.. code-block::

$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json

Please refer to
`their documentation <https://cloud.google.com/vision/docs/libraries>`_ for
more information about installing and using Google Cloud Vision services.

License
#######

Apache 2.0

Testing
#######

First, install ``pytest``, ``pytest-cov`` and ``pytest-mock``. You could
install them with ``pip install -r requirements.txt``.

Go to the root directory of this repository.

To run unit tests without using the OCR libraries run:

.. code-block::

$ pytest

To run unit tests using the OCR libraries run:

.. code-block::

$ pytest --nomock

To get a coverage report, run

.. code-block::

$ pytest --cov --cov-report term-missing

To test only the Tesseract functions run:

.. code-block::

$ pytest -m tesseract

To test only the Google Cloud Vision functions run:

.. code-block::

$ pytest -m googlevision

How to contribute
#################

This project is developed to be used by a specific application, so it is not
very versatile. If you wish to have new features or if you have any kind of
problems, please feel free to contact me via e-mail or open an issue here on
GitHub.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Gpyocr-1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distributions

gpyocr-1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

Gpyocr-1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file Gpyocr-1.0.tar.gz.

File metadata

  • Download URL: Gpyocr-1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for Gpyocr-1.0.tar.gz
Algorithm Hash digest
SHA256 ef565377eac318737ef47ffffc8a122ea59accd105e1ce266cdde39b7f563c12
MD5 608a4aef4980730717728589fea4b700
BLAKE2b-256 18123579c30359200acaee90460b09ee851aaba703a39e3e9849e4d78eeb9baa

See more details on using hashes here.

File details

Details for the file gpyocr-1.0-py3-none-any.whl.

File metadata

  • Download URL: gpyocr-1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for gpyocr-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f64b3893553cf988080e70937811809b74888f770cd132873e57c5039281e1f
MD5 1540d1bdc7475dc0e3f44ac06316d629
BLAKE2b-256 76d0126487dc22b19dc1da74b122583e3a65728b9252f1576e789cad538c1e71

See more details on using hashes here.

File details

Details for the file Gpyocr-1.0-py3-none-any.whl.

File metadata

  • Download URL: Gpyocr-1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for Gpyocr-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 363831a04b68ac1f453ee11ea786ae26105b5817545ee1b22aeed1dec7f34c1b
MD5 4aecc4257019b47f81c15fc8a1da9664
BLAKE2b-256 15dd114e998aeaa4d2744f8ea3873c36c3fec7eb739bda22a2b724b168433f28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page