mirror of
https://github.com/RapidAI/RapidOCR.git
synced 2026-11-11 13:30:51 +08:00
Python版RapidOCR
- 所有常用的参数配置都在
config.yaml下,一目了然,更加便捷 - 每个独立的模块下均有独立的
config.yaml配置文件,可以单独使用 det的mobile和server版,共用一个推理代码,直接更改配置文件中模型路径即可rec的mobile和server版本,共用一个推理代码,直接更改配置文件中模型路径即可- onnxruntime和OpenVINO调用方式如下:
# 基于onnxruntime引擎推理 from rapidocr_onnxruntime import TextSystem # 基于openvin引擎推理 from rapidocr_openvino import TextSystem - 值得说明的是,基于openvino推理部分中
ch_ppocr_v2_cls部分仍然是基于onnxruntime的,原因是openvino有bug,详情见openvino/issue
使用步骤
-
下载当前下的
rapidocr_onnxruntime目录到本地 -
下载链接下的
resources目录(包含模型和显示的字体文件)- 百度网盘 | Google Drive
- 最终目录如下:
. ├── README.md ├── config.yaml ├── test_demo.py ├── rapidocr_onnxruntime │ ├── __init__.py │ ├── ch_ppocr_v2_cls │ ├── ch_ppocr_v2_det │ ├── ch_ppocr_v2_rec │ ├── ch_ppocr_v3_rec │ └── rapid_ocr_api.py ├── requirements.txt ├── resources │ ├── fonts │ │ └── msyh.ttc │ ├── models │ │ ├── ch_PP-OCRv2_det_infer.onnx.onnx │ │ ├── ch_ppocr_mobile_v2.0_cls_infer.onnx │ │ └── ch_ppocr_mobile_v2.0_rec_infer.onnx │ └── rec_dict │ ├── en_dict.txt │ ├── japan_dict.txt │ ├── korean_dict.txt │ └── ppocr_keys_v1.txt └── test_images ├── det_images └── rec_images
-
安装运行环境
- 基于onnxruntime推理所需环境安装:
pip install onnxruntime>=1.7.0 - 基于OpenVINO推理所需环境安装:
# Windows端 pip install openvino==2022.1.0 pip install -r requirements.txt -i https://pypi.douban.com/simple/ - Note: 在Windows端,Shapely库可能自动安装会有问题,解决方案参见Q15
- 基于onnxruntime推理所需环境安装:
-
运行示例
- 接口调用
# 基于onnxruntime引擎推理 from rapidocr_onnxruntime import TextSystem # 基于OpenVINO引擎推理 # from rapidocr_openvino import TextSystem config_path = 'config.yaml' text_sys = TextSystem(config_path) image_path = r'test_images/det_images/ch_en_num.jpg' img = cv2.imread(image_path) dt_boxes, rec_res = text_sys(img) - 直接运行
test_demo.pypython test_demp.py rec部分,如果想要使用PPOCR-v3的话,需要将config.yaml中的Rec部分改为如下:module_name: ch_ppocr_v3_rec class_name: TextRecognizer model_path: resources/models/PPOCRv3/ch_PP-OCRv3_rec_infer.onnx rec_img_shape: [3, 48, 320] rec_batch_num: 6 keys_path: resources/rec_dict/ppocr_keys_v1.txt
- 接口调用
config.yaml中常用参数介绍
| 参数名称 | 作用 | 建议取值范围 | 默认值 |
|---|---|---|---|
| box_thresh | 文本检测所得框是否保留的阈值,值越大,召回率越低 | [0, 1] | 0.5 |
| unclip_ratio | 控制文本检测框的大小,值越大,检测框整体越大 | [1.6, 2.0] | 1.6 |
| text_score | 文本识别结果置信度,值越大,把握越大 | [0, 1] | 0.5 |
onnxruntime-gpu版推理配置
- onnxruntime-gpu需要严格按照与cuda、cudnn版本对应来安装,具体参考文档,这一步关乎后面是否可以成功调用GPU
$ pip install onnxruntime-gpu==1.xxx - 推理代码中,加载onnx模型部分,用以下对应语言代码替换即可,详细参见:官方教程
- python版本
# 根据机器配置,安装对应版本的onnxruntime-gpu # pip install onnxruntime-gpu==xxxx import onnxruntime as ort model_path = '<path to model>' providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] session = ort.InferenceSession(model_path, providers=providers) - C/C++版本
OrtSessionOptions* session_options = /* ... */; OrtCUDAProviderOptions options; options.device_id = 0; options.arena_extend_strategy = 0; options.gpu_mem_limit = 2 * 1024 * 1024 * 1024; options.cudnn_conv_algo_search = OrtCudnnConvAlgoSearch::EXHAUSTIVE; options.do_copy_in_default_stream = 1; SessionOptionsAppendExecutionProvider_CUDA(session_options, &options);
- python版本
- 推理时间粗略对比(完整跑完一张图像)
推理方式 推理图像数目 耗费时间 CPU 46张图像 191 s GPU 46张图像 52.38 s