使用 pyinstaller 创建 .exe 文件时出现 Pytesseract 错误

Getting Pytesseract Error while creating .exe file using pyinstaller

所以基本上我正在尝试创建一个简单的烧瓶应用程序,我们可以在其中使用 pytesseract 对图像和 return 字符串中的数据进行 OCR。在使用 pyarmor 对 python 文件进行混淆之后,我还使用 pyinstaller 将整个应用程序打包到 .exe 文件中。

我还复制了 pytesseract 文件夹并将其粘贴到文件旁边,以便在创建 .exe 期间将其添加到 run.spec 文件中,因为我需要将此依赖项与 .exe 文件捆绑在一起。执行 .exe 文件时出现以下错误

pytesseract.pytesseract.TesseractError: (1, 'Error opening data file C:\Users\Akash\AppData\Local\Temp\_MEI87082\Tesseract-OCR\tessdata\e13b.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language \'e13b\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')

为了解决这个问题,我添加了以下行来设置环境变量:

os.environ['TESSDATA_PREFIX']='Tesseract-OCR/tessdata/'

也试过将tessdata添加到image_to_string()函数的config属性的解决方案如下:

tessdata_dir_config = r'--tessdata-dir "Tesseract-OCR/tessdata/"'
content = pytesseract.image_to_string(image, lang='e13b', config=tessdata_dir_config)
print(content)

但 .exe 文件仍然提供相同的错误。

同时为了解决路径问题,我使用了以下函数来设置文件的绝对路径。

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
import os,sys
try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    base_path = sys._MEIPASS
except Exception:
            try:
                    base_path = sys._MEIPASS2
            except Exception:
                    base_path = os.path.abspath(".")
#print("base_path",base_path)
#print("relative_path",relative_path)
return os.path.join(base_path, relative_path)

我希望这些信息足以回答问题,如果您需要更多信息,请提出并回复。

提前致谢。

所以后来当我检查我的 local/temp 文件夹时,.exe 文件正在其中提取整个文件,它开始意识到在提取后它没有我们提供的任何 \tessdata 文件夹路径和 e13b.traindata 被直接提取到 "Tesseract-OCR" 文件夹中。

所以在app.py给出了

的路径
tessdata_dir_config = r'--tessdata-dir "Tesseract-OCR/tessdata/"'

tessdata_dir_config = r'--tessdata-dir "Tesseract-OCR"'

终于解决了这个问题。

但又陷入另一个错误... 那是另一个故事了。