Error opening data file ./eng.traineddata

5 篇文章 0 订阅
3 篇文章 0 订阅

在Mac上安装 tesseract 成功后,使用tess4j 出现这个错误

Error opening data file ./eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!
Warning: Invalid resolution 0 dpi. Using 70 instead.

解决办法进入到 /usr/local/Cellar/tesseract/4.1.1/share/tessdata 把eng.traineddata 加入到工程目录中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个错误提示是因为pytesseract在默认路径下找不到中文语言包。解决方法是下载中文语言包并将其放置在tesseract的语言包目录下,或者手动指定语言包路径。 以下是两种解决方法: 1. 下载语言包并手动指定路径 你可以从tesseract官网上下载中文语言包(chi_sim.traineddata),然后将其放置在tesseract的语言包目录下。例如,在Windows系统上,语言包目录可能是 `C:\Program Files\Tesseract-OCR\tessdata`。将chi_sim.traineddata文件放置在这个目录下即可。 然后,你可以在代码中手动指定语言包路径,例如: ```python import pytesseract from PIL import Image # 指定 tesseract 所在路径 pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # 指定语言包路径 tessdata_dir_config = '--tessdata-dir "C:\\Program Files\\Tesseract-OCR\\tessdata"' # 打开图片并进行识别 img = Image.open('chinese_text.png') text = pytesseract.image_to_string(img, lang='chi_sim', config=tessdata_dir_config) # 输出识别结果 print(text) ``` 在这个示例中,我们使用 `--tessdata-dir` 参数指定语言包所在路径,并将其赋值给 `tessdata_dir_config` 变量。然后在 `image_to_string` 函数中使用 `config` 参数将 `tessdata_dir_config` 传递给pytesseract。 2. 使用pytesseract.pytesseract.get_tesseract_version()获取tesseract语言包路径 另一种方法是使用 `pytesseract.pytesseract.get_tesseract_version()` 方法获取tesseract语言包所在路径,然后手动指定语言为中文。例如: ```python import pytesseract from PIL import Image # 指定 tesseract 所在路径 pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # 获取语言包路径并指定语言为中文 tessdata_dir = pytesseract.pytesseract.get_tesseract_version()[1] tessdata_dir_config = '--tessdata-dir "{}"'.format(tessdata_dir) lang = 'chi_sim' # 打开图片并进行识别 img = Image.open('chinese_text.png') text = pytesseract.image_to_string(img, lang=lang, config=tessdata_dir_config) # 输出识别结果 print(text) ``` 在这个示例中,我们使用 `pytesseract.pytesseract.get_tesseract_version()` 方法获取tesseract语言包所在路径,然后将其赋值给 `tessdata_dir` 变量。然后,我们使用 `format()` 方法将 `tessdata_dir` 和语言代码拼接成语言包路径,并将其赋值给 `tessdata_dir_config` 变量。最后,在 `image_to_string` 函数中指定语言为中文,并将 `tessdata_dir_config` 传递给pytesseract。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值