栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

利用OCR识别图像中的英文和文字

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用OCR识别图像中的英文和文字

一、Tesseract—OCR简介

将图片翻译成文字一般称为光学文字识别(Optical Character Recognition,OCR)。可以实现OCR的底层并不多,目前很多库都是实用共同的几个底层OCR库,或者是在上面进行定制。
Tesseract是一个OCR库,目前由Google赞助。Tesseract是目前公认最优秀、最精确的开源OCR系统

二、下载

https://digi.bib.uni-mannheim.de/tesseract/

1.尽量不要下载dev(开发中的版本),alpha(内部测试版,一般不向外部发布,会有很多Bug),beta(公测版本,即针对所有用户公开的测试版本)等版本。

2.建议下载最新稳定版本:
tesseract-ocr-w64-setup-v5.0.1.20220118.exe
下面进行傻瓜式安装




三、下载pytesseract库

在anaconda里面进行安装,然后直接使用anaconda环境就可以。

四、识别英文
#导入PIL,pytesseract库
import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'D:Pythontesseract.exe'

#读取待识别的图片
image = Image.open("7.jpg");
#将图片识别为英文文字
text = pytesseract.image_to_string(image)
#输出识别的文字
print(text)

下面是“7.jpg”文件

下面是运行结果

五、识别中文

tesseract默认安装可能不带中文简体识别包,需要额外下载。
在github中直接搜索tesseract,下载tessdata文件到Tesseract安装文件中,

#导入PIL,pytesseract库
import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'D:Pythontesseract.exe'

#读取待识别的图片
image = Image.open("8.jpg");
#将图片识别为英文文字
text = pytesseract.image_to_string(image, lang='chi_sim')
#输出识别的文字
print(text)

下面是“8.jpg”文件

下面是识别结果

六、如何识别单个字符

运行过上面代码的同学,如果把输入图像换为单个字母或者文字的图像就会输出失败,例如下面的图片,这里是因为OCR是用作识别多文字的情景,使用单个文字会被认为是图片,就会自动跳过。


这里怎么解决呢
改为如下代码

#导入PIL,pytesseract库
import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'D:Pythontesseract.exe'

#读取待识别的图片
image = Image.open("9.jpg");
#将图片识别为英文文字
cong = r'--psm 10'
text = pytesseract.image_to_string(image, config=cong)
#输出识别的文字
print(text)

添加

cong = r’–psm 10’

text = pytesseract.image_to_string(image, config=cong)

就可以输出了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869131.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号