适合⼩⽩的⼏个⼊门级Pythonocr识别库
适合⼩⽩的⼏个⼊门级Python ocr识别库
⼯作⽣活中经常会遇到需要提取图⽚中⽂字信息的情况,以前都是⼿动⾃⼰把图⽚⾥的字敲出来,但随着这⼏年⼈⼯智能技术的愈发成熟,市⾯上有越来越多的ocr产品了,基本上能⼤部分正常图⽚的⽂字提取需求。当然有时候需要提取⽂字的图⽚数量较多或者有某个应⽤程序编写需求时,就需要借助代码来实现了,这⾥介绍⼏个⽐较适合新⼿⼩⽩的python ocr库,简单实⽤,可满⾜绝⼤多数常规的图⽚⽂字提取、验证码识别需求。
1、pytesseract
pytesseract需要配合安装在本地的⽂件⼀起使⽤,安装教程可参考这⾥:,需要注意的是安装时⼀定要选中中⽂包,默认是只⽀持英⽂识别。
python库安装命令如下:
pip install pytesseract
待识别图⽚如下:
代码实现:
import pytesseract
from PIL import Image
text = pytesseract.image_to_string(Image.open(r"d:\Desktop\39DEE621-40EA-4ad1-90CC-79EB51D39347.png"))
oracle用户名和口令忘了怎么办print(text)
识别结果输出:
Using Tesseract OCR with Python
# import the necessary packages
from PIL import Image
import pytesseract小白学python买什么书
import ergperse
import cv2
import os
# construct the argument parse and parse the arguments
ap = argparse.ArgunentParser()
ap.add_argument("-i","--image", required-True,
help="path to input image to be OCR'd")
ap.add_argument("-p","--preprocess", typesstr, default="thresh",
helpe"type of preprocessing to be done")
args =vars(ap.parse_args())
2、PaddleOCR
PaddleOCR是百度开源的⼀款基于深度学习的ocr识别库,对中⽂的识别精度相当不错,可以应付绝⼤多数的⽂字提取需求。
需要依次安装三个依赖库,安装命令如下,其中shapely库可能会受系统影响安装报错,具体解决⽅案参考这篇博客:
pip install paddlepaddle
pip install shapely
pip install paddleocr
含有unicode格式的字符
待识别图⽚如下:
代码实现:
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
# 输⼊待识别图⽚路径
img_path =r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png"
# 输出结果保存路径
result = (img_path, cls=True)
for line in result:
print(line)
from PIL import Image
image = Image.open(img_path).convert('RGB')
boxes =[line[0]for line in result]
txts =[line[1][0]for line in result]
scores =[line[1][1]for line in result]
im_show = draw_ocr(image, boxes, txts, scores)
im_show = Image.fromarray(im_show)
im_show.show()
识别结果输出如下,会显⽰出每个区域字体识别的置信度,以及其坐标位置信息:
Namespace(cls=False, cls_batch_num=30, cls_image_shape='3, 48, 192', cls_model_dir='C:\\Users\\Administrator/.paddleocr/cls', cls_thresh=0.9, det=Tru e, det_algorithm='DB', det_db_box_thresh=0.5, det_db_thresh=0.3, det_db_unclip_ratio=2.0, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_ east_score_thresh=0.8, det_max_side_len=960, det_model_dir='C:\\Users\\Administrator/.paddleocr/det', enable_mkldnn=False, gpu_mem=8000, image_ dir=None, ir_optim=True, label_list=['0','180'], lang='ch', max_text_length=25, rec=True, rec_algorithm='CRNN', rec_batch_num=30, rec_char_dict_path='. /ppocr/utils/ppocr_', rec_char_type='ch', rec_image_shape='3, 32, 320', rec_model_dir='C:\\Users\\Administrator/.paddleocr/rec/ch', use_angle_ cls=True, use_gpu=True, use_space_char=True, use_tensorrt=False, use_zero_copy_run=False)
dt_boxes num :16, elapse :0.04799485206604004amaze ui聊天室框架
cls num  :16, elapse :0.1860027313232422
rec_res num  :16, elapse :0.4859299659729004
[[[6.0,2.0],[85.0,2.0],[85.0,31.0],[6.0,31.0]],['帮助⽂档',0.99493873]]
[[[309.0,13.0],[324.0,13.0],[324.0,28.0],[309.0,28.0]],['X',0.9667116]]
[[[82.0,50.0],[120.0,50.0],[120.0,71.0],[82.0,71.0]],['⽬录',0.993418]]
[[[136.0,50.0],[176.0,50.0],[176.0,71.0],[136.0,71.0]],['标题',0.99969745]]
[[[13.0,53.0],[60.0,53.0],[60.0,70.0],[13.0,70.0]],['快捷键',0.9995322]]
[[[191.0,49.0],[314.0,49.0],[314.0,72.0],[191.0,72.0]],['⽂本样式列表',0.9967863]]
[[[61.0,84.0],[120.0,84.0],[120.0,101.0],[61.0,101.0]],['代码⽚',0.9997086]]
[[[134.0,81.0],[181.0,84.0],[180.0,104.0],[132.0,101.0]],['表格',0.9891155]]
[[[187.0,84.0],[232.0,84.0],[232.0,101.0],[187.0,101.0]],['注脚',0.99958]]
[[[13.0,115.0],[90.0,115.0],[90.0,135.0],[13.0,135.0]],['⾃定义列表',0.99823236]]
[[[109.0,115.0],[219.0,115.0],[219.0,135.0],[109.0,135.0]],['LaTeX数学公式',0.98812836]]
[[[237.0,115.0],[315.0,115.0],[315.0,135.0],[237.0,135.0]],['插⼊⽢特图',0.9982792]]
[[[12.0,148.0],[94.0,148.0],[94.0,167.0],[12.0,167.0]],['插⼊UML图',0.9926085]]
[[[113.0,148.0],[249.0,148.0],[249.0,167.0],[113.0,167.0]],['插⼊Mermaid流程图',0.996088]]
[[[11.0,176.0],[153.0,176.0],[153.0,200.0],[11.0,200.0]],['插⼊Flowchart流程图',0.9780351]]
[[[174.0,179.0],[237.0,179.0],[237.0,200.0],[174.0,200.0]],['插⼊类图',0.9519753]]
3、easyocr
github上⼀万多个star的开源ocr项⽬(github地址:),⽀持80多种语⾔的识别,识别精度超⾼。
python库安装命令如下:
pip install easyocr
待识别图⽚如下:
代码实现:
import easyocr
#设置识别中英⽂两种语⾔
awkbeginend用法reader = easyocr.Reader(['ch_sim','en'], gpu =False)# need to run only once to load model into memory
result = adtext(r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png", detail =0)
print(result)
初次运⾏需要在线下载检测模型和识别模型,建议在⽹速好点的环境运⾏:
Using CPU. Note: This module is much faster with a GPU.
Downloading detection model, please wait. This may take several minutes depending upon your network connection.wrap me in th plastic
Downloading recognition model, please wait. This may take several minutes depending upon your ne
twork connection.
识别结果输出如下,没有遗漏任何⼀个⽂字,精度甚⾄要优于前⾯的PaddleOCR:
['帮助⽂档','快捷键','⽬录','标题','⽂本样式','列表','链接','代码⽚','表格','注脚','注释','⾃定义列表','LaTex 数学公式','插⼊⽢犄图','插⼊UML图','插⼊Mer naid流程图','插⼊ Flowchart流程图','插⼊类图']
4、muggle_ocr
muggle_ocr是⼀款轻量级的ocr识别库,从名字也可以看出来,专为⿇⽠设计!使⽤也⾮常简单,但其强项主要是⽤于识别各类验证码,⼀般⽂字提取效果就稍差了。
python库安装命令如下:
pip install muggle_ocr
待识别验证码如下:
代码实现:
import muggle_ocr
# 初始化sdk;model_type 包含了 ModelType.OCR/ModelType.Captcha 两种模式,分别对应常规图⽚与验证码
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
with open(r"d:\Desktop\四位验证码.png","rb")as f:
img = f.read()
text = sdk.predict(image_bytes=img)
print(text)
识别结果输出如下:
MuggleOCR Session [captcha] Loaded.
3n3d
5、dddd_ocr
dddd_ocr也是⼀个⽤于识别验证码的开源库,⼜名带带弟弟ocr,爬⾍界⼤佬sml2h3开发,识别效果也是⾮常不错,对⼀些常规的数字、字母验证码识别有奇效。
python库安装命令如下:
pip install dddd_ocr
待识别验证码如下:
代码实现:
import ddddocr
ocr = ddddocr.DdddOcr()
with open("d:\Desktop\四位验证码2.png",'rb')as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)
识别结果输出如下,可以看出即使有⼀些线条⼲扰,还是准确的识别出了四个字母:jepv
6、其他
还有其他优秀的ocr识别库,以后慢慢更新

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。