一、介绍
目前文字识别有两种方式
1、远端服务器识别返回结果
2、本地识别Tesseract-OCR

远端识别需要联网,这里以本地识别为例

二、相关库安装
pip install PIL
pip install pytesseract

下载Tesseract-OCR
链接:https://pan.baidu.com/s/1stgWF-aBKJ-l86vAMWaH1g
提取码:7m4m

在这里插入图片描述
默认安装就行
复制中文数据级进入
在这里插入图片描述修改配置
#tesseract_cmd = ‘tesseract’
tesseract_cmd = ‘C:/Program Files (x86)/Tesseract-OCR/tesseract.exe’

在这里插入图片描述
三、函数介绍
1、get_tesseract_version返回系统中安装的Tesseract版本。
2、image_to_string将图像上的Tesseract OCR运行结果返回到字符串
3、image_to_boxes返回包含已识别字符及其框边界的结果
4、image_to_data返回包含框边界,置信度和其他信息的结果。需要Tesseract 3.05+。有关更多信息,请查看Tesseract TSV文档
5、image_to_osd返回包含有关方向和脚本检测的信息的结果

image_to_data(image,lang = None,config =’’, nice = 0,output_type = Output.STRING)
参数
image Object,PIL Image /由Tesseract处理的图像的NumPy数组
lang String,Tesseract语言代码字符串
config String,任何其他配置为字符串,例如:config =’ - psm 6’
nice Integer,修改Tesseract运行的处理器优先级。Windows不支持。尼斯调整了类似unix的流程的优点。
output_type Class属性,指定输出的类型,默认为string。有关所有支持类型的完整列表,请检查pytesseract.Output类的定义

四、程序示例
1、图片文字识别

from PIL import Image
import pytesseract

text=pytesseract.image_to_string(Image.open('test.jpg'),lang='chi_sim')
print(text)

原始图片
在这里插入图片描述
识别结果

在这里插入图片描述

2、摄像头文字识别
预留

总体能识别,还是有误识别需要修改参数

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐