打开数据文件时出现恼人的python tesseract错误/tessdata/eng.trainedd

2024-06-06 04:33:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了这个错误,这个错误让我对tesseract的python包装有些疯狂,它是一个名为tesseract的python模块。

下面是我要运行的python代码:

img = cv2.imread(image, 0)
api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetPageSegMode(tesseract.PSM_AUTO)
tesseract.SetCvImage(img,api)
url = api.GetUTF8Text()
conf=api.MeanTextConf()
print('Extracted URL : ' + url)
api.End()

这就是我得到的:

Error opening data file ./tessdata/eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!

我不明白为什么要这样做,因为我已经将TESSDATA_PREFIX env变量正确设置为我的tesseract安装的正确路径(带有尾随斜杠)。

当我尝试直接从powershell运行Tesseract(我在windows 7 btw上)时,请执行以下操作:

 tesseract.exe .\data\test.tif -psm 7 out

它就像一个符咒! 另外,当我在python脚本中使用Popen调用Tesseract时,它工作得很好,但我不喜欢不能直接从stdout获取OCR文本的想法。实际上,除了向Tesseract提供一个输出文件名,然后fopen并从该文件中读取之外,似乎别无选择。我觉得仅仅为了得到OCR的输出而处理临时文本文件会非常糟糕。。。

帮忙?


Tags: 模块theapiurlimgdataprefix错误
2条回答
get location of ur tessdata folder by typing in command prompt: 
$ brew list tesseract
in may case:
/usr/local/Cellar/tesseract/3.05.01/bin/tesseract
/usr/local/Cellar/tesseract/3.05.01/include/tesseract/ (27 files)
/usr/local/Cellar/tesseract/3.05.01/lib/libtesseract.3.dylib
/usr/local/Cellar/tesseract/3.05.01/lib/pkgconfig/tesseract.pc
/usr/local/Cellar/tesseract/3.05.01/lib/ (2 other files)
/usr/local/Cellar/tesseract/3.05.01/share/man/ (11 files)
/usr/local/Cellar/tesseract/3.05.01/share/tessdata/ (28 files)

now
tessdata_dir_config = r'--tessdata-dir "/usr/local/Cellar/tesseract/3.05.01/share/tessdata"'

txt= image_to_string(img,lang='eng',config=tessdata_dir_config)

api.Init的第一个参数应该是TESSDATA_前缀。

相关问题 更多 >