一、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
下面进行傻瓜式安装
![在这里插入图片描述](https://img-blog.csdnimg.cn/56211548ca294697bd706173555b5752.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/22130d8ff5ed4c5dbf38fed5976aeeb6.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/9dc82490c4ce481bb62d826dd9b19ddc.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/ffc3cf1659774965b922fd1678783029.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/1871164372a8438e839fbb91937eeaf8.png)
三、下载pytesseract库
在anaconda里面进行安装,然后直接使用anaconda环境就可以。![在这里插入图片描述](https://img-blog.csdnimg.cn/792c7f3bf56f410189543e5449ad3d78.png)
四、识别英文
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”文件
![请添加图片描述](https://img-blog.csdnimg.cn/ba4193dc8b5e4cfa9ce873c9737f3ce4.png)
下面是运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/a68d205eea4c4c0082107a9f50a8dbe3.png)
五、识别中文
tesseract默认安装可能不带中文简体识别包,需要额外下载。
在github中直接搜索tesseract,下载tessdata文件到Tesseract安装文件中,
![,下载](https://img-blog.csdnimg.cn/2e982030fb1e4b5ca6178da599d5bd2a.png)
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”文件
![请添加图片描述](https://img-blog.csdnimg.cn/cfd6c85d7f1b4999a9755b5b85730799.png)
下面是识别结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/75127e7dc1b24c9282128767dd1c9907.png)
六、如何识别单个字符
运行过上面代码的同学,如果把输入图像换为单个字母或者文字的图像就会输出失败,例如下面的图片,这里是因为OCR是用作识别多文字的情景,使用单个文字会被认为是图片,就会自动跳过。
![请添加图片描述](https://img-blog.csdnimg.cn/8d86f2ed964f42299c302c89bb01e161.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/5539c1ec32e74453b7d052f034a564ff.png)
这里怎么解决呢
改为如下代码
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)
就可以输出了
![在这里插入图片描述](https://img-blog.csdnimg.cn/b8d741dfa24b46c497ee2c9dc44ee932.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)