一、PaddlePaddle的OCR模型转onnx
1、首先建立一个新的虚拟环境
conda create -n ppocr python==3.10 -y
conda activate ppocr
2、进入paddlepaddle官网输入以下指令安装paddlepaddle GPU版本
(我的cuda版本是11.8,根据你电脑装合适版本)
pip install paddlepaddle-gpu==2.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
3、进入PaddlePaddle / PaddleOCR官网下载 PaddleOCR_2.7版本,放在你主目录下:
首先要克隆paddleocr项目,项目地址。(老是有地址不能用多备几个)
git clone https://github.com/PaddlePaddle/Paddle.git
克隆到本地
git clone https://gitcode.com/gh_mirrors/pa/PaddleOCR.git
之后安装命令:
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
4、安装RKNNtoolkit2
ONNX转换为RKNN模型需要使用官方rknn_model_zoo工具:rknn_model_zoo-2.2.0
该处环境部署代码使用到官方rknn-toolkit2工具:rknn-toolkit2
RKNNtoolkit2的作用是将onnx模型转为rknn模型

在该文件夹下找到你对应的python版本
pip install -r requirements_cp310-2.3.0.txt -i https://mirrors.aliyun.com/pypi/simple/
安装paddle2onnx
这一步为下面模型转换做打算:
paddle2onnx的作用:
paddle模型------>onnx模型
RKNNtoolkit2的作用:
onnx模型------>rknn模型
安装paddle2onnx的过程极为简单,在终端输入:
pip install paddle2onnx
至此,ubuntu上面的环境已经搭建完毕!!!
5、OCR程序的编译
按照官方教程安装三个模型:
此处装模型操作均在Ubuntu系统上,注意不是在板子上!!!
PaddleOCR模型下载,PaddleOCR模型官网
最新更新模型地址:ppocr模型官网
下载到你的ppocr目录的一个文件夹下:

随即进入paddle----onnx模型步骤
paddle2onnx --model_dir ch_PP-OCRv4_det_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ch_PP-OCRv4_det_infer/ch_PP-OCRv4_det_infer.onnx
paddle2onnx --model_dir ch_ppocr_mobile_v2.0_cls_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx
--enable_auto_update_opset
paddle2onnx --model_dir ch_PP-OCRv4_rec_infer \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file ch_PP-OCRv4_rec_infer/ch_PP-OCRv4_rec_infer.onnx

然后固定onnx模型的形状:
这里需要注意的是,根据ubuntu系统上python版本的不同,python指令可能会替换为python3
固定模型的输入shape
python3 -m paddle2onnx.optimize --input_model ch_PP-OCRv4_det_infer/ch_PP-OCRv4_det_infer.onnx \
--output_model ch_PP-OCRv4_det_infer/ch_PP-OCRv4_det_infer.onnx \
--input_shape_dict "{'x':[1,3,960,960]}"
python3 -m paddle2onnx.optimize --input_model ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
--output_model ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.onnx \
--input_shape_dict "{'x':[1,3,48,192]}"
python3 -m paddle2onnx.optimize --input_model ch_PP-OCRv4_rec_infer/ch_PP-OCRv4_rec_infer.onnx \
--output_model ch_PP-OCRv4_rec_infer/ch_PP-OCRv4_rec_infer.onnx \
--input_shape_dict "{'x':[1,3,48,320]}"
至此执行完毕后,paddle模型转到onnx模型完毕,接下来是onnx模型转到rknn模型。
二、PaddlePaddle的OCR模型onnx在转rknn模型

把对应的python包放入你的ppocr目录下安装转换RKNN模型的环境:执行
pip install -r requirements_cp310-2.3.0.txt -i https://mirrors.aliyun.com/pypi/simple/
将这个rknpu2_tools文件夹的内容搬到ppocr文件夹下

修改yaml文件路径和你的模型文件对应

随后输入以下三条指令:
python3 rknpu2_tools/export.py --config_path rknpu2_tools/config/ppocrv3_det.yaml --target_platform rk3588
python3 rknpu2_tools/export.py --config_path rknpu2_tools/config/ppocrv3_rec.yaml --target_platform rk3588
python3 rknpu2_tools/export.py --config_path rknpu2_tools/config/ppocrv3_cls.yaml --target_platform rk3588
当三条指令结束运行时,终端内容应该都是:
D RKNN: [14:06:30.472] Total Internal Memory Size: 519.75KB
D RKNN: [14:06:30.472] Total Weight Memory Size: 339.188KB
D RKNN: [14:06:30.472] ----------------------------------------
D RKNN: [14:06:30.472] <<<<<<<< end: rknn::RKNNMemStatisticsPass
I rknn building done.
I Target is None, use simulator!
Export OK!

没有完善,有时间完善一下
可以看到rknn模型已经生成了

三、在RK3588的板子上完成fastdeploy及python的编译
这里直接参考我的另一篇文章
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
后面开发板要用到的环境

把这个文件放在你的开发板上,3588
FastDeploy库的编译(在rk3588板子上进行)
rk3588性能强劲,可以直接在板子上借助图形界面编译FastDeploy库
使用git clone https://github.com/PaddlePaddle/FastDeploy.git指令拉取代码
官网fastdeploy官网
下载FastDeploy-release-1.0.7
放到你的工作目录下执行:
后面的步骤直接参考我的另一篇文章就行
链接:ppocr部署在RK3588_python编译-2_rk3588怎么做ocr识别-CSDN博客
参考:记录如何在RK3588板子上跑通paddle的OCR模型。重点是对齐rknntoolkit版本和板子上的librknnrt.so库_paddleocr rk3588-CSDN博客