PaddleOCR Model Convert
- 🎉 推出知识星球RapidAI私享群,这里的提问会优先得到回答和支持,也会享受到RapidAI组织后续持续优质的服务。欢迎大家的加入。
- 本仓库主要是针对性地将PaddleOCR中推理模型转换为ONNX格式。
⚠️ 注意:
整体思路
flowchart TD
A([PaddleOCR inference model]) --paddle2onnx--> B([ONNX])
B --> C([Change Dynamic Input]) --> D([Rec: 字典写入onnx中])
D --> E([保存])
使用步骤
-
安装
paddleocr_convert
pip install paddleocr_convert
-
命令行使用
- 用法:
$ paddleocr_convert -h usage: paddleocr_convert [-h] [-p MODEL_PATH] [-o SAVE_DIR] [-txt_path TXT_PATH] optional arguments: -h, --help show this help message and exit -p MODEL_PATH, --model_path MODEL_PATH The inference model url or local path of paddleocr. e.g. https://paddleocr.bj.bcebos.com/PP- OCRv3/chinese/ch_PP-OCRv3_det_infer.tar or models/ch_PP-OCRv3_det_infer.tar -o SAVE_DIR, --save_dir SAVE_DIR The directory of saving the model. -txt_path TXT_PATH, --txt_path TXT_PATH The raw txt url or local txt path, if the model is recognition model.
- 示例:
# online $ paddleocr_convert -p https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar \ -o models $ paddleocr_convert -p https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar \ -o models \ -txt_path https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt # offline $ paddleocr_convert -p models/ch_PP-OCRv3_det_infer.tar \ -o models $ paddleocr_convert -p models/ch_PP-OCRv3_rec_infer.tar \ -o models \ -txt_path models/ppocr_keys_v1.txt
- 用法:
-
脚本使用
- online mode
from paddleocr_convert import PaddleOCRModelConvert converter = PaddleOCRModelConvert() save_dir = 'models' # online url = 'https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar' txt_url = 'https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt' converter(url, save_dir, txt_path=txt_url)
- offline mode
from paddleocr_convert import PaddleOCRModelConvert converter = PaddleOCRModelConvert() save_dir = 'models' model_path = 'models/ch_PP-OCRv3_rec_infer.tar' txt_path = 'models/ppocr_keys_v1.txt' converter(model_path, save_dir, txt_path=txt_path)
- online mode
-
使用模型方法:
- 假设要用日文识别模型,且已经转好,路径为
local/models/japan.onnx
- 安装
rapidocr_onnxruntime
库pip install rapidocr_onnxruntime
- 脚本使用
from rapidocr_onnxruntime import RapidOCR model_path = 'local/models/japan.onnx' engine = RapidOCR(rec_model_path=model_path) img = '1.jpg' result, elapse = engine(img)
- 命令行使用
$ rapidocr_onnxruntime -img 1.jpg --rec_model_path local/models/japan.onnx
- 假设要用日文识别模型,且已经转好,路径为
更新日志
- 2023-04-13 update:
- 添加在线转换程序link
- 2023-03-05 v0.0.4~7 update:
- 支持对本地的模型和字典转写
- 优化内部逻辑和错误反馈
- 2023-02-28 v0.0.3 update:
- 添加对不是动态输入的模型自动更改为动态输入的设置
- 2023-02-27 v0.0.2 update:
- 将转换模型代码封装成包,便于自助转模型
- 2022-08-15 v0.0.1 update:
- 将识别模型的字典写入到onnx模型中的meta中,便于后续分发。