Skip to main content

basic

1. Yolo11 Demo

1.1 概述

这个Demo是基于Rockchip平台,使用Neardi的rk3588/rk3576/rk3568系列产品,采用C++开发从摄像头读取数据, 送给RKNN进行推理并显示的应用。整体框架如下:

1.2 环境准备

  • Neardi rk3588开发板, 或者rk3576/rk3568开发板
  • X86 Linux Ubuntu Jammy电脑(用于做模型转换)

2. ONNX转换成RKNN

首先在X86电脑上面下载RKNN-Toolkit2, 按照文档安装RKNN转换环境; 再下载RKNN-Model-Zoo.

接着下载yolo11n.onnx模型, 进入如下目录:

$/rknn_model_zoo$ cd examples/yolo11/
$/rknn_model_zoo/examples/yolo11$ ls
cpp model model_comparison python README.md result.png
$/rknn_model_zoo/examples/yolo11$ cd model
$/rknn_model_zoo/examples/yolo11/model$ ls
bus.jpg coco_80_labels_list.txt dataset.txt download_model.sh
$/rknn_model_zoo/examples/yolo11/model$ bash download_model.sh
--2025-09-12 09:12:29-- https://ftrg.zbox.filez.com/v2/delivery/data/95f00b0fc900458ba134f8b180b3f7a1/examples/yolo11/yolo11n.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: 10527859 (10M) [application/octet-stream]
Saving to: ‘./yolo11n.onnx’

./yolo11n.onnx 100%[====================================================================================>] 10.04M 10.4MB/s in 1.0s

2025-09-12 09:12:30 (10.4 MB/s) - ‘./yolo11n.onnx’ saved [10527859/10527859]

$/rknn_model_zoo/examples/yolo11/model$ ls
bus.jpg coco_80_labels_list.txt dataset.txt download_model.sh yolo11n.onnx

接着进行RKNN模型转换, 如下:

$/rknn_model_zoo/examples/yolo11/python$ python3 convert.py ../model/yolo11n.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 Model converted from pytorch, 'opset_version' should be set 19 in torch.onnx.export for successful convert!
I Loading : 100%|██████████████████████████████████████████████| 188/188 [00:00<00:00, 68933.40it/s]
done
--> Building model
W build: found outlier value, this may affect quantization accuracy
const name abs_mean abs_std outlier value
model.23.cv3.1.1.1.conv.weight 0.55 0.60 -12.173
model.23.cv3.0.0.0.conv.weight 0.25 0.35 -15.593
I GraphPreparing : 100%|████████████████████████████████████████| 231/231 [00:00<00:00, 6579.72it/s]
I Quantizating : 100%|████████████████████████████████████████████| 231/231 [00:05<00:00, 42.29it/s]
W build: The default input dtype of 'images' 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 '462' 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 'onnx::ReduceSum_476' 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 '480' 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 '487' 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 'onnx::ReduceSum_501' 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 '505' 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 '512' 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 'onnx::ReduceSum_526' 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 '530' 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
$/rknn_model_zoo/examples/yolo11/python$ ls ../model
bus.jpg coco_80_labels_list.txt dataset.txt download_model.sh yolo11n.onnx yolo11.rknn

3. 编译yolo11源代码

这里是在Neardi rk3588设备上进行, 也需要下载RKNN-Model-Zoo

3.1 更改编译脚本

在设备上面编译需要更改编译脚本, 如下:

--- a/build-linux.sh
+++ b/build-linux.sh
@@ -55,6 +55,8 @@ if [ -z ${TARGET_SOC} ] || [ -z ${BUILD_DEMO_NAME} ]; then
exit -1
fi

+GCC_COMPILER=aarch64-linux-gnu
+
if [[ -z ${GCC_COMPILER} ]];then
if [[ ${TARGET_SOC} = "rv1106" || ${TARGET_SOC} = "rv1103" ]];then
echo "Please set GCC_COMPILER for $TARGET_SOC"

再把build-linux.sh设置可执行属性, 如下:

neardi@LPA3588:~/rknn/rknn_model_zoo$ chmod +x build-linux.sh

3.2 拷贝RKNN

把在x86电脑上面转换好的RKNN模型, 拷贝到设备上面, 放在如下目录:

neardi@LPA3588:~/rknn/rknn_model_zoo/examples/yolo11/model$ ls
bus.jpg coco_80_labels_list.txt dataset.txt download_model.sh yolo11n.onnx yolo11.rknn

3.3 编译

如下编译命令:

neardi@LPA3588:~/rknn/rknn_model_zoo$ ./build-linux.sh -t rk3588 -a aarch64 -d yolo11 -b Release
./build-linux.sh -t rk3588 -a aarch64 -d yolo11 -b Release
aarch64-linux-gnu
===================================
BUILD_DEMO_NAME=yolo11
BUILD_DEMO_PATH=examples/yolo11/cpp
TARGET_SOC=rk3588
TARGET_ARCH=aarch64
BUILD_TYPE=Release
ENABLE_ASAN=OFF
DISABLE_RGA=OFF
DISABLE_LIBJPEG=OFF
INSTALL_DIR=/home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo
BUILD_DIR=/home/neardi/rknn/rknn_model_zoo/build/build_rknn_yolo11_demo_rk3588_linux_aarch64_Release
CC=aarch64-linux-gnu-gcc
CXX=aarch64-linux-gnu-g++
===================================
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/aarch64-linux-gnu-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- !!!!!!!!!!!CMAKE_SYSTEM_NAME: Linux
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/neardi/rknn/rknn_model_zoo/build/build_rknn_yolo11_demo_rk3588_linux_aarch64_Release
[ 6%] Building C object utils.out/CMakeFiles/fileutils.dir/file_utils.c.o
[ 18%] Building C object utils.out/CMakeFiles/audioutils.dir/audio_utils.c.o
[ 18%] Building C object utils.out/CMakeFiles/imageutils.dir/image_utils.c.o
[ 25%] Building C object utils.out/CMakeFiles/imagedrawing.dir/image_drawing.c.o
/home/neardi/rknn/rknn_model_zoo/utils/image_utils.c: In function ‘write_image’:
/home/neardi/rknn/rknn_model_zoo/utils/image_utils.c:290:35: warning: passing argument 1 of ‘get_image_size’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
290 | int size = get_image_size(img);
| ^~~
In file included from /home/neardi/rknn/rknn_model_zoo/utils/image_utils.c:19:
/home/neardi/rknn/rknn_model_zoo/utils/image_utils.h:67:36: note: expected ‘image_buffer_t *’ but argument is of type ‘const image_buffer_t *’
67 | int get_image_size(image_buffer_t* image);
| ~~~~~~~~~~~~~~~~^~~~~
/home/neardi/rknn/rknn_model_zoo/utils/image_utils.c: In function ‘convert_image_rga’:
/home/neardi/rknn/rknn_model_zoo/utils/image_utils.c:634:27: warning: initialization of ‘char *’ from incompatible pointer type ‘int *’ [-Wincompatible-pointer-types]
634 | char* p_imcolor = &imcolor;
| ^
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c: In function ‘rbbox_to_corners’:
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1541:19: warning: implicit declaration of function ‘cos’ [-Wimplicit-function-declaration]
1541 | float a_cos = cos(angle);
| ^~~
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:8:1: note: include ‘<math.h>’ or provide a declaration of ‘cos’
7 | #include "font.h"
+++ |+#include <math.h>
8 |
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1541:19: warning: incompatible implicit declaration of built-in function ‘cos’ [-Wbuiltin-declaration-mismatch]
1541 | float a_cos = cos(angle);
| ^~~
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1541:19: note: include ‘<math.h>’ or provide a declaration of ‘cos’
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1542:19: warning: implicit declaration of function ‘sin’ [-Wimplicit-function-declaration]
1542 | float a_sin = sin(angle);
| ^~~
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1542:19: note: include ‘<math.h>’ or provide a declaration of ‘sin’
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1542:19: warning: incompatible implicit declaration of built-in function ‘sin’ [-Wbuiltin-declaration-mismatch]
/home/neardi/rknn/rknn_model_zoo/utils/image_drawing.c:1542:19: note: include ‘<math.h>’ or provide a declaration of ‘sin’
[ 31%] Linking C static library libaudioutils.a
[ 37%] Linking C static library libfileutils.a
[ 37%] Built target fileutils
[ 37%] Built target audioutils
[ 43%] Linking C static library libimagedrawing.a
[ 43%] Built target imagedrawing
[ 50%] Linking C static library libimageutils.a
[ 50%] Built target imageutils
[ 56%] Building CXX object CMakeFiles/rknn_yolo11_demo.dir/main.cc.o
[ 62%] Building CXX object CMakeFiles/rknn_yolo11_demo.dir/rknpu2/yolo11.cc.o
[ 68%] Building CXX object CMakeFiles/rknn_yolo11_demo.dir/postprocess.cc.o
[ 75%] Building CXX object CMakeFiles/rknn_yolo11_demo_zero_copy.dir/main.cc.o
[ 81%] Building CXX object CMakeFiles/rknn_yolo11_demo_zero_copy.dir/postprocess.cc.o
[ 87%] Building CXX object CMakeFiles/rknn_yolo11_demo_zero_copy.dir/rknpu2/yolo11_zero_copy.cc.o
/home/neardi/rknn/rknn_model_zoo/examples/yolo11/cpp/postprocess.cc: In function ‘char* coco_cls_to_name(int)’:
/home/neardi/rknn/rknn_model_zoo/examples/yolo11/cpp/postprocess.cc:675:16: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
675 | return "null";
| ^~~~~~
/home/neardi/rknn/rknn_model_zoo/examples/yolo11/cpp/postprocess.cc:683:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
683 | return "null";
| ^~~~~~
/home/neardi/rknn/rknn_model_zoo/examples/yolo11/cpp/postprocess.cc: In function ‘char* coco_cls_to_name(int)’:
/home/neardi/rknn/rknn_model_zoo/examples/yolo11/cpp/postprocess.cc:675:16: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
675 | return "null";
| ^~~~~~
/home/neardi/rknn/rknn_model_zoo/examples/yolo11/cpp/postprocess.cc:683:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
683 | return "null";
| ^~~~~~
[ 93%] Linking CXX executable rknn_yolo11_demo
[100%] Linking CXX executable rknn_yolo11_demo_zero_copy
[100%] Built target rknn_yolo11_demo
[100%] Built target rknn_yolo11_demo_zero_copy
Consolidate compiler generated dependencies of target imageutils
[ 12%] Built target imageutils
Consolidate compiler generated dependencies of target fileutils
[ 25%] Built target fileutils
Consolidate compiler generated dependencies of target imagedrawing
[ 37%] Built target imagedrawing
Consolidate compiler generated dependencies of target rknn_yolo11_demo
[ 62%] Built target rknn_yolo11_demo
Consolidate compiler generated dependencies of target rknn_yolo11_demo_zero_copy
[ 87%] Built target rknn_yolo11_demo_zero_copy
Consolidate compiler generated dependencies of target audioutils
[100%] Built target audioutils
Install the project...
-- Install configuration: "Release"
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/./rknn_yolo11_demo_zero_copy
-- Set runtime path of "/home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/./rknn_yolo11_demo_zero_copy" to "$ORIGIN/../lib"
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/./rknn_yolo11_demo
-- Set runtime path of "/home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/./rknn_yolo11_demo" to "$ORIGIN/../lib"
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/model/bus.jpg
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/model/coco_80_labels_list.txt
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/model/yolo11.rknn
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/lib/librknnrt.so
-- Installing: /home/neardi/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo/lib/librga.so

编译成功后, 生成的文件在如下路径:

neardi@LPA3588:~/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo$ tree
.
├── lib
│   ├── librga.so
│   └── librknnrt.so
├── model
│   ├── bus.jpg
│   ├── coco_80_labels_list.txt
│   └── yolo11.rknn
├── out.png
├── rknn_yolo11_demo
└── rknn_yolo11_demo_zero_copy

3.4 运行Yolo11 Demo

如下运行:

neardi@LPA3588:~/rknn/rknn_model_zoo/install/rk3588_linux_aarch64/rknn_yolo11_demo$ ./rknn_yolo11_demo ./model/yolo11.rknn ./model/bus.jpg
load lable ./model/coco_80_labels_list.txt
model input num: 1, output num: 9
input tensors:
index=0, name=images, n_dims=4, dims=[1, 640, 640, 3], n_elems=1228800, size=1228800, fmt=NHWC, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
output tensors:
index=0, name=462, n_dims=4, dims=[1, 64, 80, 80], n_elems=409600, size=409600, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-44, scale=0.135466
index=1, name=onnx::ReduceSum_476, n_dims=4, dims=[1, 80, 80, 80], n_elems=512000, size=512000, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003027
index=2, name=480, n_dims=4, dims=[1, 1, 80, 80], n_elems=6400, size=6400, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003036
index=3, name=487, n_dims=4, dims=[1, 64, 40, 40], n_elems=102400, size=102400, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-38, scale=0.093536
index=4, name=onnx::ReduceSum_501, n_dims=4, dims=[1, 80, 40, 40], n_elems=128000, size=128000, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003476
index=5, name=505, n_dims=4, dims=[1, 1, 40, 40], n_elems=1600, size=1600, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
index=6, name=512, n_dims=4, dims=[1, 64, 20, 20], n_elems=25600, size=25600, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-42, scale=0.084287
index=7, name=onnx::ReduceSum_526, n_dims=4, dims=[1, 80, 20, 20], n_elems=32000, size=32000, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003903
index=8, name=530, n_dims=4, dims=[1, 1, 20, 20], n_elems=400, size=400, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
model is NHWC input fmt
model input height=640, width=640, channel=3
origin size=640x640 crop size=640x640
input image: 640 x 640, subsampling: 4:2:0, colorspace: YCbCr, orientation: 1
scale=1.000000 dst_box=(0 0 639 639) allow_slight_change=1 _left_offset=0 _top_offset=0 padding_w=0 padding_h=0
rga_api version 1.10.1_[0]
rknn_run
bus @ (91 135 554 439) 0.948
person @ (108 236 224 535) 0.890
person @ (212 240 284 509) 0.847
person @ (476 229 559 520) 0.831
person @ (79 358 117 516) 0.431
write_image path: out.png width=640 height=640 channel=3 data=0x558ccd3720

输出推理图片如下: