赞
踩
本文介绍针对PP-OCR模型库的Python推理引擎使用方法,内容依次为文本检测、文本识别、方向分类器以及三者串联在CPU、GPU上的预测方法。
PaddleOCR 版本是: PaddleOCR-release-2.3
PaddleOCR-release-2.3 官网
文本检测模型推理,默认使用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_type
和det_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
超轻量中文识别模型推理,可以执行如下命令:
# 下载超轻量中文识别模型:
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)
如果您需要预测的是其他语言模型,可以在此链接中找到对应语言的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)
方向分类模型推理,可以执行如下命令:
# 下载超轻量中文方向分类器模型:
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]
以超轻量中文OCR模型推理为例,在执行预测时,需要通过参数image_dir
指定单张图像或者图像集合的路径、参数det_model_dir
,cls_model_dir
和rec_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#deprecations 0, 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#deprecations 0, 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。