Package: gpyocr
Python wrapper forTesseract OCRandGoogle Vision OCRto 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.UsageThegpyocrmodule 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
thecommand-line Tesseract toolexcept for the output file.google_vision_ocr(image, langs=None): it returns a tuple
(text,confidence) obtained with Google Vision API. Thelangsparameter
is a list of languages to look for during the OCR process. More information
about the supported languages are described onthis pageThe parameterimagecould be:a string containing the path to the image filea 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 withget_tesseract_version()andget_google_vision_version()respectively.The installation of the package also provides a command-line tool, please run$ gpyocr --helpfor more information.ExamplesExamples to read the text in the imagetests/resources/european-test.pngon this repository.>>>importgpyocr>>>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)>>>importcv2# support for OpenCV library>>>image=cv2.imread('tests/resources/european-test.png')>>>gpyocr.tesseract_ocr(image)('The (quick) [brown]{fox}... ',87.13636363636364)>>>fromPILimportImage# support for Pillow library>>>image=Image.open('tests/resources/european-test.png')>>>gpyocr.tesseract_ocr(image)('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.Installationgpyocris a pip package available in the Python Package Index.
To install it in your Python environment run:$ pip install gpyocrIf 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 scripttesseract_installer.shthat
you find in the repository. If you want Tesseract 3.05 (the suggested version)
then run:$ sudo ./tesseract_installer 3.05If you want to try Tesseract 4.1.0 then run:$ sudo ./tesseract_installer 4.1.0Note: 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:$ sudo ./tesseract_installer.sh 3.05 --no-dependenciesTo use Google Cloud Vision API, you have to authenticate with$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.jsonPlease refer totheir documentationfor
more information about installing and using Google Cloud Vision services.LicenseApache 2.0TestingFirst, installpytest,pytest-covandpytest-mock. You could
install them withpip install-rrequirements.txt.Go to the root directory of this repository.To run unit tests without using the OCR libraries run:$ pytestTo run unit tests using the OCR libraries run:$ pytest --nomockTo get a coverage report, run$ pytest --cov --cov-report term-missingTo test only the Tesseract functions run:$ pytest -m tesseractTo test only the Google Cloud Vision functions run:$ pytest -m googlevisionHow to contributeThis 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.