【问题标题】:cx_Freeze: Executable with pytesseract fails on other PCcx_Freeze:pytesseract 的可执行文件在其他 PC 上失败
【发布时间】:2018-11-28 07:33:27
【问题描述】:

我使用 cx_Freeze 从我的 Python - OCR 程序 (import pytesseract) 创建了一个可执行文件。生成的 .exe 在我自己的机器上运行良好。为了测试该程序,我制作了一个 zip 文件夹并在共享点上与一些朋友和同事共享。 现在可执行文件存在以下问题。 pytesseract 似乎不包含在可执行文件/zip 文件夹中。

这是错误日志:

The file is not a PNG-file:  20181108 Kontaktanzeigen-Er_sucht_sie.jpg
The file is not a PNG-file:  20181108 Kontaktanzeigen-Sie_sucht_ihn.jpg
The file is not a PNG-file:  20181108 Language family tree.jpg
The file is not a PNG-file:  20181108 relax.jpg
The file is not a PNG-file:  20181109 Essen wie Gott am Südpol S1.JPG
The file is not a PNG-file:  20181109 Fahrzeugschein1.jpg
The file is not a PNG-file:  20181112 sie_sind_alle_so_dumm.jpg
Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseract
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 707, in __init__
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 992, in _execute_child
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 916, in _bootstrap_inner
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 864, in run
  File "C:\Users\TFischer\PycharmProjects\Test_Project\OCR.py", line 67, in img_to_text
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 172, in run_tesseract
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseract
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 707, in __init__
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py", line 992, in _execute_child
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 916, in _bootstrap_inner
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 864, in run
  File "C:\Users\TFischer\PycharmProjects\Test_Project\OCR.py", line 73, in img_to_text1
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 294, in image_to_string
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 202, in run_and_get_output
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pytesseract\pytesseract.py", line 172, in run_tesseract
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

第一行是绝对清楚的。我找到了一些想法来修改setup.py 以将所有导入包含在由cx_Freeze 创建的可执行文件中。这是最新版本。

from cx_Freeze import setup, Executable
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

additional_mods = ['numpy.core._methods', 'numpy.lib.format']
additional_files = [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]

options = {
    'build_exe': {'include_files': additional_files, 'includes': additional_mods}
    }

setup(options = options,
      name = "main" ,
      version = "0.1" ,
      description = "" ,
      executables = [Executable("main.py")])

【问题讨论】:

    标签: python executable cx-freeze python-tesseract


    【解决方案1】:

    pytesseract 是 Google 的 Tesseract-OCR 引擎的包装器。为了pytesseractworks,需要安装 Tesseract-OCR 引擎,并且必须能够以tesseract 调用 tesseract 命令(这意味着 Tesseract-OCR 安装目录需要在 PATH 中)。

    我相信pytesseract 已正确包含在可执行文件中,但tesseract 命令在“其他PC”上不起作用(在那里没有安装Tesseract-OCR 或者没有安装Tesseract-OCR 安装目录在 PATH 那里)。

    您需要告诉“其他 PC”的所有者安装 Tesseract-OCR,并检查它是否可以从任何位置使用命令 tesseract 启动。

    另一个选项是使用build_exeoption include_files(您的变量additional_files)将整个 Tesseract-OCR 安装目录包含在冻结的可执行文件中,并确保此包含的目录包含在 PATH 上目标 PC,但这可能与平台无关,因此不推荐。

    【讨论】:

    • 因为我想把 zip-Folder 和一些 .png 文件给学生,所以我想让它尽可能简单。我想我会尝试第二个想法。因此,如果他们将解压缩的文件放入C:\OCR,他们必须将C:\OCR 添加到他们的环境变量中,还是需要将确切的路径添加到子文件夹C:\OCR\build\...\tesseract
    • 我很确定您需要将确切的路径添加到找到tesseract.exe 的子文件夹中。您甚至可以设法在主应用程序中使用 os.environ['PATH'] += os.pathsep + path_to_tesseract_folderpath_to_tesseract_folder 在运行时根据主应用程序位置计算动态地执行此操作。
    • 我目前正在测试这些更改。当我有结果时,我会告诉你它对我的效果。
    猜你喜欢
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2021-02-07
    • 1970-01-01
    • 2020-11-06
    相关资源
    最近更新 更多