★
Embedded AI Series – Analysis of YOLOv5 Model Deployment Example Code on RK RV1126 Platform – Part 1
”
1. Introduction
In the previous article “Embedded AI Series – Deploying YOLOv5 Model on RK RV1126 Platform”, we successfully compiled and ran the official YOLOv5 model deployment example code from RK. However, this is just the beginning.
To learn more about model deployment and application on the RK platform, and to better serve actual project development work, we need to further study and analyze the example code.
2. Project Build Management
Before reading the source code, I personally prefer to browse through the contents of each subdirectory and read the build scripts and installation scripts, as this provides an overall impression of the project.
Specifically for rknn_yolov5_demo, it mainly uses shell scripts to set key variables for the cross-compilation toolchain and indirectly calls CMake to perform build and installation tasks.
This method of project construction is commonly used today and is worth imitating in your own project development.
3. Source Code Overview
3.1 Overall Process
The overall process for deploying YOLOv5 is the same as that for deploying YOLOv3, mainly involving calls to interfaces starting with rknn_. For more details, refer to the article “Embedded AI Series – YOLOv3 Model Deployment Code Analysis – Overall Process” and subsequent articles, which explain the functions and usage of these rknn_ interfaces in detail, so I will not elaborate further here.
3.2 Image Processing
The YOLOv5 deployment demo uses the open-source CImg Library for image processing functions, including loading images, drawing boxes, writing text, and saving images.
Compared to the OpenCV library used in previous articles for deploying YOLOv3 code, the CImg library has significant advantages:
- Easy to integrate and install: Simply copy the header file CImg.h to use it, with zero dependencies and no version compatibility issues;
- Simple interface, easy to learn;
- Small footprint after compilation, usually less than 1MB;
Of course, there are also disadvantages:
- Functionally, it only supports basic image processing;
- Since it is purely CPU-based processing without hardware acceleration, there may be performance limitations;
Therefore, if you want to quickly implement basic image algorithms, avoid complex dependency management, and are not sensitive to processing performance, consider using the CImg library.
In the demo, the CImg library is mainly used to draw detection results on the source image, visually presenting the inference results. The source code is annotated as follows:
CImg<unsigned char> img(image_name); // Load image from file
char text[256]; // Buffer for formatted text
const unsigned char blue[] = {0, 0, 255}; // RGB blue (0,0,255)
const unsigned char white[] = {255, 255, 255}; // RGB white (255,255,255)
// Iterate through all detection results
for (int i = 0; i < detect_result_group.count; i++) {
// Get current detection result pointer
detect_result_t *det_result = &(detect_result_group.results[i]);
// Format text: class name + confidence (two decimal places)
sprintf(text, "%s %.2f", det_result->name, det_result->prop);
// Print detection result information to console
printf("%s @ (%d %d %d %d) %f\n", det_result->name, det_result->box.left, det_result->box.top, det_result->box.right, det_result->box.bottom, det_result->prop);
// Extract bounding box coordinates
int x1 = det_result->box.left;
int y1 = det_result->box.top;
int x2 = det_result->box.right;
int y2 = det_result->box.bottom;
// Draw blue bounding box on image
// Parameters: x1,y1 (top-left), x2,y2 (bottom-right), color, opacity (1=opaque), drawing mode (~0U=all channels)
img.draw_rectangle(x1, y1, x2, y2, blue, 1, ~0U);
// Draw white text label above bounding box
// Parameters: x,y (text starting position), text content, text color
img.draw_text(x1, y1 - 12, text, white);
}
// Save processed image as BMP format
img.save("./out.bmp");
3.3 Preprocessing
Similar to the preprocessing flow introduced in “Embedded AI Series – YOLOv3 Deployment Code Analysis – Preprocessing”, the preprocessing for YOLOv5 also includes several steps such as decoding, format conversion, scaling, and padding. The difference is that the YOLOv5 demo code uses a different implementation, particularly utilizing RGA hardware acceleration, significantly improving performance.
3.3.1 Image Loading and Decoding
In the image loading and decoding stage, the demo uses the interface stbi_load, which comes from the very popular single-file public domain image loading library stb_image, and has three main functions:
1. Load file: Automatically detects and parses the format of the image file read from disk, such as JPEG format;
2. Decode image: For example, decodes JPEG compressed data into raw pixel data stored in memory;
3. Channel conversion: Forces the output to be converted to RGB format (3 channels), regardless of whether the original image is grayscale (1 channel) or has Alpha (4 channels), it converts to 3-channel RGB;
static unsigned char *load_image(const char *image_path, int *org_height, int *org_width, int *org_ch, rknn_tensor_attr *input_attr){ //... unsigned char *image_data = stbi_load(image_path, &width, &height, &channel, req_channel); //... return image_data;}
3.3.2 Image Scaling and Padding
In the image scaling stage, the demo mainly uses the Rockchip RGA (Raster Graphic Acceleration) hardware acceleration library’s interface to implement scaling, padding, and other 2D image operations.
Note: librga is written in C++, and this demo only demonstrates the C-style interface of RGA. If you are developing in C++, you can directly use the C++ interface of RGA.
1. First, the RGA library is dynamically loaded, and function pointers for c_RkRgaInit, c_RkRgaDeInit, and c_RkRgaBlit are obtained, followed by RGA initialization:
int RGA_init(rga_context *rga_ctx){ rga_ctx->rga_handle = dlopen("/usr/lib/librga.so", RTLD_LAZY); if (!rga_ctx->rga_handle) { printf("dlopen /usr/lib/librga.so failed\n"); return -1; } rga_ctx->init_func = (FUNC_RGA_INIT)dlsym(rga_ctx->rga_handle, "c_RkRgaInit"); rga_ctx->deinit_func = (FUNC_RGA_DEINIT)dlsym(rga_ctx->rga_handle, "c_RkRgaDeInit"); rga_ctx->blit_func = (FUNC_RGA_BLIT)dlsym(rga_ctx->rga_handle, "c_RkRgaBlit"); rga_ctx->init_func(); return 0;}
- During runtime, the function pointer for c_RkRgaBlit is used for image scaling:
void rga_resize(rga_context *rga_ctx, int src_fd, void *src_virt, int src_w, int src_h, int dst_fd, void *dst_virt, int dst_w, int dst_h){ if (rga_ctx->rga_handle) { int ret = 0; rga_info_t src, dst;
memset(&src, 0, sizeof(rga_info_t)); src.fd = src_fd; src.mmuFlag = 1; src.virAddr = (void *)src_virt;
memset(&dst, 0, sizeof(rga_info_t)); dst.fd = dst_fd; dst.mmuFlag = 1; dst.virAddr = dst_virt;
dst.nn.nn_flag = 0;
rga_set_rect(&src.rect, 0, 0, src_w, src_h, src_w, src_h, RK_FORMAT_RGB_888); rga_set_rect(&dst.rect, 0, 0, dst_w, dst_h, dst_w, dst_h, RK_FORMAT_RGB_888);
ret = rga_ctx->blit_func(&src, &dst, NULL); if (ret) { printf("c_RkRgaBlit error : %s\n", strerror(errno)); }
return; } return;}
- After use, the function pointer for c_RkRgaDeInit must be called for deinitialization:
int RGA_deinit(rga_context *rga_ctx){ if(rga_ctx->rga_handle) { dlclose(rga_ctx->rga_handle); rga_ctx->rga_handle = NULL; } return 0;}
4. Conclusion
- In actual project development, you can imitate the project build management method used in the demo;
- For production code with high performance requirements, refer to the demo’s use of RGA interface for hardware-accelerated image processing;
- For code with low performance requirements that does not use hardware acceleration and needs to easily add basic image processing functions, consider using the open-source CImg or stb_image single-file library;