From 31d8740bee72794ac7c4f4c5b33a7fc8eb480eb9 Mon Sep 17 00:00:00 2001 From: GuanYuankai Date: Mon, 15 Sep 2025 14:43:57 +0800 Subject: [PATCH] feat: draw fixed rectangle for intrusion detection --- services/intrusion-detection/CMakeLists.txt | 2 +- .../intrusion-detection/include/rkYolov5s.hpp | 32 ++- services/intrusion-detection/src/rkYolov5s.cc | 195 +++++++++++++----- 3 files changed, 175 insertions(+), 54 deletions(-) diff --git a/services/intrusion-detection/CMakeLists.txt b/services/intrusion-detection/CMakeLists.txt index 39ad61d..9c3b107 100644 --- a/services/intrusion-detection/CMakeLists.txt +++ b/services/intrusion-detection/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.4.1) project(IntrusionDetectionService CXX) # 设置 C++ 标准和编译选项 -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_FLAGS "-pthread") diff --git a/services/intrusion-detection/include/rkYolov5s.hpp b/services/intrusion-detection/include/rkYolov5s.hpp index 66ddfb5..e97e50a 100644 --- a/services/intrusion-detection/include/rkYolov5s.hpp +++ b/services/intrusion-detection/include/rkYolov5s.hpp @@ -2,14 +2,30 @@ #define RKYOLOV5S_H #include "rknn_api.h" - #include "opencv2/core/core.hpp" +#include // 用于存储跟踪目标 +#include // 确保线程安全 +#include // 使用 std::string +#include // 使用 std::vector +#include "postprocess.h" +// 前置声明 static void dump_tensor_attr(rknn_tensor_attr *attr); static unsigned char *load_data(FILE *fp, size_t ofst, size_t sz); static unsigned char *load_model(const char *filename, int *model_size); static int saveFloat(const char *file_name, float *output, int element_size); +// 用于跟踪单个目标的结构体 +struct TrackedPerson +{ + int id; // 唯一ID + cv::Rect box; // 当前位置 + double entry_time; // 进入入侵区域的时间戳 (秒) + bool is_in_zone; // 是否在区域内 + bool alarm_triggered; // 是否已触发报警 + int frames_unseen; // 消失的帧数 +}; + class rkYolov5s { private: @@ -29,12 +45,24 @@ private: float nms_threshold, box_conf_threshold; + // 入侵检测和跟踪相关成员 + cv::Rect intrusion_zone; // 入侵区域 + std::map tracked_persons; // 存储所有被跟踪的人 + int next_track_id; // 用于分配新的唯一ID + double intrusion_time_threshold; // 入侵时间阈值 (秒) + + // 跟踪逻辑的私有方法 + void update_tracker(detect_result_group_t &detect_result_group); + public: rkYolov5s(const std::string &model_path); int init(rknn_context *ctx_in, bool isChild); rknn_context *get_pctx(); cv::Mat infer(cv::Mat &ori_img); ~rkYolov5s(); + + // 用于从外部设置入侵区域的公共方法 + void set_intrusion_zone(const cv::Rect& zone); }; -#endif \ No newline at end of file +#endif // RKYOLOV5S_H \ No newline at end of file diff --git a/services/intrusion-detection/src/rkYolov5s.cc b/services/intrusion-detection/src/rkYolov5s.cc index b86b816..33fb796 100644 --- a/services/intrusion-detection/src/rkYolov5s.cc +++ b/services/intrusion-detection/src/rkYolov5s.cc @@ -1,16 +1,33 @@ #include #include -#include "rknn_api.h" +#include // 用于计时 +#include // 使用 std::string +#include // 使用 std::vector +#include // 使用 std::min/max #include "postprocess.h" #include "preprocess.h" +#include "rkYolov5s.hpp" +#include "coreNum.hpp" #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" +#include "rknn_api.h" -#include "coreNum.hpp" -#include "rkYolov5s.hpp" +// 报警接口函数 (目前只打印信息) +void trigger_alarm(int person_id, const cv::Rect& box) { + printf("[ALARM] Intrusion detected! Person ID: %d at location (%d, %d, %d, %d)\n", + person_id, box.x, box.y, box.width, box.height); + // TODO: 在这里实现真正的报警逻辑,例如发送网络消息、写入数据库等。 +} + +// 获取当前时间的函数 (返回秒) +double get_current_time_seconds() { + return std::chrono::duration_cast>( + std::chrono::high_resolution_clock::now().time_since_epoch() + ).count(); +} static void dump_tensor_attr(rknn_tensor_attr *attr) { @@ -19,13 +36,6 @@ static void dump_tensor_attr(rknn_tensor_attr *attr) { shape_str += ", " + std::to_string(attr->dims[i]); } - - // printf(" index=%d, name=%s, n_dims=%d, dims=[%s], n_elems=%d, size=%d, w_stride = %d, size_with_stride=%d, fmt=%s, " - // "type=%s, qnt_type=%s, " - // "zp=%d, scale=%f\n", - // attr->index, attr->name, attr->n_dims, shape_str.c_str(), attr->n_elems, attr->size, attr->w_stride, - // attr->size_with_stride, get_format_string(attr->fmt), get_type_string(attr->type), - // get_qnt_type_string(attr->qnt_type), attr->zp, attr->scale); } static unsigned char *load_data(FILE *fp, size_t ofst, size_t sz) @@ -95,8 +105,19 @@ static int saveFloat(const char *file_name, float *output, int element_size) rkYolov5s::rkYolov5s(const std::string &model_path) { this->model_path = model_path; - nms_threshold = NMS_THRESH; // 默认的NMS阈值 - box_conf_threshold = BOX_THRESH; // 默认的置信度阈值 + nms_threshold = NMS_THRESH; + box_conf_threshold = BOX_THRESH; + + // 初始化跟踪器和入侵检测参数 + next_track_id = 1; + intrusion_time_threshold = 3.0; // 报警时间阈值:3秒 + // 默认设置一个无效的入侵区域,将在第一帧时根据图像大小初始化 + intrusion_zone = cv::Rect(0, 0, 0, 0); +} + +void rkYolov5s::set_intrusion_zone(const cv::Rect& zone) { + std::lock_guard lock(mtx); + this->intrusion_zone = zone; } int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) @@ -104,7 +125,6 @@ int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) printf("Loading model...\n"); int model_data_size = 0; model_data = load_model(model_path.c_str(), &model_data_size); - // 模型参数复用/Model parameter reuse if (share_weight == true) ret = rknn_dup_context(ctx_in, &ctx); else @@ -115,7 +135,6 @@ int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) return -1; } - // 设置模型绑定的核心/Set the core of the model that needs to be bound rknn_core_mask core_mask; switch (get_core_num()) { @@ -145,7 +164,6 @@ int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) } printf("sdk version: %s driver version: %s\n", version.api_version, version.drv_version); - // 获取模型输入输出参数/Obtain the input and output parameters of the model ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num)); if (ret < 0) { @@ -154,7 +172,6 @@ int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) } printf("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output); - // 设置输入参数/Set the input parameters input_attrs = (rknn_tensor_attr *)calloc(io_num.n_input, sizeof(rknn_tensor_attr)); for (int i = 0; i < io_num.n_input; i++) { @@ -168,7 +185,6 @@ int rkYolov5s::init(rknn_context *ctx_in, bool share_weight) dump_tensor_attr(&(input_attrs[i])); } - // 设置输出参数/Set the output parameters output_attrs = (rknn_tensor_attr *)calloc(io_num.n_output, sizeof(rknn_tensor_attr)); for (int i = 0; i < io_num.n_output; i++) { @@ -208,6 +224,94 @@ rknn_context *rkYolov5s::get_pctx() return &ctx; } +void rkYolov5s::update_tracker(detect_result_group_t &detect_result_group) +{ + std::vector current_detections; + for (int i = 0; i < detect_result_group.count; i++) { + detect_result_t *det_result = &(detect_result_group.results[i]); + if (strcmp(det_result->name, "person") == 0) { + current_detections.push_back(cv::Rect( + det_result->box.left, det_result->box.top, + det_result->box.right - det_result->box.left, + det_result->box.bottom - det_result->box.top)); + } + } + + // 1. 对于已有的跟踪目标,增加其未见帧数 + for (auto it = tracked_persons.begin(); it != tracked_persons.end(); ++it) { + it->second.frames_unseen++; + } + + // 2. 将当前帧的检测结果与已有的跟踪目标进行匹配 + for (const auto& det_box : current_detections) { + bool is_matched = false; + int best_match_id = -1; + double max_iou = 0.3; // IoU阈值,用于判断是否为同一目标 + + for (auto const& [id, person] : tracked_persons) { + // 计算交并比 (Intersection over Union) + double iou = (double)(det_box & person.box).area() / (double)(det_box | person.box).area(); + if (iou > max_iou) { + max_iou = iou; + best_match_id = id; + } + } + + if (best_match_id != -1) { + // 匹配成功,更新目标信息 + tracked_persons[best_match_id].box = det_box; + tracked_persons[best_match_id].frames_unseen = 0; + is_matched = true; + } else { + // 匹配失败,创建新的跟踪目标 + TrackedPerson new_person; + new_person.id = next_track_id++; + new_person.box = det_box; + new_person.entry_time = 0; + new_person.is_in_zone = false; + new_person.alarm_triggered = false; + new_person.frames_unseen = 0; + tracked_persons[new_person.id] = new_person; + } + } + + // 3. 处理和更新每个目标的状态 + double current_time = get_current_time_seconds(); + for (auto it = tracked_persons.begin(); it != tracked_persons.end(); ++it) { + TrackedPerson& person = it->second; + // 判断人员包围盒是否与入侵区域有交集 + bool currently_in_zone = (intrusion_zone & person.box).area() > 0; + + if (currently_in_zone) { + if (!person.is_in_zone) { + // 刚进入区域 + person.is_in_zone = true; + person.entry_time = current_time; + } else { + // 已在区域内,检查是否超时 + if (!person.alarm_triggered && (current_time - person.entry_time) > intrusion_time_threshold) { + person.alarm_triggered = true; + trigger_alarm(person.id, person.box); + } + } + } else { + // 不在区域内,重置状态 + person.is_in_zone = false; + person.entry_time = 0; + person.alarm_triggered = false; + } + } + + // 4. 移除消失太久的目标 + for (auto it = tracked_persons.begin(); it != tracked_persons.end(); ) { + if (it->second.frames_unseen > 20) { // 超过20帧未见则移除 + it = tracked_persons.erase(it); + } else { + ++it; + } + } +} + cv::Mat rkYolov5s::infer(cv::Mat &orig_img) { std::lock_guard lock(mtx); @@ -220,14 +324,12 @@ cv::Mat rkYolov5s::infer(cv::Mat &orig_img) memset(&pads, 0, sizeof(BOX_RECT)); cv::Size target_size(width, height); cv::Mat resized_img(target_size.height, target_size.width, CV_8UC3); - // 计算缩放比例/Calculate the scaling ratio + float scale_w = (float)target_size.width / img.cols; float scale_h = (float)target_size.height / img.rows; - // 图像缩放/Image scaling if (img_width != width || img_height != height) { - // rga rga_buffer_t src; rga_buffer_t dst; memset(&src, 0, sizeof(src)); @@ -237,13 +339,6 @@ cv::Mat rkYolov5s::infer(cv::Mat &orig_img) { fprintf(stderr, "resize with rga error\n"); } - /********* - // opencv - float min_scale = std::min(scale_w, scale_h); - scale_w = min_scale; - scale_h = min_scale; - letterbox(img, resized_img, pads, min_scale, target_size); - *********/ inputs[0].buf = resized_img.data; } else @@ -260,11 +355,9 @@ cv::Mat rkYolov5s::infer(cv::Mat &orig_img) outputs[i].want_float = 0; } - // 模型推理/Model inference ret = rknn_run(ctx, NULL); ret = rknn_outputs_get(ctx, io_num.n_output, outputs, NULL); - // 后处理/Post-processing detect_result_group_t detect_result_group; std::vector out_scales; std::vector out_zps; @@ -276,32 +369,32 @@ cv::Mat rkYolov5s::infer(cv::Mat &orig_img) post_process((int8_t *)outputs[0].buf, (int8_t *)outputs[1].buf, (int8_t *)outputs[2].buf, height, width, box_conf_threshold, nms_threshold, pads, scale_w, scale_h, out_zps, out_scales, &detect_result_group); - // 绘制框体/Draw the box - char text[256]; - for (int i = 0; i < detect_result_group.count; i++) - { - detect_result_t *det_result = &(detect_result_group.results[i]); + // 更新跟踪器状态 + // 首次运行时,根据图像尺寸初始化入侵区域 (设定在画面中央) + if (intrusion_zone.width == 0 || intrusion_zone.height == 0) { + intrusion_zone = cv::Rect(orig_img.cols / 4, orig_img.rows / 4, orig_img.cols / 2, orig_img.rows / 2); + } + update_tracker(detect_result_group); + + // 绘制入侵区域 + cv::rectangle(orig_img, intrusion_zone, cv::Scalar(255, 255, 0), 2); // 黄色 + + // 绘制框体和报警状态 + for (auto const& [id, person] : tracked_persons) { + // 根据是否触发报警决定颜色 (BGR: 红色 vs 绿色) + cv::Scalar box_color = person.alarm_triggered ? cv::Scalar(0, 0, 255) : cv::Scalar(0, 255, 0); + int line_thickness = person.alarm_triggered ? 3 : 2; - // ========================== 修改开始 ========================== - // 增加一个判断,只处理标签名称为 "person" 的结果 - if (strcmp(det_result->name, "person") == 0) - { - sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100); - // 打印预测物体的信息/Prints information about the predicted object - // 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); - int x1 = det_result->box.left; - int y1 = det_result->box.top; - int x2 = det_result->box.right; - int y2 = det_result->box.bottom; - rectangle(orig_img, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(256, 0, 0, 256), 3); - putText(orig_img, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255)); + cv::rectangle(orig_img, person.box, box_color, line_thickness); + std::string label = "Person " + std::to_string(id); + if (person.is_in_zone) { + label += " (In Zone)"; } - // ========================== 修改结束 ========================== + cv::putText(orig_img, label, cv::Point(person.box.x, person.box.y - 10), + cv::FONT_HERSHEY_SIMPLEX, 0.5, box_color, 2); } ret = rknn_outputs_release(ctx, io_num.n_output, outputs); - return orig_img; } @@ -318,4 +411,4 @@ rkYolov5s::~rkYolov5s() free(input_attrs); if (output_attrs) free(output_attrs); -} +} \ No newline at end of file