简体   繁体   English

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

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

So basically I am trying to create a simple flask app where we can use pytesseract to do OCR on image and return the data in string.所以基本上我正在尝试创建一个简单的烧瓶应用程序,我们可以在其中使用 pytesseract 对图像进行 OCR 并以字符串形式返回数据。 And also i am packaging the whole app into the .exe file using the pyinstaller after doing the obfuscation of the python files using pyarmor.在使用 pyarmor 对 python 文件进行混淆之后,我还使用 pyinstaller 将整个应用程序打包到 .exe 文件中。

I have also copied the pytesseract folder and pasted it next to the files to add it in run.spec file during the .exe creation as I need to bundle this dependency with the .exe file.我还复制了 pytesseract 文件夹并将其粘贴到文件旁边,以便在 .exe 创建期间将其添加到 run.spec 文件中,因为我需要将此依赖项与 .exe 文件捆绑在一起。 I am getting the following error while the execution of the .exe file执行 .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.')

To do the solution of this i have added the following line to set environment variable:为了解决这个问题,我添加了以下行来设置环境变量:

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

and have also tried the solution of adding the tessdata attributed to image_to_string() function's config attribute as follows:并且还尝试了添加归因于 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)

But still the .exe file is providing the same error.但是 .exe 文件仍然提供相同的错误。

And also to resolve the path problems I have used the following function to set the absolute path of files.并且为了解决路径问题,我使用了以下函数来设置文件的绝对路径。

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)

I hope that this information is enough to answer the question and if you need more information just ask it and will respond to it.我希望这些信息足以回答问题,如果您需要更多信息,请提出并回复。

Thanks in advance.提前致谢。

So Later on when i check my local/temp folder where the .exe file was extracting the whole files, it came to realize that after the extract it do not have any \\tessdata folder of which we were giving the path and the e13b.traindata was direclty extracted inside the "Tesseract-OCR" folder.所以后来当我检查我的本地/临时文件夹时 .exe 文件正在提取整个文件,它开始意识到在提取后它没有任何 \\tessdata 文件夹,我们提供了路径和 e13b.traindata直接提取到“Tesseract-OCR”文件夹中。

So in app.py gave the path of所以在 app.py 中给出了路径

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

To

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

and finally this resolved the issue.最后这解决了这个问题。

But again stuck into another error ... well that's another story.但再次陷入另一个错误......好吧,那是另一个故事。

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

相关问题 使用 Pyinstaller 创建 .exe 时出错 - Error while creating .exe using Pyinstaller PyInstaller 未创建 .exe 文件 - PyInstaller is not creating an .exe file 运行pyinstaller制作的exe文件时出错 - Error while running the exe file made with pyinstaller 我使用 pyinstaller 和 py-to-exe 将 my.py 转换为 .exe,但在执行时出现错误 - I converted my .py into .exe using pyinstaller and py-to-exe but i am getting error while executing Pyinstaller在创建exe文件时无法导入pylsd - Pyinstaller unable to import pylsd while creating exe file 使用 pyinstaller 将我的 python gui 程序转换为 .exe 时出现致命错误 - Getting a fatal error while converting my python gui program to .exe using pyinstaller 使用 pyinstaller 将“.py 文件”(具有“导入光栅”)转换为“.exe 文件”时出现“导入错误:DLL 加载失败” - "Import Error: DLL load failed" while converting ".py file" (having "import rasterio") to ".exe file" using pyinstaller "使用 pyinstaller 编译为单个 exe 时找不到 .Kv 文件" - .Kv file not found while compiling to single exe using pyinstaller 使用 PyInstaller 将 python 3.7 脚本转换为 exe 时出错:ModuleNotFoundError - Error while converting python 3.7 script to exe using PyInstaller: ModuleNotFoundError 使用pyinstaller创建单个exe文件时导入错误 - Import error when using pyinstaller to create a single exe file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM