PPOCR
PP-OCR 是一套灵活的 OCR 解决方案,既支持检测与识别模块的独立调用,也支持端到端的系统集成。 本案例将演示如何利用 Rockchip 平台的计算资源,部署并运行这套“图片进、文字出”的高性能文字识别系统。
环境配置
信息
参考 RKNN 安装 配置好相关环境。
参考 RKNN Model Zoo 下载示例文件。
模型下载
下载 onnx 模型文件。
X64 Linux PC
cd rknn_model_zoo/examples/PPOCR/PPOCR-Det/model/
bash download_model.sh
cd ../../PPOCR-Rec/model/
bash download_model.sh
模型转换
选择目标平台。
- rk3588
- rk356x
- rk3576
X64 Linux PC
export TARGET_PLATFORM=rk3588
X64 Linux PC
export TARGET_PLATFORM=rk356x
X64 Linux PC
export TARGET_PLATFORM=rk3576
将 onnx 模型转换为 rknn 模型。
X64 Linux PC
cd ../python
python convert.py ../model/ppocrv4_rec.onnx ${TARGET_PLATFORM}
cd ../../PPOCR-Det/python/
python convert.py ../model/ppocrv4_det.onnx ${TARGET_PLATFORM}
将转换好的模型拷贝到 PPOCR-System/model 目录下。
X64 Linux PC
cd ../../PPOCR-System/model/
cp ../../PPOCR-Det/model/ppocrv4_det.rknn ./
cp ../../PPOCR-Rec/model/ppocrv4_rec.rknn ./
C API
编译示例
切换到 rknn_model_zoo 目录下执行 build-linux.sh 编译脚本。
X64 Linux PC
cd ../../../..
bash build-linux.sh -t ${TARGET_PLATFORM} -a aarch64 -d PPOCR-System
文件同步
然后将编译生成的 install 目录下的 demo 目录推送到板端。
X64 Linux PC
cd install/${TARGET_PLATFORM}_linux_aarch64/
scp -r rknn_PPOCR-System_demo/ user@your_device_ip:target_directory
运行示例
导出运行时库到环境变量。
Device
cd rknn_PPOCR-System_demo
export LD_LIBRARY_PATH=./lib
运行示例。
Device
./rknn_ppocr_system_demo ./model/ppocrv4_det.rknn ./model/ppocrv4_rec.rknn ./model/test.jpg
$ ./rknn_ppocr_system_demo ./model/ppocrv4_det.rknn ./model/ppocrv4_rec.rknn ./model/test.jpg
model input num: 1, output num: 1
input tensors:
index=0, name=x, n_dims=4, dims=[1, 480, 480, 3], n_elems=691200, size=691200, fmt=NHWC, type=INT8, qnt_type=AFFINE, zp=-14, scale=0.018658
output tensors:
index=0, name=sigmoid_0.tmp_0, n_dims=4, dims=[1, 1, 480, 480], n_elems=230400, size=230400, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
model is NHWC input fmt
model input height=480, width=480, channel=3
model input num: 1, output num: 1
input tensors:
index=0, name=x, n_dims=4, dims=[1, 48, 320, 3], n_elems=46080, size=92160, fmt=NHWC, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
output tensors:
index=0, name=softmax_11.tmp_0, n_dims=3, dims=[1, 40, 6625, 0], n_elems=265000, size=530000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
model is NHWC input fmt
model input height=48, width=320, channel=3
origin size=500x500 crop size=496x496
input image: 500 x 500, subsampling: 4:4:4, colorspace: YCbCr, orientation: 1
src width is not 4/16-aligned, convert image use cpu
finish
DRAWING OBJECT
[0] @ [(28, 37), (302, 39), (301, 71), (27, 69)]
recognize result: 纯臻营养护发素, score=0.711077
[1] @ [(26, 82), (172, 82), (172, 104), (26, 104)]
recognize result: 产品信息/参数, score=0.709612
[2] @ [(27, 112), (332, 112), (332, 134), (27, 134)]
recognize result: (45元/每公斤,100公斤起订), score=0.691406
[3] @ [(28, 142), (282, 144), (281, 163), (27, 162)]
recognize result: 每瓶22元,1000瓶起订), score=0.706613
[4] @ [(25, 179), (298, 177), (300, 194), (26, 195)]
recognize result: 【品牌】:代加工方式/OEMODM, score=0.704963
[5] @ [(26, 209), (234, 209), (234, 228), (26, 228)]
recognize result: 【品名】:纯臻营养护发素, score=0.710124
[6] @ [(26, 240), (241, 240), (241, 259), (26, 259)]
recognize result: 【产品编号】:YM-X-3011, score=0.703522
[7] @ [(413, 233), (429, 233), (429, 305), (413, 305)]
recognize result: ODMOEM, score=0.708415
[8] @ [(25, 270), (179, 270), (179, 289), (25, 289)]
recognize result: 【净含量】:220ml, score=0.707519
[9] @ [(26, 303), (252, 303), (252, 321), (26, 321)]
recognize result: 【适用人群】:适合所有肤质, score=0.709698
[10] @ [(26, 333), (341, 333), (341, 351), (26, 351)]
recognize result: 【主要成分】:鲸蜡硬脂醇、燕麦β-葡聚, score=0.689684
[11] @ [(27, 363), (283, 365), (282, 384), (26, 382)]
recognize result: 糖、椰油酰胺丙基甜菜碱、泛酸, score=0.691807
[12] @ [(368, 368), (476, 368), (476, 388), (368, 388)]
recognize result: (成品包材), score=0.706706
[13] @ [(27, 394), (362, 396), (361, 414), (26, 413)]
recognize result: 【主要功能】:可紧致头发磷层,从而达到, score=0.696854
[14] @ [(27, 428), (371, 428), (371, 446), (27, 446)]
recognize result: 即时持久改善头发光泽的效果,给干燥的头, score=0.711040
[15] @ [(27, 459), (136, 459), (136, 478), (27, 478)]
recognize result: 发足够的滋养, score=0.711344
SAVE TO ./out.jpg
write_image path: ./out.jpg width=500 height=500 channel=3 data=0x2bf82010
效果展示

Python API
激活虚拟环境
Device
conda activate rknn
运行示例
信息
依赖说明:运行下面的命令安装依赖。
pip install shapely pyclipper
将相关文件推送到板端执行下面的命令。
Device
python ppocr_system.py --det_model_path ../model/ppocrv4_det.rknn --rec_model_path ../model/ppocrv4_rec.rknn --target ${TARGET_PLATFORM}
$ python ppocr_system.py --det_model_path ../model/ppocrv4_det.rknn --rec_model_path ../model/ppocrv4_rec.rknn --target rk3588
/home/radxa/miniforge3/envs/rknn/lib/python3.12/site-packages/rknn/api/rknn.py:51: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
self.rknn_base = RKNNBase(cur_path, verbose)
I rknn-toolkit2 version: 2.3.2
--> Init runtime environment
I target set by user is: rk3588
done
Model-../model/ppocrv4_det.rknn is rknn model, starting val
I rknn-toolkit2 version: 2.3.2
--> Init runtime environment
I target set by user is: rk3588
done
Model-../model/ppocrv4_rec.rknn is rknn model, starting val
W inference: The 'data_format' is not set, and its default value is 'nhwc'!
W inference: The 'data_format' is not set, and its default value is 'nhwc'!
W inference: The 'data_format' is not set, and its default value is 'nhwc'!
W inference: The 'data_format' is not set, and its default value is 'nhwc'!
[[('纯臻营养护发素', 0.7113560438156128)], [('产品信息/参数', 0.7074497938156128)], [('(45元/每公斤,100公斤起订)', 0.6900849938392639)], [('每瓶22元,1000瓶起订)', 0.7073799967765808)], [('【品牌】:代加工方式/OEMODM', 0.7077493071556091)], [('【品名】:纯臻营养护发素', 0.7105305790901184)], [('【产品编号】:YM-X-3011', 0.705413818359375)], [('ODM OEM', 0.6839424967765808)], [('【净含量】:220ml', 0.7086736559867859)], [('【适用人群】:适合所有肤质', 0.7099984884262085)], [('【主要成分】:鲸蜡硬脂醇、燕麦β-葡聚', 0.6929739117622375)], [('糖、椰油酰胺丙基甜菜碱、泛酸', 0.6709420084953308)], [('(成品包材)', 0.708251953125)], [('【主要功能】:可紧致头发磷层,从而达到', 0.7064401507377625)], [('即时持久改善头发光泽的效果,给干燥的头', 0.7103207111358643)], [('发足够的滋养', 0.7110188603401184)]]