【问题标题】:Tesseract OCR doesn't work when Python script is converted to exe without console在没有控制台的情况下将 Python 脚本转换为 exe 时,Tesseract OCR 不起作用
【发布时间】:2021-03-05 21:43:19
【问题描述】:

我有一个机器学习解决方案。我在这个解决方案中使用 Pytesseract。我需要从中创建一个可执行文件。所以我使用pyinstaller。为了创建一个可以调用另一个 exe 的可执行文件,即 tesseract exe,我遵循了https://stackoverflow.com/a/60679256/13080899。当我使用控制台 Tesseract 创建 exe 时,在我的 exe 中调用 exe 并给我输出,但是如果我在没有控制台的情况下创建 exe,Tesseract 将不起作用。我找不到任何解决方案。我该如何解决这个问题?

这是我的 .spec 文件:

# -*- 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:由于非控制台模式,无法调试 exe。

【问题讨论】:

  • doesn't work 你的意思是 exe 没有启动或看不到输出吗?如果您的程序向控制台提供输出,那么很明显使用--no-console 模式不会显示输出,您可能需要考虑创建一个显示控制台输出的小型 GUI。
  • 我的应用程序可以工作,但是当我单击触发文本识别模块的按钮时rec = pytesseract.image_to_data(processed, output_type='data.frame', config= config_) 这行不起作用。我试图用 try-except 包装它并将错误写入文件,但它给了我一个空文件。我无法捕捉到错误。

标签: python pyinstaller tesseract python-tesseract


【解决方案1】:

解决办法如下:

@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()

Python\Python36\Lib\site-packages\pytesseract\pytesseract.py 中添加了 stdin=subprocess.DEVNULL, 行。

欲了解更多信息:https://github.com/pyinstaller/pyinstaller/issues/5601

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多