tesserocr-2.5.2/0000755000175000017500000000000014063456316012621 5ustar fzfz00000000000000tesserocr-2.5.2/LICENSE0000666000175000017500000000207213561370402013624 0ustar fzfz00000000000000The MIT License (MIT) Copyright (c) 2015 Fayez Zouheiry Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tesserocr-2.5.2/MANIFEST.in0000644000175000017500000000015114063446477014363 0ustar fzfz00000000000000include README.rst include LICENSE include *.pyx *.pxd include tests/*.py tests/*.png exclude *.cpp *.so tesserocr-2.5.2/PKG-INFO0000644000175000017500000003020214063456316013713 0ustar fzfz00000000000000Metadata-Version: 2.1 Name: tesserocr Version: 2.5.2 Summary: A simple, Pillow-friendly, Python wrapper around tesseract-ocr API using Cython Home-page: https://github.com/sirfz/tesserocr Author: Fayez Zouheiry Author-email: iamfayez@gmail.com License: MIT Description: ========= tesserocr ========= A simple, |Pillow|_-friendly, wrapper around the ``tesseract-ocr`` API for Optical Character Recognition (OCR). .. image:: https://travis-ci.org/sirfz/tesserocr.svg?branch=master :target: https://travis-ci.org/sirfz/tesserocr :alt: TravisCI build status .. image:: https://img.shields.io/pypi/v/tesserocr.svg?maxAge=2592000 :target: https://pypi.python.org/pypi/tesserocr :alt: Latest version on PyPi .. image:: https://img.shields.io/pypi/pyversions/tesserocr.svg?maxAge=2592000 :alt: 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. .. |Pillow| replace:: ``Pillow`` .. _Pillow: http://python-pillow.github.io/ 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. .. _manually compile tesseract: https://github.com/tesseract-ocr/tesseract/wiki/Compiling .. |Cython| replace:: ``Cython`` .. _Cython: http://cython.org/ 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 .. |pkg-config| replace:: **pkg-config** .. _pkg-config: https://pkgconfig.freedesktop.org/ 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 `conda-forge `_ channel to install from Conda: :: > 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 .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`_. .. _Windows.build.md: 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: .. code:: python from tesserocr import get_languages print(get_languages('/usr/share/tessdata')) # or any other path that applies to your system Usage ===== Initialize and re-use the tesseract API instance to score multiple images: .. code:: python from tesserocr import PyTessBaseAPI images = ['sample.jpg', 'sample2.jpg', 'sample3.jpg'] with PyTessBaseAPI() as api: for img in images: api.SetImageFile(img) print(api.GetUTF8Text()) print(api.AllWordConfidences()) # api is automatically finalized when used in a with-statement (context manager). # otherwise api.End() should be explicitly called when it's no longer needed. ``PyTessBaseAPI`` exposes several tesseract API methods. Make sure you read their docstrings for more info. Basic example using available helper functions: .. code:: python import tesserocr from PIL import Image print(tesserocr.tesseract_version()) # print tesseract-ocr version print(tesserocr.get_languages()) # prints tessdata path and list of available languages image = Image.open('sample.jpg') print(tesserocr.image_to_text(image)) # print ocr text from image # or print(tesserocr.file_to_text('sample.jpg')) ``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: ``````````````````````````` .. code:: python from PIL import Image from tesserocr import PyTessBaseAPI, RIL image = Image.open('/usr/src/tesseract/testing/phototest.tif') with PyTessBaseAPI() as api: api.SetImage(image) boxes = api.GetComponentImages(RIL.TEXTLINE, True) print('Found {} textline image components.'.format(len(boxes))) for i, (im, box, _, _) in enumerate(boxes): # im is a PIL image object # box is a dict with x, y, w and h keys api.SetRectangle(box['x'], box['y'], box['w'], box['h']) ocrResult = api.GetUTF8Text() conf = api.MeanTextConf() print(u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, " "confidence: {1}, text: {2}".format(i, conf, ocrResult, **box)) Orientation and script detection (OSD): ``````````````````````````````````````` .. code:: python from PIL import Image from tesserocr import PyTessBaseAPI, PSM with PyTessBaseAPI(psm=PSM.AUTO_OSD) as api: image = Image.open("/usr/src/tesseract/testing/eurotext.tif") api.SetImage(image) api.Recognize() it = api.AnalyseLayout() orientation, direction, order, deskew_angle = it.Orientation() print("Orientation: {:d}".format(orientation)) print("WritingDirection: {:d}".format(direction)) print("TextlineOrder: {:d}".format(order)) print("Deskew angle: {:.4f}".format(deskew_angle)) or more simply with ``OSD_ONLY`` page segmentation mode: .. code:: python from tesserocr import PyTessBaseAPI, PSM with PyTessBaseAPI(psm=PSM.OSD_ONLY) as api: api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif") os = api.DetectOS() print("Orientation: {orientation}\nOrientation confidence: {oconfidence}\n" "Script: {script}\nScript confidence: {sconfidence}".format(**os)) more human-readable info with tesseract 4+ (demonstrates LSTM engine usage): .. code:: python from tesserocr import PyTessBaseAPI, PSM, OEM with PyTessBaseAPI(psm=PSM.OSD_ONLY, oem=OEM.LSTM_ONLY) as api: api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif") os = api.DetectOrientationScript() print("Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\n" "Script: {script_name}\nScript confidence: {script_conf}".format(**os)) Iterator over the classifier choices for a single symbol: ````````````````````````````````````````````````````````` .. code:: python from __future__ import print_function from tesserocr import PyTessBaseAPI, RIL, iterate_level with PyTessBaseAPI() as api: api.SetImageFile('/usr/src/tesseract/testing/phototest.tif') api.SetVariable("save_blob_choices", "T") api.SetRectangle(37, 228, 548, 31) api.Recognize() ri = api.GetIterator() level = RIL.SYMBOL for r in iterate_level(ri, level): symbol = r.GetUTF8Text(level) # r == ri conf = r.Confidence(level) if symbol: print(u'symbol {}, conf: {}'.format(symbol, conf), end='') indent = False ci = r.GetChoiceIterator() for c in ci: if indent: print('\t\t ', end='') print('\t- ', end='') choice = c.GetUTF8Text() # c == ci print(u'{} conf: {}'.format(choice, c.Confidence())) indent = True print('---------------------------------------------') Keywords: Tesseract,tesseract-ocr,OCR,optical character recognition,PIL,Pillow,Cython Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: Topic :: Multimedia :: Graphics :: Capture :: Scanners Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion Classifier: Topic :: Scientific/Engineering :: Image Recognition Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: POSIX Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Programming Language :: Cython Description-Content-Type: text/x-rst tesserocr-2.5.2/README.rst0000644000175000017500000002126414063456067014320 0ustar fzfz00000000000000========= tesserocr ========= A simple, |Pillow|_-friendly, wrapper around the ``tesseract-ocr`` API for Optical Character Recognition (OCR). .. image:: https://travis-ci.org/sirfz/tesserocr.svg?branch=master :target: https://travis-ci.org/sirfz/tesserocr :alt: TravisCI build status .. image:: https://img.shields.io/pypi/v/tesserocr.svg?maxAge=2592000 :target: https://pypi.python.org/pypi/tesserocr :alt: Latest version on PyPi .. image:: https://img.shields.io/pypi/pyversions/tesserocr.svg?maxAge=2592000 :alt: 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. .. |Pillow| replace:: ``Pillow`` .. _Pillow: http://python-pillow.github.io/ 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. .. _manually compile tesseract: https://github.com/tesseract-ocr/tesseract/wiki/Compiling .. |Cython| replace:: ``Cython`` .. _Cython: http://cython.org/ 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 .. |pkg-config| replace:: **pkg-config** .. _pkg-config: https://pkgconfig.freedesktop.org/ 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 `conda-forge `_ channel to install from Conda: :: > 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 .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`_. .. _Windows.build.md: 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: .. code:: python from tesserocr import get_languages print(get_languages('/usr/share/tessdata')) # or any other path that applies to your system Usage ===== Initialize and re-use the tesseract API instance to score multiple images: .. code:: python from tesserocr import PyTessBaseAPI images = ['sample.jpg', 'sample2.jpg', 'sample3.jpg'] with PyTessBaseAPI() as api: for img in images: api.SetImageFile(img) print(api.GetUTF8Text()) print(api.AllWordConfidences()) # api is automatically finalized when used in a with-statement (context manager). # otherwise api.End() should be explicitly called when it's no longer needed. ``PyTessBaseAPI`` exposes several tesseract API methods. Make sure you read their docstrings for more info. Basic example using available helper functions: .. code:: python import tesserocr from PIL import Image print(tesserocr.tesseract_version()) # print tesseract-ocr version print(tesserocr.get_languages()) # prints tessdata path and list of available languages image = Image.open('sample.jpg') print(tesserocr.image_to_text(image)) # print ocr text from image # or print(tesserocr.file_to_text('sample.jpg')) ``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: ``````````````````````````` .. code:: python from PIL import Image from tesserocr import PyTessBaseAPI, RIL image = Image.open('/usr/src/tesseract/testing/phototest.tif') with PyTessBaseAPI() as api: api.SetImage(image) boxes = api.GetComponentImages(RIL.TEXTLINE, True) print('Found {} textline image components.'.format(len(boxes))) for i, (im, box, _, _) in enumerate(boxes): # im is a PIL image object # box is a dict with x, y, w and h keys api.SetRectangle(box['x'], box['y'], box['w'], box['h']) ocrResult = api.GetUTF8Text() conf = api.MeanTextConf() print(u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, " "confidence: {1}, text: {2}".format(i, conf, ocrResult, **box)) Orientation and script detection (OSD): ``````````````````````````````````````` .. code:: python from PIL import Image from tesserocr import PyTessBaseAPI, PSM with PyTessBaseAPI(psm=PSM.AUTO_OSD) as api: image = Image.open("/usr/src/tesseract/testing/eurotext.tif") api.SetImage(image) api.Recognize() it = api.AnalyseLayout() orientation, direction, order, deskew_angle = it.Orientation() print("Orientation: {:d}".format(orientation)) print("WritingDirection: {:d}".format(direction)) print("TextlineOrder: {:d}".format(order)) print("Deskew angle: {:.4f}".format(deskew_angle)) or more simply with ``OSD_ONLY`` page segmentation mode: .. code:: python from tesserocr import PyTessBaseAPI, PSM with PyTessBaseAPI(psm=PSM.OSD_ONLY) as api: api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif") os = api.DetectOS() print("Orientation: {orientation}\nOrientation confidence: {oconfidence}\n" "Script: {script}\nScript confidence: {sconfidence}".format(**os)) more human-readable info with tesseract 4+ (demonstrates LSTM engine usage): .. code:: python from tesserocr import PyTessBaseAPI, PSM, OEM with PyTessBaseAPI(psm=PSM.OSD_ONLY, oem=OEM.LSTM_ONLY) as api: api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif") os = api.DetectOrientationScript() print("Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\n" "Script: {script_name}\nScript confidence: {script_conf}".format(**os)) Iterator over the classifier choices for a single symbol: ````````````````````````````````````````````````````````` .. code:: python from __future__ import print_function from tesserocr import PyTessBaseAPI, RIL, iterate_level with PyTessBaseAPI() as api: api.SetImageFile('/usr/src/tesseract/testing/phototest.tif') api.SetVariable("save_blob_choices", "T") api.SetRectangle(37, 228, 548, 31) api.Recognize() ri = api.GetIterator() level = RIL.SYMBOL for r in iterate_level(ri, level): symbol = r.GetUTF8Text(level) # r == ri conf = r.Confidence(level) if symbol: print(u'symbol {}, conf: {}'.format(symbol, conf), end='') indent = False ci = r.GetChoiceIterator() for c in ci: if indent: print('\t\t ', end='') print('\t- ', end='') choice = c.GetUTF8Text() # c == ci print(u'{} conf: {}'.format(choice, c.Confidence())) indent = True print('---------------------------------------------') tesserocr-2.5.2/setup.cfg0000644000175000017500000000004614063456316014442 0ustar fzfz00000000000000[egg_info] tag_build = tag_date = 0 tesserocr-2.5.2/setup.py0000644000175000017500000002560514063446477014352 0ustar fzfz00000000000000import codecs import errno import glob import itertools import logging import os import re import subprocess import sys from os.path import abspath, dirname from os.path import join as pjoin from os.path import split as psplit from setuptools import setup from setuptools.command.build_ext import build_ext from setuptools.extension import Extension _LOGGER = logging.getLogger() if os.environ.get('DEBUG'): _LOGGER.setLevel(logging.DEBUG) else: _LOGGER.setLevel(logging.INFO) _LOGGER.addHandler(logging.StreamHandler(sys.stderr)) _TESSERACT_MIN_VERSION = '3.04.00' _CYTHON_COMPILE_TIME_ENV = None # find_version from pip https://github.com/pypa/pip/blob/1.5.6/setup.py#L33 here = abspath(dirname(__file__)) EXTRA_COMPILE_ARGS = { 'msvc': ['/std:c11', '-DUSE_STD_NAMESPACE'], 'gcc': ['-std=c++11', '-DUSE_STD_NAMESPACE'], } def read(*parts): return codecs.open(pjoin(here, *parts), 'r').read() def find_version(*file_paths): version_file = read(*file_paths) version_match = re.search('^__version__ = [\'"]([^\'"]*)[\'"]', version_file, re.M) if version_match: return version_match.group(1) raise RuntimeError('Unable to find version string.') if sys.version_info >= (3, 0): def _read_string(s): return s.decode('UTF-8') else: def _read_string(s): return s def major_version(version): versions = version.split('.') major = int(versions[0]) _LOGGER.info('Tesseract major version %s', major) return major def version_to_int(version): subversion = None subtrahend = 0 # Subtracts a certain amount from the version number to differentiate # between alpha, beta and release versions. if 'alpha' in version: version_split = version.split('alpha') subversion = version_split[1] subtrahend = 2 elif 'beta' in version: version_split = version.split('beta') subversion = version_split[1] subtrahend = 1 version = re.search(r'((?:\d+\.)+\d+)', version).group() # Split the groups on ".", take only the first one, and print each # group with leading 0 if needed. To be safe, also handle cases where # an extra group is added to the version string, or if one or two # groups are dropped. version_groups = (version.split('.') + [0, 0])[:3] version_str = '{:02}{:02}{:02}'.format(*map(int, version_groups)) version_str = str((int(version_str, 10) - subtrahend)) # Adds a 2 digit subversion number for the subversionrelease. subversion_str = '00' if subversion is not None and subversion != '': subversion = re.search(r'(?:\d+)', subversion).group() subversion_groups = (subversion.split('-') + [0, 0])[:1] subversion_str = '{:02}'.format(*map(int, subversion_groups)) version_str += subversion_str return int(version_str, 16) def package_config(): """Use pkg-config to get library build parameters and tesseract version.""" p = subprocess.Popen( [ 'pkg-config', '--exists', '--atleast-version={}'.format(_TESSERACT_MIN_VERSION), '--print-errors', 'tesseract', ], stderr=subprocess.PIPE, ) _, error = p.communicate() if p.returncode != 0: if isinstance(error, bytes): error = error.decode() raise Exception(error) p = subprocess.Popen( ['pkg-config', '--libs', '--cflags', 'tesseract'], stdout=subprocess.PIPE ) output, _ = p.communicate() flags = _read_string(output).strip().split() p = subprocess.Popen( ['pkg-config', '--libs', '--cflags', 'lept'], stdout=subprocess.PIPE ) output, _ = p.communicate() flags2 = _read_string(output).strip().split() options = {'-L': 'library_dirs', '-I': 'include_dirs', '-l': 'libraries'} config = {'library_dirs': [], 'include_dirs': [], 'libraries': []} for f in itertools.chain(flags, flags2): try: opt = options[f[:2]] except KeyError: continue val = f[2:] if opt == 'include_dirs' and psplit(val)[1].strip(os.sep) in ( 'leptonica', 'tesseract', ): val = dirname(val) config[opt] += [val] p = subprocess.Popen( ['pkg-config', '--modversion', 'tesseract'], stdout=subprocess.PIPE ) version, _ = p.communicate() version = _read_string(version).strip() _LOGGER.info('Supporting tesseract v%s', version) config['compile_time_env'] = { 'TESSERACT_MAJOR_VERSION': major_version(version), 'TESSERACT_VERSION': version_to_int(version) } _LOGGER.info('Configs from pkg-config: %s', config) return config def find_library(pattern, path_list, version=''): """Help routine to find library.""" result = [] for path in path_list: filepattern = os.path.join(path, pattern) result += glob.glob(filepattern) # ignore debug library result = [i for i in result if not i.endswith('d.lib')] if version: result = [i for i in result if version in i] return result def get_tesseract_version(): """Try to extract version from tesseract otherwise default min version.""" config = {'libraries': ['tesseract', 'lept']} try: p = subprocess.Popen( ['tesseract', '-v'], stderr=subprocess.PIPE, stdout=subprocess.PIPE ) stdout_version, version = p.communicate() version = _read_string(version).strip() if version == '': version = _read_string(stdout_version).strip() version_match = re.search(r'^tesseract ((?:\d+\.)+\d+).*', version, re.M) if version_match: version = version_match.group(1) else: _LOGGER.warning( 'Failed to extract tesseract version number from: %s', version ) version = _TESSERACT_MIN_VERSION except OSError as e: _LOGGER.warning('Failed to extract tesseract version from executable: %s', e) version = _TESSERACT_MIN_VERSION _LOGGER.info('Supporting tesseract v%s', version) config['compile_time_env'] = { 'TESSERACT_MAJOR_VERSION': major_version(version), 'TESSERACT_VERSION': version_to_int(version) } if sys.platform == 'win32': libpaths = os.getenv('LIBPATH', None) if libpaths: libpaths = list(filter(None, libpaths.split(';'))) else: libpaths = [] if version: lib_version = ''.join(version.split('.')[:2]) else: lib_version = None tess_lib = find_library('tesseract*.lib', libpaths, lib_version) if len(tess_lib) >= 1: base = os.path.basename(sorted(tess_lib, reverse=True)[0]) tess_lib = os.path.splitext(base)[0] else: error = 'Tesseract library not found in LIBPATH: {}'.format(libpaths) raise RuntimeError(error) lept_lib = find_library('lept*.lib', libpaths) if len(lept_lib) >= 1: base = os.path.basename(sorted(lept_lib, reverse=True)[0]) lept_lib = os.path.splitext(base)[0] else: error = 'Leptonica library not found in LIBPATH: {}'.format(libpaths) raise RuntimeError(error) includepaths = os.getenv('INCLUDE', None) if includepaths: includepaths = list(filter(None, includepaths.split(';'))) else: includepaths = [] config['libraries'] = [tess_lib, lept_lib] config['library_dirs'] = libpaths config['include_dirs'] = includepaths _LOGGER.info('Building with configs: %s', config) return config def get_build_args(): """Return proper build parameters.""" try: build_args = package_config() except Exception as e: if isinstance(e, OSError): if e.errno != errno.ENOENT: _LOGGER.warning('Failed to run pkg-config: %s', e) else: _LOGGER.warning( 'pkg-config failed to find tesseract/leptonica libraries: %s', e ) build_args = get_tesseract_version() _LOGGER.debug('build parameters: %s', build_args) return build_args def make_extension(): global _CYTHON_COMPILE_TIME_ENV build_args = get_build_args() _CYTHON_COMPILE_TIME_ENV = build_args.pop('compile_time_env') return Extension( 'tesserocr', sources=['tesserocr.pyx'], language='c++', **build_args ) class my_build_ext(build_ext, object): def build_extensions(self): compiler = self.compiler.compiler_type _LOGGER.info('Detected compiler: %s', compiler) extra_args = EXTRA_COMPILE_ARGS.get(compiler, EXTRA_COMPILE_ARGS['gcc']) if isinstance(_CYTHON_COMPILE_TIME_ENV, dict): version = _CYTHON_COMPILE_TIME_ENV.get('TESSERACT_VERSION', 0) else: version = 0 for extension in self.extensions: if version >= 0x3050200: _LOGGER.debug('tesseract >= 03.05.02 requires c++11 compiler support') extension.extra_compile_args = extra_args build_ext.build_extensions(self) def finalize_options(self): from Cython.Build.Dependencies import cythonize self.distribution.ext_modules[:] = cythonize( self.distribution.ext_modules, compile_time_env=_CYTHON_COMPILE_TIME_ENV ) super(my_build_ext, self).finalize_options() setup( name='tesserocr', version=find_version('tesserocr.pyx'), description='A simple, Pillow-friendly, Python wrapper around ' 'tesseract-ocr API using Cython', long_description=read('README.rst'), long_description_content_type='text/x-rst', url='https://github.com/sirfz/tesserocr', author='Fayez Zouheiry', author_email='iamfayez@gmail.com', license='MIT', classifiers=[ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'Topic :: Multimedia :: Graphics :: Capture :: Scanners', 'Topic :: Multimedia :: Graphics :: Graphics Conversion', 'Topic :: Scientific/Engineering :: Image Recognition', 'License :: OSI Approved :: MIT License', 'Operating System :: POSIX', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Programming Language :: Cython', ], keywords='Tesseract,tesseract-ocr,OCR,optical character recognition,' 'PIL,Pillow,Cython', cmdclass={'build_ext': my_build_ext}, ext_modules=[make_extension()], test_suite='tests', setup_requires=['Cython>=0.23'], ) tesserocr-2.5.2/tesseract.pxd0000644000175000017500000004774714063447375015363 0ustar fzfz00000000000000from libcpp cimport bool from libcpp.pair cimport pair from libcpp.vector cimport vector ctypedef const char cchar_t ctypedef const char * cchar_tp ctypedef const unsigned char cuchar_t cdef extern from "leptonica/allheaders.h" nogil: struct Pix: int informat struct Box: int x int y int w int h struct Boxa: int n # number of box in ptr array Box **box # box ptr array struct Pixa: int n # number of Pix in ptr array Pix **pix # the array of ptrs to pix Boxa *boxa # array of boxes struct Pta: int n # actual number of pts float *x float *y # arrays of floats char *getImagelibVersions() char *getLeptonicaVersion() Pix *pixRead(cchar_t *) Pix *pixReadMem(cuchar_t *, size_t) Pix *pixReadMemBmp(cuchar_t *, size_t) int pixWriteMemJpeg(unsigned char **, size_t *, Pix *, int, int) int pixWriteMem(unsigned char **, size_t *, Pix *, int) void pixDestroy(Pix **) void ptaDestroy(Pta **) int setMsgSeverity(int) void pixaDestroy(Pixa **) void boxaDestroy(Boxa **) cdef enum: L_SEVERITY_EXTERNAL = 0 # Get the severity from the environment L_SEVERITY_ALL = 1 # Lowest severity: print all messages L_SEVERITY_DEBUG = 2 # Print debugging and higher messages L_SEVERITY_INFO = 3 # Print informational and higher messages L_SEVERITY_WARNING = 4 # Print warning and higher messages L_SEVERITY_ERROR = 5 # Print error and higher messages L_SEVERITY_NONE = 6 # Highest severity: print no messages cdef extern from "tesseract/publictypes.h" nogil: cdef enum PolyBlockType: PT_UNKNOWN # Type is not yet known. Keep as the first element. PT_FLOWING_TEXT # Text that lives inside a column. PT_HEADING_TEXT # Text that spans more than one column. PT_PULLOUT_TEXT # Text that is in a cross-column pull-out region. PT_EQUATION # Partition belonging to an equation region. PT_INLINE_EQUATION # Partition has inline equation. PT_TABLE # Partition belonging to a table region. PT_VERTICAL_TEXT # Text-line runs vertically. PT_CAPTION_TEXT # Text that belongs to an image. PT_FLOWING_IMAGE # Image that lives inside a column. PT_HEADING_IMAGE # Image that spans more than one column. PT_PULLOUT_IMAGE # Image that is in a cross-column pull-out region. PT_HORZ_LINE # Horizontal Line. PT_VERT_LINE # Vertical Line. PT_NOISE # Lies outside of any column. PT_COUNT cdef extern from "tesseract/publictypes.h" namespace "tesseract" nogil: cdef enum TessOrientation "tesseract::Orientation": ORIENTATION_PAGE_UP ORIENTATION_PAGE_RIGHT ORIENTATION_PAGE_DOWN ORIENTATION_PAGE_LEFT cdef enum TessWritingDirection "tesseract::WritingDirection": WRITING_DIRECTION_LEFT_TO_RIGHT WRITING_DIRECTION_RIGHT_TO_LEFT WRITING_DIRECTION_TOP_TO_BOTTOM cdef enum TessTextlineOrder "tesseract::TextlineOrder": TEXTLINE_ORDER_LEFT_TO_RIGHT TEXTLINE_ORDER_RIGHT_TO_LEFT TEXTLINE_ORDER_TOP_TO_BOTTOM cdef enum TessParagraphJustification "tesseract::ParagraphJustification": JUSTIFICATION_UNKNOWN JUSTIFICATION_LEFT JUSTIFICATION_CENTER JUSTIFICATION_RIGHT cdef extern from "tesseract/unichar.h" nogil: cdef enum StrongScriptDirection: DIR_NEUTRAL # Text contains only neutral characters. DIR_LEFT_TO_RIGHT # Text contains no Right-to-Left characters. DIR_RIGHT_TO_LEFT # Text contains no Left-to-Right characters. DIR_MIX # Text contains a mixture of left-to-right # and right-to-left characters. cdef extern from "tesseract/genericvector.h" nogil: cdef cppclass GenericVector[T]: int size() const int push_back(T) bool empty() const T &operator[](int) const cdef extern from "tesseract/strngs.h" nogil: cdef cppclass STRING: cchar_t *c_str() const STRING &operator=(cchar_t *) cdef extern from "tesseract/ocrclass.h" nogil: ctypedef bool (*CANCEL_FUNC)(void *, int) cdef cppclass ETEXT_DESC: ETEXT_DESC() except + CANCEL_FUNC cancel # returns true to cancel void *cancel_this # this or other data for cancel void set_deadline_msecs(int) cdef extern from "tesseract/pageiterator.h" namespace "tesseract" nogil: cdef cppclass PageIterator: void Begin() void RestartParagraph() bool IsWithinFirstTextlineOfParagraph() const void RestartRow() bool Next(PageIteratorLevel) bool IsAtBeginningOf(PageIteratorLevel) const bool IsAtFinalElement(PageIteratorLevel, PageIteratorLevel) const void SetBoundingBoxComponents(bool, bool) bool BoundingBox(PageIteratorLevel, const int, int *, int *, int *, int *) const bool BoundingBoxInternal(PageIteratorLevel, int *, int *, int *, int *) const bool Empty(PageIteratorLevel) const PolyBlockType BlockType() const Pta *BlockPolygon() const Pix *GetBinaryImage(PageIteratorLevel) const Pix *GetImage(PageIteratorLevel, int, Pix *, int *, int *) const bool Baseline(PageIteratorLevel, int *, int *, int *, int *) const void Orientation(TessOrientation *, TessWritingDirection *, TessTextlineOrder *, float *) const void ParagraphInfo(TessParagraphJustification *, bool *, bool *, int *) const cdef extern from "tesseract/ltrresultiterator.h" namespace "tesseract" nogil: IF TESSERACT_VERSION >= 0x4000000: cdef cppclass LTRResultIterator(PageIterator): char *GetUTF8Text(PageIteratorLevel) const void SetLineSeparator(cchar_t *) void SetParagraphSeparator(cchar_t *) float Confidence(PageIteratorLevel) const void RowAttributes(float *, float *, float *) const cchar_t *WordFontAttributes(bool *, bool *, bool *, bool *, bool *, bool *, int *, int *) const cchar_t *WordRecognitionLanguage() const StrongScriptDirection WordDirection() const bool WordIsFromDictionary() const int BlanksBeforeWord() const bool WordIsNumeric() const bool HasBlamerInfo() const cchar_t *GetBlamerDebug() const cchar_t *GetBlamerMisadaptionDebug() const bool HasTruthString() const bool EquivalentToTruth(cchar_t *) const char *WordTruthUTF8Text() const char *WordNormedUTF8Text() const cchar_t *WordLattice(int *) const bool SymbolIsSuperscript() const bool SymbolIsSubscript() const bool SymbolIsDropcap() const ELIF TESSERACT_VERSION >= 0x3040100: cdef cppclass LTRResultIterator(PageIterator): char *GetUTF8Text(PageIteratorLevel) const void SetLineSeparator(cchar_t *) void SetParagraphSeparator(cchar_t *) float Confidence(PageIteratorLevel) const void RowAttributes(float *, float *, float *) const cchar_t *WordFontAttributes(bool *, bool *, bool *, bool *, bool *, bool *, int *, int *) const cchar_t *WordRecognitionLanguage() const StrongScriptDirection WordDirection() const bool WordIsFromDictionary() const bool WordIsNumeric() const bool HasBlamerInfo() const cchar_t *GetBlamerDebug() const cchar_t *GetBlamerMisadaptionDebug() const bool HasTruthString() const bool EquivalentToTruth(cchar_t *) const char *WordTruthUTF8Text() const char *WordNormedUTF8Text() const cchar_t *WordLattice(int *) const bool SymbolIsSuperscript() const bool SymbolIsSubscript() const bool SymbolIsDropcap() const ELSE: cdef cppclass LTRResultIterator(PageIterator): char *GetUTF8Text(PageIteratorLevel) const void SetLineSeparator(cchar_t *) void SetParagraphSeparator(cchar_t *) float Confidence(PageIteratorLevel) const cchar_t *WordFontAttributes(bool *, bool *, bool *, bool *, bool *, bool *, int *, int *) const cchar_t *WordRecognitionLanguage() const StrongScriptDirection WordDirection() const bool WordIsFromDictionary() const bool WordIsNumeric() const bool HasBlamerInfo() const cchar_t *GetBlamerDebug() const cchar_t *GetBlamerMisadaptionDebug() const bool HasTruthString() const bool EquivalentToTruth(cchar_t *) const char *WordTruthUTF8Text() const char *WordNormedUTF8Text() const cchar_t *WordLattice(int *) const bool SymbolIsSuperscript() const bool SymbolIsSubscript() const bool SymbolIsDropcap() const cdef cppclass ChoiceIterator: ChoiceIterator(const LTRResultIterator &) except + bool Next() cchar_t *GetUTF8Text() const float Confidence() const cdef extern from "tesseract/resultiterator.h" namespace "tesseract" nogil: IF TESSERACT_VERSION >= 0x4000000: cdef cppclass ResultIterator(LTRResultIterator): bool ParagraphIsLtr() const vector[vector[pair[cchar_tp, float]]] *GetBestLSTMSymbolChoices() const ELSE: cdef cppclass ResultIterator(LTRResultIterator): bool ParagraphIsLtr() const cdef extern from "tesseract/renderer.h" namespace "tesseract" nogil: cdef cppclass TessResultRenderer: void insert(TessResultRenderer *) cdef cppclass TessTextRenderer(TessResultRenderer): TessTextRenderer(cchar_t *) except + cdef cppclass TessHOcrRenderer(TessResultRenderer): TessHOcrRenderer(cchar_t *, bool) except + IF TESSERACT_VERSION >= 0x3999800: cdef cppclass TessPDFRenderer(TessResultRenderer): TessPDFRenderer(cchar_t *, cchar_t *, bool) except + ELSE: cdef cppclass TessPDFRenderer(TessResultRenderer): TessPDFRenderer(cchar_t *, cchar_t *) except + cdef cppclass TessUnlvRenderer(TessResultRenderer): TessUnlvRenderer(cchar_t *) except + cdef cppclass TessBoxTextRenderer(TessResultRenderer): TessBoxTextRenderer(cchar_t *) except + IF TESSERACT_VERSION >= 0x3040100: cdef cppclass TessOsdRenderer(TessResultRenderer): TessOsdRenderer(cchar_t *) except + cdef extern from "tesseract/osdetect.h" nogil: struct OSBestResult: int orientation_id int script_id float sconfidence float oconfidence ctypedef int (*get_best_script)(int) struct OSResults: get_best_script get_best_script OSBestResult best_result cdef extern from "tesseract/baseapi.h" namespace "tesseract" nogil: IF TESSERACT_VERSION >= 0x3999800: cdef enum OcrEngineMode: OEM_TESSERACT_ONLY OEM_LSTM_ONLY OEM_TESSERACT_LSTM_COMBINED OEM_DEFAULT ELSE: cdef enum OcrEngineMode: OEM_TESSERACT_ONLY OEM_CUBE_ONLY OEM_TESSERACT_CUBE_COMBINED OEM_DEFAULT cdef enum PageSegMode: PSM_OSD_ONLY, # Orientation and script detection only. PSM_AUTO_OSD, # Automatic page segmentation with orientation and # script detection. (OSD) PSM_AUTO_ONLY, # Automatic page segmentation, but no OSD, or OCR. PSM_AUTO, # Fully automatic page segmentation, but no OSD. PSM_SINGLE_COLUMN, # Assume a single column of text of variable sizes. PSM_SINGLE_BLOCK_VERT_TEXT, # Assume a single uniform block of vertically # aligned text. PSM_SINGLE_BLOCK, # Assume a single uniform block of text. (Default.) PSM_SINGLE_LINE, # Treat the image as a single text line. PSM_SINGLE_WORD, # Treat the image as a single word. PSM_CIRCLE_WORD, # Treat the image as a single word in a circle. PSM_SINGLE_CHAR, # Treat the image as a single character. PSM_SPARSE_TEXT, # Find as much text as possible in no particular order. PSM_SPARSE_TEXT_OSD, # Sparse text with orientation and script det. PSM_RAW_LINE, # Treat the image as a single text line, bypassing # hacks that are Tesseract-specific. PSM_COUNT # Number of enum entries. cdef enum PageIteratorLevel: RIL_BLOCK, # of text/image/separator line. RIL_PARA, # within a block. RIL_TEXTLINE, # within a paragraph. RIL_WORD, # within a textline. RIL_SYMBOL # character within a word. IF TESSERACT_VERSION >= 0x3999800: cdef cppclass TessBaseAPI: TessBaseAPI() except + @staticmethod cchar_t *Version() @staticmethod void ClearPersistentCache() void SetInputName(cchar_t *) cchar_t *GetInputName() void SetInputImage(Pix *) Pix *GetInputImage() int GetSourceYResolution() cchar_t *GetDatapath() void SetOutputName(cchar_t *) bool SetVariable(cchar_t *, cchar_t *) bool SetDebugVariable(cchar_t *, cchar_t *) bool GetIntVariable(cchar_t *, int *) const bool GetBoolVariable(cchar_t *, bool *) const bool GetDoubleVariable(cchar_t *, double *) const cchar_t *GetStringVariable(cchar_t *) const bool GetVariableAsString(cchar_t *, STRING *) int Init(cchar_t *, cchar_t *, OcrEngineMode mode, char **, int, const GenericVector[STRING] *, const GenericVector[STRING] *, bool) int Init(cchar_t *, cchar_t *, OcrEngineMode) int Init(cchar_t *, cchar_t *) cchar_t *GetInitLanguagesAsString() const void GetLoadedLanguagesAsVector(GenericVector[STRING] *) const void GetAvailableLanguagesAsVector(GenericVector[STRING] *) const void InitForAnalysePage() void ReadConfigFile(cchar_t *) void SetPageSegMode(PageSegMode) PageSegMode GetPageSegMode() const char *TesseractRect(cuchar_t *, int, int, int, int, int, int) void ClearAdaptiveClassifier() void SetImage(cuchar_t *, int, int, int, int) void SetImage(Pix *) void SetSourceResolution(int) void SetRectangle(int, int, int, int) Pix *GetThresholdedImage() Boxa *GetRegions(Pixa **) Boxa *GetTextlines(const bool, const int, Pixa **, int **, int **) Boxa *GetStrips(Pixa **, int **) Boxa *GetWords(Pixa **) Boxa *GetConnectedComponents(Pixa **) Boxa *GetComponentImages(const PageIteratorLevel, const bool, const bool, const int, Pixa **, int **, int **) int GetThresholdedImageScaleFactor() const PageIterator *AnalyseLayout(bool) int Recognize(ETEXT_DESC *) int RecognizeForChopTest(ETEXT_DESC *) bool ProcessPages(cchar_t *, cchar_t *, int, TessResultRenderer *) bool ProcessPage(Pix *, int, cchar_t *, cchar_t *, int, TessResultRenderer *) ResultIterator *GetIterator() char *GetUTF8Text() char *GetHOCRText(int) char *GetTSVText(int) char *GetBoxText(int) char *GetUNLVText() bool DetectOrientationScript(int *, float *, cchar_t **, float *) int MeanTextConf() int *AllWordConfidences() bool AdaptToWordStr(PageSegMode, cchar_t *) void Clear() void End() int IsValidWord(cchar_t *) bool IsValidCharacter(cchar_t *) bool GetTextDirection(int *, float *) bool DetectOS(OSResults *); cchar_t *GetUnichar(int) const OcrEngineMode oem() const void set_min_orientation_margin(double) ELSE: cdef cppclass TessBaseAPI: TessBaseAPI() except + @staticmethod cchar_t *Version() @staticmethod void ClearPersistentCache() void SetInputName(cchar_t *) cchar_t *GetInputName() void SetInputImage(Pix *) Pix *GetInputImage() int GetSourceYResolution() cchar_t *GetDatapath() void SetOutputName(cchar_t *) bool SetVariable(cchar_t *, cchar_t *) bool SetDebugVariable(cchar_t *, cchar_t *) bool GetIntVariable(cchar_t *, int *) const bool GetBoolVariable(cchar_t *, bool *) const bool GetDoubleVariable(cchar_t *, double *) const cchar_t *GetStringVariable(cchar_t *) const bool GetVariableAsString(cchar_t *, STRING *) int Init(cchar_t *, cchar_t *, OcrEngineMode mode, char **, int, const GenericVector[STRING] *, const GenericVector[STRING] *, bool) int Init(cchar_t *, cchar_t *, OcrEngineMode) int Init(cchar_t *, cchar_t *) cchar_t *GetInitLanguagesAsString() const void GetLoadedLanguagesAsVector(GenericVector[STRING] *) const void GetAvailableLanguagesAsVector(GenericVector[STRING] *) const void InitForAnalysePage() void ReadConfigFile(cchar_t *) void SetPageSegMode(PageSegMode) PageSegMode GetPageSegMode() const char *TesseractRect(cuchar_t *, int, int, int, int, int, int) void ClearAdaptiveClassifier() void SetImage(cuchar_t *, int, int, int, int) void SetImage(Pix *) void SetSourceResolution(int) void SetRectangle(int, int, int, int) Pix *GetThresholdedImage() Boxa *GetRegions(Pixa **) Boxa *GetTextlines(const bool, const int, Pixa **, int **, int **) Boxa *GetStrips(Pixa **, int **) Boxa *GetWords(Pixa **) Boxa *GetConnectedComponents(Pixa **) Boxa *GetComponentImages(const PageIteratorLevel, const bool, const bool, const int, Pixa **, int **, int **) int GetThresholdedImageScaleFactor() const PageIterator *AnalyseLayout(bool) int Recognize(ETEXT_DESC *) int RecognizeForChopTest(ETEXT_DESC *) bool ProcessPages(cchar_t *, cchar_t *, int, TessResultRenderer *) bool ProcessPage(Pix *, int, cchar_t *, cchar_t *, int, TessResultRenderer *) ResultIterator *GetIterator() char *GetUTF8Text() char *GetHOCRText(int) char *GetBoxText(int) char *GetUNLVText() int MeanTextConf() int *AllWordConfidences() bool AdaptToWordStr(PageSegMode, cchar_t *) void Clear() void End() int IsValidWord(cchar_t *) bool IsValidCharacter(cchar_t *) bool GetTextDirection(int *, float *) bool DetectOS(OSResults *); cchar_t *GetUnichar(int) const OcrEngineMode oem() const void set_min_orientation_margin(double) tesserocr-2.5.2/tesseract5.pxd0000644000175000017500000003302414063454534015422 0ustar fzfz00000000000000from libcpp cimport bool from libcpp.pair cimport pair from libcpp.string cimport string from libcpp.vector cimport vector ctypedef const char cchar_t ctypedef const char * cchar_tp ctypedef const unsigned char cuchar_t cdef extern from "leptonica/allheaders.h" nogil: struct Pix: int informat struct Box: int x int y int w int h struct Boxa: int n # number of box in ptr array Box **box # box ptr array struct Pixa: int n # number of Pix in ptr array Pix **pix # the array of ptrs to pix Boxa *boxa # array of boxes struct Pta: int n # actual number of pts float *x float *y # arrays of floats char *getImagelibVersions() char *getLeptonicaVersion() Pix *pixRead(cchar_t *) Pix *pixReadMem(cuchar_t *, size_t) Pix *pixReadMemBmp(cuchar_t *, size_t) int pixWriteMemJpeg(unsigned char **, size_t *, Pix *, int, int) int pixWriteMem(unsigned char **, size_t *, Pix *, int) void pixDestroy(Pix **) void ptaDestroy(Pta **) int setMsgSeverity(int) void pixaDestroy(Pixa **) void boxaDestroy(Boxa **) cdef enum: L_SEVERITY_EXTERNAL = 0 # Get the severity from the environment L_SEVERITY_ALL = 1 # Lowest severity: print all messages L_SEVERITY_DEBUG = 2 # Print debugging and higher messages L_SEVERITY_INFO = 3 # Print informational and higher messages L_SEVERITY_WARNING = 4 # Print warning and higher messages L_SEVERITY_ERROR = 5 # Print error and higher messages L_SEVERITY_NONE = 6 # Highest severity: print no messages cdef extern from "tesseract/publictypes.h" namespace "tesseract" nogil: cdef enum PolyBlockType: PT_UNKNOWN # Type is not yet known. Keep as the first element. PT_FLOWING_TEXT # Text that lives inside a column. PT_HEADING_TEXT # Text that spans more than one column. PT_PULLOUT_TEXT # Text that is in a cross-column pull-out region. PT_EQUATION # Partition belonging to an equation region. PT_INLINE_EQUATION # Partition has inline equation. PT_TABLE # Partition belonging to a table region. PT_VERTICAL_TEXT # Text-line runs vertically. PT_CAPTION_TEXT # Text that belongs to an image. PT_FLOWING_IMAGE # Image that lives inside a column. PT_HEADING_IMAGE # Image that spans more than one column. PT_PULLOUT_IMAGE # Image that is in a cross-column pull-out region. PT_HORZ_LINE # Horizontal Line. PT_VERT_LINE # Vertical Line. PT_NOISE # Lies outside of any column. PT_COUNT cdef extern from "tesseract/publictypes.h" namespace "tesseract" nogil: cdef enum TessOrientation "tesseract::Orientation": ORIENTATION_PAGE_UP ORIENTATION_PAGE_RIGHT ORIENTATION_PAGE_DOWN ORIENTATION_PAGE_LEFT cdef enum TessWritingDirection "tesseract::WritingDirection": WRITING_DIRECTION_LEFT_TO_RIGHT WRITING_DIRECTION_RIGHT_TO_LEFT WRITING_DIRECTION_TOP_TO_BOTTOM cdef enum TessTextlineOrder "tesseract::TextlineOrder": TEXTLINE_ORDER_LEFT_TO_RIGHT TEXTLINE_ORDER_RIGHT_TO_LEFT TEXTLINE_ORDER_TOP_TO_BOTTOM cdef enum TessParagraphJustification "tesseract::ParagraphJustification": JUSTIFICATION_UNKNOWN JUSTIFICATION_LEFT JUSTIFICATION_CENTER JUSTIFICATION_RIGHT cdef extern from "tesseract/unichar.h" namespace "tesseract" nogil: cdef enum StrongScriptDirection: DIR_NEUTRAL # Text contains only neutral characters. DIR_LEFT_TO_RIGHT # Text contains no Right-to-Left characters. DIR_RIGHT_TO_LEFT # Text contains no Left-to-Right characters. DIR_MIX # Text contains a mixture of left-to-right # and right-to-left characters. cdef extern from "tesseract/ocrclass.h" namespace "tesseract" nogil: ctypedef bool (*CANCEL_FUNC)(void *, int) cdef cppclass ETEXT_DESC: ETEXT_DESC() except + CANCEL_FUNC cancel # returns true to cancel void *cancel_this # this or other data for cancel void set_deadline_msecs(int) cdef extern from "tesseract/pageiterator.h" namespace "tesseract" nogil: cdef cppclass PageIterator: void Begin() void RestartParagraph() bool IsWithinFirstTextlineOfParagraph() const void RestartRow() bool Next(PageIteratorLevel) bool IsAtBeginningOf(PageIteratorLevel) const bool IsAtFinalElement(PageIteratorLevel, PageIteratorLevel) const void SetBoundingBoxComponents(bool, bool) bool BoundingBox(PageIteratorLevel, const int, int *, int *, int *, int *) const bool BoundingBoxInternal(PageIteratorLevel, int *, int *, int *, int *) const bool Empty(PageIteratorLevel) const PolyBlockType BlockType() const Pta *BlockPolygon() const Pix *GetBinaryImage(PageIteratorLevel) const Pix *GetImage(PageIteratorLevel, int, Pix *, int *, int *) const bool Baseline(PageIteratorLevel, int *, int *, int *, int *) const void Orientation(TessOrientation *, TessWritingDirection *, TessTextlineOrder *, float *) const void ParagraphInfo(TessParagraphJustification *, bool *, bool *, int *) const cdef extern from "tesseract/ltrresultiterator.h" namespace "tesseract" nogil: cdef cppclass LTRResultIterator(PageIterator): char *GetUTF8Text(PageIteratorLevel) const void SetLineSeparator(cchar_t *) void SetParagraphSeparator(cchar_t *) float Confidence(PageIteratorLevel) const void RowAttributes(float *, float *, float *) const cchar_t *WordFontAttributes(bool *, bool *, bool *, bool *, bool *, bool *, int *, int *) const cchar_t *WordRecognitionLanguage() const StrongScriptDirection WordDirection() const bool WordIsFromDictionary() const int BlanksBeforeWord() const bool WordIsNumeric() const bool HasBlamerInfo() const cchar_t *GetBlamerDebug() const cchar_t *GetBlamerMisadaptionDebug() const bool HasTruthString() const bool EquivalentToTruth(cchar_t *) const char *WordTruthUTF8Text() const char *WordNormedUTF8Text() const cchar_t *WordLattice(int *) const bool SymbolIsSuperscript() const bool SymbolIsSubscript() const bool SymbolIsDropcap() const cdef cppclass ChoiceIterator: ChoiceIterator(const LTRResultIterator &) except + bool Next() cchar_t *GetUTF8Text() const float Confidence() const cdef extern from "tesseract/resultiterator.h" namespace "tesseract" nogil: cdef cppclass ResultIterator(LTRResultIterator): bool ParagraphIsLtr() const vector[vector[pair[cchar_tp, float]]] *GetBestLSTMSymbolChoices() const cdef extern from "tesseract/renderer.h" namespace "tesseract" nogil: cdef cppclass TessResultRenderer: void insert(TessResultRenderer *) cdef cppclass TessTextRenderer(TessResultRenderer): TessTextRenderer(cchar_t *) except + cdef cppclass TessHOcrRenderer(TessResultRenderer): TessHOcrRenderer(cchar_t *, bool) except + cdef cppclass TessPDFRenderer(TessResultRenderer): TessPDFRenderer(cchar_t *, cchar_t *, bool) except + cdef cppclass TessUnlvRenderer(TessResultRenderer): TessUnlvRenderer(cchar_t *) except + cdef cppclass TessBoxTextRenderer(TessResultRenderer): TessBoxTextRenderer(cchar_t *) except + cdef cppclass TessOsdRenderer(TessResultRenderer): TessOsdRenderer(cchar_t *) except + cdef extern from "tesseract/osdetect.h" namespace "tesseract" nogil: struct OSBestResult: int orientation_id int script_id float sconfidence float oconfidence ctypedef int (*get_best_script)(int) struct OSResults: get_best_script get_best_script OSBestResult best_result cdef extern from "tesseract/baseapi.h" namespace "tesseract" nogil: cdef enum OcrEngineMode: OEM_TESSERACT_ONLY OEM_LSTM_ONLY OEM_TESSERACT_LSTM_COMBINED OEM_DEFAULT cdef enum PageSegMode: PSM_OSD_ONLY, # Orientation and script detection only. PSM_AUTO_OSD, # Automatic page segmentation with orientation and # script detection. (OSD) PSM_AUTO_ONLY, # Automatic page segmentation, but no OSD, or OCR. PSM_AUTO, # Fully automatic page segmentation, but no OSD. PSM_SINGLE_COLUMN, # Assume a single column of text of variable sizes. PSM_SINGLE_BLOCK_VERT_TEXT, # Assume a single uniform block of vertically # aligned text. PSM_SINGLE_BLOCK, # Assume a single uniform block of text. (Default.) PSM_SINGLE_LINE, # Treat the image as a single text line. PSM_SINGLE_WORD, # Treat the image as a single word. PSM_CIRCLE_WORD, # Treat the image as a single word in a circle. PSM_SINGLE_CHAR, # Treat the image as a single character. PSM_SPARSE_TEXT, # Find as much text as possible in no particular order. PSM_SPARSE_TEXT_OSD, # Sparse text with orientation and script det. PSM_RAW_LINE, # Treat the image as a single text line, bypassing # hacks that are Tesseract-specific. PSM_COUNT # Number of enum entries. cdef enum PageIteratorLevel: RIL_BLOCK, # of text/image/separator line. RIL_PARA, # within a block. RIL_TEXTLINE, # within a paragraph. RIL_WORD, # within a textline. RIL_SYMBOL # character within a word. cdef cppclass TessBaseAPI: TessBaseAPI() except + @staticmethod cchar_t *Version() @staticmethod void ClearPersistentCache() void SetInputName(cchar_t *) cchar_t *GetInputName() void SetInputImage(Pix *) Pix *GetInputImage() int GetSourceYResolution() cchar_t *GetDatapath() void SetOutputName(cchar_t *) bool SetVariable(cchar_t *, cchar_t *) bool SetDebugVariable(cchar_t *, cchar_t *) bool GetIntVariable(cchar_t *, int *) const bool GetBoolVariable(cchar_t *, bool *) const bool GetDoubleVariable(cchar_t *, double *) const cchar_t *GetStringVariable(cchar_t *) const bool GetVariableAsString(cchar_t *, string *) int Init(cchar_t *, cchar_t *, OcrEngineMode mode, char **, int, const vector[string] *, const vector[string] *, bool) int Init(cchar_t *, cchar_t *, OcrEngineMode) int Init(cchar_t *, cchar_t *) cchar_t *GetInitLanguagesAsString() const void GetLoadedLanguagesAsVector(vector[string] *) const void GetAvailableLanguagesAsVector(vector[string] *) const void InitForAnalysePage() void ReadConfigFile(cchar_t *) void SetPageSegMode(PageSegMode) PageSegMode GetPageSegMode() const char *TesseractRect(cuchar_t *, int, int, int, int, int, int) void ClearAdaptiveClassifier() void SetImage(cuchar_t *, int, int, int, int) void SetImage(Pix *) void SetSourceResolution(int) void SetRectangle(int, int, int, int) Pix *GetThresholdedImage() Boxa *GetRegions(Pixa **) Boxa *GetTextlines(const bool, const int, Pixa **, int **, int **) Boxa *GetStrips(Pixa **, int **) Boxa *GetWords(Pixa **) Boxa *GetConnectedComponents(Pixa **) Boxa *GetComponentImages(const PageIteratorLevel, const bool, const bool, const int, Pixa **, int **, int **) int GetThresholdedImageScaleFactor() const PageIterator *AnalyseLayout(bool) int Recognize(ETEXT_DESC *) bool ProcessPages(cchar_t *, cchar_t *, int, TessResultRenderer *) bool ProcessPage(Pix *, int, cchar_t *, cchar_t *, int, TessResultRenderer *) ResultIterator *GetIterator() char *GetUTF8Text() char *GetHOCRText(int) char *GetTSVText(int) char *GetBoxText(int) char *GetUNLVText() bool DetectOrientationScript(int *, float *, cchar_t **, float *) int MeanTextConf() int *AllWordConfidences() bool AdaptToWordStr(PageSegMode, cchar_t *) void Clear() void End() int IsValidWord(cchar_t *) bool IsValidCharacter(cchar_t *) bool GetTextDirection(int *, float *) bool DetectOS(OSResults *); cchar_t *GetUnichar(int) const OcrEngineMode oem() const void set_min_orientation_margin(double) tesserocr-2.5.2/tesserocr.egg-info/0000755000175000017500000000000014063456316016324 5ustar fzfz00000000000000tesserocr-2.5.2/tesserocr.egg-info/PKG-INFO0000666000175000017500000003020214063456315017421 0ustar fzfz00000000000000Metadata-Version: 2.1 Name: tesserocr Version: 2.5.2 Summary: A simple, Pillow-friendly, Python wrapper around tesseract-ocr API using Cython Home-page: https://github.com/sirfz/tesserocr Author: Fayez Zouheiry Author-email: iamfayez@gmail.com License: MIT Description: ========= tesserocr ========= A simple, |Pillow|_-friendly, wrapper around the ``tesseract-ocr`` API for Optical Character Recognition (OCR). .. image:: https://travis-ci.org/sirfz/tesserocr.svg?branch=master :target: https://travis-ci.org/sirfz/tesserocr :alt: TravisCI build status .. image:: https://img.shields.io/pypi/v/tesserocr.svg?maxAge=2592000 :target: https://pypi.python.org/pypi/tesserocr :alt: Latest version on PyPi .. image:: https://img.shields.io/pypi/pyversions/tesserocr.svg?maxAge=2592000 :alt: 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. .. |Pillow| replace:: ``Pillow`` .. _Pillow: http://python-pillow.github.io/ 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. .. _manually compile tesseract: https://github.com/tesseract-ocr/tesseract/wiki/Compiling .. |Cython| replace:: ``Cython`` .. _Cython: http://cython.org/ 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 .. |pkg-config| replace:: **pkg-config** .. _pkg-config: https://pkgconfig.freedesktop.org/ 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 `conda-forge `_ channel to install from Conda: :: > 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 .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`_. .. _Windows.build.md: 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: .. code:: python from tesserocr import get_languages print(get_languages('/usr/share/tessdata')) # or any other path that applies to your system Usage ===== Initialize and re-use the tesseract API instance to score multiple images: .. code:: python from tesserocr import PyTessBaseAPI images = ['sample.jpg', 'sample2.jpg', 'sample3.jpg'] with PyTessBaseAPI() as api: for img in images: api.SetImageFile(img) print(api.GetUTF8Text()) print(api.AllWordConfidences()) # api is automatically finalized when used in a with-statement (context manager). # otherwise api.End() should be explicitly called when it's no longer needed. ``PyTessBaseAPI`` exposes several tesseract API methods. Make sure you read their docstrings for more info. Basic example using available helper functions: .. code:: python import tesserocr from PIL import Image print(tesserocr.tesseract_version()) # print tesseract-ocr version print(tesserocr.get_languages()) # prints tessdata path and list of available languages image = Image.open('sample.jpg') print(tesserocr.image_to_text(image)) # print ocr text from image # or print(tesserocr.file_to_text('sample.jpg')) ``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: ``````````````````````````` .. code:: python from PIL import Image from tesserocr import PyTessBaseAPI, RIL image = Image.open('/usr/src/tesseract/testing/phototest.tif') with PyTessBaseAPI() as api: api.SetImage(image) boxes = api.GetComponentImages(RIL.TEXTLINE, True) print('Found {} textline image components.'.format(len(boxes))) for i, (im, box, _, _) in enumerate(boxes): # im is a PIL image object # box is a dict with x, y, w and h keys api.SetRectangle(box['x'], box['y'], box['w'], box['h']) ocrResult = api.GetUTF8Text() conf = api.MeanTextConf() print(u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, " "confidence: {1}, text: {2}".format(i, conf, ocrResult, **box)) Orientation and script detection (OSD): ``````````````````````````````````````` .. code:: python from PIL import Image from tesserocr import PyTessBaseAPI, PSM with PyTessBaseAPI(psm=PSM.AUTO_OSD) as api: image = Image.open("/usr/src/tesseract/testing/eurotext.tif") api.SetImage(image) api.Recognize() it = api.AnalyseLayout() orientation, direction, order, deskew_angle = it.Orientation() print("Orientation: {:d}".format(orientation)) print("WritingDirection: {:d}".format(direction)) print("TextlineOrder: {:d}".format(order)) print("Deskew angle: {:.4f}".format(deskew_angle)) or more simply with ``OSD_ONLY`` page segmentation mode: .. code:: python from tesserocr import PyTessBaseAPI, PSM with PyTessBaseAPI(psm=PSM.OSD_ONLY) as api: api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif") os = api.DetectOS() print("Orientation: {orientation}\nOrientation confidence: {oconfidence}\n" "Script: {script}\nScript confidence: {sconfidence}".format(**os)) more human-readable info with tesseract 4+ (demonstrates LSTM engine usage): .. code:: python from tesserocr import PyTessBaseAPI, PSM, OEM with PyTessBaseAPI(psm=PSM.OSD_ONLY, oem=OEM.LSTM_ONLY) as api: api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif") os = api.DetectOrientationScript() print("Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\n" "Script: {script_name}\nScript confidence: {script_conf}".format(**os)) Iterator over the classifier choices for a single symbol: ````````````````````````````````````````````````````````` .. code:: python from __future__ import print_function from tesserocr import PyTessBaseAPI, RIL, iterate_level with PyTessBaseAPI() as api: api.SetImageFile('/usr/src/tesseract/testing/phototest.tif') api.SetVariable("save_blob_choices", "T") api.SetRectangle(37, 228, 548, 31) api.Recognize() ri = api.GetIterator() level = RIL.SYMBOL for r in iterate_level(ri, level): symbol = r.GetUTF8Text(level) # r == ri conf = r.Confidence(level) if symbol: print(u'symbol {}, conf: {}'.format(symbol, conf), end='') indent = False ci = r.GetChoiceIterator() for c in ci: if indent: print('\t\t ', end='') print('\t- ', end='') choice = c.GetUTF8Text() # c == ci print(u'{} conf: {}'.format(choice, c.Confidence())) indent = True print('---------------------------------------------') Keywords: Tesseract,tesseract-ocr,OCR,optical character recognition,PIL,Pillow,Cython Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: Topic :: Multimedia :: Graphics :: Capture :: Scanners Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion Classifier: Topic :: Scientific/Engineering :: Image Recognition Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: POSIX Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Programming Language :: Cython Description-Content-Type: text/x-rst tesserocr-2.5.2/tesserocr.egg-info/SOURCES.txt0000666000175000017500000000044614063456315020217 0ustar fzfz00000000000000LICENSE MANIFEST.in README.rst setup.py tesseract.pxd tesseract5.pxd tesserocr.pyx tesserocr_experiment.pyx tesserocr.egg-info/PKG-INFO tesserocr.egg-info/SOURCES.txt tesserocr.egg-info/dependency_links.txt tesserocr.egg-info/top_level.txt tests/__init__.py tests/eurotext.png tests/test_api.pytesserocr-2.5.2/tesserocr.egg-info/dependency_links.txt0000666000175000017500000000000114063456315022375 0ustar fzfz00000000000000 tesserocr-2.5.2/tesserocr.egg-info/top_level.txt0000666000175000017500000000001214063456315021052 0ustar fzfz00000000000000tesserocr tesserocr-2.5.2/tesserocr.pyx0000644000175000017500000027412114063447375015410 0ustar fzfz00000000000000#!python #cython: c_string_type=unicode, c_string_encoding=utf-8, language_level=3 """Python wrapper around the Tesseract-OCR C++ API This module provides a wrapper class :class:`PyTessBaseAPI` to call Tesseract API methods. See :class:`PyTessBaseAPI` for details. In addition, helper functions are provided for ocr operations: >>> text = image_to_text(Image.open('./image.jpg').convert('L'), lang='eng') >>> text = file_to_text('./image.jpg', psm=PSM.AUTO) >>> print tesseract_version() tesseract 3.04.00 leptonica-1.72 libjpeg 8d (libjpeg-turbo 1.3.0) : libpng 1.2.51 : libtiff 4.0.3 : zlib 1.2.8 >>> get_languages() ('/usr/share/tesseract-ocr/tessdata/', ['eng', 'osd', 'equ']) """ __version__ = '2.5.2' import os from io import BytesIO from os.path import abspath, join try: from PIL import Image except ImportError: # PIL.Image won't be supported pass IF TESSERACT_MAJOR_VERSION < 5: from tesseract cimport * ELSE: from tesseract5 cimport * from libc.stdlib cimport malloc, free from libcpp.pair cimport pair from libcpp.vector cimport vector from cython.operator cimport preincrement as inc, dereference as deref from cpython.version cimport PY_MAJOR_VERSION cdef bytes _b(s): if PY_MAJOR_VERSION > 3: if isinstance(s, str): return s.encode('UTF-8') elif isinstance(s, unicode): return s.encode('UTF-8') return s # default parameters setMsgSeverity(L_SEVERITY_NONE) # suppress leptonica error messages cdef TessBaseAPI _api _api.SetVariable('debug_file', '/dev/null') # suppress tesseract debug messages _api.Init(NULL, NULL) IF TESSERACT_VERSION >= 0x3999800: cdef _DEFAULT_PATH = _api.GetDatapath() # "tessdata/" is not appended by tesseract since commit dba13db ELSE: cdef _DEFAULT_PATH = abspath(join(_api.GetDatapath(), os.pardir)) + os.sep _init_lang = _api.GetInitLanguagesAsString() if _init_lang == '': _init_lang = 'eng' cdef _DEFAULT_LANG = _init_lang _api.End() TessBaseAPI.ClearPersistentCache() cdef class _Enum: def __init__(self): raise TypeError('{} is an enum and cannot be instantiated'.format(type(self).__name__)) cdef class OEM(_Enum): """An enum that defines available OCR engine modes. Attributes: TESSERACT_ONLY: Run Tesseract only - fastest LSTM_ONLY: Run just the LSTM line recognizer. (>=v4.00) TESSERACT_LSTM_COMBINED: Run the LSTM recognizer, but allow fallback to Tesseract when things get difficult. (>=v4.00) CUBE_ONLY: Specify this mode when calling Init*(), to indicate that any of the above modes should be automatically inferred from the variables in the language-specific config, command-line configs, or if not specified in any of the above should be set to the default `OEM.TESSERACT_ONLY`. TESSERACT_CUBE_COMBINED: Run Cube only - better accuracy, but slower. DEFAULT: Run both and combine results - best accuracy. """ TESSERACT_ONLY = OEM_TESSERACT_ONLY IF TESSERACT_VERSION >= 0x3999800: LSTM_ONLY = OEM_LSTM_ONLY TESSERACT_LSTM_COMBINED = OEM_TESSERACT_LSTM_COMBINED ELSE: CUBE_ONLY = OEM_CUBE_ONLY TESSERACT_CUBE_COMBINED = OEM_TESSERACT_CUBE_COMBINED DEFAULT = OEM_DEFAULT cdef class PSM(_Enum): """An enum that defines all available page segmentation modes. Attributes: OSD_ONLY: Orientation and script detection only. AUTO_OSD: Automatic page segmentation with orientation and script detection. (OSD) AUTO_ONLY: Automatic page segmentation, but no OSD, or OCR. AUTO: Fully automatic page segmentation, but no OSD. (:mod:`tesserocr` default) SINGLE_COLUMN: Assume a single column of text of variable sizes. SINGLE_BLOCK_VERT_TEXT: Assume a single uniform block of vertically aligned text. SINGLE_BLOCK: Assume a single uniform block of text. SINGLE_LINE: Treat the image as a single text line. SINGLE_WORD: Treat the image as a single word. CIRCLE_WORD: Treat the image as a single word in a circle. SINGLE_CHAR: Treat the image as a single character. SPARSE_TEXT: Find as much text as possible in no particular order. SPARSE_TEXT_OSD: Sparse text with orientation and script det. RAW_LINE: Treat the image as a single text line, bypassing hacks that are Tesseract-specific. COUNT: Number of enum entries. """ OSD_ONLY = PSM_OSD_ONLY """Orientation and script detection only.""" AUTO_OSD = PSM_AUTO_OSD """Automatic page segmentation with orientation and script detection. (OSD)""" AUTO_ONLY = PSM_AUTO_ONLY """Automatic page segmentation, but no OSD, or OCR.""" AUTO = PSM_AUTO """Fully automatic page segmentation, but no OSD. (tesserocr default)""" SINGLE_COLUMN = PSM_SINGLE_COLUMN """Assume a single column of text of variable sizes.""" SINGLE_BLOCK_VERT_TEXT = PSM_SINGLE_BLOCK_VERT_TEXT """Assume a single uniform block of vertically aligned text.""" SINGLE_BLOCK = PSM_SINGLE_BLOCK """Assume a single uniform block of text. (Default.)""" SINGLE_LINE = PSM_SINGLE_LINE """Treat the image as a single text line.""" SINGLE_WORD = PSM_SINGLE_WORD """Treat the image as a single word.""" CIRCLE_WORD = PSM_CIRCLE_WORD """Treat the image as a single word in a circle.""" SINGLE_CHAR = PSM_SINGLE_CHAR """Treat the image as a single character.""" SPARSE_TEXT = PSM_SPARSE_TEXT """Find as much text as possible in no particular order.""" SPARSE_TEXT_OSD = PSM_SPARSE_TEXT_OSD """Sparse text with orientation and script det.""" RAW_LINE = PSM_RAW_LINE """Treat the image as a single text line, bypassing hacks that are Tesseract-specific.""" COUNT = PSM_COUNT """Number of enum entries.""" cdef class RIL(_Enum): """An enum that defines available Page Iterator levels. Attributes: BLOCK: of text/image/separator line. PARA: within a block. TEXTLINE: within a paragraph. WORD: within a textline. SYMBOL: character within a word. """ BLOCK = RIL_BLOCK """of text/image/separator line.""" PARA = RIL_PARA """within a block.""" TEXTLINE = RIL_TEXTLINE """within a paragraph.""" WORD = RIL_WORD """within a textline.""" SYMBOL = RIL_SYMBOL """character within a word.""" cdef class PT(_Enum): """An enum that defines available Poly Block types. Attributes: UNKNOWN: Type is not yet known. Keep as the first element. FLOWING_TEXT: Text that lives inside a column. HEADING_TEXT: Text that spans more than one column. PULLOUT_TEXT: Text that is in a cross-column pull-out region. EQUATION: Partition belonging to an equation region. INLINE_EQUATION: Partition has inline equation. TABLE: Partition belonging to a table region. VERTICAL_TEXT: Text-line runs vertically. CAPTION_TEXT: Text that belongs to an image. FLOWING_IMAGE: Image that lives inside a column. HEADING_IMAGE: Image that spans more than one column. PULLOUT_IMAGE: Image that is in a cross-column pull-out region. HORZ_LINE: Horizontal Line. VERT_LINE: Vertical Line. NOISE: Lies outside of any column. COUNT: Count """ UNKNOWN = PT_UNKNOWN """Type is not yet known. Keep as the first element.""" FLOWING_TEXT = PT_FLOWING_TEXT """Text that lives inside a column.""" HEADING_TEXT = PT_HEADING_TEXT """Text that spans more than one column.""" PULLOUT_TEXT = PT_PULLOUT_TEXT """Text that is in a cross-column pull-out region.""" EQUATION = PT_EQUATION """Partition belonging to an equation region.""" INLINE_EQUATION = PT_INLINE_EQUATION """Partition has inline equation.""" TABLE = PT_TABLE """Partition belonging to a table region.""" VERTICAL_TEXT = PT_VERTICAL_TEXT """Text-line runs vertically.""" CAPTION_TEXT = PT_CAPTION_TEXT """Text that belongs to an image.""" FLOWING_IMAGE = PT_FLOWING_IMAGE """Image that lives inside a column.""" HEADING_IMAGE = PT_HEADING_IMAGE """Image that spans more than one column.""" PULLOUT_IMAGE = PT_PULLOUT_IMAGE """Image that is in a cross-column pull-out region.""" HORZ_LINE = PT_HORZ_LINE """Horizontal Line.""" VERT_LINE = PT_VERT_LINE """Vertical Line.""" NOISE = PT_NOISE """Lies outside of any column.""" COUNT = PT_COUNT cdef class Orientation(_Enum): """Enum for orientation options.""" PAGE_UP = ORIENTATION_PAGE_UP PAGE_RIGHT = ORIENTATION_PAGE_RIGHT PAGE_DOWN = ORIENTATION_PAGE_DOWN PAGE_LEFT = ORIENTATION_PAGE_LEFT cdef class WritingDirection(_Enum): """Enum for writing direction options.""" LEFT_TO_RIGHT = WRITING_DIRECTION_LEFT_TO_RIGHT RIGHT_TO_LEFT = WRITING_DIRECTION_RIGHT_TO_LEFT TOP_TO_BOTTOM = WRITING_DIRECTION_TOP_TO_BOTTOM cdef class TextlineOrder(_Enum): """Enum for text line order options.""" LEFT_TO_RIGHT = TEXTLINE_ORDER_LEFT_TO_RIGHT RIGHT_TO_LEFT = TEXTLINE_ORDER_RIGHT_TO_LEFT TOP_TO_BOTTOM = TEXTLINE_ORDER_TOP_TO_BOTTOM cdef class Justification(_Enum): """Enum for justification options.""" UNKNOWN = JUSTIFICATION_UNKNOWN LEFT = JUSTIFICATION_LEFT CENTER = JUSTIFICATION_CENTER RIGHT = JUSTIFICATION_RIGHT cdef class DIR(_Enum): """Enum for strong text direction values. Attributes: NEUTRAL: Text contains only neutral characters. LEFT_TO_RIGHT: Text contains no Right-to-Left characters. RIGHT_TO_LEFT: Text contains no Left-to-Right characters. MIX: Text contains a mixture of left-to-right and right-to-left characters. """ NEUTRAL = DIR_NEUTRAL """Text contains only neutral characters.""" LEFT_TO_RIGHT = DIR_LEFT_TO_RIGHT """Text contains no Right-to-Left characters.""" RIGHT_TO_LEFT = DIR_RIGHT_TO_LEFT """Text contains no Left-to-Right characters.""" MIX = DIR_MIX """Text contains a mixture of left-to-right and right-to-left characters.""" cdef unicode _free_str(char *text): """Return unicode string and free the c pointer""" try: return text finally: free(text) cdef bytes _image_buffer(image): """Return raw bytes of a PIL Image""" with BytesIO() as f: image.save(f, image.format or 'BMP') return f.getvalue() cdef _pix_to_image(Pix *pix): """Convert Pix object to PIL.Image.""" cdef: unsigned char *buff size_t size int result int fmt = pix.informat if fmt > 0: result = pixWriteMem(&buff, &size, pix, fmt) else: # write as IFF_BMP if format is unknown result = pixWriteMem(&buff, &size, pix, 1) try: if result == 1: raise RuntimeError("Failed to convert pix image to PIL.Image") with BytesIO(buff[:size]) as f: image = Image.open(f) image.load() finally: free(buff) return image cdef boxa_to_list(Boxa *boxa): """Convert Boxa (boxes array) to list of boxes dicts.""" boxes = [] for box in boxa.box[:boxa.n]: boxes.append(box[0]) return boxes cdef pixa_to_list(Pixa *pixa): """Convert Pixa (Array of pixes and boxes) to list of pix, box tuples.""" return list(zip((_pix_to_image(pix) for pix in pixa.pix[:pixa.n]), boxa_to_list(pixa.boxa))) cdef class PyPageIterator: """Wrapper around Tesseract's ``PageIterator`` class. Returned by :meth:`PyTessBaseAPI.AnalyseLayout`. Instances of this class and its subclasses cannot be instantiated from Python. Accessing data ============== Coordinate system: Integer coordinates are at the cracks between the pixels. The top-left corner of the top-left pixel in the image is at (0,0). The bottom-right corner of the bottom-right pixel in the image is at (width, height). Every bounding box goes from the top-left of the top-left contained pixel to the bottom-right of the bottom-right contained pixel, so the bounding box of the single top-left pixel in the image is: (0,0)->(1,1). If an image rectangle has been set in the API, then returned coordinates relate to the original (full) image, rather than the rectangle. .. note:: You can iterate through the elements of a level using the :func:`iterate_level` helper function: >>> for e in iterate_level(api.AnalyseLayout(), RIL.WORD): ... orientation = e.Orientation() .. warning:: This class points to data held within the :class:`PyTessBaseAPI` instance, and therefore can only be used while the :class:`PyTessBaseAPI` instance still exists and has not been subjected to a call of :meth:`Init`, :meth:`SetImage`, :meth:`Recognize`, :meth:`Clear`, :meth:`End`, or anything else that changes the internal `PAGE_RES`. """ cdef PageIterator *_piter @staticmethod cdef PyPageIterator createPageIterator(PageIterator *piter): cdef PyPageIterator pyiter = PyPageIterator.__new__(PyPageIterator) pyiter._piter = piter return pyiter def __cinit__(self): self._piter = NULL def __dealloc__(self): if self._piter != NULL: del self._piter def __init__(self): raise TypeError('{} cannot be instantiated from Python'.format(type(self).__name__)) def Begin(self): """Move the iterator to point to the start of the page to begin an iteration.""" self._piter.Begin() def RestartParagraph(self): """Move the iterator to the beginning of the paragraph. This class implements this functionality by moving it to the zero indexed blob of the first (leftmost) word on the first row of the paragraph. """ self._piter.RestartParagraph() def IsWithinFirstTextlineOfParagraph(self): """Return whether this iterator points anywhere in the first textline of a paragraph.""" return self._piter.IsWithinFirstTextlineOfParagraph() def RestartRow(self): """Move the iterator to the beginning of the text line. This class implements this functionality by moving it to the zero indexed blob of the first (leftmost) word of the row. """ return self._piter.RestartRow() def Next(self, PageIteratorLevel level): """Move to the start of the next object at the given level in the page hierarchy, and returns false if the end of the page was reached. .. note:: :attr:`RIL.SYMBOL` will skip non-text blocks, but all other :class:`RIL` level values will visit each non-text block once. Think of non text blocks as containing a single para, with a single line, with a single imaginary word. Calls to Next with different levels may be freely intermixed. This function iterates words in right-to-left scripts correctly, if the appropriate language has been loaded into Tesseract. Args: level (int): Iterator level. See :class:`RIL`. """ return self._piter.Next(level) def IsAtBeginningOf(self, PageIteratorLevel level): """Return whether the iterator is at the start of an object at the given level. For instance, suppose an iterator it is pointed to the first symbol of the first word of the third line of the second paragraph of the first block in a page, then:: it.IsAtBeginningOf(RIL.BLOCK) is False it.IsAtBeginningOf(RIL.PARA) is False it.IsAtBeginningOf(RIL.TEXTLINE) is True it.IsAtBeginningOf(RIL.WORD) is True it.IsAtBeginningOf(RIL.SYMBOL) is True Args: level (int): Iterator level. See :class:`RIL`. Returns: bool: ``True`` if the iterator is at the start of an object at the given level. """ return self._piter.IsAtBeginningOf(level) def IsAtFinalElement(self, PageIteratorLevel level, PageIteratorLevel element): """Return whether the iterator is positioned at the last element in a given level. (e.g. the last word in a line, the last line in a block) Here's some two-paragraph example text: It starts off innocuously enough but quickly turns bizarre. The author inserts a cornucopia of words to guard against confused references. Now take an iterator ``it`` pointed to the start of "bizarre." it.IsAtFinalElement(RIL.PARA, RIL.SYMBOL) = False it.IsAtFinalElement(RIL.PARA, RIL.WORD) = True it.IsAtFinalElement(RIL.BLOCK, RIL.WORD) = False Args: level (int): Iterator Level. See :class:`RIL`. element (int): Element level. See :class:`RIL`. Returns: bool: ``True`` if the iterator is positioned at the last element in the given level. """ return self._piter.IsAtFinalElement(level, element) def SetBoundingBoxComponents(self, bool include_upper_dots, bool include_lower_dots): """Controls what to include in a bounding box. Bounding boxes of all levels between :attr:`RIL.WORD` and :attr:`RIL.BLOCK` can include or exclude potential diacritics. Between layout analysis and recognition, it isn't known where all diacritics belong, so this control is used to include or exclude some diacritics that are above or below the main body of the word. In most cases where the placement is obvious, and after recognition, it doesn't make as much difference, as the diacritics will already be included in the word. Args: include_upper_dots (bool): Include upper dots. include_lower_dots (bool): Include lower dots. """ self._piter.SetBoundingBoxComponents(include_upper_dots, include_lower_dots) def BoundingBox(self, PageIteratorLevel level, const int padding=0): """Return the bounding rectangle of the current object at the given level. See comment on coordinate system above. Args: level (int): Page Iteration Level. See :class:`RIL` for available levels. Kwargs: padding (int): The padding argument to :meth:`GetImage` can be used to expand the image to include more foreground pixels. Returns: tuple or None if there is no such object at the current position. The returned bounding box (left, top, right and bottom values respectively) is guaranteed to match the size and position of the image returned by :meth:`GetBinaryImage`, but may clip foreground pixels from a grey image. """ cdef int left, top, right, bottom if not self._piter.BoundingBox(level, padding, &left, &top, &right, &bottom): return None return left, top, right, bottom def BoundingBoxInternal(self, PageIteratorLevel level): """Return the bounding rectangle of the object in a coordinate system of the working image rectangle having its origin at (rect_left_, rect_top_) with respect to the original image and is scaled by a factor scale_. Args: level (int): Page Iteration Level. See :class:`RIL` for available levels. Returns: tuple or None if there is no such object at the current position. The returned bounding box is represented as a tuple with left, top, right and bottom values respectively. """ cdef int left, top, right, bottom if not self._piter.BoundingBoxInternal(level, &left, &top, &right, &bottom): return None return left, top, right, bottom def Empty(self, PageIteratorLevel level): """Return whether there is no object of a given level. Args: level (int): Iterator level. See :class:`RIL`. Returns: bool: ``True`` if there is no object at the given level. """ return self._piter.Empty(level) def BlockType(self): """Return the type of the current block. See :class:`PolyBlockType` for possible types. """ return self._piter.BlockType() def BlockPolygon(self): """Return the polygon outline of the current block. Returns: list or None: list of points (x,y tuples) which list the vertices of the polygon, and the last edge is the line segment between the last point and the first point. ``None`` will be returned if the iterator is at the end of the document or layout analysis was not used. """ cdef Pta *pta = self._piter.BlockPolygon() if pta == NULL: return None try: return list(zip((x for x in pta.x[:pta.n]), (y for y in pta.y[:pta.n]))) finally: ptaDestroy(&pta) def GetBinaryImage(self, PageIteratorLevel level): """Return a binary image of the current object at the given level. The image is masked along the polygon outline of the current block, as given by :meth:`BlockPolygon`. (Pixels outside the mask will be white.) The position and size match the return from :meth:`BoundingBoxInternal`, and so this could be upscaled with respect to the original input image. Args: level (int): Iterator level. See :class:`RIL`. Returns: :class:`PIL.Image`: Image object or None if no image is returned. """ cdef Pix *pix = self._piter.GetBinaryImage(level) if pix == NULL: return None try: return _pix_to_image(pix) finally: pixDestroy(&pix) def GetImage(self, PageIteratorLevel level, int padding, original_image): """Return an image of the current object at the given level in greyscale if available in the input. The image is masked along the polygon outline of the current block, as given by :meth:`BlockPolygon`. (Pixels outside the mask will be white.) To guarantee a binary image use :meth:`BinaryImage`. Args: level (int): Iterator level. See :class:`RIL`. padding (int): Padding by which to expand the returned image. .. note:: in order to give the best possible image, the bounds are expanded slightly over the binary connected component, by the supplied padding, so the top-left position of the returned image is returned along with the image (left, top respectively). These will most likely not match the coordinates returned by :meth:`BoundingBox`. original_image (:class:`PIL.Image`): Original image. If you do not supply an original image (None), you will get a binary one. Returns: tuple: The image (:class:`PIL.Image`) of the current object at the given level in greyscale followed by its top and left positions. """ cdef: Pix *pix Pix *opix = NULL size_t size cuchar_t *buff int left int top if original_image: raw = _image_buffer(original_image) size = len(raw) buff = raw opix = pixReadMem(buff, size) pix = self._piter.GetImage(level, padding, opix, &left, &top) try: return _pix_to_image(pix), left, top finally: pixDestroy(&pix) if opix != NULL: pixDestroy(&opix) def Baseline(self, PageIteratorLevel level): """Return the baseline of the current object at the given level. The baseline is the line that passes through (x1, y1) and (x2, y2). .. warning:: with vertical text, baselines may be vertical! Args: level (int): Iterator level. See :class:`RIL`. Returns: tuple: Baseline points' coordinates (x1, y1), (x2, y2). ``None`` if there is no baseline at the current position. """ cdef int x1, y1, x2, y2 if not self._piter.Baseline(level, &x1, &y1, &x2, &y2): return False return (x1, y1), (x2, y2) def Orientation(self): """Return the orientation for the block the iterator points to. Returns: tuple: The following values are returned respectively:: orientation: See :class:`Orientation` writing_direction: See :class:`WritingDirection` textline_order: See :class:`TextlineOrder` deskew_angle: After rotating the block so the text orientation is upright, how many radians does one have to rotate the block anti-clockwise for it to be level? -Pi/4 <= deskew_angle <= Pi/4 """ cdef: TessOrientation orientation TessWritingDirection writing_direction TessTextlineOrder textline_order float deskew_angle self._piter.Orientation(&orientation, &writing_direction, &textline_order, &deskew_angle) return orientation, writing_direction, textline_order, deskew_angle def ParagraphInfo(self): """Return information about the current paragraph, if available. Returns: tuple: The following values are returned respectively:: justification: LEFT if ragged right, or fully justified and script is left-to-right. RIGHT if ragged left, or fully justified and script is right-to-left. UNKNOWN if it looks like source code or we have very few lines. See :class:`Justification`. is_list_item: ``True`` if we believe this is a member of an ordered or unordered list. is_crown: ``True`` if the first line of the paragraph is aligned with the other lines of the paragraph even though subsequent paragraphs have first line indents. This typically indicates that this is the continuation of a previous paragraph or that it is the very first paragraph in the chapter. first_line_indent: For LEFT aligned paragraphs, the first text line of paragraphs of this kind are indented this many pixels from the left edge of the rest of the paragraph. for RIGHT aligned paragraphs, the first text line of paragraphs of this kind are indented this many pixels from the right edge of the rest of the paragraph. NOTE 1: This value may be negative. NOTE 2: if ``is_crown == True``, the first line of this paragraph is actually flush, and first_line_indent is set to the "common" first_line_indent for subsequent paragraphs in this block of text. """ cdef: TessParagraphJustification justification bool is_list_item bool is_crown int first_line_indent self._piter.ParagraphInfo(&justification, &is_list_item, &is_crown, &first_line_indent) return justification, is_list_item, is_crown, first_line_indent cdef class PyLTRResultIterator(PyPageIterator): cdef LTRResultIterator *_ltrriter def __cinit__(self): self._ltrriter = NULL def __dealloc__(self): if self._ltrriter != NULL: del self._ltrriter self._piter = NULL def GetChoiceIterator(self): """Return `PyChoiceIterator` instance to iterate over symbol choices. Returns `None` on failure. """ cdef: const LTRResultIterator *ltrriter = self._ltrriter ChoiceIterator *citer = new ChoiceIterator(ltrriter[0]) if citer == NULL: return None return PyChoiceIterator.create(citer) def GetUTF8Text(self, PageIteratorLevel level): """Returns the UTF-8 encoded text string for the current object at the given level. Args: level (int): Iterator level. See :class:`RIL`. Returns: unicode: UTF-8 encoded text for the given level's current object. Raises: :exc:`RuntimeError`: If no text returned. """ cdef char *text = self._ltrriter.GetUTF8Text(level) if text == NULL: raise RuntimeError('No text returned') return _free_str(text) def SetLineSeparator(self, separator): """Set the string inserted at the end of each text line. "\n" by default.""" cdef bytes py_sep = _b(separator) self._ltrriter.SetLineSeparator(py_sep) def SetParagraphSeparator(self, separator): """Set the string inserted at the end of each paragraph. "\n" by default.""" cdef bytes py_sep = _b(separator) self._ltrriter.SetParagraphSeparator(py_sep) def Confidence(self, PageIteratorLevel level): """Return the mean confidence of the current object at the given level. The number should be interpreted as a percent probability. (0.0-100.0) """ return self._ltrriter.Confidence(level) IF TESSERACT_VERSION >= 0x3040100: def RowAttributes(self): """Return row_height, descenders and ascenders in a dict""" cdef: float row_height float descenders float ascenders self._ltrriter.RowAttributes(&row_height, &descenders, &ascenders) return { 'row_height': row_height, 'descenders': descenders, 'ascenders': ascenders } def WordFontAttributes(self): """Return the font attributes of the current word. .. note:: If iterating at a higher level object than words, eg textlines, then this will return the attributes of the first word in that textline. Returns: dict: `None` if nothing found or a dictionary with the font attributes:: font_name: String representing a font name. Lifespan is the same as the iterator itself, ie rendered invalid by various members of :class:`PyTessBaseAPI`, including `Init`, `SetImage`, `End` or deleting the :class:`PyTessBaseAPI`. bold (bool): ``True`` if bold. italic (bool): ``True`` if italic. underlined (bool): ``True`` if underlined. monospace (bool): ``True`` if monospace. serif (bool): ``True`` if serif. smallcaps (bool): ``True`` if smallcaps. pointsize (int): printers points (1/72 inch.) font_id (int): font id. """ cdef: bool is_bold, bool is_italic bool is_underlined bool is_monospace bool is_serif bool is_smallcaps int pointsize int font_id cchar_t *font_name font_name = self._ltrriter.WordFontAttributes(&is_bold, &is_italic, &is_underlined, &is_monospace, &is_serif, &is_smallcaps, &pointsize, &font_id) if font_name == NULL: return None return { 'font_name': font_name, 'bold': is_bold, 'italic': is_italic, 'underlined': is_underlined, 'monospace': is_monospace, 'serif': is_serif, 'smallcaps': is_smallcaps, 'pointsize': pointsize, 'font_id': font_id } def WordRecognitionLanguage(self): """Return the name of the language used to recognize this word. Returns ``None`` on error. """ cdef cchar_t *lang = self._ltrriter.WordRecognitionLanguage() if lang == NULL: return None return lang def WordDirection(self): """Return the overall directionality of this word. See :class:`DIR` for available values. """ return self._ltrriter.WordDirection() def WordIsFromDictionary(self): """Return True if the current word was found in a dictionary.""" return self._ltrriter.WordIsFromDictionary() IF TESSERACT_VERSION >= 0x4000000: def BlanksBeforeWord(self): """Return True if the current word is numeric.""" return self._ltrriter.BlanksBeforeWord() def WordIsNumeric(self): """Return True if the current word is numeric.""" return self._ltrriter.WordIsNumeric() def HasBlamerInfo(self): """Return True if the word contains blamer information.""" return self._ltrriter.HasBlamerInfo() def GetBlamerDebug(self): """Return a string with blamer information for this word.""" return self._ltrriter.GetBlamerDebug() def GetBlamerMisadaptionDebug(self): """Return a string with misadaption information for this word.""" return self._ltrriter.GetBlamerMisadaptionDebug() def HasTruthString(self): """Returns True if a truth string was recorded for the current word.""" return self._ltrriter.HasTruthString() def EquivalentToTruth(self, text): """Return True if the given string is equivalent to the truth string for the current word.""" cdef bytes py_text = _b(text) return self._ltrriter.EquivalentToTruth(py_text) def WordTruthUTF8Text(self): """Return a UTF-8 encoded truth string for the current word.""" cdef char *text = self._ltrriter.WordTruthUTF8Text() return _free_str(text) def WordNormedUTF8Text(self): """Returns a UTF-8 encoded normalized OCR string for the current word.""" cdef char *text = self._ltrriter.WordNormedUTF8Text() return _free_str(text) def WordLattice(self): """Return a serialized choice lattice.""" cdef: cchar_t *word_lattice int lattice_size word_lattice = self._ltrriter.WordLattice(&lattice_size) if not lattice_size: return None return word_lattice[:lattice_size] def SymbolIsSuperscript(self): """Return True if the current symbol is a superscript. If iterating at a higher level object than symbols, eg words, then this will return the attributes of the first symbol in that word. """ return self._ltrriter.SymbolIsSuperscript() def SymbolIsSubscript(self): """Return True if the current symbol is a subscript. If iterating at a higher level object than symbols, eg words, then this will return the attributes of the first symbol in that word. """ return self._ltrriter.SymbolIsSubscript() def SymbolIsDropcap(self): """Return True if the current symbol is a dropcap. If iterating at a higher level object than symbols, eg words, then this will return the attributes of the first symbol in that word. """ return self._ltrriter.SymbolIsDropcap() cdef class PyResultIterator(PyLTRResultIterator): """Wrapper around Tesseract's ``ResultIterator`` class. .. note:: You can iterate through the elements of a level using the :func:`iterate_level` helper function: >>> for e in iterate_level(api.GetIterator(), RIL.WORD): ... word = e.GetUTF8Text() See :class:`PyPageIterator` for more details. """ cdef ResultIterator *_riter @staticmethod cdef PyResultIterator createResultIterator(ResultIterator *riter): cdef PyResultIterator pyiter = PyResultIterator.__new__(PyResultIterator) pyiter._piter = riter pyiter._ltrriter = riter pyiter._riter = riter return pyiter def __cinit__(self): self._riter = NULL def __dealloc__(self): if self._riter != NULL: del self._riter # set super class pointers to NULL # to avoid multiple deletes self._ltrriter = NULL def IsAtBeginningOf(self, PageIteratorLevel level): """Return whether we're at the logical beginning of the given level. (as opposed to :class:`PyResultIterator`'s left-to-right top-to-bottom order). Otherwise, this acts the same as :meth:`PyPageIterator.IsAtBeginningOf`. """ return self._riter.IsAtBeginningOf(level) def ParagraphIsLtr(self): """Return whether the current paragraph's dominant reading direction is left-to-right (as opposed to right-to-left). """ return self._riter.ParagraphIsLtr() IF TESSERACT_VERSION >= 0x4000000: def GetBestLSTMSymbolChoices(self): """Returns the LSTM choices for every LSTM timestep for the current word.""" cdef: vector[vector[pair[cchar_tp, float]]] *output = self._riter.GetBestLSTMSymbolChoices() vector[vector[pair[cchar_tp, float]]].iterator it vector[pair[cchar_tp, float]].iterator cit vector[pair[cchar_tp, float]] configpairs pair[cchar_tp, float] configpair LSTMSymbolChoices = [] if output == NULL: return LSTMSymbolChoices it = output.begin() while it != output.end(): timestep = [] configpairs = deref(it) cit = configpairs.begin() while cit != configpairs.end(): configpair = deref(cit) timestep.append((configpair.first, configpair.second)) inc(cit) LSTMSymbolChoices.append(timestep) inc(it) return LSTMSymbolChoices cdef class PyChoiceIterator: cdef ChoiceIterator *_citer @staticmethod cdef PyChoiceIterator create(ChoiceIterator *citer): cdef PyChoiceIterator pyciter = PyChoiceIterator.__new__(PyChoiceIterator) pyciter._citer = citer return pyciter def __cinit__(self): self._citer = NULL def __dealloc__(self): if self._citer != NULL: del self._citer def __init__(self, ltr_iterator): raise TypeError('ChoiceIterator cannot be instantiated from Python') def __iter__(self): return iterate_choices(self) def Next(self): """Move to the next choice for the symbol and returns False if there are none left.""" return self._citer.Next() def GetUTF8Text(self): """Return the UTF-8 encoded text string for the current choice.""" cdef cchar_t *text = self._citer.GetUTF8Text() if text == NULL: return None return text def Confidence(self): """Return the confidence of the current choice. The number should be interpreted as a percent probability. (0.0f-100.0f) """ return self._citer.Confidence() def iterate_choices(citerator): """Helper generator function to iterate :class:`PyChoiceIterator`.""" yield citerator while citerator.Next(): yield citerator def iterate_level(iterator, PageIteratorLevel level): """Helper generator function to iterate a :class:`PyPageIterator` level. Args: iterator: Instance of :class:`PyPageIterator` level: Page iterator level :class:`RIL` """ yield iterator while iterator.Next(level): yield iterator cdef class PyTessBaseAPI: """Cython wrapper class around the C++ TessBaseAPI class. Usage as a context manager: >>> with PyTessBaseAPI(path='./', lang='eng') as tesseract: ... tesseract.SetImage(image) ... text = tesseract.GetUTF8Text() Example with manual handling: >>> tesseract = PyTessBaseAPI(path='./', lang='eng') >>> try: ... tesseract.SetImage(image) ... text = tesseract.GetUTF8Text() ... finally: ... tesseract.End() Args: path (str): The name of the parent directory of tessdata. Must end in /. lang (str): An ISO 639-3 language string. Defaults to 'eng'. The language may be a string of the form [~][+[~]]* indicating that multiple languages are to be loaded. Eg hin+eng will load Hindi and English. Languages may specify internally that they want to be loaded with one or more other languages, so the ~ sign is available to override that. Eg if hin were set to load eng by default, then hin+~eng would force loading only hin. The number of loaded languages is limited only by memory, with the caveat that loading additional languages will impact both speed and accuracy, as there is more work to do to decide on the applicable language, and there is more chance of hallucinating incorrect words. psm (int): Page segmentation mode. Defaults to :attr:`PSM.AUTO`. See :class:`PSM` for available psm values. init (bool): If ``False``, :meth:`Init` will not be called and has to be called after initialization. oem (int): OCR engine mode. Defaults to :attr:`OEM.DEFAULT`. Raises: :exc:`RuntimeError`: If `init` is ``True`` and API initialization fails. """ cdef: TessBaseAPI _baseapi Pix *_pix @staticmethod def Version(): return TessBaseAPI.Version() @staticmethod def ClearPersistentCache(): return TessBaseAPI.ClearPersistentCache() def __cinit__(self, path=_DEFAULT_PATH, lang=_DEFAULT_LANG, PageSegMode psm=PSM_AUTO, bool init=True, OcrEngineMode oem=OEM_DEFAULT): cdef: bytes py_path = _b(path) bytes py_lang = _b(lang) cchar_t *cpath = py_path cchar_t *clang = py_lang with nogil: self._pix = NULL if init: self._init_api(cpath, clang, oem, NULL, 0, NULL, NULL, False, psm) def __dealloc__(self): self._end_api() IF TESSERACT_MAJOR_VERSION >= 5: cdef int _init_api(self, cchar_t *path, cchar_t *lang, OcrEngineMode oem, char **configs, int configs_size, const vector[string] *vars_vec, const vector[string] *vars_vals, bool set_only_non_debug_params, PageSegMode psm) nogil except -1: cdef int ret = self._baseapi.Init(path, lang, oem, configs, configs_size, vars_vec, vars_vals, set_only_non_debug_params) if ret == -1: with gil: raise RuntimeError('Failed to init API, possibly an invalid tessdata path: {}'.format(path)) self._baseapi.SetPageSegMode(psm) return ret ELSE: cdef int _init_api(self, cchar_t *path, cchar_t *lang, OcrEngineMode oem, char **configs, int configs_size, const GenericVector[STRING] *vars_vec, const GenericVector[STRING] *vars_vals, bool set_only_non_debug_params, PageSegMode psm) nogil except -1: cdef int ret = self._baseapi.Init(path, lang, oem, configs, configs_size, vars_vec, vars_vals, set_only_non_debug_params) if ret == -1: with gil: raise RuntimeError('Failed to init API, possibly an invalid tessdata path: {}'.format(path)) self._baseapi.SetPageSegMode(psm) return ret cdef void _end_api(self) nogil: self._destroy_pix() self._baseapi.End() cdef void _destroy_pix(self) nogil: if self._pix != NULL: pixDestroy(&self._pix) self._pix = NULL def GetDatapath(self): """Return tessdata parent directory""" return self._baseapi.GetDatapath() def SetOutputName(self, name): """Set the name of the bonus output files. Needed only for debugging.""" cdef bytes py_name = _b(name) self._baseapi.SetOutputName(py_name) def SetVariable(self, name, val): """Set the value of an internal parameter. Supply the name of the parameter and the value as a string, just as you would in a config file. Eg SetVariable("tessedit_char_blacklist", "xyz"); to ignore x, y and z. Or SetVariable("classify_bln_numeric_mode", "1"); to set numeric-only mode. SetVariable may be used before Init, but settings will revert to defaults on End(). Args: name (str): Variable name value (str): Variable value Returns: bool: ``False`` if the name lookup failed. """ cdef: bytes py_name = _b(name) bytes py_val = _b(val) return self._baseapi.SetVariable(py_name, py_val) def SetDebugVariable(self, name, val): """Set the value of an internal parameter. (debug) Supply the name of the parameter and the value as a string, just as you would in a config file. Eg SetVariable("tessedit_char_blacklist", "xyz"); to ignore x, y and z. Or SetVariable("classify_bln_numeric_mode", "1"); to set numeric-only mode. SetVariable may be used before Init, but settings will revert to defaults on End(). Args: name (str): Variable name value (str): Variable value Returns: bool: ``False`` if the name lookup failed. """ cdef: bytes py_name = _b(name) bytes py_val = _b(val) return self._baseapi.SetDebugVariable(py_name, py_val) def GetIntVariable(self, name): """Return the value of the given int parameter if it exists among Tesseract parameters. Returns ``None`` if the parameter was not found. """ cdef: bytes py_name = _b(name) int val if self._baseapi.GetIntVariable(py_name, &val): return val return None def GetBoolVariable(self, name): """Return the value of the given bool parameter if it exists among Tesseract parameters. Returns ``None`` if the parameter was not found. """ cdef: bytes py_name = _b(name) bool val if self._baseapi.GetBoolVariable(py_name, &val): return val return None def GetDoubleVariable(self, name): """Return the value of the given double parameter if it exists among Tesseract parameters. Returns ``None`` if the parameter was not found. """ cdef: bytes py_name = _b(name) double val if self._baseapi.GetDoubleVariable(py_name, &val): return val return None def GetStringVariable(self, name): """Return the value of the given string parameter if it exists among Tesseract parameters. Returns ``None`` if the parameter was not found. """ cdef: bytes py_name = _b(name) cchar_t *val = self._baseapi.GetStringVariable(py_name) if val != NULL: return val return None def GetVariableAsString(self, name): """Return the value of named variable as a string (regardless of type), if it exists. Returns ``None`` if parameter was not found. """ IF TESSERACT_MAJOR_VERSION >= 5: cdef: bytes py_name = _b(name) string val ELSE: cdef: bytes py_name = _b(name) STRING val if self._baseapi.GetVariableAsString(py_name, &val): return val.c_str() return None def InitFull(self, path=_DEFAULT_PATH, lang=_DEFAULT_LANG, OcrEngineMode oem=OEM_DEFAULT, list configs=[], dict variables={}, bool set_only_non_debug_params=False): """Initialize the API with the given parameters (advanced). It is entirely safe (and eventually will be efficient too) to call :meth:`Init` multiple times on the same instance to change language, or just to reset the classifier. Page Segmentation Mode is set to :attr:`PSM.AUTO` after initialization by default. Args: path (str): The name of the parent directory of tessdata. Must end in /. lang (str): An ISO 639-3 language string. Defaults to 'eng'. The language may be a string of the form [~][+[~]]* indicating that multiple languages are to be loaded. Eg hin+eng will load Hindi and English. Languages may specify internally that they want to be loaded with one or more other languages, so the ~ sign is available to override that. Eg if hin were set to load eng by default, then hin+~eng would force loading only hin. The number of loaded languages is limited only by memory, with the caveat that loading additional languages will impact both speed and accuracy, as there is more work to do to decide on the applicable language, and there is more chance of hallucinating incorrect words. oem (int): OCR engine mode. Defaults to :attr:`OEM.DEFAULT`. See :class:`OEM` for all available options. configs (list): List of config files to load variables from. variables (dict): Extra variables to be set. set_only_non_debug_params (bool): If ``True``, only params that do not contain "debug" in the name will be set. Raises: :exc:`RuntimeError`: If API initialization fails. """ IF TESSERACT_MAJOR_VERSION >= 5: cdef: bytes py_path = _b(path) bytes py_lang = _b(lang) cchar_t *cpath = py_path cchar_t *clang = py_lang int configs_size = len(configs) char **configs_ = malloc(configs_size * sizeof(char *)) vector[string] vars_vec vector[string] vars_vals cchar_t *val string sval ELSE: cdef: bytes py_path = _b(path) bytes py_lang = _b(lang) cchar_t *cpath = py_path cchar_t *clang = py_lang int configs_size = len(configs) char **configs_ = malloc(configs_size * sizeof(char *)) GenericVector[STRING] vars_vec GenericVector[STRING] vars_vals cchar_t *val STRING sval for i, c in enumerate(configs): c = _b(c) configs_[i] = c for k, v in variables.items(): k = _b(k) val = k sval = val vars_vec.push_back(sval) v = _b(v) val = v sval = val vars_vals.push_back(sval) with nogil: try: self._init_api(cpath, clang, oem, configs_, configs_size, &vars_vec, &vars_vals, set_only_non_debug_params, PSM_AUTO) finally: free(configs_) def Init(self, path=_DEFAULT_PATH, lang=_DEFAULT_LANG, OcrEngineMode oem=OEM_DEFAULT): """Initialize the API with the given data path, language and OCR engine mode. See :meth:`InitFull` for more initialization info and options. Args: path (str): The name of the parent directory of tessdata. Must end in /. Uses default installation path if not specified. lang (str): An ISO 639-3 language string. Defaults to 'eng'. See :meth:`InitFull` for full description of this parameter. oem (int): OCR engine mode. Defaults to :attr:`OEM.DEFAULT`. See :class:`OEM` for all available options. Raises: :exc:`RuntimeError`: If API initialization fails. """ cdef: bytes py_path = _b(path) bytes py_lang = _b(lang) cchar_t *cpath = py_path cchar_t *clang = py_lang with nogil: self._init_api(cpath, clang, oem, NULL, 0, NULL, NULL, False, PSM_AUTO) def GetInitLanguagesAsString(self): """Return the languages string used in the last valid initialization. If the last initialization specified "deu+hin" then that will be returned. If hin loaded eng automatically as well, then that will not be included in this list. To find the languages actually loaded use :meth:`GetLoadedLanguages`. """ return self._baseapi.GetInitLanguagesAsString() def GetLoadedLanguages(self): """Return the loaded languages as a list of STRINGs. Includes all languages loaded by the last Init, including those loaded as dependencies of other loaded languages. """ IF TESSERACT_MAJOR_VERSION >= 5: cdef vector[string] langs ELSE: cdef GenericVector[STRING] langs self._baseapi.GetLoadedLanguagesAsVector(&langs) return [langs[i].c_str() for i in xrange(langs.size())] def GetAvailableLanguages(self): """Return list of available languages in the init data path""" IF TESSERACT_MAJOR_VERSION >= 5: cdef: vector[string] v int i ELSE: cdef: GenericVector[STRING] v int i langs = [] self._baseapi.GetAvailableLanguagesAsVector(&v) langs = [v[i].c_str() for i in xrange(v.size())] return langs def InitForAnalysePage(self): """Init only for page layout analysis. Use only for calls to :meth:`SetImage` and :meth:`AnalysePage`. Calls that attempt recognition will generate an error. """ self._baseapi.InitForAnalysePage() def ReadConfigFile(self, filename): """Read a "config" file containing a set of param, value pairs. Searches the standard places: tessdata/configs, tessdata/tessconfigs. Args: filename: config file name. Also accepts relative or absolute path name. """ cdef bytes py_fname = _b(filename) self._baseapi.ReadConfigFile(py_fname) def SetPageSegMode(self, PageSegMode psm): """Set page segmentation mode. Args: psm (int): page segmentation mode. See :class:`PSM` for all available psm options. """ with nogil: self._baseapi.SetPageSegMode(psm) def GetPageSegMode(self): """Return the current page segmentation mode.""" return self._baseapi.GetPageSegMode() def TesseractRect(self, imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height): """Recognize a rectangle from an image and return the result as a string. May be called many times for a single Init. Currently has no error checking. .. note:: `TesseractRect` is the simplified convenience interface. For advanced uses, use :meth:`SetImage`, (optionally) :meth:`SetRectangle`, :meth:`Recognize`, and one or more of the `Get*Text` methods below. Args: imagedata (str): Raw image bytes. bytes_per_pixel (int): bytes per pixel. Greyscale of 8 and color of 24 or 32 bits per pixel may be given. Palette color images will not work properly and must be converted to 24 bit. Binary images of 1 bit per pixel may also be given but they must be byte packed with the MSB of the first byte being the first pixel, and a 1 represents WHITE. For binary images set bytes_per_pixel=0. bytes_per_line (int): bytes per line. left (int): left rectangle ordonate. top (int): top rectangle ordonate. width (int): image width. height (int): image height. Returns: unicode: The recognized text as UTF8. """ cdef: bytes py_imagedata = _b(imagedata) cuchar_t *cimagedata = py_imagedata char *text with nogil: text = self._baseapi.TesseractRect(cimagedata, bytes_per_pixel, bytes_per_line, left, top, width, height) if text == NULL: with gil: raise RuntimeError('Failed to recognize image') return _free_str(text) def ClearAdaptiveClassifier(self): """Call between pages or documents etc to free up memory and forget adaptive data. """ self._baseapi.ClearAdaptiveClassifier() def SetImageBytes(self, imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line): """Provide an image for Tesseract to recognize. Format is as :meth:`TesseractRect` above. Does not copy the image buffer, or take ownership. The source image may be destroyed after Recognize is called, either explicitly or implicitly via one of the `Get*Text` methods. This method clears all recognition results, and sets the rectangle to the full image, so it may be followed immediately by a :meth:`GetUTF8Text`, and it will automatically perform recognition. Args: imagedata (str): Raw image bytes. width (int): image width. height (int): image height. bytes_per_pixel (int): bytes per pixel. Greyscale of 8 and color of 24 or 32 bits per pixel may be given. Palette color images will not work properly and must be converted to 24 bit. Binary images of 1 bit per pixel may also be given but they must be byte packed with the MSB of the first byte being the first pixel, and a 1 represents WHITE. For binary images set bytes_per_pixel=0. bytes_per_line (int): bytes per line. """ cdef: bytes py_imagedata = _b(imagedata) cuchar_t *cimagedata = py_imagedata with nogil: self._destroy_pix() self._baseapi.SetImage(cimagedata, width, height, bytes_per_pixel, bytes_per_line) def SetImageBytesBmp(self, imagedata): """Provide an image for Tesseract to recognize. Args: imagedata (:bytes): Raw bytes of a BMP image. Raises: :exc:`RuntimeError`: If for any reason the api failed to load the given image. """ cdef: bytes py_imagedata = _b(imagedata) size_t size = len(py_imagedata) cuchar_t *cimagedata = py_imagedata with nogil: self._destroy_pix() self._pix = pixReadMemBmp(cimagedata, size) if self._pix == NULL: with gil: raise RuntimeError('Error reading image') self._baseapi.SetImage(self._pix) def SetImage(self, image): """Provide an image for Tesseract to recognize. This method can be called multiple times after :meth:`Init`. Args: image (:class:PIL.Image): Image object. Raises: :exc:`RuntimeError`: If for any reason the api failed to load the given image. """ cdef: cuchar_t *buff size_t size bytes raw raw = _image_buffer(image) buff = raw size = len(raw) with nogil: self._destroy_pix() self._pix = pixReadMem(buff, size) if self._pix == NULL: with gil: raise RuntimeError('Error reading image') self._baseapi.SetImage(self._pix) def SetImageFile(self, filename): """Set image from file for Tesseract to recognize. Args: filename (str): Image file relative or absolute path. Raises: :exc:`RuntimeError`: If for any reason the api failed to load the given image. """ cdef: bytes py_fname = _b(filename) cchar_t *fname = py_fname with nogil: self._destroy_pix() self._pix = pixRead(fname) if self._pix == NULL: with gil: # missing leptonica support? Try PIL image = Image.open(fname) self.SetImage(image) self._baseapi.SetImage(self._pix) def SetSourceResolution(self, int ppi): """Set the resolution of the source image in pixels per inch so font size information can be calculated in results. Call this after :meth:`SetImage`. """ self._baseapi.SetSourceResolution(ppi) def SetRectangle(self, int left, int top, int width, int height): """Restrict recognition to a sub-rectangle of the image. Call after :meth:`SetImage`. Each SetRectangle clears the recogntion results so multiple rectangles can be recognized with the same image. Args: left (int): position from left top (int): position from top width (int): width height (int): height """ self._baseapi.SetRectangle(left, top, width, height) def GetThresholdedImage(self): """Return a copy of the internal thresholded image from Tesseract. May be called any time after SetImage. """ cdef Pix *pix = self._baseapi.GetThresholdedImage() if pix == NULL: return None try: return _pix_to_image(pix) finally: pixDestroy(&pix) def GetRegions(self): """Get the result of page layout analysis as a list of image, box bounds {x, y, width, height} tuples in reading order. Can be called before or after :meth:`Recognize`. Returns: list: List of tuples containing the following values respectively:: image (:class:`PIL.Image`): Image object. bounding box (dict): dict with x, y, w, h keys. """ cdef: Pixa *pixa Boxa *boxa boxa = self._baseapi.GetRegions(&pixa) if boxa == NULL: return [] try: return pixa_to_list(pixa) finally: boxaDestroy(&boxa) pixaDestroy(&pixa) def GetTextlines(self, const bool raw_image=False, const int raw_padding=0, const bool blockids=True, const bool paraids=False): """Get the textlines as a list of image, box bounds {x, y, width, height} tuples in reading order. Can be called before or after :meth:`Recognize`. Args: raw_image (bool): If ``True``, then extract from the original image instead of the thresholded image and pad by `raw_padding` pixels. raw_padding (int): Padding pixels. Kwargs: blockids (bool): If ``True`` (default), the block-id of each line is also included in the returned tuples (`None` otherwise). paraids (bool): If ``True``, the paragraph-id of each line within its block is also included in the returned tuples (`None` otherwise). Default is ``False``. Returns: list: List of tuples containing the following values respectively:: image (:class:`PIL.Image`): Image object. bounding box (dict): dict with x, y, w, h keys. block id (int): textline block id (if blockids is ``True``). ``None`` otherwise. paragraph id (int): textline paragraph id within its block (if paraids is True). ``None`` otherwise. """ cdef: Pixa *pixa Boxa *boxa int *_blockids int *_paraids if not blockids: _blockids = NULL if not paraids: _paraids = NULL boxa = self._baseapi.GetTextlines(raw_image, raw_padding, &pixa, &_blockids, &_paraids) if boxa == NULL: return [] try: pixa_list = pixa_to_list(pixa) if blockids: blockids_ = [bid for bid in _blockids[:pixa.n]] free(_blockids) else: blockids_ = [None] * pixa.n if paraids: paraids_ = [pid for pid in _paraids[:pixa.n]] free(_paraids) else: paraids_ = [None] * pixa.n return [p + (blockids_[n], paraids_[n]) for n, p in enumerate(pixa_list)] finally: boxaDestroy(&boxa) pixaDestroy(&pixa) def GetStrips(self, bool blockids=True): """Get the textlines and strips of image regions as a list of image, box bounds {x, y, width, height} tuples in reading order. Enables downstream handling of non-rectangular regions. Can be called before or after :meth:`Recognize`. Kwargs: blockids (bool): If ``True`` (default), the block-id of each line is also included in the returned tuples. Returns: list: List of tuples containing the following values respectively:: image (:class:`PIL.Image`): Image object. bounding box (dict): dict with x, y, w, h keys. block id (int): textline block id (if blockids is ``True``). ``None`` otherwise. """ cdef: Pixa *pixa Boxa *boxa int *_blockids if not blockids: _blockids = NULL boxa = self._baseapi.GetStrips(&pixa, &_blockids) if boxa == NULL: return [] try: pixa_list = pixa_to_list(pixa) if blockids: blockids_ = [bid for bid in _blockids[:pixa.n]] free(_blockids) else: blockids_ = [None] * pixa.n return [p + (blockids_[n], ) for n, p in enumerate(pixa_list)] finally: boxaDestroy(&boxa) pixaDestroy(&pixa) def GetWords(self): """Get the words as a list of image, box bounds {x, y, width, height} tuples in reading order. Can be called before or after :meth:`Recognize`. Returns: list: List of tuples containing the following values respectively:: image (:class:`PIL.Image`): Image object. bounding box (dict): dict with x, y, w, h keys. """ cdef: Boxa *boxa Pixa *pixa boxa = self._baseapi.GetWords(&pixa) if boxa == NULL: return [] try: return pixa_to_list(pixa) finally: boxaDestroy(&boxa) pixaDestroy(&pixa) def GetConnectedComponents(self): """Gets the individual connected (text) components (created after pages segmentation step, but before recognition) as a list of image, box bounds {x, y, width, height} tuples in reading order. Can be called before or after :meth:`Recognize`. Returns: list: List of tuples containing the following values respectively: image (:class:`PIL.Image`): Image object. bounding box (dict): dict with x, y, w, h keys. """ cdef: Boxa *boxa Pixa *pixa boxa = self._baseapi.GetConnectedComponents(&pixa) if boxa == NULL: return [] try: return pixa_to_list(pixa) finally: boxaDestroy(&boxa) pixaDestroy(&pixa) def GetComponentImages(self, const PageIteratorLevel level, const bool text_only, const bool raw_image=False, const int raw_padding=0, const bool blockids=True, const bool paraids=False): """Get the given level kind of components (block, textline, word etc.) as a list of image, box bounds {x, y, width, height} tuples in reading order. Can be called before or after :meth:`Recognize`. Args: level (int): Iterator level. See :class:`RIL`. text_only (bool): If ``True``, then only text components are returned. Kwargs: raw_image (bool): If ``True``, then portions of the original image are extracted instead of the thresholded image and padded with `raw_padding`. Defaults to ``False``. raw_padding (int): Image padding pixels. Defaults to 0. blockids (bool): If ``True``, the block-id of each component is also included in the returned tuples (`None` otherwise). Defaults to ``True``. paraids (bool): If ``True``, the paragraph-id of each component with its block is also included in the returned tuples. Returns: list: List of tuples containing the following values respectively:: image (:class:`PIL.Image`): Image object. bounding box (dict): dict with x, y, w, h keys. block id (int): textline block id (if blockids is ``True``). ``None`` otherwise. paragraph id (int): textline paragraph id within its block (if paraids is True). ``None`` otherwise. """ cdef: Boxa *boxa Pixa *pixa int *_blockids = NULL int *_paraids = NULL int **blockids_addr = &_blockids int **paraids_addr = &_paraids if not blockids: blockids_addr = NULL if not paraids: paraids_addr = NULL boxa = self._baseapi.GetComponentImages(level, text_only, raw_image, raw_padding, &pixa, blockids_addr, paraids_addr) if boxa == NULL: # no components found return [] try: pixa_list = pixa_to_list(pixa) if blockids: blockids_ = [bid for bid in _blockids[:pixa.n]] free(_blockids) else: blockids_ = [None] * pixa.n if paraids: paraids_ = [pid for pid in _paraids[:pixa.n]] free(_paraids) else: paraids_ = [None] * pixa.n return [p + (blockids_[n], paraids_[n]) for n, p in enumerate(pixa_list)] finally: boxaDestroy(&boxa) pixaDestroy(&pixa) def GetThresholdedImageScaleFactor(self): """Return the scale factor of the thresholded image that would be returned by GetThresholdedImage(). Returns: int: 0 if no thresholder has been set. """ return self._baseapi.GetThresholdedImageScaleFactor() def AnalyseLayout(self, bool merge_similar_words=False): """Runs page layout analysis in the mode set by :meth:`SetPageSegMode`. May optionally be called prior to :meth:`Recognize` to get access to just the page layout results. Returns a :class:`PyPageIterator` iterator to the results. Kwargs: merge_similar_words (bool): If ``True``, words are combined where suitable for use with a line recognizer. Use if you want to use AnalyseLayout to find the textlines, and then want to process textline fragments with an external line recognizer. Returns: :class:`PyPageIterator`: Page iterator or `None` on error or an empty page. """ cdef PageIterator *piter piter = self._baseapi.AnalyseLayout(merge_similar_words) if piter == NULL: return None return PyPageIterator.createPageIterator(piter) cpdef bool Recognize(self, int timeout=0): """Recognize the image from :meth:`SetImage`, generating Tesseract internal structures. Returns ``True`` on success. Optional. The `Get*Text` methods below will call :meth:`Recognize` if needed. After :meth:`Recognize`, the output is kept internally until the next :meth:`SetImage`. Kwargs: timeout (int): time to wait in milliseconds before timing out. Returns: bool: ``True`` if the operation is successful. """ cdef: ETEXT_DESC monitor int res with nogil: if timeout > 0: monitor.cancel = NULL monitor.cancel_this = NULL monitor.set_deadline_msecs(timeout) res = self._baseapi.Recognize(&monitor) else: res = self._baseapi.Recognize(NULL) return res == 0 """Methods to retrieve information after :meth:`SetImage`, :meth:`Recognize` or :meth:`TesseractRect`. (:meth:`Recognize` is called implicitly if needed.)""" IF TESSERACT_MAJOR_VERSION < 5: cpdef bool RecognizeForChopTest(self, int timeout=0): """Variant on :meth:`Recognize` used for testing chopper.""" cdef: ETEXT_DESC monitor int res with nogil: if timeout > 0: monitor.cancel = NULL monitor.cancel_this = NULL monitor.set_deadline_msecs(timeout) res = self._baseapi.RecognizeForChopTest(&monitor) else: res = self._baseapi.RecognizeForChopTest(NULL) return res == 0 cdef TessResultRenderer *_get_renderer(self, cchar_t *outputbase): cdef: bool b bool font_info IF TESSERACT_VERSION >= 0x3999800: bool textonly TessResultRenderer *temp TessResultRenderer *renderer = NULL IF TESSERACT_VERSION >= 0x3040100: if self._baseapi.GetPageSegMode() == PSM.OSD_ONLY: renderer = new TessOsdRenderer(outputbase) return renderer self._baseapi.GetBoolVariable("tessedit_create_hocr", &b) if b: self._baseapi.GetBoolVariable("hocr_font_info", &font_info) renderer = new TessHOcrRenderer(outputbase, font_info) self._baseapi.GetBoolVariable("tessedit_create_pdf", &b) if b: IF TESSERACT_VERSION >= 0x3999800: self._baseapi.GetBoolVariable("textonly_pdf", &textonly) temp = new TessPDFRenderer(outputbase, self._baseapi.GetDatapath(), textonly) ELSE: temp = new TessPDFRenderer(outputbase, self._baseapi.GetDatapath()) if renderer == NULL: renderer = temp else: renderer.insert(temp) self._baseapi.GetBoolVariable("tessedit_write_unlv", &b) if b: temp = new TessUnlvRenderer(outputbase) if renderer == NULL: renderer = temp else: renderer.insert(temp) self._baseapi.GetBoolVariable("tessedit_create_boxfile", &b) if b: temp = new TessBoxTextRenderer(outputbase) if renderer == NULL: renderer = temp else: renderer.insert(temp) self._baseapi.GetBoolVariable("tessedit_create_txt", &b) if b: temp = new TessTextRenderer(outputbase) if renderer == NULL: renderer = temp else: renderer.insert(temp) return renderer def ProcessPages(self, outputbase, filename, retry_config=None, int timeout=0): """Turns images into symbolic text. Set at least one of the following variables to enable renderers before calling this method:: tessedit_create_hocr (bool): hOCR Renderer if ``font_info`` is ``True`` then it'll be included in the output. tessedit_create_pdf (bool): PDF Renderer tessedit_write_unlv (bool): UNLV Renderer tessedit_create_boxfile (bool): Box Text Renderer tessedit_create_txt (bool): Text Renderer .. note: If tessedit_page_number variable is non-negative, will only process that single page. Works for multi-page tiff file, or filelist. Args: outputbase (str): The name of the output file excluding extension. For example, "/path/to/chocolate-chip-cookie-recipe". filename (str): Can point to a single image, a multi-page TIFF, or a plain text list of image filenames. Kwargs: retry_config (str): Is useful for debugging. If specified, you can fall back to an alternate configuration if a page fails for some reason. timeout (int): Terminates processing if any single page takes too long (`timeout` milliseconds). Defaults to 0 (unlimited). Returns: bool: True if successful, False on error. Raises: :exc:`RuntimeError`: If no renderers enabled in api variables. """ cdef: bytes py_outputbase = _b(outputbase) TessResultRenderer *renderer = self._get_renderer(py_outputbase) bytes py_fname = _b(filename) bytes py_config cchar_t *cconfig if renderer != NULL: if retry_config is not None: py_config = _b(retry_config) cconfig = py_config else: cconfig = NULL try: return self._baseapi.ProcessPages(py_fname, cconfig, timeout, renderer) finally: del renderer raise RuntimeError('No renderers enabled') def ProcessPage(self, outputbase, image, int page_index, filename, retry_config=None, int timeout=0): """Turn a single image into symbolic text. See :meth:`ProcessPages` for descriptions of the keyword arguments and all other details. Args: outputbase (str): The name of the output file excluding extension. For example, "/path/to/chocolate-chip-cookie-recipe". image (:class:`PIL.Image`): The image processed. page_index (int): Page index (metadata). filename (str): `filename` and `page_index` are metadata used by side-effect processes, such as reading a box file or formatting as hOCR. Raises: RuntimeError: If `image` is invalid or no renderers are enabled. """ cdef: bytes py_fname = _b(filename) cchar_t *cfname = py_fname bytes py_outputbase = _b(outputbase) TessResultRenderer *renderer = self._get_renderer(py_outputbase) bytes py_config cchar_t *cconfig cuchar_t *buff size_t size Pix *pix raw = _image_buffer(image) size = len(raw) buff = raw pix = pixReadMem(buff, size) if pix == NULL: raise RuntimeError('Failed to read image') if renderer != NULL: if retry_config is not None: py_config = _b(retry_config) cconfig = py_config else: cconfig = NULL try: return self._baseapi.ProcessPage(pix, page_index, cfname, cconfig, timeout, renderer) finally: pixDestroy(&pix) del renderer raise RuntimeError('No renderers enabled') def GetIterator(self): """Get a reading-order iterator to the results of :meth:`LayoutAnalysis` and/or :meth:`Recognize`. Returns: :class:`PyResultIterator`: reading-order iterator or `None` on failure. """ cdef ResultIterator *iterator = self._baseapi.GetIterator() if iterator == NULL: return None return PyResultIterator.createResultIterator(iterator) def GetUTF8Text(self): """Return the recognized text coded as UTF-8 from the image.""" cdef char *text with nogil: text = self._baseapi.GetUTF8Text() self._destroy_pix() if text == NULL: with gil: raise RuntimeError('Failed to recognize. No image set?') return _free_str(text) IF TESSERACT_VERSION >= 0x4000000: def GetBestLSTMSymbolChoices(self): """Return Symbol choices as multi-dimensional array of tupels. The first dimension contains words. The second dimension contains the LSTM timesteps of the respective word. They are either accumulated over characters or pure which depends on the value set in lstm_choice_mode: 1 = pure; 2 = accumulated. The third dimension contains the symbols and their probability as tupels for the respective timestep. Returns an empty list if :meth:`Recognize` was not called first. """ if self.GetVariableAsString("lstm_choice_mode") == "0": raise RuntimeError('lstm_choice_mode Parameter is 0. Set it to 1 or 2') words = [] wi = self.GetIterator() if wi: for w in iterate_level(wi, RIL.WORD): words.append(w.GetBestLSTMSymbolChoices()) return words def GetHOCRText(self, int page_number): """Return a HTML-formatted string with hOCR markup from the internal data structures. Args: page_number (int): Page number is 0-based but will appear in the output as 1-based. """ cdef char *text with nogil: text = self._baseapi.GetHOCRText(page_number) self._destroy_pix() if text == NULL: with gil: raise RuntimeError('Failed to recognize. No image set?') return _free_str(text) IF TESSERACT_VERSION >= 0x3999800: def GetTSVText(self, int page_number): """Make a TSV-formatted string from the internal data structures. Args: page_number (int): Page number is 0-based but will appear in the output as 1-based. """ cdef char *text with nogil: text = self._baseapi.GetTSVText(page_number) self._destroy_pix() if text == NULL: with gil: raise RuntimeError('Failed to recognize. No image set?') return _free_str(text) def GetBoxText(self, int page_number): """Return recognized text coded in the same format as a box file used in training. Constructs coordinates in the original image - not just the rectangle. Args: page_number (int): Page number is a 0-based page index that will appear in the box file. """ cdef char *text with nogil: text = self._baseapi.GetBoxText(page_number) self._destroy_pix() if text == NULL: with gil: raise RuntimeError('Failed to recognize. No image set?') return _free_str(text) def GetUNLVText(self): """Return the recognized text coded as UNLV format Latin-1 with specific reject and suspect codes. """ cdef char *text with nogil: text = self._baseapi.GetUNLVText() self._destroy_pix() if text == NULL: with gil: raise RuntimeError('Failed to recognize. No image set?') return _free_str(text) IF TESSERACT_VERSION >= 0x3999800: def DetectOrientationScript(self): """Detect the orientation of the input image and apparent script (alphabet). Returns: `dict` or `None` if image was not successfully processed. dict contains: - orient_deg: Orientation of detected clockwise rotation of the input image in degrees (0, 90, 180, 270). - orient_conf: The orientation confidence (15.0 is reasonably confident). - script_name: ASCII string, the name of the script, e.g. "Latin". - script_conf: Script confidence. """ cdef: int orient_deg float orient_conf cchar_t *script_name float script_conf if self._baseapi.DetectOrientationScript(&orient_deg, &orient_conf, &script_name, &script_conf): return {'orient_deg': orient_deg, 'orient_conf': orient_conf, 'script_name': script_name, 'script_conf': script_conf} return None def MeanTextConf(self): """Return the (average) confidence value between 0 and 100.""" return self._baseapi.MeanTextConf() def AllWordConfidences(self): """Return all word confidences (between 0 and 100) as a list. The number of confidences should correspond to the number of space- delimited words in `GetUTF8Text`. """ cdef: int *confidences = self._baseapi.AllWordConfidences() int confidence size_t i = 0 confs = [] while confidences[i] != -1: confidence = confidences[i] confs.append(confidence) i += 1 free(confidences) return confs def AllWords(self): """Return list of all detected words. Returns an empty list if :meth:`Recognize` was not called first. """ words = [] wi = self.GetIterator() if wi: for w in iterate_level(wi, RIL.WORD): words.append(w.GetUTF8Text(RIL.WORD)) return words def MapWordConfidences(self): """Return list of word, confidence tuples""" return list(zip(self.AllWords(), self.AllWordConfidences())) def AdaptToWordStr(self, PageSegMode psm, word): """Apply the given word to the adaptive classifier if possible. Assumes that :meth:`SetImage` / :meth:`SetRectangle` have been used to set the image to the given word. Args: psm (int): Should be :attr:`PSM.SINGLE_WORD` or :attr:`PSM.CIRCLE_WORD`, as that will be used to control layout analysis. The currently set PageSegMode is preserved. word (str): The word must be SPACE-DELIMITED UTF-8 - l i k e t h i s , so it can tell the boundaries of the graphemes. Returns: bool: ``False`` if adaption was not possible for some reason. """ cdef bytes py_word = _b(word) return self._baseapi.AdaptToWordStr(psm, py_word) def Clear(self): """Free up recognition results and any stored image data, without actually freeing any recognition data that would be time-consuming to reload. """ with nogil: self._destroy_pix() self._baseapi.Clear() def End(self): """Close down tesseract and free up all memory.""" with nogil: self._end_api() def IsValidCharacter(self, character): """Return True if character is defined in the UniCharset. Args: character: UTF-8 encoded character. """ cdef bytes py_character = _b(character) return self._baseapi.IsValidCharacter(py_character) def GetTextDirection(self): """Get text direction. Returns: tuple: offset and slope """ cdef: int out_offset float out_slope self._baseapi.GetTextDirection(&out_offset, &out_slope) return out_offset, out_slope def DetectOS(self): """Estimate the Orientation and Script of the image. Returns: `dict` or `None` if image was not successfully processed. dict contains: - orientation: Orientation ids [0..3] map to [0, 270, 180, 90] degree orientations of the page respectively, where the values refer to the amount of clockwise rotation to be applied to the page for the text to be upright and readable. - oconfidence: Orientation confidence. - script: Index of the script with the highest score for this orientation. (This is _not_ the index of :meth:`get_languages`, which is in alphabetical order.) - sconfidence: script confidence. """ cdef OSResults results if self._baseapi.DetectOS(&results): return {'orientation': results.best_result.orientation_id, 'oconfidence': results.best_result.oconfidence, 'script': results.get_best_script(results.best_result.orientation_id), 'sconfidence': results.best_result.sconfidence} return None def GetUnichar(self, int unichar_id): """Return the string form of the specified unichar. Args: unichar_id (int): unichar id. """ return self._baseapi.GetUnichar(unichar_id) def oem(self): """Return the last set OCR engine mode.""" return self._baseapi.oem() def set_min_orientation_margin(self, double margin): """Set minimum orientation margin. Args: margin (float): orientation margin. """ self._baseapi.set_min_orientation_margin(margin) def __enter__(self): return self def __exit__(self, exc_tp, exc_val, exc_tb): with nogil: self._end_api() return False cdef char *_image_to_text(Pix *pix, cchar_t *lang, const PageSegMode pagesegmode, cchar_t *path, OcrEngineMode oem) nogil: cdef: TessBaseAPI baseapi char *text if baseapi.Init(path, lang, oem) == -1: return NULL baseapi.SetPageSegMode(pagesegmode) baseapi.SetImage(pix) text = baseapi.GetUTF8Text() pixDestroy(&pix) baseapi.End() return text def image_to_text(image, lang=_DEFAULT_LANG, PageSegMode psm=PSM_AUTO, path=_DEFAULT_PATH, OcrEngineMode oem=OEM_DEFAULT): """Recognize OCR text from an image object. Args: image (:class:`PIL.Image`): image to be processed. Kwargs: lang (str): An ISO 639-3 language string. Defaults to 'eng'. psm (int): Page segmentation mode. Defaults to :attr:`PSM.AUTO`. See :class:`PSM` for all available psm options. path (str): The name of the parent directory of tessdata. Must end in /. oem (int): OCR engine mode. Defaults to :attr:`OEM.DEFAULT`. see :class:`OEM` for all available oem options. Returns: unicode: The text extracted from the image. Raises: :exc:`RuntimeError`: When image fails to be loaded or recognition fails. """ cdef: bytes py_path = _b(path) bytes py_lang = _b(lang) cchar_t *cpath = py_path cchar_t *clang = py_lang Pix *pix cuchar_t *buff size_t size char *text bytes raw raw = _image_buffer(image) buff = raw size = len(raw) with nogil: pix = pixReadMem(buff, size) if pix == NULL: with gil: raise RuntimeError('Failed to read picture') text = _image_to_text(pix, clang, psm, cpath, oem) if text == NULL: with gil: raise RuntimeError('Failed to init API, possibly an invalid tessdata path: {}'.format(path)) return _free_str(text) def file_to_text(filename, lang=_DEFAULT_LANG, PageSegMode psm=PSM_AUTO, path=_DEFAULT_PATH, OcrEngineMode oem=OEM_DEFAULT): """Extract OCR text from an image file. Args: filename (str): Image file relative or absolute path. Kwargs: lang (str): An ISO 639-3 language string. Defaults to 'eng' psm (int): Page segmentation mode. Defaults to :attr:`PSM.AUTO` See :class:`PSM` for all available psm options. path (str): The name of the parent directory of tessdata. Must end in /. oem (int): OCR engine mode. Defaults to :attr:`OEM.DEFAULT`. see :class:`OEM` for all available oem options. Returns: unicode: The text extracted from the image. Raises: :exc:`RuntimeError`: When image fails to be loaded or recognition fails. """ cdef: bytes py_fname = _b(filename) bytes py_lang = _b(lang) bytes py_path = _b(path) cchar_t *cfname = py_fname cchar_t *clang = py_lang cchar_t *cpath = py_path Pix *pix char *text with nogil: pix = pixRead(cfname) if pix == NULL: with gil: raise RuntimeError('Failed to read picture') text = _image_to_text(pix, clang, psm, cpath, oem) if text == NULL: with gil: raise RuntimeError('Failed to init API, possibly an invalid tessdata path: {}'.format(path)) return _free_str(text) def tesseract_version(): """Return tesseract-ocr and leptonica version info""" version_str = u"tesseract {}\n {}\n {}" tess_v = TessBaseAPI.Version() lept_v = _free_str(getLeptonicaVersion()) libs_v = _free_str(getImagelibVersions()) return version_str.format(tess_v, lept_v, libs_v) def get_languages(path=_DEFAULT_PATH): """Return available languages in the given path. Args: path (str): The name of the parent directory of tessdata. Must end in /. Default tesseract-ocr datapath is used if no path is provided. Returns tuple: Tuple with two elements: - path (str): tessdata parent directory path - languages (list): list of available languages as ISO 639-3 strings. """ IF TESSERACT_MAJOR_VERSION >= 5: cdef: bytes py_path = _b(path) TessBaseAPI baseapi vector[string] v int i ELSE: cdef: bytes py_path = _b(path) TessBaseAPI baseapi GenericVector[STRING] v int i baseapi.Init(py_path, NULL) path = baseapi.GetDatapath() baseapi.GetAvailableLanguagesAsVector(&v) langs = [v[i].c_str() for i in xrange(v.size())] baseapi.End() return path, langs tesserocr-2.5.2/tesserocr_experiment.pyx0000666000175000017500000001275613561370402017644 0ustar fzfz00000000000000# An attempt to address the PIL.Image buffer directly without copying it. # # This is achieved by extracting the buffer ptr from Image.im.unsafe_ptrs # the xsize, ysize, pixelsize and linesize are extracted as well to be used # in TessBaseAPI.SetImage(buffer, width, height, bytes_per_pixel, bytes_per_line) # # This works but for sometimes the output is different than the original code. I assume # this is due to the different image format used in this method. # # The performance advantage was not significant based on benchmarks on my machine. from libc.stdint cimport uintptr_t cdef object _mode_to_bpp = {'1':1, 'L':8, 'P':8, 'RGB':24, 'RGBA':32, 'CMYK':32, 'YCbCr':24, 'I':32, 'F':32} cdef void _image_buffer2(image, cuchar_t **buff, int *width, int *height, int *bpp, int *bpl): """Read image meta data from unsafe pointers.""" cdef uintptr_t buff_ptr # get buffer from unsafe pointers without copying it image.load() ptrs = dict(image.im.unsafe_ptrs) width[0] = ptrs['xsize'] height[0] = ptrs['ysize'] buff_ptr = ptrs['image'] buff[0] = (buff_ptr)[0] bpp[0] = ptrs['pixelsize'] bpl[0] = ptrs['linesize'] # for f in ptrs: # name = f[0] # if name == 'xsize': # width # width[0] = f[1] # elif name == 'ysize': # height # height[0] = f[1] # elif name == 'image': # buffer address # buff_ptr = f[1] # buff[0] = (buff_ptr)[0] # elif name == 'pixelsize': # bytes_per_pixel # bpp[0] = f[1] # elif name == 'linesize': # bytes_per_line # bpl[0] = f[1] cdef char *_image_to_text2(const unsigned char *buff, int width, int height, int bpp, int bpl, const char *lang, const PageSegMode pagesegmode, const char *path) nogil except NULL: cdef: TessBaseAPI baseapi char *text if baseapi.Init(path, lang) == -1: return NULL baseapi.SetPageSegMode(pagesegmode) baseapi.SetImage(buff, width, height, bpp, bpl) text = baseapi.GetUTF8Text() baseapi.End() return text def image_to_text2(image, const char *lang=_DEFAULT_LANG, const PageSegMode pagesegmode=PSM_AUTO, const char *path=_DEFAULT_PATH): """Recognize OCR text from an image object. Args: image (:class:`PIL.Image`): image to be processed. Kwargs: lang (str): An ISO 639-3 language string. Defaults to 'eng'. pagesegmode (int): Page segmentation mode. Defaults to `PSM.AUTO`. See :class:`~tesserocr.PSM` for all available psm options. path (str): The name of the parent directory of tessdata. Must end in /. Returns: str: The text extracted from the image. Raises: RuntimeError: When image fails to be loaded or recognition fails. """ cdef: cuchar_t *buff = NULL int width = 0 int height = 0 int bpp = 0 int bpl = 0 char *text _image_buffer2(image, &buff, &width, &height, &bpp, &bpl) # print width, height # print bpp # print bpl with nogil: text = _image_to_text2(buff, width, height, bpp, bpl, lang, pagesegmode, path) if text == NULL: with gil: raise RuntimeError('Failed to recognize image text.') return _free_str(text) cdef Pix *raw_to_pix(cuchar_t *buff, int bpp, int width, int height, int bpl) nogil: """Convert PIL image to Pix. Applies the same logic done by tesseract's api.SetImage.""" cdef: int x int y int wpl uint *data Pix *pix bpp = bpp * 8 pix = pixCreate(width, height, 32 if bpp == 24 else bpp) wpl = pixGetWpl(pix) data = pixGetData(pix) if bpp == 8: # Greyscale just copies the bytes in the right order. for y in xrange(height): for x in xrange(width): SET_DATA_BYTE(data, x, buff[x]) data += wpl buff += bpl elif bpp == 24: # Put the colors in the correct places in the line buffer. for y in xrange(height): for x in xrange(width): SET_DATA_BYTE(data, COLOR_RED, buff[3 * x]) SET_DATA_BYTE(data, COLOR_GREEN, buff[3 * x + 1]) SET_DATA_BYTE(data, COLOR_BLUE, buff[3 * x + 2]) data += 1 buff += bpl elif bpp == 32: # Maintain byte order consistency across different endianness. for y in xrange(height): for x in xrange(width): data[x] = (buff[x * 4] << 24) | (buff[x * 4 + 1] << 16) | (buff[x * 4 + 2] << 8) | buff[x * 4 + 3] data += wpl buff += bpl else: with gil: raise RuntimeError("Cannot convert RAW image to Pix with bpp = {}".format(bpp)) return pix def image_to_text3(image, const char *lang=_DEFAULT_LANG, const PageSegMode psm=PSM_AUTO, const char *path=_DEFAULT_PATH): cdef: Pix *pix cuchar_t *buff = NULL int width = 0 int height = 0 int bpp = 0 int bpl = 0 char *text _image_buffer2(image, &buff, &width, &height, &bpp, &bpl) with nogil: pix = raw_to_pix(buff, bpp, width, height, bpl) text = _image_to_text(pix, lang, psm, path) if text == NULL: with gil: raise RuntimeError('Failed recognize picture') return _free_str(text) tesserocr-2.5.2/tests/0000755000175000017500000000000014063456316013763 5ustar fzfz00000000000000tesserocr-2.5.2/tests/__init__.py0000666000175000017500000000000013561370402016057 0ustar fzfz00000000000000tesserocr-2.5.2/tests/eurotext.png0000644000175000017500000003464414063446477016372 0ustar fzfz00000000000000PNG  IHDR ؂xPLTEٟ pHYs.#.#x?v IDATxoɒ߫J2"r Z`;Geabz0/t':oJ囒w't|#IV7{~o3####2"ȏbF`F`F`F`F`F`F`F E={L(t ->81^O/@l=Oս h>>,/-ȵmuлX6Wۈ]H1:B#2kZMQs\EZve8~`dUyp:N:Yp)/7 ((x`@ݠVhnK^ j ls #PS(L0U9}u-XY|hJ[Q\PWu+ L<ɝƢ e5 DIS&v@BN}h^/*4&r7#uS0@P6 ݍ@c9l`DK- y[z @~obcEh^}IDlb68I­;S&Ґx-~  %|0?4[X(o(,Ey!F@,Pt,L-W /C\$ i(t*LS0 c?^g A6$/YbF&r0e2m{(g~8ҤDlK#C 3UI%.1  4&'ruڽBumTZK&n:yTq'ӄ GE8_h ތ0@[Q-n5{Y\ a`[8жL*o .w U2Mz;h";*@4* r8=)_"Ԧ U6H$@Fx4!]-:6IO`jJAG]XtfWSq.r"6<~պ [i0j|ԁđJV@U[͢j3[X8qT p:񑶛je ~$[x[&Otnؤ%l?:de8R[-,)O(E2ϕ YguLg6Y36sA{@jd; ϿVA\F[m7+}2Z0 e{CѾ|8PdBz `b+\gAբGCXx\X=x٬i'8mߨ/1d[#Д0RL=6ՒQpUp/X8w~K=@)&08pvٲs^"lӔIzW-dt?` p ~' ugg}mS'+)(V80)fյ Pҟ ڴ ЉeKWNN6z@\q *}l{~l{I)86{GG:4ڳÈU a= O=@[.LzWrc^#x9MqDXl?tFWb\ .p,6 =m }FM]J MjuFG^+@YprE%Htx] QHyJE00 @}y MT-B`-!Z֗-L0e])lpZkkv@ݾEJ;&N#վR={#`:C5!GJ+%݁N/K^e{$Le0{''EDk: k^Vt&$-W/an"'7|kB58n`-28'$ bpBlW% aP#7\@vQ5p$gi/@`i~UZ@:Mڌ @l )Yh7$ŧ pu lx+1F7}x3;N:X2lY[8>MT&_6H*3hiʧ@ҶOXmLdP }KE-CMcCV١jl]yi;,JaP9(*#}L@3A \5y;M g9$!*qOKW:Yu ͑0N~+O$' MlO(Ƹf*X *ښ_I (I,^4;w|3w֩4;dϝ>X0NNW?@s'NeAQ](<ܦG*9bҨy,' W;d%@rM77,fISy:s}1w.w9CĞ4*}s7Z@_W/Gu@ p0wf눮XEn(^+O/_ te盽M C碭cc bZ?_ pn@@^`f@,Yжi>nւ7C pߠo3 @Ώ*s(=clzbC(n>{W/M*RϜc N^#=V .b|B8P\'SAKD?Tra,Q;AGzòrd"8oC:\dEZxW@`f.{ Ŷ¶҆ci!$7銁lxeO6؋`&{qP[n[*`{@[sh)@S[ w )t`?8f ؜ڽup9?ȫ  hR$pSpT 鯲FfZ.fn=!z.C=Es/} ",x ͡j%}Q A5ej<`p)9I4* {DsܣloE:Hzȯۍ@4#8 tӛ]^җ\|\¦}H>,v_ XyAQˠ`C3/p@&,hK.>,^+dۦK e!V@O$G&h+VNKTxEhDJLt/oଜ"635m^CF9MJs*x<۪eU! 1QQ aDEMti#4s(V@آ/0_$`*g_)&Ts=@Pس۫i'ŕ1\ @D )M@06`4Ơu gNy â&IwjD:8]NwOtzX}U j>}<҃* .L "Hhua+jD1-+}/\@/cV|fa2@̓EHZ@(nWRnSL%e8N1`  M X_ vC0}'~A-&-)BKq(@c&T̏w{9}i@LXF聩?f`眉pΕ9` -224{e AHE͉*p~ HTK*pI{O 2K8'I<+OTs=pǣ8QPE;mA5Z%MMze5.~YDtDS\ZdV;;|M+4h.iwBK7P (p=!-+6@<qR$s) $vJܖ\{+ <1*K۱l r[pF]uބlD QxJ݆'1^t` ֏ <6#', k24ǤXZ7o070:4@[^pIn0ST1yڮ4A0.klnS]3[ĭW^6EnsM4T.|V\0mg:Tqz[l}wTM)㤌Դ>@5ۻ %;('\78uN=~_LIJ?u>חB'Wn.P]OPf|j)j\4xpa*-,^3!Jctt{U`cQxGOp{'2I^Q$lP`V{BiRBT-$35:yP0@PaIkh2& \w()PQYL)\K(|!3I[m, aĐHmQ ;m 3G r(T98 H o \@a@S7v40@:'Hn@ h'٫2+Ӵaзsj@,4%< ɘ"LdN|EVCVGh`]P:r (ux&,U `i.r G *V5oeXT 8J34mSPr̖<~d~#EQ1}$n\eW@] PXgiuG>.ez >P7v!LU'l\\H'T~ *| ! h; '2j&/2@,ljcf@8g醊nޤ8Em57HZ=tK &Ae ˯WNh ^$OI mǩ,Ehh/)rBHFzUQZh@Cx"?HEM*Heq{RS&j`XrÐ] [89h`.@ ql8Pl玛!jY@EDw@p x%+M,֦y\wM!6; @L+|O=cEz=XQL2 2GMSHpy&~hc@8oeUU3Ut:XU][ 3b.N{\0pܾީaWx;HGHڗAkG d P[G3xiQ6=9@8?mBj"\Rȸ ע@Z( D$@st *1|e i hb"@mp&<(ʂLm7ˀEs-@4y8Kt?h-h\Y_=`\"pPT$k 5jdjԭr @xze7`o3 >Eei^[V=zP 5#|(O9ȡJsB2.2$y2p @8yɄhx?JGyH]tS`W @O\]iXYJ M(M2 b3xZ3,:Y;~8@`4?dk:P'!t?Bz <`vŦIii-n\zY+z@-|K:vp#lfeL*bіLk X\&?8?=4+ˊF׬h,}"07{փR4f6ifEKz`cEMRD`iXI[ȶZ%jPk PF 11*=iȑtPq8^AJOB6o St]L&C;*$XԱ\#0}lTP  Z|d52֬0`C_ok@=`oU~?H:_L /Եe`z5 pqa%*Aq9u87]*8| [ewopr 9rMĪ%!.dgrGw ?;@2r ,eku}po)#{Ox&b?oT :9ei%<(u#Kd!|{T$z}z?xO۽{>ܴ3vtFWS+ NTl=esܼSےU>]O/uz# u_rN1A4AVwo \э%Mmc$Am@em[2T `ZSct4W'$jT"6ȫp:m(2J]rzv 1+}jv4E`kf<|< F5u*_`45\&>?8IDATl y(=9!1Mʁz@O.XxL_XA0@~U3x@+`9V0I=gW*= *` V1U0Et1Bզ9dgp4!I`E 3R,7 P  `bZ./^hʗ_/9Otg /CÊ(Y` ~ [@hfRsEdG U銹ؐe&p'xkBX$hX}I|oҷ8K s@wBڃ^.xX?fBJzczEn,)]?P/`#G /R`%>8;> =0 l$9V`< ` }2 WSP/81/{ kP B  7Ev .jr@@}I2yGEnRt@+p `!R#3EXv01K ^ А*&] cU8Rvp)кgey^Us@ej/iRs5f>w*ax t1RC1DR6qL*Z /ږbxF:" sfʩArJ&8ሗ!-rMALjdw#@drTm"OʿDj)cyfC tNS^! *\R 9^ X:[;TJ(S {!Lux \u[ @#RY% JVV   0` ,9mu[MiPmItIt-j/Ф#DXW 8!0Cjm.$R`.}5Pm$}Go)Ʌ2vU#PuntMT+ KPES| f;,@'vSegxhKNGېtQ-rSaHOI4;ƌ%o (Ir*4TX4Pjh"UJPJN!kj@1 m+jw_gk.pugiJs/;n@#G#c@1'O#o fS{(tSO٩?Y?*|Qzf3c {<6@[>2@(} (>i^{0=k&v\Wdkz N~'Pz[nvɀWqB&mm*@:Ocҕ5> j)m |=bC, !jpGVcZV$4mk1&wsu5孝8ҩӇ5d[:f0lܻ97l}@I>ǿ~@ːJ˥/yw.%ib5\ ! MlD%wh0429t3/O&njuf QiK qM_$|i|Y=PLo8.FE9co꒽$IO`XXLp ܵUA׳xL6"&9Ƹ\=|.׷ ,85,#$ !*yJz_G HhS|O)SDU ,f-@ pfPIZI/Z.I6Gšl9emxr, ` \i@H'4+m&! lXז] RA̢po*@[pƪp:f,@"@pI&7z{X ܃]PD~zeZfk%S@87s #{.=X.cv[rK^нhu.24@|bc[M cGD(P}pBVdqW}dS <_%猑"v4 ?-_;8>PٸSEGF:NiBdUXȐVpmcQn VCԊIѪTu;P}d Yg]_vr @azJjQbQ>2@# c#Wև`ca[? [Lg+\=,\kVOe` `W{}),{ꊝEb_X0kW{W2I˵+- 8; {lf}Zvȶr:zT>LԹ"~S`ǃ?•Yt]pw8@[.BեѦr=2-t# 8I|N'k#UjX zB 0*2\0 t4 r Q5aFZ1O*d %yui |;UB 7v,V zyn% i RAZ,CWxz6nGs@t @ZifIa|̫a HLAAv{ ڨbbqpb`n iNB,8DR8n.9&HܻN%; b©@aޮh$-ry#@{>yG1z8^sT `1eK ))uR`=a]Ƿ'0GvVcŽT9gъ/ @WwSK?)ߏ6 CbaL%.zqZK:ߒt!'Ď"rfeT).Et e:9S6cav 0')DR Wxҫګ@s]9FeNmu 2;mNf#k+Fo˨GxO<6@G1`1>6@@?2tp]*1:}?ErpR#]G2sܘp ~_㹉 .B79>E#[ 'xtʞGAvpT?MLfmw:MWᐷ?%SRwа5һ{uzP ])yHVqMH/)Gq'(^nc->$7eUVwJzBsW!g',8KE"NHS,*7{ `E!pH9X T]Tl*Jwqi#P]Y4  s\9׉ -"VͥzXP @ӆ<ظ?hO|#/[h72 XWn fszz`%!9n ]6u ^M ~T[͗= ` fXT4>WsLT@d3TG6|i~IaTQD6#I`𴿂Ohc@;l^ ~acy`a8mdr2- Ke SD*oݟ4j?_/lHz&* S;txv 0$ӏ 1D F]{GXq)VzN?X2 zc[~uzoxmͿZZ"{6knUwkqN^ rREXW[~6&eT =rTnͥp|E$(:@{zJ'm\ x &謲8BYs(`h%$N3L=z d r %l @}8k5x\ a %lPrP笊gAxKHHpWڧRvg%:CUgY4 -~ʯOzmH~)T@n T W$W=5W@͖z.[ɛ:aorXgwr/O.ݝ: pRؚ u1/c@`iqI#vz:H chmaj+ZP+W +tXD{Km ]?gy%hB,<$2bpy;_G푇5 |@|xueh#v>;tp[>@ݝ]jwȍc ަT̝M-Y}`F`F`F`F`F`F`F`F`F`F`F`F`F`F`F`F`FG @IENDB`tesserocr-2.5.2/tests/test_api.py0000644000175000017500000003322314063446477016157 0ustar fzfz00000000000000import unittest import re import os.path import tesserocr try: from PIL import Image pil_installed = True except ImportError: pil_installed = False def version_to_int(version): subversion = None subtrahend = 0 # Subtracts a certain amount from the version number to differentiate # between alpha, beta and release versions. if "alpha" in version: version_split = version.split("alpha") subversion = version_split[1] subtrahend = 2 elif "beta" in version: version_split = version.split("beta") subversion = version_split[1] subtrahend = 1 version = re.search(r"((?:\d+\.)+\d+)", version).group() # Split the groups on ".", take only the first one, and print each # group with leading 0 if needed. To be safe, also handle cases where # an extra group is added to the version string, or if one or two # groups are dropped. version_groups = (version.split(".") + [0, 0])[:3] version_str = "{:02}{:02}{:02}".format(*map(int, version_groups)) version_str = str((int(version_str, 10) - subtrahend)) # Adds a 2 digit subversion number for the subversionrelease. subversion_str = "00" if subversion is not None and subversion != "": subversion = re.search(r"(?:\d+)", subversion).group() subversion_groups = (subversion.split("-") + [0, 0])[:1] subversion_str = "{:02}".format(*map(int, subversion_groups)) version_str += subversion_str return int(version_str, 16) _TESSERACT_VERSION = version_to_int(tesserocr.PyTessBaseAPI.Version()) class TestTessBaseApi(unittest.TestCase): _test_dir = os.path.abspath(os.path.dirname(__file__)) _image_file = os.path.join(_test_dir, "eurotext.png") def setUp(self): if pil_installed: with open(self._image_file, "rb") as f: self._image = Image.open(f) self._image.load() self._api = tesserocr.PyTessBaseAPI(init=True) def tearDown(self): if pil_installed: self._image.close() self._api.End() def test_context_manager(self): """Test context manager behavior""" with self._api as api: self.assertIs(api, self._api) api.SetImageFile(self._image_file) self.assertEqual(api.GetUTF8Text(), self._api.GetUTF8Text()) # assert api has Ended self.assertRaises(RuntimeError, self._api.GetUTF8Text) def test_init_full(self): """Test InitFull.""" # check default settings self.assertEqual(self._api.GetVariableAsString("file_type"), ".tif") self.assertEqual(self._api.GetVariableAsString("edges_childarea"), "0.5") # use box.train config variables configs = ["box.train"] # change edges_childarea vars_ = {"edges_childarea": "0.7"} self._api.End() self._api.InitFull(configs=configs, variables=vars_) # assert file_type from box.train and custom edges_childarea self.assertEqual(self._api.GetVariableAsString("file_type"), ".bl") self.assertEqual(self._api.GetVariableAsString("edges_childarea"), "0.7") # reset back to default self._api.End() self._api.Init() def test_init(self): """Test Init calls with different lang and oem.""" self._api.Init(lang="eng+osd") self.assertEqual(self._api.GetInitLanguagesAsString(), "eng+osd") self._api.Init(lang="eng") self.assertEqual(self._api.GetInitLanguagesAsString(), "eng") self._api.Init(oem=tesserocr.OEM.TESSERACT_ONLY) self.assertEqual(self._api.oem(), tesserocr.OEM.TESSERACT_ONLY) @unittest.skipIf(not pil_installed, "Pillow not installed") def test_image(self): """Test SetImage and GetUTF8Text.""" self._api.SetImage(self._image) text = self._api.GetUTF8Text() self.assertIn("quick", text) text2 = tesserocr.image_to_text(self._image) self.assertEqual(text, text2) def test_image_file(self): """Test SetImageFile and GetUTF8Text.""" self._api.SetImageFile(self._image_file) text = self._api.GetUTF8Text() self.assertIn("quick", text) text2 = tesserocr.file_to_text(self._image_file) self.assertEqual(text, text2) @unittest.skipIf(not pil_installed, "Pillow not installed") def test_thresholded_image(self): """Test GetThresholdedImage and GetThresholdedImageScaleFactor.""" orig_size = self._image.size self._api.SetImage(self._image) image = self._api.GetThresholdedImage() self.assertIsNot(image, None) self.assertIsInstance(image, Image.Image) self.assertEqual(image.size, orig_size) self.assertEqual(self._api.GetThresholdedImageScaleFactor(), 1) def test_page_seg_mode(self): """Test SetPageSegMode and GetPageSegMode.""" self._api.SetPageSegMode(tesserocr.PSM.SINGLE_WORD) self.assertEqual(self._api.GetPageSegMode(), tesserocr.PSM.SINGLE_WORD) self._api.SetPageSegMode(tesserocr.PSM.AUTO) self.assertEqual(self._api.GetPageSegMode(), tesserocr.PSM.AUTO) def test_data_path(self): """Test GetDatapath and Init with an invalid data path.""" path = self._api.GetDatapath() self._api.End() self.assertRaises( RuntimeError, self._api.Init, path=(self._test_dir + os.path.sep) ) # no tessdata if _TESSERACT_VERSION >= 0x3999800: new_path = path else: new_path = os.path.abspath(os.path.join(path, os.path.pardir)) + os.path.sep self._api.End() self._api.Init(new_path) self.assertEqual(self._api.GetDatapath(), path) def test_langs(self): """Test get langs methods.""" self._api.Init(lang="eng") lang = self._api.GetInitLanguagesAsString() self.assertEqual(lang, "eng") langs = self._api.GetLoadedLanguages() self.assertEqual(langs, ["eng"]) self.assertIn("eng", self._api.GetAvailableLanguages()) def test_variables(self): """Test SetVariable and GetVariableAsString.""" self._api.SetVariable("debug_file", "/dev/null") self.assertEqual(self._api.GetVariableAsString("debug_file"), "/dev/null") @unittest.skipIf(not pil_installed, "Pillow not installed") def test_rectangle(self): """Test SetRectangle.""" self._api.SetImage(self._image) self._api.SetRectangle(0, 0, 100, 43) thresh = self._api.GetThresholdedImage() self.assertEqual(thresh.size, (100, 43)) def test_word_confidences(self): """Test AllWordConfidences and MapWordConfidences.""" self._api.SetImageFile(self._image_file) words = self._api.AllWords() self.assertEqual(words, []) self._api.Recognize() words = self._api.AllWords() confidences = self._api.AllWordConfidences() self.assertEqual(len(words), len(confidences)) mapped_confidences = self._api.MapWordConfidences() self.assertEqual([v[0] for v in mapped_confidences], words) self.assertEqual([v[1] for v in mapped_confidences], confidences) @unittest.skipIf(_TESSERACT_VERSION < 0x4000000, "tesseract < 4") def test_LSTM_choices(self): """Test GetBestLSTMSymbolChoices.""" self._api.SetVariable("lstm_choice_mode", "2") self._api.SetImageFile(self._image_file) self._api.Recognize() LSTM_choices = self._api.GetBestLSTMSymbolChoices() words = self._api.AllWords() self.assertEqual(len(words), len(LSTM_choices)) for choice, word in zip(LSTM_choices, words): chosen_word = "" for timestep in choice: for alternative in timestep: self.assertGreaterEqual(alternative[1], 0.0) self.assertLessEqual(alternative[1], 2.0) chosen_symbol = timestep[0][0] if chosen_symbol != " ": chosen_word += chosen_symbol self.assertEqual(chosen_word, word) @unittest.skipIf(_TESSERACT_VERSION < 0x4000000, "tesseract < 4") def test_result_iterator(self): """Test result iterator.""" self._api.SetImageFile(self._image_file) self._api.Recognize() it = self._api.GetIterator() level = tesserocr.RIL.WORD for i, w in enumerate(tesserocr.iterate_level(it, level)): text = w.GetUTF8Text(level) blanks = w.BlanksBeforeWord() if i == 0: self.assertEqual(text, "The") self.assertEqual(blanks, 0) elif i == 1: self.assertEqual(text, "(quick)") self.assertEqual(blanks, 1) else: break def test_detect_os(self): """Test DetectOS and DetectOrientationScript (tesseract v4+).""" self._api.SetPageSegMode(tesserocr.PSM.OSD_ONLY) self._api.SetImageFile(self._image_file) orientation = self._api.DetectOS() all( self.assertIn(k, orientation) for k in ["sconfidence", "oconfidence", "script", "orientation"] ) self.assertEqual(orientation["orientation"], 0) # this is sorted alphabetically! languages = tesserocr.get_languages()[1] self.assertLess(orientation["script"], len(languages)) # therefore does not work # script_name = languages[orientation["script"]] # self.assertEqual(script_name, 'Latin') # cannot test: not reliable if _TESSERACT_VERSION >= 0x3999800: orientation = self._api.DetectOrientationScript() all( self.assertIn(k, orientation) for k in ["orient_deg", "orient_conf", "script_name", "script_conf"] ) self.assertEqual(orientation["orient_deg"], 0) self.assertEqual(orientation["script_name"], "Latin") def test_clear(self): """Test Clear.""" self._api.SetImageFile(self._image_file) self._api.GetUTF8Text() self._api.Clear() self.assertRaises(RuntimeError, self._api.GetUTF8Text) def test_end(self): """Test End.""" self._api.End() self._api.SetImageFile(self._image_file) self.assertRaises(RuntimeError, self._api.GetUTF8Text) @unittest.skipIf(not pil_installed, "Pillow not installed") def test_empty_getcomponents(self): self._api.Init() image = Image.new("RGB", (100, 100), (1, 1, 1)) self._api.SetImage(image) result = self._api.GetComponentImages(tesserocr.RIL.TEXTLINE, True) # Test if empty self.assertFalse(result) @unittest.skipIf(not pil_installed, "Pillow not installed") def test_empty_small_getcomponents(self): self._api.Init() image = Image.new("RGB", (1, 1), (1, 1, 1)) self._api.SetImage(image) result = self._api.GetComponentImages(tesserocr.RIL.TEXTLINE, True) # Test if empty self.assertFalse(result) def test_layout_getcomponents(self): self._api.Init() self._api.SetImageFile(self._image_file) result = self._api.GetComponentImages(tesserocr.RIL.BLOCK, True) # Test if not empty self.assertTrue(result) _, xywh, _, _ = result[0] # bbox of largest self.assertIn("w", xywh) self.assertIn("h", xywh) area = xywh["w"] * xywh["h"] # Test if the largest block is quite large self.assertGreater(area, 400000) def test_layout_boundingbox(self): self._api.Init() self._api.SetImageFile(self._image_file) layout = self._api.AnalyseLayout() # Test if not empty self.assertTrue(layout) self.assertFalse(layout.Empty(tesserocr.RIL.BLOCK)) result = layout.BoundingBox(tesserocr.RIL.BLOCK) # bbox of largest self.assertIsNot(result, None) x0, y0, x1, y1 = result area = (x1 - x0) * (y1 - y0) # Test if the largest block is quite large self.assertGreater(area, 400000) def test_layout_blockpolygon(self): self._api.Init() self._api.SetImageFile(self._image_file) layout = self._api.AnalyseLayout() # Test if not empty self.assertTrue(layout) self.assertFalse(layout.Empty(tesserocr.RIL.BLOCK)) result = layout.BlockPolygon() # polygon of largest # Test if not empty self.assertIsNot(result, None) # Test there are at least 4 contour points self.assertGreaterEqual(len(result), 4) xs, ys = zip(*result) x0, y0, x1, y1 = min(xs), min(ys), max(xs), max(ys) area = (x1 - x0) * (y1 - y0) # Test if the largest block is quite large self.assertGreater(area, 400000) def test_recognize(self): """Test Recognize with and without timeout.""" self._api.SetImageFile(self._image_file) # timeout after 1 milliseconds (likely) res = self._api.Recognize(1) self.assertFalse(res) self._api.SetImageFile(self._image_file) # timeout after 10 seconds (unlikely) res = self._api.Recognize(10000) self.assertTrue(res) self._api.SetImageFile(self._image_file) # no timeout res = self._api.Recognize() self.assertTrue(res) @unittest.skipIf(_TESSERACT_VERSION < 0x3040100, "tesseract < 4") def test_row_attributes(self): self._api.SetImageFile(self._image_file) self._api.Recognize() it = self._api.GetIterator() attrs = it.RowAttributes() self.assertIsInstance(attrs["row_height"], float) self.assertIsInstance(attrs["ascenders"], float) self.assertIsInstance(attrs["descenders"], float) if __name__ == "__main__": unittest.main()