Python 图片文字识别和 tesseract 问题解决

1.图片文字识别测试代码

安装需要的工具包

pip install Pillow
pip install pytesseract

测试代码

import pytesseract
from PIL import Image
# 定义图片地址变量
image_path = '/Users/guanfawang/Downloads/Untitled-31.png'
# 打开图片文件
image_open = Image.open(image_path)
# 使用 PyTesseract 进行 OCR 文字识别
image_text = pytesseract.image_to_string(Image.open(image_path), lang='chi_sim')
# 打印结果
print(image_text)

2.存在问题和解决

  • 执行步骤1代码发现报以下错误:

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it’s not in your PATH. See README file for more information.

原因是tesseract未安装,使用pip install tesseract 会存在问题,可以使用以下命令安装和查看:

# 安装 tesseract
brew install tesseract
# 查看是否安装
tesseract --version

如果没有brew说明没有安装Homebrew,可以查看 Homebrew 安装卸载和配置使用 文章。

找到并复制tesseract的路径位置,将pytesseract.py文件的tesseract_cmd变量更改为对应路径;

在这里插入图片描述

在这里插入图片描述

也可以不调整pytesseract.py文件内容,在步骤1的代码增加一个全局变量;

pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/Cellar/tesseract/5.3.3/bin/tesseract'
  • 再次执行步骤1发现报以下错误:

pytesseract.pytesseract.TesseractError: (1, ‘Error opening data file /opt/homebrew/share/tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your “tessdata” directory. Failed loading language ‘chi_sim’ Tesseract couldn’t load any languages! Could not initialize tesseract.’)

该错误提示表明在运行 pytesseract 时遇到了问题,具体是由于无法找到中文语言包 chi_sim.traineddata 或无法初始化 tesseract 导致的。

Tesseract OCR 官方的语言包下载页面:https://github.com/tesseract-ocr/tessdata

搜索并下载chi_sim.traineddata,需要将 chi_sim.traineddata 文件移动到 Tesseract 的 “tessdata” 目录中,重新执行就可以了。

常见的语言参数选择有 chi_sim(简体中文)、eng(英文)、osd、snum。

print(pytesseract.get_languages(config=''))  # 输出 ['chi_sim', 'eng', 'osd', 'snum']

3.完整代码

import pytesseract
from PIL import Image
# 调整tesseract路径
pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/Cellar/tesseract/5.3.3/bin/tesseract'
# 定义图片地址变量
image_path = '/Users/guanfawang/Downloads/Untitled-48.png'
# 打开图片文件
image_open = Image.open(image_path)
# 使用 PyTesseract 进行 OCR 文字识别
image_text = pytesseract.image_to_string(Image.open(image_path), lang='chi_sim')
# 打印结果
print(image_text)
# print(text.replace(' ',''))

图片和打印结果如下:
在这里插入图片描述
总结:通过多次测试,tesseract对宋体、印刷体等笔画严谨的字体识别准确率很高,其他字体识别不太准确,符号也不太准确,图片倾斜也会影响结果,中文字识别还会显示每个字之间有空格,有时候还需要适当调整lang参数得出更准确的结果……

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有请小发菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值