繁体   English   中英

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

[英]Getting Pytesseract Error while creating .exe file using pyinstaller

所以基本上我正在尝试创建一个简单的烧瓶应用程序,我们可以在其中使用 pytesseract 对图像进行 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/'

并且还尝试了添加归因于 image_to_string image_to_string()函数的 config 属性的 tessdata 的解决方案,如下所示:

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)

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

提前致谢。

所以后来当我检查我的本地/临时文件夹时 .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"'

最后这解决了这个问题。

但再次陷入另一个错误......好吧,那是另一个故事。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM