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

简介: 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参数得出更准确的结果……

相关文章
|
1月前
|
程序员 Python
tesseract库的安装与使用及在python中使用,Python程序员秋招三面蚂蚁金服
tesseract库的安装与使用及在python中使用,Python程序员秋招三面蚂蚁金服
|
17天前
|
文字识别 Android开发 C++
Tesseract OCR集成Android Studio实现OCR识别
Tesseract OCR集成Android Studio实现OCR识别
29 0
|
1月前
|
机器学习/深度学习 文字识别 自然语言处理
Python图片格式转换与文字识别:技术与实践
Python图片格式转换与文字识别:技术与实践
80 0
|
10月前
|
文字识别 API Python
python实现图片文字识别
代码有点久远了...就留作纪念了 现在网上应该很多了
92 0
|
6月前
|
数据采集 文字识别 PHP
Python(四十二)百度智能云OCR文字识别的坑
这篇的内容其实跟python的关系不是很大,是在使用python做文字识别的时候遇到的一个坑,这里大概记录一下,希望大家在使用百度智能云的OCR文字识别的时候,能够快速的解决这个问题。 业务需求大概是这个样子的,学生在使用仪器做完实验之后,仪器会将实验结果,打印在一张小票上。正常,学生需要将小票上边的数据,逐一输入到系统中,但是,客户觉得这个操作太麻烦了,想用文字识别将小票上边的数据识别出来,自动填入,学生只需要检查一遍识别的是否正确即可。 需求很简单,我这里的后端是使用PHP做的,这也不耽误我使用python做文字识别。最开始的python文字识别的尝试,我这里就不做赘述了,具体,请
69 0
|
9月前
|
机器学习/深度学习 文字识别 算法
文本识别 (OCR)引擎之Tesseract的使用
esseract是一个开源文本识别 (OCR)引擎,用于识别图片中的文字并将其转换为可编辑的文本。
631 0
|
10月前
|
文字识别 Python
用python中的pytesseract实现文字识别的教程
用python中的pytesseract实现文字识别的教程
188 0
|
12月前
|
文字识别 小程序 Python
图像文字识别与保存-python
图像文字识别与保存-python
95 0
|
人工智能 文字识别 Linux
Tesseract OCR与文本智能识别
Tesseract OCR与文本智能识别
322 0
|
文字识别 Linux iOS开发
python开发:开源pytesseract文字识别
python开发:开源pytesseract文字识别
167 0