首页 技术 正文
技术 2022年11月14日
0 收藏 655 点赞 3,953 浏览 1611 个字

一、安装依赖库

pip install pytesseract

pip install pillow

二、安装识图引擎tesseract-ocr

https://pan.baidu.com/s/1QaYJc4ggpqhljf4sq_-WQw
密码:2v4a

下载tesseract-ocr-setup-4.00.00dev.exe并安装

python3 自动识图

三、修改pytesseract库指向tesseract的配置

1、找到python3的安装路径

python3 自动识图

2、修改pytesseract.py文件

python3 自动识图

2、将tesseract_cmd的配置改成tesseract安装的执行文件

python3 自动识图

四、测试识图

1、图片内容

python3 自动识图

2、代码

from PIL import Image
from pytesseract import image_to_stringtessdata_dir_config = '--tessdata-dir "C:/Program Files (x86)/Tesseract-OCR/tessdata"'
img = Image.open("1.png")
text = image_to_string(img,lang = 'eng',config=tessdata_dir_config)
print(text)

3、结果

python3 自动识图

五、支持中文

所有语音包地址

https://github.com/tesseract-ocr/tessdata

1、下载中文语音包

https://github.com/tesseract-ocr/tessdata/raw/master/chi_sim.traineddata

2、将下载好的chi_sim.traineddata包放入Tesseract-OCR安装地址中的tessdata目录中

python3 自动识图

3、测试中文图片

中文图片

python3 自动识图

测试代码

from PIL import Image
from pytesseract import image_to_stringtessdata_dir_config = '--tessdata-dir "C:/Program Files (x86)/Tesseract-OCR/tessdata"'
img = Image.open("3.png")
text = image_to_string(img,lang = 'chi_sim',config=tessdata_dir_config) #之前安装的中文包名
print(text)

测试结果

python3 自动识图

4、图标二值化

灰度化和二值化后的图片

python3 自动识图

代码

from PIL import Image
from pytesseract import image_to_stringtessdata_dir_config = '--tessdata-dir "C:/Program Files (x86)/Tesseract-OCR/tessdata"'
img = Image.open("3.png")#灰度化
image = img.convert('L')pixels = image.load()threshold = 200 #阈值#二值化
for x in range(image.width):
for y in range(image.height):
if pixels[x, y] > threshold:
pixels[x, y] = 255
else:
pixels[x, y] = 0
image.show()
text = image_to_string(image,lang = 'chi_sim',config=tessdata_dir_config)
print(text)

#结果

python3 自动识图

#结论

汉字的识别率不是太高,如果要求高的话可以使用百度云的百度识图,文档地址:https://cloud.baidu.com/doc/IMAGERECOGNITION/ImageClassify-Python-SDK.html#.E5.8A.A8.E7.89.A9.E8.AF.86.E5.88.AB

使用百度识图需要创建百度云账号,百度识图有免费额度(我没试过),阿里云的识图有点贵。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,427
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,244
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,687
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726