0

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.

Ugurcan
  • 366
  • 1
  • 4
  • 17
  • By *doesn't work* do you mean that the exe doesn't launch or the output is not seen? If your program gives an output to the console then it is obvious that using `--no-console` mode will not show the output, you might have to look into creating a small GUI that displays the console output. – astqx Mar 05 '21 at 00:56
  • My application works but when I click the button that triggers the text recognition module `rec = pytesseract.image_to_data(processed, output_type='data.frame', config= config_)` This line doesn't work. I tried to wrap it with try-except and write error to a file but it gave me an empty file. I couldn't catch the error. – Ugurcan Mar 05 '21 at 11:08

1 Answers1

0

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

Ugurcan
  • 366
  • 1
  • 4
  • 17