1. Paddle-OCR Demo
1.1 环境准备
- Neardi lkd3588, lkd3576, lkd3568其中任意一款设备。
- 下载Rockchip官方的RKNN-Model-Zoo.
1.2 模型转换
需要把paddle-ocr onnx模型转换成rknn模型, 先在PC电脑上面下载, 如下:
linx@ubuntu2004:~/rknn/rknn_model_zoo/examples/PPOCR/PPOCR-Det/model$ bash download_model.sh
--2025-09-25 11:27:25-- https://ftrg.zbox.filez.com/v2/delivery/data/95f00b0fc900458ba134f8b180b3f7a1/examples/PPOCR/ppocrv4_det.onnx
Resolving ftrg.zbox.filez.com (ftrg.zbox.filez.com)... 180.184.171.46
Connecting to ftrg.zbox.filez.com (ftrg.zbox.filez.com)|180.184.171.46|:443... connected.
HTTP request sent, awaiting response... 200
Length: 4771801 (4.5M) [application/octet-stream]
Saving to: ‘./ppocrv4_det.onnx’
./ppocrv4_det.onnx 100%[====================================================================================>] 4.55M 10.6MB/s in 0.4s
2025-09-25 11:27:26 (10.6 MB/s) - ‘./ppocrv4_det.onnx’ saved [4771801/4771801]
接着进行模型转换, 如下:
linx@ubuntu2004:~/rknn/rknn_model_zoo/examples/PPOCR/PPOCR-Det/python$ python3 convert.py ../model/ppocrv4_det.onnx rk3588
I rknn-toolkit2 version: 2.0.0b0+9bab5682
--> Config model
done
--> Loading model
I It is recommended onnx opset 19, but your onnx model opset is 12!
I Loading : 100%|█████████████████████████████████████████████| 342/342 [00:00<00:00, 197582.92it/s]
done
--> Building model
W build: found outlier value, this may affect quantization accuracy
const name abs_mean abs_std outlier value
conv2d_398.w_0 6.53 8.93 58.173
conv2d_402.w_0 2.41 3.73 34.766
conv2d_403.w_0 0.14 0.16 9.510
conv2d_406.w_0 0.44 0.87 13.446
conv2d_412.w_0 0.29 0.86 -44.813
conv2d_416.w_0 0.37 0.94 29.860
conv2d_418.w_0 0.37 0.62 41.572
conv2d_420.w_0 0.33 0.69 -25.894
conv2d_421.w_0 0.08 0.09 11.623
I GraphPreparing : 100%|████████████████████████████████████████| 214/214 [00:00<00:00, 6118.61it/s]
I Quantizating : 100%|████████████████████████████████████████████| 214/214 [00:02<00:00, 77.05it/s]
W build: The default input dtype of 'x' is changed from 'float32' to 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of 'sigmoid_0.tmp_0' is changed from 'float32' to 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model with Runtime API!
I rknn building ...
I rknn buiding done.
done
--> Export rknn model
done
linx@ubuntu2004:~/rknn/rknn_model_zoo/examples/PPOCR/PPOCR-Det/python$ ls ../model/
download_model.sh ppocrv4_det.onnx ppocrv4_det.rknn test.jpg
使用同样的方法, 把识别模型ppocrv4_rec也转换成RKNN, 如下:
linx@ubuntu2004:~/rknn/rknn_model_zoo/examples/PPOCR/PPOCR-Rec/python$ python3 convert.py ../model/ppocrv4_rec.onnx rk3588
I rknn-toolkit2 version: 2.0.0b0+9bab5682
--> Config model
done
--> Loading model
I It is recommended onnx opset 19, but your onnx model opset is 12!
I Loading : 100%|█████████████████████████████████████████████| 420/420 [00:00<00:00, 179572.65it/s]
W load_onnx: The config.mean_values is None, zeros will be set for input 0!
W load_onnx: The config.std_values is None, ones will be set for input 0!
done
--> Building model
I rknn building ...
I rknn buiding done.
done
--> Export rknn model
done
1.2 编译代码
Paddle-OCR包含2个模型, 即ppocrv4_det.rknn和ppocrv4_rec.rknn。 把这2个模型拷贝到设备上面, 如下:
neardi@LPA3588:/userdata/work/rknn_model_zoo/examples/PPOCR/PPOCR-System/model$ ls
ppocr_keys_v1.txt ppocrv4_det.rknn ppocrv4_rec.rknn simfang.ttf test.jpg
编译PPOCR-System应用, 如下:
neardi@LPA3588:/userdata/work/rknn_model_zoo$ ./build-linux.sh -t rk3588 -a aarch64 -d PPOCR-System -b Release
./build-linux.sh -t rk3588 -a aarch64 -d PPOCR-System -b Release
aarch64-linux-gnu
===================================
BUILD_DEMO_NAME=PPOCR-System
BUILD_DEMO_PATH=examples/PPOCR/PPOCR-System/cpp
TARGET_SOC=rk3588
TARGET_ARCH=aarch64
BUILD_TYPE=Release
ENABLE_ASAN=OFF
DISABLE_RGA=OFF
DISABLE_LIBJPEG=OFF
INSTALL_DIR=/userdata/work/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_PPOCR-System_demo
BUILD_DIR=/userdata/work/rknn_model_zoo/build/build_rknn_PPOCR-System_demo_rk3588_linux_aarch64_Release
CC=aarch64-linux-gnu-gcc
CXX=aarch64-linux-gnu-g++
===================================
-- The C compiler identification is GNU 11.4.0
编译成功后, 生成如下目录及文件:
neardi@LPA3588:/userdata/work/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_PPOCR-System_demo$ tree
.
├── lib
│ ├── librga.so
│ └── librknnrt.so
├── model
│ ├── ppocrv4_det.rknn
│ ├── ppocrv4_rec.rknn
│ └── test.jpg
├── out.jpg
└── rknn_ppocr_system_demo
1.3 识别测试
使用如下命令进行测试:
neardi@LPA3588:/userdata/work/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_PPOCR-System_demo$ ./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)]
regconize result: 纯臻营养护发素, score=0.711077
[1] @ [(26, 82), (172, 82), (172, 104), (26, 104)]
regconize result: 产品信息/参数, score=0.709612
[2] @ [(27, 112), (332, 112), (332, 134), (27, 134)]
regconize result: (45元/每公斤,100公斤起订), score=0.692498
[3] @ [(28, 142), (282, 144), (281, 163), (27, 162)]
regconize result: 每瓶22元,1000瓶起订), score=0.706578
[4] @ [(25, 179), (298, 177), (300, 194), (26, 195)]
regconize result: 【品牌】:代加工方式/OEMODM, score=0.705049
[5] @ [(26, 209), (234, 209), (234, 228), (26, 228)]
regconize result: 【品名】:纯臻营养护发素, score=0.710124
[6] @ [(26, 240), (241, 240), (241, 259), (26, 259)]
regconize result: 【产品编号】:YM-X-3011, score=0.703461
[7] @ [(413, 233), (429, 233), (429, 305), (413, 305)]
regconize result: ODMOEM, score=0.708415
[8] @ [(25, 270), (179, 270), (179, 289), (25, 289)]
regconize result: 【净含量】:220ml, score=0.707519
[9] @ [(26, 303), (252, 303), (252, 321), (26, 321)]
regconize result: 【适用人群】:适合所有肤质, score=0.709660
[10] @ [(26, 333), (341, 333), (341, 351), (26, 351)]
regconize result: 【主要成分】:鲸蜡硬脂醇、燕麦β-葡聚, score=0.689864
[11] @ [(27, 363), (283, 365), (282, 384), (26, 382)]
regconize result: 糖、椰油酰胺丙基甜菜碱、泛酸, score=0.691947
[12] @ [(368, 368), (476, 368), (476, 388), (368, 388)]
regconize result: (成品包材), score=0.706624
[13] @ [(27, 394), (362, 396), (361, 414), (26, 413)]
regconize result: 【主要功能】:可紧致头发磷层,从而达到, score=0.697137
[14] @ [(27, 428), (371, 428), (371, 446), (27, 446)]
regconize result: 即时持久改善头发光泽的效果,给干燥的头, score=0.711040
[15] @ [(27, 459), (136, 459), (136, 478), (27, 478)]
regconize result: 发足够的滋养, score=0.711344
SAVE TO ./out.jpg
write_image path: ./out.jpg width=500 height=500 channel=3 data=0x5582fd73a0
图片结果如下:
