基于Python引擎的PP-OCR模型库推理

本文介绍针对PP-OCR模型库的Python推理引擎使用方法,内容依次为文本检测、文本识别、方向分类器以及三者串联在CPU、GPU上的预测方法。

  • 1. 文本检测模型推理
  • 2. 文本识别模型推理
    • 2.1 超轻量中文识别模型推理
    • 2.2 多语言模型的推理
  • 3. 方向分类模型推理
  • 4. 文本检测、方向分类和文字识别串联推理

PaddleOCR 版本是: PaddleOCR-release-2.3
PaddleOCR-release-2.3 官网

1. 文本检测模型推理

文本检测模型推理,默认使用DB模型的配置参数。超轻量中文检测模型推理,可以执行如下命令:

将模型权重保存在主项目目录的model_self

# 下载超轻量中文检测模型:
cd model_self
wget  https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_det_infer.tar
tar xf ch_PP-OCRv2_det_infer.tar

# fixme: 执行下面那句话需要回到主项目目录
python tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./ch_PP-OCRv2_det_infer.tar/"


可视化文本检测结果默认保存到./inference_results文件夹里面,结果文件的名称前缀为’det_res’。结果示例如下:

通过参数limit_typedet_limit_side_len来对图片的尺寸进行限制,
limit_type可选参数为[max, min],
det_limit_size_len 为正整数,一般设置为32 的倍数,比如960。

参数默认设置为limit_type='max', det_limit_side_len=960。表示网络输入图像的最长边不能超过960,
如果超过这个值,会对图像做等宽比的resize操作,确保最长边为det_limit_side_len
设置为limit_type='min', det_limit_side_len=960 则表示限制图像的最短边为960。

如果输入图片的分辨率比较大,而且想使用更大的分辨率预测,可以设置det_limit_side_len 为想要的值,比如1216:

python tools/infer/predict_det.py --image_dir="./doc/imgs/1.jpg" --det_model_dir="./inference/ch_PP-OCRv2_det_infer/" --det_limit_type=max --det_limit_side_len=1216

如果想使用CPU进行预测,执行命令如下

python tools/infer/predict_det.py --image_dir="./doc/imgs/1.jpg" --det_model_dir="./inference/ch_PP-OCRv2_det_infer/"  --use_gpu=False

2. 文本识别模型推理

2.1 超轻量中文识别模型推理

超轻量中文识别模型推理,可以执行如下命令:

# 下载超轻量中文识别模型:
wget  https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_rec_infer.tar
tar xf ch_PP-OCRv2_rec_infer.tar
python tools/infer/predict_rec.py --image_dir="./doc/imgs_words/ch/word_4.jpg" --rec_model_dir="./model_self/ch_PP-OCRv2_rec_infer/"


执行命令后,上面图像的预测结果(识别的文本和得分)会打印到屏幕上,示例如下:

Predicts of ./doc/imgs_words/ch/word_4.jpg:('实力活力', 0.98127246)

2.2 多语言模型的推理

如果您需要预测的是其他语言模型,可以在此链接中找到对应语言的inference模型,在使用inference模型预测时,需要通过--rec_char_dict_path指定使用的字典路径, 同时为了得到正确的可视化结果,需要通过 --vis_font_path 指定可视化的字体路径,doc/fonts/ 路径下有默认提供的小语种字体,例如韩文识别:

注意:要下载韩文的推理模型

wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/multilingual/korean_mobile_v2.0_rec_infer.tar
python tools/infer/predict_rec.py --image_dir="./doc/imgs_words/korean/1.jpg" --rec_model_dir="./model_self/korean_mobile_v2.0_rec_infer/" --rec_char_type="korean" --rec_char_dict_path="ppocr/utils/dict/korean_dict.txt" --vis_font_path="doc/fonts/korean.ttf"


执行命令后,上图的预测结果为:

Predicts of ./doc/imgs_words/korean/1.jpg:('바탕으로', 0.99255276)

3. 方向分类模型推理

方向分类模型推理,可以执行如下命令:

# 下载超轻量中文方向分类器模型:
wget  https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar xf ch_ppocr_mobile_v2.0_cls_infer.tar
python tools/infer/predict_cls.py --image_dir="./doc/imgs_words/ch/word_1.jpg" --cls_model_dir="./model_self/ch_ppocr_mobile_v2.0_cls_infer"

执行命令后,上面图像的预测结果(分类的方向和得分)会打印到屏幕上,示例如下:

Predicts of ./doc/imgs_words/ch/word_1.jpg:['0', 0.9998864]

4. 文本检测、方向分类和文字识别串联推理

以超轻量中文OCR模型推理为例,在执行预测时,需要通过参数image_dir指定单张图像或者图像集合的路径、参数det_model_dir,cls_model_dirrec_model_dir分别指定检测,方向分类和识别的inference模型路径。参数use_angle_cls用于控制是否启用方向分类模型。use_mp表示是否使用多进程。total_process_num表示在使用多进程时的进程数。可视化识别结果默认保存到 ./inference_results 文件夹里面。

# 使用方向分类器
python tools/infer/predict_system.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./model_self/ch_PP-OCRv2_det_infer/" --cls_model_dir="./model_self/ch_ppocr_mobile_v2.0_cls_infer/" --rec_model_dir="./model_self/ch_PP-OCRv2_rec_infer/" --use_angle_cls=true
# 不使用方向分类器
python tools/infer/predict_system.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./model_self/ch_PP-OCRv2_det_infer/" --rec_model_dir="./model_self/ch_PP-OCRv2_rec_infer/" --use_angle_cls=false
# 使用多进程
python tools/infer/predict_system.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./model_self/ch_PP-OCRv2_det_infer/" --rec_model_dir="./model_self/ch_PP-OCRv2_rec_infer/" --use_angle_cls=false --use_mp=True --total_process_num=6

执行命令后,识别结果图像如下:

终端执行结果如下:

/home/yyq/.conda/envs/paddle/lib/python3.8/site-packages/skimage/morphology/_skeletonize.py:241: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations0, 1, 1, 0, 0, 1, 0, 0, 0], dtype=np.bool)
/home/yyq/.conda/envs/paddle/lib/python3.8/site-packages/skimage/morphology/_skeletonize.py:256: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 1.5393850803375244
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 9.5367431640625e-07
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.012524604797363281
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 1.430511474609375e-06
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.01199483871459961
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 9.5367431640625e-07
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.012188434600830078
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 1.1920928955078125e-06
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.011774539947509766
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 9.5367431640625e-07
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.011682510375976562
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 7.152557373046875e-07
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.011684894561767578
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 1.1920928955078125e-06
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.011770248413085938
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 1.1920928955078125e-06
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.011766433715820312
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 9.5367431640625e-07
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 0, elapse : 0.011704444885253906
[2021/11/25 15:00:36] root DEBUG: cls num  : 0, elapse : 0
[2021/11/25 15:00:36] root DEBUG: rec_res num  : 0, elapse : 7.152557373046875e-07
[2021/11/25 15:00:36] root DEBUG: dt_boxes num : 69, elapse : 0.02576732635498047
[2021/11/25 15:00:36] root DEBUG: cls num  : 69, elapse : 0.06086087226867676
[2021/11/25 15:00:37] root DEBUG: rec_res num  : 69, elapse : 0.20027732849121094
[2021/11/25 15:00:37] root INFO: 0  Predict time of ./doc/imgs/00018069.jpg: 0.291s
[2021/11/25 15:00:37] root INFO: 代号, 0.973
[2021/11/25 15:00:37] root INFO: 项目, 0.989
[2021/11/25 15:00:37] root INFO: 结果, 0.991
[2021/11/25 15:00:37] root INFO: 参考值, 0.999
[2021/11/25 15:00:37] root INFO: 单位, 0.992
[2021/11/25 15:00:37] root INFO: 谷丙转氨酶, 0.980
[2021/11/25 15:00:37] root INFO: 25.6, 0.900
[2021/11/25 15:00:37] root INFO: 0--40, 0.981
[2021/11/25 15:00:37] root INFO: ALT, 0.825
[2021/11/25 15:00:37] root INFO: U/L, 0.941
[2021/11/25 15:00:37] root INFO: 总胆红素, 0.975
[2021/11/25 15:00:37] root INFO: 11.2, 0.964
[2021/11/25 15:00:37] root INFO: TBIL, 0.990
[2021/11/25 15:00:37] root INFO: <20, 0.732
[2021/11/25 15:00:37] root INFO: UMOL/L, 0.708
[2021/11/25 15:00:37] root INFO: 直接胆红素, 0.975
[2021/11/25 15:00:37] root INFO: 3.3, 0.968
[2021/11/25 15:00:37] root INFO: DBIL, 0.997
[2021/11/25 15:00:37] root INFO: 0--7, 0.952
[2021/11/25 15:00:37] root INFO: UMOL/L, 0.700
[2021/11/25 15:00:37] root INFO: 间接胆红素, 0.942
[2021/11/25 15:00:37] root INFO: IBIL, 0.994
[2021/11/25 15:00:37] root INFO: 7.9, 0.874
[2021/11/25 15:00:37] root INFO: 1.5--15, 0.860
[2021/11/25 15:00:37] root INFO: UMOL/L, 0.856
[2021/11/25 15:00:37] root INFO: TP, 0.869
[2021/11/25 15:00:37] root INFO: 总蛋白, 0.994
[2021/11/25 15:00:37] root INFO: *689, 0.563
[2021/11/25 15:00:37] root INFO: 60--80, 0.995
[2021/11/25 15:00:37] root INFO: S/L, 0.703
[2021/11/25 15:00:37] root INFO: 白蛋白, 0.994
[2021/11/25 15:00:37] root INFO: ALB, 0.986
[2021/11/25 15:00:37] root INFO: 35.1, 0.911
[2021/11/25 15:00:37] root INFO: 33--55, 0.968
[2021/11/25 15:00:37] root INFO: 8/L, 0.760
[2021/11/25 15:00:37] root INFO: GLO, 0.991
[2021/11/25 15:00:37] root INFO: 球蛋白, 0.972
[2021/11/25 15:00:37] root INFO: 23.8, 0.919
[2021/11/25 15:00:37] root INFO: 20--30, 0.982
[2021/11/25 15:00:37] root INFO: 8/L, 0.647
[2021/11/25 15:00:37] root INFO: 白球比, 0.996
[2021/11/25 15:00:37] root INFO: 1.5, 0.965
[2021/11/25 15:00:37] root INFO: A/G, 0.803
[2021/11/25 15:00:37] root INFO: 11.5--2.5, 0.874
[2021/11/25 15:00:37] root INFO: 碱性磷酸酶, 0.986
[2021/11/25 15:00:37] root INFO: 93, 0.885
[2021/11/25 15:00:37] root INFO: ALP, 0.925
[2021/11/25 15:00:37] root INFO: 15--112, 0.929
[2021/11/25 15:00:37] root INFO: IU/L, 0.972
[2021/11/25 15:00:37] root INFO: 谷氨酰转肽酶, 0.935
[2021/11/25 15:00:37] root INFO: GGT, 0.834
[2021/11/25 15:00:37] root INFO: 14.3, 0.800
[2021/11/25 15:00:37] root INFO: (50, 0.748
[2021/11/25 15:00:37] root INFO: U/L, 0.942
[2021/11/25 15:00:37] root INFO: 谷草转氨酶, 0.955
[2021/11/25 15:00:37] root INFO: 16.3, 0.912
[2021/11/25 15:00:37] root INFO: 8--40, 0.862
[2021/11/25 15:00:37] root INFO: AST, 0.839
[2021/11/25 15:00:37] root INFO: U/L, 0.983
[2021/11/25 15:00:37] root INFO: 乳酸脱氢酶, 0.929
[2021/11/25 15:00:37] root INFO: 167, 0.824
[2021/11/25 15:00:37] root INFO: 114--240, 0.940
[2021/11/25 15:00:37] root INFO: LDH, 0.977
[2021/11/25 15:00:37] root INFO: U/L, 0.970
[2021/11/25 15:00:37] root INFO: 腺甘脱氨酶, 0.980
[2021/11/25 15:00:37] root INFO: ADA, 0.672
[2021/11/25 15:00:37] root INFO: 12.6, 0.953
[2021/11/25 15:00:37] root INFO: 4--24, 0.909
[2021/11/25 15:00:37] root INFO: U/L, 0.952
[2021/11/25 15:00:37] root INFO: The visualized image saved in ./inference_results/00018069.jpg
[2021/11/25 15:00:37] root INFO: The predict total time is 0.3227837085723877
[2021/11/25 15:00:37] root INFO:
The predict total time is 0.29076361656188965

PaddleOCR---基于Python引擎的PP-OCR模型库推理相关推荐

  1. python 高精度时间_如何基于Python代码实现高精度免费OCR工具

    近期Github开源了一款基于Python开发.名为Textshot的截图工具,刚开源不到半个月已经500+Star. 这两天抽空看了一下Textshot的源码,的确是一个值得介绍的项目. 相对于大多 ...

  2. 基于Python的离线OCR图片文字识别(一)——命令行方式对图像文件处理生成同名txt文件

    应用背景:在正式开始文章之前,先阐述一下项目的应用背景--项目需要对已有的电子档案数据进行"大数据"处理和呈现,但是由于之前进行档案电子化时都是以扫描文件的图像格式存储在硬盘上(准 ...

  3. Python+OpenCV:基于SVM手写数据OCR(OCR of Hand-written Data using SVM)

    Python+OpenCV:基于SVM手写数据OCR(OCR of Hand-written Data using SVM) dsize = 20 affine_flags = lmc_cv.WARP ...

  4. Python+OpenCV:基于KNN手写数据OCR(OCR of Hand-written Data using kNN)

    Python+OpenCV:基于KNN手写数据OCR(OCR of Hand-written Data using kNN) OCR of Hand-written Digits ########## ...

  5. 【优秀课设】基于Python的百度API的OCR名片识别【含完整API账户】

    基于Python的百度API的OCR名片识别[含完整API账户] API账户:(AK及SK) client_id ='WiXDt5e70NI5w0qSeoUBZClZ' client_secret = ...

  6. AI大事件 | 谷歌的计算引擎鸟枪换炮用上了更快的GPU,基于Python的亚马逊AWS深度学习AMI

    大数据文摘作品 编译 | 宁云州 呜啦啦啦啦啦大家好呀,又到了本周的AI大事件时间了.过去的一周中AI圈都发生了什么?大佬们互撕了哪些问题?研究者们发布了哪些值得一读的论文?又有哪些开源的代码和数据库 ...

  7. python如何ocr_基于Python的OCR实现示例

    摘要: 近几天在做一个东西,其中需要对图像中的文字进行识别,看了前辈们的文章,找到两个较简单的方法:使用python的pytesseract库和调用百度AI平台接口.写下这篇文章做一个比较简短的记录和 ...

  8. python游戏开发引擎_基于Python的网络游戏脚本系统的设计与实现

    基于 Python 的网络游戏脚本系统的设计与实现 摘要:传统的网络游戏开发采用 c/c++ 实现游戏的逻辑功能,随 着游戏内容越来越丰富以及快速变化的市场需要, c/c ++ 已经不能 满足开发者对 ...

  9. python实现ocr识别算法_基于Python的OCR实现示例

    摘要: 近几天在做一个东西,其中需要对图像中的文字进行识别,看了前辈们的文章,找到两个较简单的方法:使用python的pytesseract库和调用百度AI平台接口.写下这篇文章做一个比较简短的记录和 ...

最新文章

  1. 综述 | 基于深度学习的目标检测算法
  2. Dynamics Ax 2012 – AIF Import CSV File
  3. python打开-Python中的打开文件对话框(转)
  4. jboss的几个常用操作
  5. 第十七次ScrumMeeting会议
  6. 如何將Clonezilla live放到一個已經有其他作業系統存在的硬碟中
  7. MongoDB3.4 版本新节点同步的一点惊喜
  8. Android 开发中常用小技巧
  9. 华为4g模块 linux驱动程序,定制Android之4G-LTE模块驱动
  10. 信息学奥赛一本通(1171:大整数的因子)
  11. 零距离泛目录站群开源版源码
  12. informix 计算 日期之差
  13. oracle 产看执行计划_ORACLE数据库查看执行计划的方法
  14. secoclient 主机检查失败_SecoClient在win10系统中连接失败解决方案
  15. 程序员夏天穿格子衫,那么冬天穿什么?答案扎心了哈哈哈哈
  16. 两代荣耀Magic历史性同框,荣耀Magic 2如何践行科技理想主义?
  17. Stata:时变Granger因果检验
  18. Mac版如何破解ps
  19. android 登录注册动画,Android开发(14)——动画实战:炫酷登录
  20. JVM-整体结构深度解析(2)

热门文章

  1. kdevelop怎么调试_Kdevelop简单应用以及调试
  2. 关于转战单片机,嵌入式的想法
  3. 这几个SpringBoot前后端分离项目(附源码),改改就能换钱。。。
  4. 根目录index.php-wp-blog-header.php-wp-load.php-wp-config.php
  5. IDEA 代码不报红但是编译不通过 报错:java: 程序包xxx.xxx不存在 java: 找不到符号 lombok不存在
  6. 问题解决:Failed to XXX mysqld.service: Unit not found
  7. 电脑考证毕业还能考吗
  8. 【评测】人前脂肪细胞和培养基
  9. 吓得我抱起了抱着我的小鲤鱼的我(递归思想)C语言
  10. 狼吃羊1(依赖关系)