Skip to main content

Python wrapper for Tesseract OCR and Google Vision OCR

Project description

Python wrapper for Tesseract OCR and Google Vision OCR 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 is supported.

Usage

The gpyocr module have two main functions:

  • tesseract_ocr(image, lang=’’, psm=None, config=’’): it returns a tuple (<text recognized>, <confidence>) obtained with Tesseract. The parameters are the same of the command-line Tesseract tool except for the output file.

  • google_vision_ocr(image, langs=None): it returns a tuple (<text recognized>, <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

The parameter image could be:

  • a path to the image file

  • a numpy object (OpenCV)

  • an Image object (Pillow/PIL)

It is possible to get some informations about the Tesseract and Google Vision version 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

$ gpyocr --help

for more information.

Examples

This Python script read the text in the image tests/resources/european-test.png on this repository.

import cv2
from PIL import Image
import gpyocr

# print ('The (quick)...', 87.14)
print(gpyocr.tesseract_ocr('tests/resources/european-test.png'))

# print ('The (quick) etc...', 98.00)
print(gpyocr.google_vision_ocr('tests/resources/european-test.png'))

# support for OpenCV library
print(gpyocr.tesseract_ocr(cv2.imread('tests/resources/european-test.png'))

# support for Pillow library
print(gpyocr.tesseract_ocr(Image.open('tests/resources/european-test.png'))

# support for tesseract parameters
print(gpyocr.tesseract_ocr('tests/resources/european-test.png'), lang='ita', psm=7,
                           config='tessedit_char_whitelist=abc')

# detect Italian and English words with Google Vision
print(gpyocr.google_vision_ocr('tests/resources/european-test.png'), langs=['en, 'it'])

Please see the unit tests for more examples.

Installation

gpyocr is a pip package. To install it in your Python environment run:

$ 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 stable version) then run:

$ sudo ./tesseract_installer.sh 3.05

If you want to try Tesseract 4.00 (it’s still in beta) then run:

$ 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

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

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

Please refer to their documentation for more information about installing and using Google Vision services.

License

Apache 2.0

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

gpyocr-0.2-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

Gpyocr-0.2-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gpyocr-0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: gpyocr-0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for gpyocr-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ca06878501c350d74c13a206cce19f1d58af3a49b04ef37323e2bfec8f8fd715
MD5 0f5f1d137964f75f3af753cfff8d0a86
BLAKE2b-256 b7f51db8e5a5545e7db01af2b9a5872234346323cd47853ee5a6ffd52391569c

See more details on using hashes here.

File details

Details for the file Gpyocr-0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: Gpyocr-0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for Gpyocr-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1007c06d00554640cb8c713c427e359b13be526dea3c4b95b892d82d943ea703
MD5 bcf01e1c2540f9d0e6a42a34aaa9ba3a
BLAKE2b-256 6a0ee07b73c649efac72d5b662a3177767c4cb39e3de7b4e00409c0d6b5e06a6

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