1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| #include <iostream> #include <vector> #include <chrono> #include <string> #include <thread> #include <opencv2/opencv.hpp> #include "rknn_api.h" #include "camera_runner.h"
#define MODEL_PATH "./model/yolov8_rk3566.rknn" #define DEVICE_ID 14 #define CONF_THRESH 0.45f #define NMS_THRESH 0.45f
std::vector<std::string> CLASSES = { "wire", "slipper", "dock" };
static unsigned char* load_model(const char* filename, int* model_size) { FILE* fp = fopen(filename, "rb"); if(fp == nullptr) { printf("Open file %s failed.\n", filename); return nullptr; } fseek(fp, 0, SEEK_END); int size = ftell(fp); fseek(fp, 0, SEEK_SET); unsigned char* data = (unsigned char*)malloc(size); fread(data, 1, size, fp); fclose(fp); *model_size = size; return data; }
void post_process(float* output, cv::Mat& img, float scale_w, float scale_h, int model_height, int model_anchors) { std::vector<int> classIds; std::vector<float> confidences; std::vector<cv::Rect> boxes;
int num_classes = model_height - 4; if (CLASSES.size() < num_classes) { printf("[Error] 代码里的类别名只有 %lu 个,但模型预测了 %d 个类别!\n", CLASSES.size(), num_classes); return; }
for (int i = 0; i < model_anchors; ++i) { float max_score = 0.0f; int class_id = -1;
for (int c = 0; c < num_classes; ++c) { float score = output[(4 + c) * model_anchors + i]; if (score > max_score) { max_score = score; class_id = c; } }
if (max_score > CONF_THRESH) { float cx = output[0 * model_anchors + i]; float cy = output[1 * model_anchors + i]; float w = output[2 * model_anchors + i]; float h = output[3 * model_anchors + i];
int left = int((cx - 0.5 * w) * scale_w); int top = int((cy - 0.5 * h) * scale_h); int width = int(w * scale_w); int height = int(h * scale_h);
boxes.push_back(cv::Rect(left, top, width, height)); confidences.push_back(max_score); classIds.push_back(class_id); } }
std::vector<int> indices; cv::dnn::NMSBoxes(boxes, confidences, CONF_THRESH, NMS_THRESH, indices);
for (int idx : indices) { cv::Rect box = boxes[idx]; std::string label = CLASSES[classIds[idx]]; float score = confidences[idx];
printf(" -> 检测到: %s (%.2f)\n", label.c_str(), score);
cv::rectangle(img, box, cv::Scalar(0, 255, 0), 2); std::string text = label + ": " + std::to_string(score).substr(0, 4); cv::putText(img, text, cv::Point(box.x, box.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.6, cv::Scalar(0, 255, 0), 2); } }
int main() { printf("--- Dynamic YOLOv8 Demo ---\n");
int model_size = 0; unsigned char* model_data = load_model(MODEL_PATH, &model_size); if (!model_data) return -1;
rknn_context ctx; int ret = rknn_init(&ctx, model_data, model_size, 0, nullptr); if (ret < 0) { printf("rknn_init failed!\n"); return -1; }
rknn_tensor_attr input_attrs[1]; input_attrs[0].index = 0; rknn_query(ctx, RKNN_QUERY_INPUT_ATTR, input_attrs, sizeof(input_attrs));
int model_width = input_attrs[0].dims[2]; int model_height = input_attrs[0].dims[1]; printf("Model Input: %d x %d\n", model_width, model_height);
rknn_tensor_attr output_attrs[1]; output_attrs[0].index = 0; rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, output_attrs, sizeof(output_attrs));
int out_features = output_attrs[0].dims[1]; int out_anchors = output_attrs[0].dims[2];
printf("Model Output: %d features, %d anchors\n", out_features, out_anchors); printf("Detected Classes: %d\n", out_features - 4);
int cam_width = 640; int cam_height = 480; CameraRunner camera(DEVICE_ID, cam_width, cam_height); if (!camera.start()) return -1; std::this_thread::sleep_for(std::chrono::milliseconds(1000));
rknn_input inputs[1]; memset(inputs, 0, sizeof(inputs));
inputs[0].index = 0;
inputs[0].type = RKNN_TENSOR_UINT8;
inputs[0].size = model_width * model_height * 3;
inputs[0].fmt = RKNN_TENSOR_NHWC;
rknn_output outputs[1]; memset(outputs, 0, sizeof(outputs));
outputs[0].want_float = 1;
cv::Mat frame, img_input; long long timestamp_ms;
while (true) { auto start = std::chrono::steady_clock::now();
if (!camera.get_latest_frame(frame, timestamp_ms)) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); continue; }
cv::cvtColor(frame, img_input, cv::COLOR_BGR2RGB); if (frame.cols != model_width || frame.rows != model_height) { cv::resize(img_input, img_input, cv::Size(model_width, model_height)); }
inputs[0].buf = img_input.data; rknn_inputs_set(ctx, 1, inputs);
ret = rknn_run(ctx, nullptr); if(ret < 0) printf("Run error\n");
ret = rknn_outputs_get(ctx, 1, outputs, nullptr);
auto end = std::chrono::steady_clock::now(); double fps = 1000.0 / std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
float scale_w = (float)cam_width / model_width; float scale_h = (float)cam_height / model_height;
post_process((float*)outputs[0].buf, frame, scale_w, scale_h, out_features, out_anchors);
printf("FPS: %.1f\n", fps);
rknn_outputs_release(ctx, 1, outputs); }
camera.stop(); rknn_destroy(ctx); free(model_data); return 0; }
|