Search code examples
pythonpyinstallertesseractpython-tesseract

Tesseract OCR doesn't work when Python script is converted to exe without console


I have an ML solution. I use Pytesseract in this solution. I need to create an executable from it. So I use the pyinstaller. To create an executable that can call another exe, the tesseract exe, I followed the https://stackoverflow.com/a/60679256/13080899. When I create the exe with console Tesseract exe is called in my exe and gives me output but if I create the exe without console Tesseract doesn't work. I couldn't find any solution. How can I solve the problem?

Here is the my .spec file:

# -*- mode: python ; coding: utf-8 -*-
import sys
sys.setrecursionlimit(5000)

block_cipher = None


a = Analysis(['Cam_Choice.py'],
             pathex=['D:\\Project\\XXX'],
             binaries=[('config\\tesseract\\tesseract.exe', 'config\\tesseract')],
             datas=[],
             hiddenimports=['boto3'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
a.datas += [('logo.ico', 'D:\\Project\\img\\logo.ico', "DATA")]

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='XXX',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False,
      icon='D:\\Project\\img\\logo.ico')

P.S: Because of non-console mode I can't debug the exe.


Solution

  • Here is the solution:

    @run_once
    def get_tesseract_version():
        """
        Returns LooseVersion object of the Tesseract version
        """
        try:
            return LooseVersion(
                subprocess.check_output(
                    [tesseract_cmd, '--version'],
                    stderr=subprocess.STDOUT,
                    env=environ,
                    stdin=subprocess.DEVNULL,
                )
                .decode('utf-8')
                .split()[1]
                .lstrip(string.printable[10:]),
            )
        except OSError:
            raise TesseractNotFoundError()
    

    Added stdin=subprocess.DEVNULL, line to the Python\Python36\Lib\site-packages\pytesseract\pytesseract.py.

    For more information: https://github.com/pyinstaller/pyinstaller/issues/5601