利用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:\Python\tesseract.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:\Python\tesseract.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:\Python\tesseract.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)

就可以输出了
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

anthony-36

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

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

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

打赏作者

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

抵扣说明:

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

余额充值