背景:官方的rknn_yolov5_demo是通过加载一张图片,然后进行推理,实际使用过程中往往需要使用摄像头capture图片后,然后再调用demo进行推理验证,很不方便。
故对官网demo进行改造,先将整个yolov5目录复制出来。偷懒的小伙伴可以直接clone我修改好的,github地址如下
LitchiCheng/RV1106_Linux: Linux test for RV1106 dev board (github.com)
https://github.com/LitchiCheng/RV1106_Linux
下面介绍几个需要特别修改的地方:
修改工程主目录下的CMakeLists.txt
指定交叉编译器
set (CMAKE_C_COMPILER "/mnt/d/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-gcc")
set (CMAKE_CXX_COMPILER "/mnt/d/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-g++")
增加预编译变量的声明
add_definitions(-DRV1106_1103)
增加我们上期分享的v4l2的工具类
add_executable(${PROJECT_NAME}
main.cc
postprocess.cc
../v4l2/tools/v4l2CapPicTool.cpp
${rknpu2_yolov5_file}
)
修改cmake安装路径,很多,这里只放一个,具体看整个工程
install(TARGETS ${PROJECT_NAME} DESTINATION ${OUTPUTPATH})
修改RV1106_Linux/yolov5/3rdparty下的CMakeLists.txt
直接设置架构类型为armhf_uclibc
if (CMAKE_C_COMPILER MATCHES "uclibc")
set (TARGET_LIB_ARCH ${TARGET_LIB_ARCH}_uclibc)
endif()
修改runtime库,直接用armhf_uclibc版本
# rknn runtime
set(RKNN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/rknpu2)
set(LIBRKNNRT ${RKNN_PATH}/${CMAKE_SYSTEM_NAME}/armhf-uclibc/librknnmrt.so)
set(LIBRKNNRT_INCLUDES ${RKNN_PATH}/include PARENT_SCOPE)
install(PROGRAMS ${LIBRKNNRT} DESTINATION ${OUTPUTPATH}/lib)
set(LIBRKNNRT ${LIBRKNNRT} PARENT_SCOPE)
然后修改main.cc,如下,增加对v4l2的调用
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("%s <model_path> <camera_path>\n", argv[0]);
return -1;
}
const char *model_path = argv[1];
const char *image_path = "tmp.jpg";
std::string camera_path = std::string(argv[2]);
v4l2CapPicTool vt(camera_path, 1080, 960, "jpg");
vt.init();
int ret;
rknn_app_context_t rknn_app_ctx;
memset(&rknn_app_ctx, 0, sizeof(rknn_app_context_t));
init_post_process();
ret = init_yolov5_model(model_path, &rknn_app_ctx);
if (ret != 0)
{
printf("init_yolov5_model fail! ret=%d model_path=%s\n", ret, model_path);
goto out;
}
vt.capture();
vt.save("tmp.jpg");
image_buffer_t src_image;
memset(&src_image, 0, sizeof(image_buffer_t));
ret = read_image("tmp.jpg", &src_image);
#if defined(RV1106_1103)
//RV1106 rga requires that input and output bufs are memory allocated by dma
ret = dma_buf_alloc(RV1106_CMA_HEAP_PATH, src_image.size, &rknn_app_ctx.img_dma_buf.dma_buf_fd,
(void **) & (rknn_app_ctx.img_dma_buf.dma_buf_virt_addr));
memcpy(rknn_app_ctx.img_dma_buf.dma_buf_virt_addr, src_image.virt_addr, src_image.size);
dma_sync_cpu_to_device(rknn_app_ctx.img_dma_buf.dma_buf_fd);
free(src_image.virt_addr);
src_image.virt_addr = (unsigned char *)rknn_app_ctx.img_dma_buf.dma_buf_virt_addr;
#endif
//.....
return 0;
}
创建构建目录
mkdir build
cd build
cmake ..
make
make install
生成的文件都放在主目录下的output
pico@luckfox:~/output$ sudo ./rknn_yolov5_realtime model/yolov5s_relu.rknn /dev/video9
init success
load lable ./model/coco_80_labels_list.txt
model input num: 1, output num: 3
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=output, n_dims=4, dims=[1, 255, 80, 80], n_elems=1632000, size=1632000, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003860
index=1, name=283, n_dims=4, dims=[1, 255, 40, 40], n_elems=408000, size=408000, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
index=2, name=285, n_dims=4, dims=[1, 255, 20, 20], n_elems=102000, size=102000, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003915
model is NHWC input fmt
model input height=640, width=640, channel=3
origin size=960x720 crop size=960x720
input image: 960 x 720, subsampling: 4:2:2, colorspace: YCbCr, orientation: 1
scale=0.666667 dst_box=(0 80 639 559) allow_slight_change=1 _left_offset=0 _top_offset=80 padding_w=0 padding_h=160
src width=960 height=720 fmt=0x1 virAddr=0x0x558b65c400 fd=0
dst width=640 height=640 fmt=0x1 virAddr=0x0x558b856810 fd=0
src_box=(0 0 959 719)
dst_box=(0 80 639 559)
color=0x72
fill dst image (x y w h)=(0 0 640 640) with color=0x72727272
rga_api version 1.10.0_[2]
rknn_run
book @ (268 189 544 457) 0.581
book @ (274 4 537 255) 0.522
book @ (238 372 555 637) 0.481
book @ (216 472 502 697) 0.477
tv @ (759 0 958 586) 0.357
suitcase @ (580 48 693 126) 0.223
write_image path: out.png width=960 height=720 channel=3 data=0x558b65c400