From b9691840cf89f9a51ee14a6d88d6042ef7233669 Mon Sep 17 00:00:00 2001 From: Qinchuanqi <2254943770@qq.com> Date: Tue, 3 Mar 2026 11:25:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=9A=E5=8A=A1=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/checkpoint/checkpoint02_biz.py | 888 +++++++++++++++++++++ biz/supervisionRoom/supervisionRoom_biz.py | 606 ++++++++++++++ biz/trajectory/trajectory01_biz.py | 578 ++++++++++++++ biz/trajectory/trajectory02_biz.py | 562 +++++++++++++ biz/trajectory/trajectory03_biz.py | 562 +++++++++++++ 5 files changed, 3196 insertions(+) create mode 100644 biz/checkpoint/checkpoint02_biz.py create mode 100644 biz/supervisionRoom/supervisionRoom_biz.py create mode 100644 biz/trajectory/trajectory01_biz.py create mode 100644 biz/trajectory/trajectory02_biz.py create mode 100644 biz/trajectory/trajectory03_biz.py diff --git a/biz/checkpoint/checkpoint02_biz.py b/biz/checkpoint/checkpoint02_biz.py new file mode 100644 index 0000000..95d565d --- /dev/null +++ b/biz/checkpoint/checkpoint02_biz.py @@ -0,0 +1,888 @@ +# rtsp_service_kadian.py +# 融合 Kadian_Detect_1221.py + rtsp_service_ws.py +# 支持多路RTSP、抽帧、分段保存MP4、WebSocket推送图像与告警 + +import cv2 +import numpy as np +import time +import threading +import queue + +import base64 + +from typing import Dict, Any, Tuple, List + + +# -------------------------- Kadian 检测相关导入 -------------------------- +from algorithm.checkpoint.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机) +from algorithm.checkpoint.npu_yolo_pose_onnx import YOLOv8_Pose_ONNX # Pose 专用模型 +from yolox.tracker.byte_tracker import BYTETracker + +from utils.logger import get_logger +logger = get_logger(__name__) + +# ========================= 配置区 ========================= +# Kadian 模型路径与ROI(可根据实际情况修改) +DETECT_MODEL_PATH = 'YOLO_Weight/car_opentrunk_person_phone.onnx' +POSE_MODEL_PATH = 'YOLO_Weight/yolov8l-pose.onnx' + + +# 三十家子101警务工作站1 +ROI_RELATIVE=np.array([ + [0.15,0.001], + [0.6,0.001], + [1.0, 0.7], + [1.0,1.0], + [0.35,1.0] +]) + +# 0088 +# ROI_RELATIVE=np.array([ +# [0.03,0.65], +# [0.25,0.60], +# +# [0.30,0.72], +# [0.05,0.87] +# ]) + +# 1008 +# ROI_RELATIVE=np.array([ +# [0.4,0.4], +# [0.58,0.4], +# +# [0.85,1.0], +# [0.55,1.0] +# ]) + +# 2108 +# ROI_RELATIVE=np.array([ +# [0.5,0.25], +# [0.63,0.25], +# +# [0.70,0.48], +# [0.5,0.48] +# ]) + + +# 6782 +# ROI_RELATIVE=np.array([ +# [0.4,0.2], +# [1.0,0.33], +# +# [1.0,0.99], +# [0.32,0.75] +# ]) + +# 新增:告警推送频率限制(秒) +ALERT_PUSH_INTERVAL = 1.0 # 相同action 5秒内仅推送一次 + +# 输入尺寸 +PERSON_CAR_INPUT_SIZE = 640 +POSE_INPUT_SIZE = 640 + +# RTSP 服务配置 +RTSP_TARGET_FPS = 10.0 + + +class KadianDetector: + def __init__(self, roi_points=ROI_RELATIVE): + # 模型加载 + # self.detector = YOLOv8_ONNX(DETECT_MODEL_PATH, conf_threshold=0.25, iou_threshold=0.45,input_size=PERSON_CAR_INPUT_SIZE) + self.detector = YOLOv8_ONNX(DETECT_MODEL_PATH, conf_threshold=0.15, iou_threshold=0.65, + input_size=PERSON_CAR_INPUT_SIZE) + + # self.pose_detector = YOLOv8_Pose_ONNX(POSE_MODEL_PATH, conf_threshold=0.7, iou_threshold=0.6,input_size=POSE_INPUT_SIZE) + self.pose_detector = YOLOv8_Pose_ONNX(POSE_MODEL_PATH, conf_threshold=0.45, iou_threshold=0.6, + input_size=POSE_INPUT_SIZE) + + # Tracker + # class TrackerArgs: + # track_thresh = 0.25 # 必须大于等于yolo的conf_threshold + # track_buffer = 30 + # match_thresh = 0.8 + # mot20 = False + class TrackerArgs: + track_thresh = 0.2 # 必须大于等于yolo的conf_threshold + track_buffer = 60 + match_thresh = 0.9 + mot20 = True + + self.tracker = BYTETracker(TrackerArgs(), frame_rate=RTSP_TARGET_FPS) + + self.track_role = {} + + self.fps = RTSP_TARGET_FPS + + # ROI 处理(支持相对/绝对) + # self.roi_points = roi_points.astype(np.int32) + self.roi_points = np.array(roi_points, dtype=np.float64) if roi_points is not None else None + + # ========================================== + # 超参数设置 (Hyperparameters) + # ========================================== + + # 1. 业务判定时间阈值 + self.TIME_THRESHOLD_ONLY_ONE = 10.0 # 单人单检判定时长 + self.TIME_THRESHOLD_NOBODY = 10.0 # 无人检查判定时长 + + # 后备箱检查判定阈值 + self.TIME_THRESHOLD_TRUNK_OPEN = 0.1 + + # 新增:手机检测判定阈值 + self.TIME_THRESHOLD_PHONE = 3.0 # 手机检测持续1秒(30帧 @30fps) + self.TIME_TOLERANCE_PHONE = 1.5 # 手机丢失缓冲时间(防抖动) + + # 新增:制服检测判定阈值 + self.TIME_THRESHOLD_UNIFORM = 2.0 # 制服不合规判定时长 + self.TIME_TOLERANCE_UNIFORM = 1.0 # 制服合规恢复缓冲时间 + + # 2. Person 丢帧缓冲 + self.TIME_TOLERANCE_PERSON = 3.0 + + # 车辆最小停留时间阈值 (小于此时间视为无人检查/直接通过) + self.TIME_THRESHOLD_CAR_MIN_DURATION = 10.0 + + # 3. Car 丢帧/ID维持缓冲 + self.TIME_TOLERANCE_CAR = 10.0 + + # 4 OnlyOne 丢帧缓冲 + self.TIME_TOLERANCE_ONLY_ONE_DURATION = 3.0 + + # --- 计算对应的帧数阈值 --- + self.frame_thresh_one = int(self.TIME_THRESHOLD_ONLY_ONE * self.fps) + self.frame_thresh_nobody = int(self.TIME_THRESHOLD_NOBODY * self.fps) + self.frame_thresh_trunk_valid = int(self.TIME_THRESHOLD_TRUNK_OPEN * self.fps) + + # 新增:手机检测帧数阈值 + self.frame_thresh_phone = int(self.TIME_THRESHOLD_PHONE * self.fps) + self.frame_buffer_phone = int(self.TIME_TOLERANCE_PHONE * self.fps) + + # 新增:制服检测帧数阈值 + self.frame_thresh_uniform = int(self.TIME_THRESHOLD_UNIFORM * self.fps) + self.frame_buffer_uniform = int(self.TIME_TOLERANCE_UNIFORM * self.fps) + + self.frame_thresh_car_min_duration = int(self.TIME_THRESHOLD_CAR_MIN_DURATION * self.fps) + + self.frame_buffer_limit_person = int(self.TIME_TOLERANCE_PERSON * self.fps) + self.frame_buffer_limit_car = int(self.TIME_TOLERANCE_CAR * self.fps) + self.frame_buffer_limit_onlyOne = int(self.TIME_TOLERANCE_ONLY_ONE_DURATION * self.fps) + + print(f"\n超参数设置:") + print(f" FPS: {self.fps:.2f}") + print(f" 判定 'Only One' / 'Nobody' 需连续: {self.frame_thresh_one} 帧") + print(f" 判定 'Trunk Checked' 需累计检测: {self.frame_thresh_trunk_valid} 帧") + print(f" 判定 'Phone Detected' 需累计检测: {self.frame_thresh_phone} 帧") + print(f" 手机丢失缓冲帧数: {self.frame_buffer_phone} 帧") + print(f" 判定 'Uniform Invalid' 需连续检测: {self.frame_thresh_uniform} 帧") + print(f" 制服合规恢复缓冲帧数: {self.frame_buffer_uniform} 帧") + print(f" 判定 'Too Fast' 最小停留: {self.frame_thresh_car_min_duration} 帧") + + self.onlyone_counter = 0 + # self.onlyone_lost_counter = 0 + # self.onlyone_buffer_limit = self.frame_buffer_limit_person # 10帧(1秒) + self.onlyone_thresh = self.frame_thresh_one # 30帧(3秒) + + self.nobody_counter = 0 + self.nobody_present_counter = 0 + self.nobody_buffer_limit = self.frame_buffer_limit_onlyOne + self.nobody_thresh = self.frame_thresh_nobody # 20帧(2秒) + + self.current_frame_idx = 0 + + self.ignore_show_seconds = 0.5 # 未检测的警告显示时长 + self.openTrunk_show_seconds = 0.5 # 打开后备箱的警告显示时长 + + # 手机检测状态变量(独立于车辆) + self.phone_detection_frames = 0 # 连续检测到手机的帧数 + self.phone_missing_frames = 0 # 连续未检测到手机的帧数 + self.phone_alert_active = False # 手机报警是否激活 + + # 新增:制服检测状态变量 + self.pose_person_count = 0 # 骨骼点模型检测的ROI内人员数量 + self.uniform_alert_active = False # 制服报警是否激活 + self.uniform_detection_frames = 0 # 连续检测到制服不合规的帧数 + self.uniform_recovery_frames = 0 # 连续恢复合规的帧数 + + # 车辆注册表 (字典) + self.roi_car_registry = {} + + # 违规车辆记录 (后备箱未检) + self.unchecked_trunk_alerts = {} + + # 违规车辆记录 (通过过快 -> 归类为 Ignore) + self.fast_pass_alerts = {} + + def _get_roi_points(self, frame_width: int, frame_height: int): + """ + 每帧动态计算正确的 ROI 绝对坐标,并确保类型为 np.int32 + 用于 pointPolygonTest 和 polylines + """ + if self.roi_points is None: + raise ValueError("ROI points must be provided; cannot be None.") + + if self.roi_points.max() <= 1.0: + # 相对坐标 → 转换为绝对 + roi_abs = self.roi_points * np.array([frame_width, frame_height]) + else: + # 绝对坐标,直接使用 + roi_abs = self.roi_points.copy() + + # 强制转为 int32(关键!解决 OpenCV 断言错误) + return roi_abs.astype(np.int32) + + def check_point_in_roi(self, roi_points, point): + return cv2.pointPolygonTest(roi_points, point, False) >= 0 + + def compute_iou(self, boxA, boxB): + # box = [x1, y1, x2, y2] + xA = max(boxA[0], boxB[0]) + yA = max(boxA[1], boxB[1]) + xB = min(boxA[2], boxB[2]) + yB = min(boxA[3], boxB[3]) + + interW = max(0, xB - xA) + interH = max(0, yB - yA) + interArea = interW * interH + + boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1]) + boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1]) + + unionArea = boxAArea + boxBArea - interArea + if unionArea == 0: + return 0.0 + + return interArea / unionArea + + def draw_alert(self, frame, text, color=(0, 0, 255), sub_text=None, offset_y=0): + """在右上角绘制警告文字 (支持垂直偏移,防止文字重叠)""" + font_scale = 1.5 + thickness = 3 + font = cv2.FONT_HERSHEY_SIMPLEX + + (text_w, text_h), _ = cv2.getTextSize(text, font, font_scale, thickness) + x = self.width - text_w - 20 + y = 50 + text_h + offset_y # 增加 Y 轴偏移 + + cv2.rectangle(frame, (x - 10, y - text_h - 10), (x + text_w + 10, y + 10), (0, 0, 0), -1) + cv2.putText(frame, text, (x, y), font, font_scale, color, thickness) + + if sub_text: + cv2.putText(frame, sub_text, (x, y + 40), font, 0.7, (200, 200, 200), 2) + + def is_point_in_box(self, point, box): + px, py = point + x1, y1, x2, y2 = box + return x1 < px < x2 and y1 < py < y2 + + def is_pose_inside_detector_person(self, pose_bbox, dets_xyxy, dets_roles): + """ + 判断一个 pose 人是否位于 detector 的 person 框内部(中心点匹配) + + 参数: + pose_bbox: [x1, y1, x2, y2] + dets_xyxy: detector 输出的所有 bbox 列表 + dets_roles: 对应的类别列表(如 "person", "car"...) + + 返回: + True -> 在某个人体框内部 + False -> 不在任何人体框内部 + """ + + px1, py1, px2, py2 = pose_bbox + cx, cy = (px1 + px2) // 2, (py1 + py2) // 2 + + for box, role in zip(dets_xyxy, dets_roles): + if role != "person": + continue + + dx1, dy1, dx2, dy2 = map(int, box) + + # 判断中心点是否在 detector person 框内 + if dx1 <= cx <= dx2 and dy1 <= cy <= dy2: + return True + + return False + + def count_pose_inside_detector_person(self, pose_results, dets_xyxy, dets_roles): + """ + 统计有多少个pose框在detector person框内部 + + 参数: + pose_results: pose检测结果列表,每个元素为字典,包含'bbox'键,值为[x1, y1, x2, y2] + dets_xyxy: detector输出的所有bbox列表 + dets_roles: 对应的类别列表(如 "person", "car"...) + + 返回: + int: 在detector person框内部的pose框数量 + """ + count = 0 + for pose in pose_results: + pose_bbox = pose['bbox'] # [x1, y1, x2, y2] + if self.is_pose_inside_detector_person(pose_bbox, dets_xyxy, dets_roles): + count += 1 + return count + + def process_frame(self, frame, camera_id: int, timestamp: float) -> Dict[str, Any]: + h, w = frame.shape[:2] + self.width, self.height = w, h + + self.current_frame_idx += 1 + # ========= 每帧动态获取正确的 ROI(int32)========= + roi_points_int32 = self._get_roi_points(w, h) # shape: (4, 2), dtype: int32 + roi_points_draw = roi_points_int32.reshape((-1, 1, 2)) # shape: (4, 1, 2) 用于绘制 + + current_time_sec = timestamp + + pose_results = self.pose_detector(frame) + + # ========= 主检测 ========= + detections = self.detector(frame) + + dets_xyxy = [] + dets_roles = [] + dets_for_tracker = [] + + # ========= 当前帧所有警告列表(关键改动)========== + current_frame_alerts = [] # 每帧清空,重新收集 + + if detections: + for det in detections: + x1, y1, x2, y2, conf, cls_id = det # x1, y1, x2, y2为角点坐标,x1 y1为左上角,x2 y2为右下角 + dets_xyxy.append([x1, y1, x2, y2]) + dets_for_tracker.append([x1, y1, x2, y2, conf]) + if cls_id == 0: + dets_roles.append("car") + + elif cls_id == 1: + dets_roles.append("opentrunk") + + elif cls_id == 2: + dets_roles.append("person") + + elif cls_id == 3: + dets_roles.append("phone") + # print(f'dets_roles: {dets_roles}') + + dets = np.array(dets_for_tracker, dtype=np.float32) if len(dets_for_tracker) else np.empty((0, 5)) + + tracks = self.tracker.update( + dets, + [self.height, self.width], + [self.height, self.width] + ) + # print("tracks: {}".format(tracks)) + # 绘制骨骼 + frame = YOLOv8_Pose_ONNX.draw_keypoints(frame, pose_results) + # ========= 绘制 ROI ========= + cv2.polylines(frame, [roi_points_draw], isClosed=True, color=(255, 0, 0), thickness=3) + + # ========= 单帧统计变量 ========= + current_roi_person_count = 0 + current_roi_trunk_count = 0 + current_roi_phone_count = 0 + + # 临时存储本帧的目标,用于后续关联分析 + current_cars = [] # {'id':, 'box':} + current_trunks = [] # (cx, cy) + + for t in tracks: + # print("t: {}".format(t)) + tid = t.track_id + # cls_id = -1 + + # IoU 匹配角色 + # if tid not in track_role and dets_xyxy: + REVALIDATE_FRAME_INTERVAL = 10 + # if tid not in self.track_role: + if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or (tid not in self.track_role): + best_iou = 0 + best_role = "unknown" + + t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2] + + for i, box in enumerate(dets_xyxy): + iou_val = self.compute_iou(t_box, box) + if iou_val > best_iou: + best_iou = iou_val + best_role = dets_roles[i] + if best_iou > 0.1: + self.track_role[tid] = best_role + else: + self.track_role[tid] = "unknown" + + role = self.track_role.get(tid, "unknown") + cls_id = -1 + if role == "car": + cls_id = 0 + elif role == "opentrunk": + cls_id = 1 + elif role == "person": + cls_id = 2 + elif role == "phone": + cls_id = 3 + # print("tid: {}, role: {}, cls: {}".format(tid, role,cls_id)) + + x1, y1, x2, y2 = map(int, t.tlbr) + + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + + color = None + label = None + + if self.check_point_in_roi(roi_points_int32, (cx, cy)): + if cls_id == 0: # Car + color = (0, 255, 0) + + current_cars.append({'id': tid, 'box': [x1, y1, x2, y2]}) + + if tid not in self.roi_car_registry: + self.roi_car_registry[tid] = { + 'first_seen': self.current_frame_idx, + 'last_seen': self.current_frame_idx, + 'trunk_frames': 0, + 'is_checked': False, + } + else: + self.roi_car_registry[tid]['last_seen'] = self.current_frame_idx + + label = f"Car:{tid} IN" + + elif cls_id == 1: # Opentrunk + current_roi_trunk_count += 1 + color = (255, 165, 0) + current_trunks.append((cx, cy)) + label = "OpenTrunk IN" + + elif cls_id == 2: # Person + current_roi_person_count += 1 + color = (255, 0, 255) + label = "Person IN" + + elif cls_id == 3: # Phone(主模型已支持) + current_roi_phone_count += 1 + color = (0, 0, 139) + + else: + color = (255, 255, 255) + label = "Unknown" + + # label = f"ID:{tid} IN" + + # 特殊显示: 如果这辆车已经合格,框变蓝色 + if cls_id == 0 and tid in self.roi_car_registry and self.roi_car_registry[tid][ + 'is_checked']: + color = (255, 255, 0) # Cyan for checked cars + label += " (Checked)" + else: + color = (0, 0, 255) + label = "OUT" + + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + # ========================================== + # 4. 从骨骼点模型中统计ROI内人员数量 + # ========================================== + self.pose_person_count = 0 + # if pose_results[0].boxes is not None: + # pose_boxes = pose_results[0].boxes + # for box in pose_boxes: + # # 获取人体检测框的中心点 + # x1, y1, x2, y2 = map(int, box.xyxy[0]) + # cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + # + # # 判断中心点是否在ROI内 + # if self.check_point_in_roi((cx, cy)): + # self.pose_person_count += 1 + + if pose_results: + for pose in pose_results: + + x1, y1, x2, y2 = pose['bbox'][0], pose['bbox'][1], pose['bbox'][2], pose['bbox'][3] + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + # 判断中心点是否在ROI内 + if self.check_point_in_roi(roi_points_int32, (cx, cy)): + self.pose_person_count += 1 + + # 统计pose框在detector person框内部的数量 + pose_inside_count = self.count_pose_inside_detector_person(pose_results, dets_xyxy, dets_roles) + + # ========================================== + # 5. 关联分析: 哪个后备箱属于哪辆车? + # ========================================== + for car_info in current_cars: + c_id = car_info['id'] # 车的id + c_box = car_info['box'] # 车的框 + + trunk_found_for_this_car = False # 开后备箱标记 + for t_pt in current_trunks: + if self.is_point_in_box(t_pt, c_box): # 如果开后备箱的框在车的框内,就设置开后备箱标记为true + trunk_found_for_this_car = True + break + + if trunk_found_for_this_car: # 如果当前车辆的开后备箱标记为true了,就设置开了后备箱的帧数+1,凑够了判断“开后备箱”这个动作的帧数之后,就设置该车"已检查" + self.roi_car_registry[c_id]['trunk_frames'] += 1 + if self.roi_car_registry[c_id]['trunk_frames'] >= self.frame_thresh_trunk_valid: + self.roi_car_registry[c_id]['is_checked'] = True + + # ========================================== + # 6. 独立的手机检测逻辑(不与车辆绑定) + # ========================================== + if current_roi_phone_count > 0: + # 检测到手机框 + self.phone_detection_frames += 1 + self.phone_missing_frames = 0 # 重置丢失计数器 + + # 当检测累计达到阈值时,激活报警 + if self.phone_detection_frames >= self.frame_thresh_phone: + self.phone_alert_active = True + else: + # 未检测到手机框 + self.phone_missing_frames += 1 + + # 如果之前检测到手机,重置检测计数器 + if self.phone_detection_frames > 0: + # 只有在连续丢失超过缓冲帧数时才重置 + if self.phone_missing_frames >= self.frame_buffer_phone: + self.phone_detection_frames = 0 + self.phone_alert_active = False + else: + # 从未检测到手机,保持状态 + pass + + # ========================================== + # 7. 制服检测逻辑(比较两个模型的人员数量) + # ========================================== + # 比较骨骼点模型和业务检测模型的人员数量 + uniform_invalid = False + + if self.pose_person_count > current_roi_person_count: + # 骨骼点模型检测到的人员多于业务检测模型 + # 说明有人没穿执勤制服 + uniform_invalid = True + self.uniform_detection_frames += 1 + self.uniform_recovery_frames = 0 # 重置恢复计数器 + + # 当连续检测不合规达到阈值时,激活报警 + if self.uniform_detection_frames >= self.frame_thresh_uniform: + self.uniform_alert_active = True + else: + # 人员数量匹配或业务模型检测更多(理论上不会) + self.uniform_recovery_frames += 1 + + # 如果之前有不合规检测,检查是否需要关闭报警 + if self.uniform_detection_frames > 0: + # 只有在连续合规超过缓冲帧数时才重置 + if self.uniform_recovery_frames >= self.frame_buffer_uniform: + self.uniform_detection_frames = 0 + self.uniform_alert_active = False + else: + # 从未检测到不合规,保持状态 + pass + + # ========================================== + # 8. 维护车辆注册表 & 生成离场报警 + # ========================================== + active_car_ids = [] + cars_to_remove = [] + + for car_id, info in self.roi_car_registry.items(): + # 遍历所有车辆,如果当前帧时间-该车辆最后可见的时间得到的值大于车辆消失时间阈值的话,就把该车添加到移除列表中,否则添加到活跃列表中 + last_seen = info['last_seen'] + + if (self.current_frame_idx - last_seen) <= self.frame_buffer_limit_car: + active_car_ids.append(car_id) + else: + cars_to_remove.append(car_id) + + # 执行删除 并 检查违规 + for car_id in cars_to_remove: + # 遍历所有移除列表中的车辆, + # 如果该车辆最后出现时间-最早出现时间的值小于车辆最小存在时间,则判断为ignore, + # 如果该车辆的“已检查”标记为true,则 + # 最后在所有车辆列表中删除该车辆 + + car_info = self.roi_car_registry[car_id] + + duration_frames = car_info['last_seen'] - car_info['first_seen'] + + # 情况1:通过时间太短 -> 归类为 Ignore (Too Fast) + if duration_frames < self.frame_thresh_car_min_duration: + print(f"ALARM: Car {car_id} passed too fast -> Regarded as Ignore Checked!") + self.fast_pass_alerts[car_id] = self.current_frame_idx + int(self.ignore_show_seconds * self.fps) + + # 情况2:时间够长,但没检查后备箱 -> Unchecked Trunk + elif not car_info['is_checked']: + print(f"ALARM: Car {car_id} left without checking trunk!") + self.unchecked_trunk_alerts[car_id] = self.current_frame_idx + int( + self.openTrunk_show_seconds * self.fps) + + del self.roi_car_registry[car_id] + + effective_car_count = len(active_car_ids) + + # ========================================== + # 9. 业务逻辑判定 (Only One / Nobody) - 重构版 + # ========================================== + if effective_car_count >= 0: # 只要没人就检测,不用等到来了车再检测 + # ----- 定义条件 ----- + onlyone_condition = (pose_inside_count == 1) + nobody_condition = (current_roi_person_count == 0 and self.pose_person_count == 0) + + # ----- Onlyone 计数器更新 ----- + if onlyone_condition: # 如果骨骼点和检测框都检测到了只有一个人时,onlyone+1,当onlyone累计够了之后触发报警 + self.onlyone_counter += 1 + # self.onlyone_lost_counter = 0 + elif current_roi_person_count > 1 or self.pose_person_count > 1: + self.onlyone_counter = 0 + # if self.onlyone_counter > 0: + # self.onlyone_lost_counter += 1 + # if self.onlyone_lost_counter > self.onlyone_buffer_limit: + # self.onlyone_counter = 0 + # self.onlyone_lost_counter = 0 + + # ----- Nobody 计数器更新 ----- + if nobody_condition: + self.nobody_counter += 1 + # self.nobody_present_counter = 0 + elif current_roi_person_count > 0 or self.pose_person_count > 0: + self.nobody_counter = 0 + # if self.nobody_counter > 0: + # self.nobody_present_counter += 1 + # if self.nobody_present_counter > self.nobody_buffer_limit: + # self.nobody_counter = 0 + # self.nobody_present_counter = 0 + + else: + # 无活跃车辆,清零所有计数器 + self.onlyone_counter = 0 + # self.onlyone_lost_counter = 0 + self.nobody_counter = 0 + self.nobody_present_counter = 0 + + # ========================================== + # 10. 显示报警 (UI分层优化) + # ========================================== + + # 更新调试信息,包含所有检测状态 + phone_status = f"Phone: {current_roi_phone_count}" + if self.phone_alert_active: + phone_status += " (ALERT)" + + uniform_status = f"Uniform: Pose={self.pose_person_count}, Model={current_roi_person_count}" + if self.uniform_alert_active: + uniform_status += " (INVALID!)" + + debug_info = f"Cars: {len(active_car_ids)} | Person: {current_roi_person_count} | Trunk: {current_roi_trunk_count} | {phone_status}" + cv2.putText(frame, debug_info, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + cv2.putText(frame, uniform_status, (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + + # 使用 offset 实现报警堆叠,防止遮挡 + alert_offset = 0 + + # A. 显示 Only One(当累积帧数达到阈值时) + if self.onlyone_counter >= self.onlyone_thresh: + current_frame_alerts.append({'time': current_time_sec, 'action': "Only One"}) + self.draw_alert(frame, "Only One", (0, 255, 255), None, offset_y=alert_offset) + alert_offset += 100 + + # B. 显示 Nobody(当累积帧数达到阈值时) + elif self.nobody_counter >= self.nobody_thresh: + current_frame_alerts.append({'time': current_time_sec, 'action': "Nobody"}) + self.draw_alert(frame, "Nobody", (0, 0, 255), None, offset_y=alert_offset) + alert_offset += 100 + + # C. 显示 Trunk Checked (在车辆存活期间) + for car_id in active_car_ids: + if car_id in self.roi_car_registry and self.roi_car_registry[car_id]['is_checked']: + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': "Trunk Checked", + } + ) + self.draw_alert(frame, "Trunk Checked!!", (0, 255, 0), offset_y=alert_offset) + alert_offset += 100 + break # 只显示一次 + + # D. 显示 Playing Phone(独立检测,不与车辆绑定) + if self.phone_alert_active: + # 可以显示检测的持续时间 + duration_seconds = self.phone_detection_frames / self.fps + # sub_text = f"Detected for {duration_seconds:.1f}s" + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': "Playing Phone", + } + ) + self.draw_alert(frame, "Playing Phone", (255, 0, 0), None, offset_y=alert_offset) + alert_offset += 100 + + # E. 新增:显示 Unvaild Uniform!! + if self.uniform_alert_active: + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': "Unvaild Uniform!!", + } + ) + self.draw_alert(frame, "Unvaild Uniform!!", (255, 165, 0), None, offset_y=alert_offset) + alert_offset += 100 + + # 第二层:离场违规 (Post-Event Alerts) + # ------------------------------------------------ + + # F. 显示 Unchecked Trunk + expired_alerts = [cid for cid, end_frame in self.unchecked_trunk_alerts.items() if + self.current_frame_idx > end_frame] + for cid in expired_alerts: + del self.unchecked_trunk_alerts[cid] + + if len(self.unchecked_trunk_alerts) > 0: + alert_text = f"Unchecked Trunk! (ID:{list(self.unchecked_trunk_alerts.keys())})" + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': "Unchecked Trunk", + } + ) + self.draw_alert(frame, alert_text, (0, 0, 255), offset_y=alert_offset) + alert_offset += 100 + + # G. 显示 Ignore (离场结果) + expired_fast_alerts = [cid for cid, end_frame in self.fast_pass_alerts.items() if + self.current_frame_idx > end_frame] + for cid in expired_fast_alerts: + del self.fast_pass_alerts[cid] + + if len(self.fast_pass_alerts) > 0: + alert_text = f"Ignore: (ID:{list(self.fast_pass_alerts.keys())})" + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': "Ignore", + } + ) + self.draw_alert(frame, alert_text, (0, 0, 255), offset_y=alert_offset) + alert_offset += 100 + + return { + "image": frame, + "alerts": current_frame_alerts, + } + + +# ========================= 帧处理线程 ========================= +class FrameProcessorWorker(threading.Thread): + def __init__(self, raw_queue: queue.Queue, ws_queue: queue.Queue, stop_event: threading.Event): + super().__init__(daemon=True) + self.raw_queue = raw_queue + self.ws_queue = ws_queue + self.stop_event = stop_event + + self.last_ts: Dict[int, float] = {} + + # 每个摄像头一个独立的 Kadian 检测器实例 + self.kadian_detectors: Dict[int, KadianDetector] = {} + + self.last_alert_push_time: Dict[int, Dict[str, float]] = {} + + def _encode_base64(self, img): + _, buf = cv2.imencode(".jpg", img) + return base64.b64encode(buf).decode("ascii") + + def run(self): + target_interval = 1.0 / RTSP_TARGET_FPS + while not self.stop_event.is_set(): + try: + item = self.raw_queue.get(timeout=0.5) + except queue.Empty: + continue + + try: + cam_id = item["camera_id"] + ts = item["timestamp"] + frame = item["frame"] + + # 抽帧控制 + if ts - self.last_ts.get(cam_id, 0) < target_interval: + # self.raw_queue.task_done() + continue + self.last_ts[cam_id] = ts + + # 获取检测器实例 + if cam_id not in self.kadian_detectors: + self.kadian_detectors[cam_id] = KadianDetector() + detector = self.kadian_detectors[cam_id] + + # 执行检测 + # detect_start = time.time() + result = detector.process_frame(frame.copy(), cam_id, ts) + # detect_time = (time.time() - detect_start) * 1000 + + result_img = result["image"] + result_type = result["alerts"] + # logger.debug(f"alerts: {result_type}") + + # ========= 核心修改:过滤5秒内重复的action ========= + # 初始化当前摄像头的推送时间记录 + if cam_id not in self.last_alert_push_time: + self.last_alert_push_time[cam_id] = {} + + # 筛选出符合推送条件的action(5秒内未推送过) + push_actions = [] + current_time = time.time() + for alert in result_type: + action = alert['action'] + last_push = self.last_alert_push_time[cam_id].get(action, 0) + # 检查是否超过推送间隔 + if current_time - last_push >= ALERT_PUSH_INTERVAL: + push_actions.append(action) + # 更新该action的最后推送时间 + self.last_alert_push_time[cam_id][action] = current_time + + # 通过 WebSocket 发送帧结果 + # encode_start = time.time() + try: + img_b64 = self._encode_base64(result_img) + except Exception as e: + logger.error(f"[ERROR] Encode image failed: {e}") + img_b64 = None + # encode_time = (time.time() - encode_start) * 1000 + + if img_b64 is not None: + # 将abnormal_actions对象数组转换为字符串数组 + # action_names = [action_info['action'] for action_info in push_actions] + + msg = { + "msg_type": "frame", + "camera_id": 0, + "timestamp": ts, + # "result_type": action_names, + "result_type": push_actions, + "image_base64": img_b64, + } + try: + self.ws_queue.put(msg, timeout=1.0) + # if push_actions and len(push_actions) > 0: + # self.ws_queue_2.put(msg, timeout=1.0) + except queue.Full: + logger.warning("[WARN] ws_send_queue full, drop frame message") + + # # 打印关键操作的耗时 + # total_time = detect_time + encode_time + # logger.info(f"[PERF] Camera {cam_id} - Total: {total_time:.1f}ms | " + # f"Detect: {detect_time:.1f}ms | " + # f"Encode: {encode_time:.1f}ms | ") + + except Exception as e: + logger.error( + f"[ERROR] Frame processing failed for camera {cam_id if 'cam_id' in locals() else 'unknown'}: {e}") + logger.exception("Exception details:") # 打印完整的堆栈跟踪 + # 继续处理下一帧,不要退出循环 + finally: + self.raw_queue.task_done() + + + diff --git a/biz/supervisionRoom/supervisionRoom_biz.py b/biz/supervisionRoom/supervisionRoom_biz.py new file mode 100644 index 0000000..b6b8973 --- /dev/null +++ b/biz/supervisionRoom/supervisionRoom_biz.py @@ -0,0 +1,606 @@ +# rtsp_service_kadian.py +# 融合 Kadian_Detect_1221.py + rtsp_service_ws.py +# 支持多路RTSP、抽帧、分段保存MP4、WebSocket推送图像与告警 + +import cv2 +import numpy as np +import os +import time +import threading +import queue +import yaml +import json +import base64 + +from typing import Dict, Any, Tuple, List + + +# -------------------------- Kadian 检测相关导入 -------------------------- +from algorithm.checkpoint.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机) + +from yolox.tracker.byte_tracker import BYTETracker +from utils.logger import get_logger +logger = get_logger(__name__) + +# ========================= 配置区 ========================= +Person_Phone_Model = r'D:\Python_Save\PoliceProject\Yolo_Weight\person_phone_model.onnx' # 人和手机的检测模型 +Smoke_Model = r'D:\Python_Save\PoliceProject\Yolo_Weight\smoke_model.onnx' # 抽烟检测模型 + +person_phone_input_size = 1280 # 模型输入尺寸,与训练时的模型一致 +smoke_input_size = 1280 # 模型输入尺寸,与训练时的模型一致 + +# RTSP 服务配置 +RTSP_TARGET_FPS = 5.0 + + +# 新增:告警推送频率限制(秒) +ALERT_PUSH_INTERVAL = 5.0 # 相同action 5秒内仅推送一次 + + +class ZhihuishiDetector: + def __init__(self): + # 模型加载 + + # 人和手机检测模型 + print(f"加载人和手机检测模型: {Person_Phone_Model}") + self.person_phone_detector = YOLOv8_ONNX(Person_Phone_Model, conf_threshold=0.6, iou_threshold=0.45, + input_size=person_phone_input_size) + + # 抽烟检测模型 + print(f"加载抽烟检测模型: {Smoke_Model}") + self.smoke_detector = YOLOv8_ONNX(Smoke_Model, conf_threshold=0.4, iou_threshold=0.65, + input_size=smoke_input_size) + + # ByteTracker + class TrackerArgs: + track_thresh = 0.25 + track_buffer = 30 + match_thresh = 0.8 + mot20 = False + + self.fps = RTSP_TARGET_FPS + + self.person_phone_tracker = BYTETracker(TrackerArgs(), frame_rate=self.fps) + self.smoke_tracker = BYTETracker(TrackerArgs(), frame_rate=self.fps) + + self.person_phone_track_role = {} + self.smoke_track_role = {} + + + # ========================================== + # 超参数设置 (Hyperparameters) + # ========================================== + + # 1. 业务判定时间阈值 + self.TIME_THRESHOLD_NOBODY = 2.0 # 无人在场判定时长 + self.TIME_TOLERANCE_NOBODY = 2.0 # 人丢失缓冲时间 + + self.TIME_THRESHOLD_SMOKE = 1.0 # 抽烟判定时长 + self.TIME_TOLERANCE_SMOKE = 0.5 # 烟丢失缓冲时间(防抖动) + + self.TIME_THRESHOLD_PHONE = 1.0 # 玩手机判定时长 + self.TIME_TOLERANCE_PHONE = 0.5 # 手机丢失缓冲时间(防抖动) + + # 无人在场帧数阈值 + self.frame_thresh_nobody = int(self.TIME_THRESHOLD_NOBODY * self.fps) + self.frame_buffer_nobody = int(self.TIME_TOLERANCE_NOBODY * self.fps) + + # 抽烟检测帧数阈值 + self.frame_thresh_smoke = int(self.TIME_THRESHOLD_SMOKE * self.fps) + self.frame_buffer_smoke = int(self.TIME_TOLERANCE_SMOKE * self.fps) + + # 手机检测帧数阈值 + self.frame_thresh_phone = int(self.TIME_THRESHOLD_PHONE * self.fps) + self.frame_buffer_phone = int(self.TIME_TOLERANCE_PHONE * self.fps) + + print(f"\n超参数设置:") + print(f" FPS: {self.fps:.2f}") + print(f" 判定 'Nobody' 需连续: {self.frame_thresh_nobody} 帧") + print(f" 判定 'Smoke Detected' 需累计检测: {self.frame_thresh_smoke} 帧") + print(f" 抽烟丢失缓冲帧数: {self.frame_buffer_smoke} 帧") + print(f" 判定 'Phone Detected' 需累计检测: {self.frame_thresh_phone} 帧") + print(f" 手机丢失缓冲帧数: {self.frame_buffer_phone} 帧") + + # ========================================== + # 状态变量初始化 + # ========================================== + + self.current_frame_idx = 0 + + # 无人在场检测状态变量 + self.nobody_detection_frames = 0 + self.nobody_missing_frames = 0 # 连续未检测到手机的帧数 + self.nobody_alert_active = False # 手机报警是否激活 + + # 手机检测状态变量 + self.phone_detection_frames = 0 # 连续检测到手机的帧数 + self.phone_missing_frames = 0 # 连续未检测到手机的帧数 + self.phone_alert_active = False # 手机报警是否激活 + + # 抽烟检测状态变量 + self.smoke_detection_frames = 0 # 连续检测到手机的帧数 + self.smoke_missing_frames = 0 # 连续未检测到手机的帧数 + self.smoke_alert_active = False # 手机报警是否激活 + + + def compute_iou(self,boxA, boxB): + # box = [x1, y1, x2, y2] + xA = max(boxA[0], boxB[0]) + yA = max(boxA[1], boxB[1]) + xB = min(boxA[2], boxB[2]) + yB = min(boxA[3], boxB[3]) + + interW = max(0, xB - xA) + interH = max(0, yB - yA) + interArea = interW * interH + + boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1]) + boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1]) + + unionArea = boxAArea + boxBArea - interArea + if unionArea == 0: + return 0.0 + + return interArea / unionArea + + def draw_alert(self, frame, text, color=(0, 0, 255), sub_text=None, offset_y=0): + """在右上角绘制警告文字 (支持垂直偏移,防止文字重叠)""" + font_scale = 1.5 + thickness = 3 + font = cv2.FONT_HERSHEY_SIMPLEX + + (text_w, text_h), _ = cv2.getTextSize(text, font, font_scale, thickness) + x = self.width - text_w - 20 + y = 50 + text_h + offset_y # 增加 Y 轴偏移 + + cv2.rectangle(frame, (x - 10, y - text_h - 10), (x + text_w + 10, y + 10), (0, 0, 0), -1) + cv2.putText(frame, text, (x, y), font, font_scale, color, thickness) + + if sub_text: + cv2.putText(frame, sub_text, (x, y + 40), font, 0.7, (200, 200, 200), 2) + + def process_frame(self, frame, camera_id: int, timestamp: float) -> Dict[str, Any]: + h, w = frame.shape[:2] + self.width, self.height = w, h + + self.current_frame_idx += 1 + + current_time_sec = timestamp + + # ========= 人和手机检测 ========= + person_phone_results = self.person_phone_detector(frame) + + # ========= 抽烟检测 ========= + smoke_results = self.smoke_detector(frame) + + person_phone_dets_xyxy = [] + person_phone_dets_roles = [] + person_phone_dets_for_tracker = [] + + smoke_dets_xyxy = [] + smoke_dets_roles = [] + smoke_dets_for_tracker = [] + + # ========= 当前帧所有警告列表(关键改动)========== + current_frame_alerts = [] # 每帧清空,重新收集 + + # 收集 人和手机的检测结果 + if person_phone_results: + for det in person_phone_results: + x1, y1, x2, y2, conf, cls_id = det # x1, y1, x2, y2为角点坐标,x1 y1为左上角,x2 y2为右下角 + person_phone_dets_xyxy.append([x1, y1, x2, y2]) + person_phone_dets_for_tracker.append([x1, y1, x2, y2, conf]) + if cls_id == 0: + person_phone_dets_roles.append("phone") + elif cls_id == 1: + person_phone_dets_roles.append("police") + + person_phone_dets = np.array(person_phone_dets_for_tracker, dtype=np.float32) if len( + person_phone_dets_for_tracker) else np.empty((0, 5)) + + person_phone_tracks = self.person_phone_tracker.update( + person_phone_dets, + [self.height, self.width], + [self.height, self.width] + ) + + # 收集 抽烟的检测结果 + if smoke_results: + for det in smoke_results: + x1, y1, x2, y2, conf, cls_id = det + smoke_dets_xyxy.append([x1, y1, x2, y2]) + smoke_dets_for_tracker.append([x1, y1, x2, y2, conf]) + if cls_id == 0: + smoke_dets_roles.append("smoke") + + smoke_dets = np.array(smoke_dets_for_tracker, dtype=np.float32) if len( + smoke_dets_for_tracker) else np.empty((0, 5)) + + smoke_tracks = self.smoke_tracker.update( + smoke_dets, + [self.height, self.width], + [self.height, self.width] + ) + + # ========= 单帧统计变量 ========= + current_person_count = 0 + current_phone_count = 0 + current_smoke_count = 0 + + # ========= 人和手机检测 ========= + for t in person_phone_tracks: + # print("t: {}".format(t)) + tid = t.track_id + # cls_id = -1 + + # IoU 匹配角色 + # IoU匹配跟踪ID和类别 + REVALIDATE_FRAME_INTERVAL = 10 + if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or (tid not in self.person_phone_track_role): + #if tid not in self.person_phone_track_role: + best_iou = 0 + best_role = "unknown" + + t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2] + + for i, box in enumerate(person_phone_dets_xyxy): + iou_val = self.compute_iou(t_box, box) + if iou_val > best_iou: + best_iou = iou_val + best_role = person_phone_dets_roles[i] + if best_iou > 0.1: + self.person_phone_track_role[tid] = best_role + else: + self.person_phone_track_role[tid] = "unknown" + + role = self.person_phone_track_role.get(tid, "unknown") + cls_id = -1 + if role == "phone": + cls_id = 0 + elif role == "police": + cls_id = 1 + # print("tid: {}, role: {}, cls: {}".format(tid, role,cls_id)) + + x1, y1, x2, y2 = map(int, t.tlbr) + + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + + color = None + label = None + + if cls_id == 0: # Person + current_phone_count += 1 + color = (255, 0, 255) + label = "Phone" + + elif cls_id == 1: # Phone(主模型已支持) + current_person_count += 1 + color = (0, 0, 139) + label = "Person" + + else: + color = (255, 255, 255) + label = "Unknown" + + # label = f"ID:{tid} IN" + + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + # ========= 抽烟检测 ========= + for t in smoke_tracks: + # print("t: {}".format(t)) + tid = t.track_id + # cls_id = -1 + + # IoU 匹配角色 + # IoU匹配跟踪ID和类别 + REVALIDATE_FRAME_INTERVAL = 10 + if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or (tid not in self.smoke_track_role): + #if tid not in self.smoke_track_role: + best_iou = 0 + best_role = "unknown" + + t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2] + + for i, box in enumerate(smoke_dets_xyxy): + iou_val = self.compute_iou(t_box, box) + if iou_val > best_iou: + best_iou = iou_val + best_role = smoke_dets_roles[i] + # self.smoke_track_role[tid] = best_role + if best_iou > 0.1: + self.smoke_track_role[tid] = best_role + else: + self.smoke_track_role[tid] = "unknown" + + role = self.smoke_track_role.get(tid, "unknown") + cls_id = -1 + if role == "smoke": + cls_id = 0 + + x1, y1, x2, y2 = map(int, t.tlbr) + + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + + color = None + label = None + + if cls_id == 0: # 抽烟 + current_smoke_count += 1 + color = (255, 255, 0) + label = "Smoke" + + else: + color = (255, 255, 255) + label = "Unknown" + + # label = f"ID:{tid} IN" + + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + # ========================================== + # 手机检测 + # ========================================== + if current_phone_count > 0: + # 检测到手机框 + self.phone_detection_frames += 1 + self.phone_missing_frames = 0 # 重置丢失计数器 + + # 当检测累计达到阈值时,激活报警 + if self.phone_detection_frames >= self.frame_thresh_phone: + self.phone_alert_active = True + else: + # 未检测到手机框 + self.phone_missing_frames += 1 + + # 如果之前检测到手机,重置检测计数器 + if self.phone_detection_frames > 0: + # 只有在连续丢失超过缓冲帧数时才重置 + if self.phone_missing_frames >= self.frame_buffer_phone: + self.phone_detection_frames = 0 + self.phone_alert_active = False + else: + # 从未检测到手机,保持状态 + pass + + # ========================================== + # 抽烟检测 + # ========================================== + if current_smoke_count > 0: + # 检测到抽烟框 + self.smoke_detection_frames += 1 + self.smoke_missing_frames = 0 # 重置丢失计数器 + + # 当检测累计达到阈值时,激活报警 + if self.smoke_detection_frames >= self.frame_thresh_smoke: + self.smoke_alert_active = True + else: + # 未检测到抽烟框 + self.smoke_missing_frames += 1 + + # 如果之前检测到抽烟,重置检测计数器 + if self.smoke_detection_frames > 0: + # 只有在连续丢失超过缓冲帧数时才重置 + if self.smoke_missing_frames >= self.frame_buffer_smoke: + self.smoke_detection_frames = 0 + self.smoke_alert_active = False + else: + # 从未检测到抽烟,保持状态 + pass + + # ========================================== + # 9. 业务逻辑判定 (Only One / Nobody) + # ========================================== + status_text = "" + + if current_person_count == 0: + self.nobody_detection_frames += 1 + self.nobody_missing_frames = 0 + + if self.nobody_detection_frames >= self.frame_thresh_nobody: + self.nobody_alert_active = True + else: + self.nobody_missing_frames += 1 + + if self.nobody_detection_frames > 0: + if self.nobody_missing_frames >= self.frame_buffer_nobody: + self.nobody_detection_frames = 0 + self.nobody_alert_active = False + else: + pass + + + # if current_person_count == 0: + # self.cnt_frame_nobody += 1 + # else: + # self.cnt_frame_nobody = 0 + + # ========================================== + # 10. 收集并生成结构化警告(核心改动) + # ========================================== + + alert_offset = 0 + + # A. Playing Phone + if self.phone_alert_active: + duration_seconds = self.phone_detection_frames / self.fps + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': 'Playing Phone', + 'confidence': 1.0, # 固定为1.0(规则判定) + 'details': f"Detected for {duration_seconds:.1f}s" + } + ) + + # A. Playing Phone + if self.smoke_alert_active: + duration_seconds = self.smoke_detection_frames / self.fps + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': 'Smoke', + 'confidence': 1.0, # 固定为1.0(规则判定) + 'details': f"Detected for {duration_seconds:.1f}s" + } + ) + + + # D. Nobody Checking + if self.nobody_alert_active: + duration_seconds = self.nobody_detection_frames / self.fps + current_frame_alerts.append({ + 'time': current_time_sec, + 'action': 'Nobody Checking', + 'confidence': 1.0, + 'details': f"Detected for {duration_seconds:.1f}s" + }) + + # ========================================== + # 11. 统一显示当前帧所有警告(可替换原分层显示) + # ========================================== + debug_info = f"Person: {current_person_count} | Phone: {current_phone_count} | Smoke: {current_smoke_count}" + cv2.putText(frame, debug_info, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + + # 统一警告显示区 + alert_y_start = 150 + for i, alert in enumerate(current_frame_alerts): + action = alert['action'] + details = alert.get('details', '') + color = (0, 0, 255) # 默认红色警告 + + if action == 'Nobody Checking': + color = (255, 255, 255) + elif action == 'Smoke': + color = (0, 0, 255) + elif action == 'Playing Phone': + color = (255, 0, 0) + + main_text = action + if details: + main_text += f" ({details})" + + y_pos = alert_y_start + i * 50 + cv2.rectangle(frame, (20, y_pos - 40), (900, y_pos + 10), (0, 0, 0), -1) + cv2.putText(frame, main_text, (30, y_pos), cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2) + + return { + "image": frame, + + "alerts":current_frame_alerts + } + + +# ========================= 帧处理线程 ========================= +class FrameProcessorWorker(threading.Thread): + def __init__(self, raw_queue: queue.Queue, ws_queue: queue.Queue, stop_event: threading.Event): + super().__init__(daemon=True) + self.raw_queue = raw_queue + self.ws_queue = ws_queue + self.stop_event = stop_event + + self.last_ts: Dict[int, float] = {} + + # 每个摄像头一个独立的 Kadian 检测器实例 + self.kadian_detectors: Dict[int, ZhihuishiDetector] = {} + + self.last_alert_push_time: Dict[int, Dict[str, float]] = {} + + def _encode_base64(self, img): + _, buf = cv2.imencode(".jpg", img) + return base64.b64encode(buf).decode("ascii") + + def run(self): + target_interval = 1.0 / RTSP_TARGET_FPS + while not self.stop_event.is_set(): + try: + item = self.raw_queue.get(timeout=0.5) + except queue.Empty: + continue + + try: + cam_id = item["camera_id"] + ts = item["timestamp"] + frame = item["frame"] + + # 抽帧控制 + if ts - self.last_ts.get(cam_id, 0) < target_interval: + # self.raw_queue.task_done() + continue + self.last_ts[cam_id] = ts + + # 获取检测器实例 + if cam_id not in self.kadian_detectors: + self.kadian_detectors[cam_id] = ZhihuishiDetector() + detector = self.kadian_detectors[cam_id] + + # 执行检测 + # detect_start = time.time() + result = detector.process_frame(frame.copy(), cam_id, ts) + # detect_time = (time.time() - detect_start) * 1000 + + result_img = result["image"] + result_type = result["alerts"] + # logger.debug(f"alerts: {result_type}") + + # ========= 核心修改:过滤5秒内重复的action ========= + # 初始化当前摄像头的推送时间记录 + if cam_id not in self.last_alert_push_time: + self.last_alert_push_time[cam_id] = {} + + # 筛选出符合推送条件的action(5秒内未推送过) + push_actions = [] + current_time = time.time() + for alert in result_type: + action = alert['action'] + last_push = self.last_alert_push_time[cam_id].get(action, 0) + # 检查是否超过推送间隔 + if current_time - last_push >= ALERT_PUSH_INTERVAL: + push_actions.append(action) + # 更新该action的最后推送时间 + self.last_alert_push_time[cam_id][action] = current_time + + # 通过 WebSocket 发送帧结果 + # encode_start = time.time() + try: + img_b64 = self._encode_base64(result_img) + except Exception as e: + logger.error(f"[ERROR] Encode image failed: {e}") + img_b64 = None + # encode_time = (time.time() - encode_start) * 1000 + + if img_b64 is not None: + # 将abnormal_actions对象数组转换为字符串数组 + # action_names = [action_info['action'] for action_info in push_actions] + + msg = { + "msg_type": "frame", + "camera_id": 0, + "timestamp": ts, + # "result_type": action_names, + "result_type": push_actions, + "image_base64": img_b64, + } + try: + self.ws_queue.put(msg, timeout=1.0) + # if push_actions and len(push_actions) > 0: + # self.ws_queue_2.put(msg, timeout=1.0) + except queue.Full: + logger.warning("[WARN] ws_send_queue full, drop frame message") + + # # 打印关键操作的耗时 + # total_time = detect_time + encode_time + # logger.info(f"[PERF] Camera {cam_id} - Total: {total_time:.1f}ms | " + # f"Detect: {detect_time:.1f}ms | " + # f"Encode: {encode_time:.1f}ms | ") + + except Exception as e: + logger.error( + f"[ERROR] Frame processing failed for camera {cam_id if 'cam_id' in locals() else 'unknown'}: {e}") + logger.exception("Exception details:") # 打印完整的堆栈跟踪 + # 继续处理下一帧,不要退出循环 + finally: + self.raw_queue.task_done() + diff --git a/biz/trajectory/trajectory01_biz.py b/biz/trajectory/trajectory01_biz.py new file mode 100644 index 0000000..1fed5ff --- /dev/null +++ b/biz/trajectory/trajectory01_biz.py @@ -0,0 +1,578 @@ +# rtsp_service_kadian.py +# 融合 Kadian_Detect_1221.py + rtsp_service_ws.py +# 支持多路RTSP、抽帧、分段保存MP4、WebSocket推送图像与告警 + +import cv2 +import numpy as np +import os +import time +import threading +import queue +import yaml +import json +import base64 +import asyncio +import websockets +from dataclasses import dataclass +from typing import Dict, Any, Tuple, List +from datetime import datetime + +# -------------------------- Kadian 检测相关导入 -------------------------- +from algorithm.checkpoint.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机) +# from rtsp_service_ws_0108 import WS_PORT + +from yolox.tracker.byte_tracker import BYTETracker + +# ========================= 配置区 ========================= +# Kadian 模型路径与ROI(可根据实际情况修改) +detector_model_path = 'YOLO_Weight/prisoner_model.onnx' + +# 输入尺寸 +input_size = 640 + +# RTSP 服务配置 +RTSP_TARGET_FPS = 10.0 + +# 新增:告警推送频率限制(秒) +ALERT_PUSH_INTERVAL = 5.0 # 相同action 5秒内仅推送一次 + +ALERT_PUSH_URL = "http://123.57.151.210:10000/picenter/websocket/test/process" + + + +class TrajectoryDetector: + def __init__(self): + # 模型加载 + self.police_prisoner_detector = YOLOv8_ONNX(detector_model_path, conf_threshold=0.5, iou_threshold=0.45, + input_size=input_size) + + # ByteTracker + class TrackerArgs: + track_thresh = 0.25 + track_buffer = 30 + match_thresh = 0.8 + mot20 = False + + self.police_prisoner_track_role = {} + + self.fps = RTSP_TARGET_FPS + + self.tracker = BYTETracker(TrackerArgs(), frame_rate=self.fps) + + # ========================================== + # 超参数设置 (Hyperparameters) + # ========================================== + self.TIME_THRESHOLD_POLICE = 1.0 # 警察判定时长 + self.TIME_TOLERANCE_POLICE = 0.5 # 警察失缓冲时间(防抖动) + + self.TIME_THRESHOLD_PRISONER = 1.0 # 犯人判定时长 + self.TIME_TOLERANCE_PRISONER = 1.0 # 犯人丢失缓冲时间(防抖动) + + # 警察检测帧数阈值 + self.frame_thresh_police = int(self.TIME_THRESHOLD_POLICE * self.fps) + self.frame_buffer_police = int(self.TIME_TOLERANCE_POLICE * self.fps) + + # 犯人检测帧数阈值 + self.frame_thresh_prisoner = int(self.TIME_THRESHOLD_PRISONER * self.fps) + self.frame_buffer_prisoner = int(self.TIME_TOLERANCE_PRISONER * self.fps) + + print(f"\n超参数设置:") + print(f" FPS: {self.fps:.2f}") + print(f" 判定 'police Detected' 需累计检测: {self.frame_thresh_police} 帧") + print(f" 警察丢失缓冲帧数: {self.frame_buffer_police} 帧") + print(f" 判定 'prisoner Detected' 需累计检测: {self.frame_thresh_prisoner} 帧") + print(f" 犯人丢失缓冲帧数: {self.frame_buffer_prisoner} 帧") + + # ========================================== + # 状态变量初始化 + # ========================================== + self.current_frame_idx = 0 + + # 警察检测状态变量 + self.police_detection_frames = 0 # 连续检测到警察的帧数 + self.police_missing_frames = 0 # 连续未检测到警察的帧数 + self.police_alert_active = False # 警察报警是否激活 + + # 犯人检测状态变量 + self.prisoner_detection_frames = 0 # 连续检测到犯人的帧数 + self.prisoner_missing_frames = 0 # 连续未检测到犯人的帧数 + self.prisoner_alert_active = False # 犯人报警是否激活 + + # ========================= + # 路线 ROI + 状态机初始化 + # ========================= + # ⚠️ 改为相对坐标(0-1区间),按 [x, y] 格式,x/y 范围 0~1 + # 示例:原 (50,100) 在 960x480 分辨率下 → x=50/960≈0.052, y=100/480≈0.208 + self.route_rois = [ + { + "name": "entry", + "polygon_rel": [(0.4, 0.05), (0.6, 0.05), (0.6, 0.35), (0.4, 0.35)] # 相对坐标 + }, + { + "name": "corridor", + "polygon_rel": [(0.4, 0.4), (0.6, 0.4), (0.6, 0.6), (0.4, 0.6)] # 相对坐标 + }, + { + "name": "exit", # finish区域 + "polygon_rel": [(0.55, 0.3), (0.75, 0.3), (0.75, 0.7), (0.55, 0.7)] # 相对坐标 + } + ] + + # 帧尺寸(动态更新) + self.width = 0 + self.height = 0 + + print(f"相对坐标 ROI: {self.route_rois}") + + # 每个犯人(track_id)一套路线状态 + self.prisoner_route_state = {} + + # 新增:记录所有曾经出现过的犯人track_id及其状态 + self.all_prisoner_tracks = {} + # 新增:记录已触发违规的track_id,避免重复告警 + self.violated_tracks = set() + + def _get_abs_polygon(self, rel_polygon): + """将相对坐标(0-1)转换为绝对像素坐标""" + return [ + (int(x * self.width), int(y * self.height)) + for x, y in rel_polygon + ] + + def compute_iou(self, boxA, boxB): + # box = [x1, y1, x2, y2] + xA = max(boxA[0], boxB[0]) + yA = max(boxA[1], boxB[1]) + xB = min(boxA[2], boxB[2]) + yB = min(boxB[3], boxB[3]) + + interW = max(0, xB - xA) + interH = max(0, yB - yA) + interArea = interW * interH + + boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1]) + boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1]) + + unionArea = boxAArea + boxBArea - interArea + if unionArea == 0: + return 0.0 + + return interArea / unionArea + + def draw_alert(self, frame, text, color=(0, 0, 255), sub_text=None, offset_y=0): + """在右上角绘制警告文字 (支持垂直偏移,防止文字重叠)""" + font_scale = 1.5 + thickness = 3 + font = cv2.FONT_HERSHEY_SIMPLEX + + (text_w, text_h), _ = cv2.getTextSize(text, font, font_scale, thickness) + x = self.width - text_w - 20 + y = 50 + text_h + offset_y # 增加 Y 轴偏移 + + cv2.rectangle(frame, (x - 10, y - text_h - 10), (x + text_w + 10, y + 10), (0, 0, 0), -1) + cv2.putText(frame, text, (x, y), font, font_scale, color, thickness) + + if sub_text: + cv2.putText(frame, sub_text, (x, y + 40), font, 0.7, (200, 200, 200), 2) + + def _point_in_polygon(self, point, polygon): + """ + 判断点是否在多边形内 + polygon: 绝对像素坐标的多边形 + """ + return cv2.pointPolygonTest( + np.array(polygon, dtype=np.int32), + point, + False + ) >= 0 + + def _draw_route_rois(self, frame): + """ + 在画面中绘制路线 ROI(动态转换为绝对坐标) + """ + for idx, roi in enumerate(self.route_rois): + # 相对坐标转绝对坐标 + abs_polygon = self._get_abs_polygon(roi["polygon_rel"]) + pts = np.array(abs_polygon, np.int32).reshape((-1, 1, 2)) + + # ROI 边框 + cv2.polylines( + frame, + [pts], + isClosed=True, + color=(0, 255, 255), + thickness=2 + ) + + # 标注名称 + text_pos = abs_polygon[0] + cv2.putText( + frame, + f"{idx + 1}:{roi['name']}", + (text_pos[0], text_pos[1] - 5), + cv2.FONT_HERSHEY_SIMPLEX, + 0.7, + (0, 255, 255), + 2 + ) + + def _update_prisoner_route(self, tid, point, timestamp): + """ + 路线状态机: + 必须按顺序进入 route_rois + """ + # 初始化状态 + if tid not in self.prisoner_route_state: + self.prisoner_route_state[tid] = { + "stage": 0, # 当前应进入的 ROI 索引 + "finished": False, # 是否完成路线 + "violation": False, # 是否违规 + "entered_entry": False, # 是否进入过entry区域 + "last_seen": timestamp # 最后出现时间 + } + # 记录所有犯人track + self.all_prisoner_tracks[tid] = self.prisoner_route_state[tid] + + state = self.prisoner_route_state[tid] + state["last_seen"] = timestamp # 更新最后出现时间 + + # 已完成或已违规,不再处理 + # 已完成或已违规,不再处理并删除该tid的状态 + if state["finished"] or state["violation"]: + # 关键修改:删除当前tid的状态记录 + if tid in self.prisoner_route_state: + del self.prisoner_route_state[tid] + # 可选:同时清理all_prisoner_tracks和已标记的违规/完成记录,避免内存泄漏 + if tid in self.all_prisoner_tracks: + del self.all_prisoner_tracks[tid] + self.violated_tracks.discard(tid) # 移除违规标记 + + return + + current_stage = state["stage"] + + # 所有阶段完成 + if current_stage >= len(self.route_rois): + state["finished"] = True + return + + # 当前应进入的 ROI(转换为绝对坐标) + current_roi_rel = self.route_rois[current_stage]["polygon_rel"] + current_roi_abs = self._get_abs_polygon(current_roi_rel) + + # 是否进入当前 ROI + if self._point_in_polygon(point, current_roi_abs): + # 标记是否进入entry区域(第一个ROI) + if current_stage == 0: + state["entered_entry"] = True + state["stage"] += 1 + + # 如果刚好完成最后一个 ROI (exit/finish) + if state["stage"] == len(self.route_rois): + state["finished"] = True + + def _check_prisoner_violation(self, current_time): + """ + 检查消失的犯人是否违规: + 1. 进入过entry区域 + 2. 未完成整个路线(未进入exit/finish) + 3. 已经消失(超过track buffer时间) + """ + violations = [] + # 遍历所有曾经出现过的犯人track + for tid, state in list(self.all_prisoner_tracks.items()): + # 跳过已完成、已违规或未进入entry的track + if state["finished"] or state["violation"] or not state["entered_entry"]: + continue + + # 检查是否已消失(超过track buffer时间,这里用3秒作为消失判定) + if current_time - state["last_seen"] > 2.0 and tid not in self.violated_tracks: + state["violation"] = True + self.violated_tracks.add(tid) + violations.append({ + 'time': current_time, + 'action': 'violation', + 'confidence': 1.0, + 'details': f"" + }) + return violations + + def process_frame(self, frame, camera_id: int, timestamp: float) -> Dict[str, Any]: + h, w = frame.shape[:2] + self.width, self.height = w, h # 更新帧尺寸 + + self.current_frame_idx += 1 + current_time_sec = timestamp + + # ========= 警察和犯人检测 ========= + police_prisoner_results = self.police_prisoner_detector(frame) + + police_prisoner_dets_xyxy = [] + police_prisoner_dets_roles = [] + police_prisoner_dets_for_tracker = [] + + # ========= 当前帧所有警告列表(关键改动)========== + current_frame_alerts = [] # 每帧清空,重新收集 + + if police_prisoner_results: + for det in police_prisoner_results: + x1, y1, x2, y2, conf, cls_id = det # x1, y1, x2, y2为角点坐标,x1 y1为左上角,x2 y2为右下角 + police_prisoner_dets_xyxy.append([x1, y1, x2, y2]) + police_prisoner_dets_for_tracker.append([x1, y1, x2, y2, conf]) + if cls_id == 0: + police_prisoner_dets_roles.append("police") + elif cls_id == 1: + police_prisoner_dets_roles.append("prisoner") + + ppolice_prisoner_dets = np.array(police_prisoner_dets_for_tracker, dtype=np.float32) if len( + police_prisoner_dets_for_tracker) else np.empty((0, 5)) + + police_prisoner_dets_tracks = self.tracker.update( + ppolice_prisoner_dets, + [self.height, self.width], + [self.height, self.width] + ) + + # 重置当前帧的犯人track标记 + current_frame_prisoner_tids = set() + + # ========= 单帧统计变量 ========= + current_police_count = 0 + current_prisoner_count = 0 + + # ========= 警察和犯人检测 ========= + for t in police_prisoner_dets_tracks: + tid = t.track_id + + # IoU 匹配角色 + REVALIDATE_FRAME_INTERVAL = 10 + if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or ( + tid not in self.police_prisoner_track_role): + best_iou = 0 + best_role = "unknown" + + t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2] + + for i, box in enumerate(police_prisoner_dets_xyxy): + iou_val = self.compute_iou(t_box, box) + if iou_val > best_iou: + best_iou = iou_val + best_role = police_prisoner_dets_roles[i] + if best_iou > 0.1: + self.police_prisoner_track_role[tid] = best_role + else: + self.police_prisoner_track_role[tid] = "unknown" + + role = self.police_prisoner_track_role.get(tid, "unknown") + cls_id = -1 + if role == "police": + cls_id = 0 + elif role == "prisoner": + cls_id = 1 + + x1, y1, x2, y2 = map(int, t.tlbr) + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + + color = None + label = None + + if cls_id == 0: # police + current_police_count += 1 + color = (255, 0, 255) + label = "police" + + elif cls_id == 1: # prisoner + current_prisoner_count += 1 + color = (0, 0, 139) + label = "prisoner" + current_frame_prisoner_tids.add(tid) + # ===== 路线状态机更新 ===== + self._update_prisoner_route( + tid=tid, + point=(cx, cy), + timestamp=current_time_sec + ) + else: + color = (255, 255, 255) + label = "Unknown" + + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + # ========================================== + # 检查犯人违规(进入entry但未到exit就消失) + # ========================================== + violation_alerts = self._check_prisoner_violation(current_time_sec) + current_frame_alerts.extend(violation_alerts) + + # ========================================== + # 犯人检测 + # ========================================== + if current_prisoner_count > 0: + self.prisoner_detection_frames += 1 + self.prisoner_missing_frames = 0 + if self.prisoner_detection_frames >= self.frame_thresh_prisoner: + self.prisoner_alert_active = True + else: + self.prisoner_missing_frames += 1 + if self.prisoner_detection_frames > 0: + if self.prisoner_missing_frames >= self.frame_buffer_prisoner: + self.prisoner_detection_frames = 0 + self.prisoner_alert_active = False + + # ========================================== + # 警察检测 + # ========================================== + if current_police_count > 0: + self.police_detection_frames += 1 + self.police_missing_frames = 0 + if self.police_detection_frames >= self.frame_thresh_police: + self.police_alert_active = True + else: + self.police_missing_frames += 1 + if self.police_detection_frames > 0: + if self.police_missing_frames >= self.frame_buffer_police: + self.police_detection_frames = 0 + self.police_alert_active = False + + alert_offset = 0 + + # A. 有犯人 + if self.prisoner_alert_active: + duration_seconds = self.prisoner_detection_frames / self.fps + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': 'prisoner', + 'confidence': 1.0, + 'details': f"Detected for {duration_seconds:.1f}s" + } + ) + self.draw_alert(frame, "prisoner", (0, 0, 255), offset_y=alert_offset) + alert_offset += 100 + + # B. 路线违规告警 + for tid, state in self.prisoner_route_state.items(): + if state["finished"]: + current_frame_alerts.append({ + "time": current_time_sec, + "action": "finished", + "confidence": 1.0, + "details": "" + }) + #state["finished"] = False + self.draw_alert(frame, "finished", (0, 255, 0), offset_y=alert_offset) + alert_offset += 100 + + # C. 路线违规告警 + for violation in violation_alerts: + self.draw_alert(frame, "ROUTE VIOLATION!", (0, 0, 255), + sub_text=violation['details'], offset_y=alert_offset) + alert_offset += 100 + + # ========================= + # 绘制路线 ROI(始终显示) + # ========================= + self._draw_route_rois(frame) + + return { + "image": frame, + "alerts": current_frame_alerts + } + + +# ========================= 帧处理线程 ========================= +class FrameProcessorWorker(threading.Thread): + def __init__(self, + raw_frame_queue: "queue.Queue[Dict[str, Any]]", + ws_send_queue: "queue.Queue[Dict[str, Any]]", + stop_event: threading.Event): + super().__init__(daemon=True) + self.raw_queue = raw_frame_queue + self.ws_queue = ws_send_queue + self.stop_event = stop_event + + self.last_ts: Dict[int, float] = {} + + # 每个摄像头一个独立的 Kadian 检测器实例 + self.trajectory_detectors: Dict[int, TrajectoryDetector] = {} + + # 新增:维护每个摄像头每个action的最后推送时间 {camera_id: {action: last_push_time}} + self.last_alert_push_time: Dict[int, Dict[str, float]] = {} + + def _encode_image_to_base64(self, image) -> str: + ok, buf = cv2.imencode(".jpg", image) + if not ok: + raise RuntimeError("Failed to encode image to JPEG") + return base64.b64encode(buf.tobytes()).decode("ascii") + + def run(self): + target_interval = 1.0 / RTSP_TARGET_FPS + while not self.stop_event.is_set(): + try: + item = self.raw_queue.get(timeout=0.5) + except queue.Empty: + continue + + cam_id = item["camera_id"] + ts = item["timestamp"] + frame = item["frame"] + + # 抽帧控制 + if ts - self.last_ts.get(cam_id, 0) < target_interval: + self.raw_queue.task_done() + continue + self.last_ts[cam_id] = ts + + # 获取检测器实例 + if cam_id not in self.trajectory_detectors: + self.trajectory_detectors[cam_id] = TrajectoryDetector() + detector = self.trajectory_detectors[cam_id] + + # 执行检测 + result = detector.process_frame(frame.copy(), cam_id, ts) + + result_img = result["image"] + result_type = result["alerts"] + + # ========= 核心修改:过滤5秒内重复的action ========= + # 初始化当前摄像头的推送时间记录 + if cam_id not in self.last_alert_push_time: + self.last_alert_push_time[cam_id] = {} + + # 筛选出符合推送条件的action(5秒内未推送过) + push_actions = [] + current_time = time.time() + for alert in result_type: + action = alert['action'] + last_push = self.last_alert_push_time[cam_id].get(action, 0) + # 检查是否超过推送间隔 + if current_time - last_push >= ALERT_PUSH_INTERVAL: + push_actions.append(action) + # 更新该action的最后推送时间 + self.last_alert_push_time[cam_id][action] = current_time + + # 通过 WebSocket 发送帧结果 + try: + img_b64 = self._encode_image_to_base64(result_img) + except Exception as e: + print(f"[ERROR] Encode image failed: {e}") + img_b64 = None + + if img_b64 is not None: + msg = { + "msg_type": "frame", + "camera_id": 1, + "timestamp": ts, + "result_type": push_actions, + "image_base64": img_b64, + } + try: + self.ws_queue.put(msg, timeout=1.0) + # if push_actions and len(push_actions) > 0: + # self.ws_queue_2.put(msg, timeout=1.0) + except queue.Full: + print("[WARN] ws_send_queue full, drop frame message") + + self.raw_queue.task_done() + diff --git a/biz/trajectory/trajectory02_biz.py b/biz/trajectory/trajectory02_biz.py new file mode 100644 index 0000000..d9ab7b5 --- /dev/null +++ b/biz/trajectory/trajectory02_biz.py @@ -0,0 +1,562 @@ +# rtsp_service_kadian.py +# 融合 Kadian_Detect_1221.py + rtsp_service_ws.py +# 支持多路RTSP、抽帧、分段保存MP4、WebSocket推送图像与告警 +# 修改为单一区域监控:犯人离开指定区域即报警 + +import cv2 +import numpy as np +import os +import time +import threading +import queue +import yaml +import json +import base64 +import asyncio +import websockets +from dataclasses import dataclass +from typing import Dict, Any, Tuple, List +from datetime import datetime + +# -------------------------- Kadian 检测相关导入 -------------------------- +from algorithm.checkpoint.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机) +# from rtsp_service_ws_0108 import WS_PORT + +from yolox.tracker.byte_tracker import BYTETracker + +# ========================= 配置区 ========================= +# Kadian 模型路径与ROI(可根据实际情况修改) +detector_model_path = 'YOLO_Weight/prisoner_model.onnx' + +# 输入尺寸 +input_size = 640 + +# RTSP 服务配置 +RTSP_TARGET_FPS = 10.0 + +# 新增:告警推送频率限制(秒) +ALERT_PUSH_INTERVAL = 5.0 # 相同action 5秒内仅推送一次 + +ALERT_PUSH_URL = "http://123.57.151.210:10000/picenter/websocket/test/process" + + +class TrajectoryDetector: + def __init__(self): + # 模型加载 + self.police_prisoner_detector = YOLOv8_ONNX(detector_model_path, conf_threshold=0.5, iou_threshold=0.45, + input_size=input_size) + + # ByteTracker + class TrackerArgs: + track_thresh = 0.25 + track_buffer = 30 + match_thresh = 0.8 + mot20 = False + + self.police_prisoner_track_role = {} + + self.fps = RTSP_TARGET_FPS + + self.tracker = BYTETracker(TrackerArgs(), frame_rate=self.fps) + + # ========================================== + # 超参数设置 (Hyperparameters) + # ========================================== + self.TIME_THRESHOLD_POLICE = 1.0 # 警察判定时长 + self.TIME_TOLERANCE_POLICE = 0.5 # 警察失缓冲时间(防抖动) + + self.TIME_THRESHOLD_PRISONER = 1.0 # 犯人判定时长 + self.TIME_TOLERANCE_PRISONER = 1.0 # 犯人丢失缓冲时间(防抖动) + + # 警察检测帧数阈值 + self.frame_thresh_police = int(self.TIME_THRESHOLD_POLICE * self.fps) + self.frame_buffer_police = int(self.TIME_TOLERANCE_POLICE * self.fps) + + # 犯人检测帧数阈值 + self.frame_thresh_prisoner = int(self.TIME_THRESHOLD_PRISONER * self.fps) + self.frame_buffer_prisoner = int(self.TIME_TOLERANCE_PRISONER * self.fps) + + print(f"\n超参数设置:") + print(f" FPS: {self.fps:.2f}") + print(f" 判定 'police Detected' 需累计检测: {self.frame_thresh_police} 帧") + print(f" 警察丢失缓冲帧数: {self.frame_buffer_police} 帧") + print(f" 判定 'prisoner Detected' 需累计检测: {self.frame_thresh_prisoner} 帧") + print(f" 犯人丢失缓冲帧数: {self.frame_buffer_prisoner} 帧") + + # ========================================== + # 状态变量初始化 + # ========================================== + self.current_frame_idx = 0 + + # 警察检测状态变量 + self.police_detection_frames = 0 # 连续检测到警察的帧数 + self.police_missing_frames = 0 # 连续未检测到警察的帧数 + self.police_alert_active = False # 警察报警是否激活 + + # 犯人检测状态变量 + self.prisoner_detection_frames = 0 # 连续检测到犯人的帧数 + self.prisoner_missing_frames = 0 # 连续未检测到犯人的帧数 + self.prisoner_alert_active = False # 犯人报警是否激活 + + # ========================= + # 区域 ROI + 状态机初始化(修改为单一区域) + # ========================= + # ⚠️ 改为相对坐标(0-1区间),按 [x, y] 格式,x/y 范围 0~1 + # 示例:原 (50,100) 在 960x480 分辨率下 → x=50/960≈0.052, y=100/480≈0.208 + self.route_rois = [ + { + "name": "zone", # 单一区域,犯人离开即报警 + "polygon_rel": [(0.47, 0.35), (0.5, 0.35), (0.7, 1.0), (0.3, 1.0)] # 相对坐标,可自定义 + } + ] + + # 帧尺寸(动态更新) + self.width = 0 + self.height = 0 + + print(f"相对坐标 ROI: {self.route_rois}") + + # 每个犯人(track_id)一套状态 + self.prisoner_route_state = {} + + # 新增:记录所有曾经出现过的犯人track_id及其状态 + self.all_prisoner_tracks = {} + # 新增:记录已触发违规的track_id,避免重复告警 + self.violated_tracks = set() + + def _get_abs_polygon(self, rel_polygon): + """将相对坐标(0-1)转换为绝对像素坐标""" + return [ + (int(x * self.width), int(y * self.height)) + for x, y in rel_polygon + ] + + def compute_iou(self, boxA, boxB): + # box = [x1, y1, x2, y2] + xA = max(boxA[0], boxB[0]) + yA = max(boxA[1], boxB[1]) + xB = min(boxA[2], boxB[2]) + yB = min(boxB[3], boxB[3]) + + interW = max(0, xB - xA) + interH = max(0, yB - yA) + interArea = interW * interH + + boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1]) + boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1]) + + unionArea = boxAArea + boxBArea - interArea + if unionArea == 0: + return 0.0 + + return interArea / unionArea + + def draw_alert(self, frame, text, color=(0, 0, 255), sub_text=None, offset_y=0): + """在右上角绘制警告文字 (支持垂直偏移,防止文字重叠)""" + font_scale = 1.5 + thickness = 3 + font = cv2.FONT_HERSHEY_SIMPLEX + + (text_w, text_h), _ = cv2.getTextSize(text, font, font_scale, thickness) + x = self.width - text_w - 20 + y = 50 + text_h + offset_y # 增加 Y 轴偏移 + + cv2.rectangle(frame, (x - 10, y - text_h - 10), (x + text_w + 10, y + 10), (0, 0, 0), -1) + cv2.putText(frame, text, (x, y), font, font_scale, color, thickness) + + if sub_text: + cv2.putText(frame, sub_text, (x, y + 40), font, 0.7, (200, 200, 200), 2) + + def _point_in_polygon(self, point, polygon): + """ + 判断点是否在多边形内 + polygon: 绝对像素坐标的多边形 + """ + return cv2.pointPolygonTest( + np.array(polygon, dtype=np.int32), + point, + False + ) >= 0 + + def _draw_route_rois(self, frame): + """ + 在画面中绘制路线 ROI(动态转换为绝对坐标) + """ + for idx, roi in enumerate(self.route_rois): + # 相对坐标转绝对坐标 + abs_polygon = self._get_abs_polygon(roi["polygon_rel"]) + pts = np.array(abs_polygon, np.int32).reshape((-1, 1, 2)) + + # ROI 边框 + cv2.polylines( + frame, + [pts], + isClosed=True, + color=(0, 255, 255), + thickness=2 + ) + + # 标注名称 + text_pos = abs_polygon[0] + cv2.putText( + frame, + f"{idx + 1}:{roi['name']}", + (text_pos[0], text_pos[1] - 5), + cv2.FONT_HERSHEY_SIMPLEX, + 0.7, + (0, 255, 255), + 2 + ) + + def _update_prisoner_route(self, tid, point, timestamp): + """ + 区域监控状态机(修改为单一区域): + 只监控一个区域,如果犯人进入过该区域,后来离开(连续多帧不在区域内或消失),则触发违规。 + """ + # 初始化状态 + if tid not in self.prisoner_route_state: + self.prisoner_route_state[tid] = { + "entered_zone": False, # 是否曾进入区域 + "in_zone": False, # 当前是否在区域内 + "out_frames": 0, # 连续不在区域内的帧数 + "violation": False, # 是否已触发离开违规 + "last_seen": timestamp # 最后出现时间 + } + # 记录所有犯人track + self.all_prisoner_tracks[tid] = self.prisoner_route_state[tid] + + state = self.prisoner_route_state[tid] + state["last_seen"] = timestamp + + # 如果已经触发违规,不再处理(可保留但不重复触发) + if state["violation"]: + return + + # 获取当前唯一区域的多边形(绝对坐标) + current_roi_rel = self.route_rois[0]["polygon_rel"] + current_roi_abs = self._get_abs_polygon(current_roi_rel) + + # 判断点是否在区域内 + in_zone = self._point_in_polygon(point, current_roi_abs) + + if in_zone: + # 在区域内 + state["in_zone"] = True + state["out_frames"] = 0 + if not state["entered_zone"]: + state["entered_zone"] = True + else: + # 不在区域内 + if state["entered_zone"]: + # 曾进入过区域,开始计数离开帧数 + state["out_frames"] += 1 + # 如果离开帧数超过阈值,触发违规 + # 使用 frame_buffer_prisoner 作为离开判定缓冲(可自定义) + if state["out_frames"] >= self.frame_buffer_prisoner: + state["violation"] = True + state["in_zone"] = False + # 如果还未进入区域,忽略 + + def _check_prisoner_violation(self, current_time): + """ + 检查消失的犯人是否违规(离开区域): + 1. 曾进入过区域 + 2. 未触发过违规 + 3. 已经消失(超过track buffer时间) + """ + violations = [] + # 遍历所有曾经出现过的犯人track + for tid, state in list(self.all_prisoner_tracks.items()): + # 跳过已违规或未进入区域的track + if state["violation"] or not state["entered_zone"]: + continue + + # 检查是否已消失(超过track buffer时间,这里用2秒作为消失判定) + if current_time - state["last_seen"] > 2.0 and tid not in self.violated_tracks: + state["violation"] = True + self.violated_tracks.add(tid) + violations.append({ + 'time': current_time, + 'action': 'violation', + 'confidence': 1.0, + 'details': "Prisoner left zone (disappeared)" + }) + return violations + + def process_frame(self, frame, camera_id: int, timestamp: float) -> Dict[str, Any]: + h, w = frame.shape[:2] + self.width, self.height = w, h # 更新帧尺寸 + + self.current_frame_idx += 1 + current_time_sec = timestamp + + # ========= 警察和犯人检测 ========= + police_prisoner_results = self.police_prisoner_detector(frame) + + police_prisoner_dets_xyxy = [] + police_prisoner_dets_roles = [] + police_prisoner_dets_for_tracker = [] + + # ========= 当前帧所有警告列表 ========== + current_frame_alerts = [] # 每帧清空,重新收集 + + if police_prisoner_results: + for det in police_prisoner_results: + x1, y1, x2, y2, conf, cls_id = det # x1, y1, x2, y2为角点坐标,x1 y1为左上角,x2 y2为右下角 + police_prisoner_dets_xyxy.append([x1, y1, x2, y2]) + police_prisoner_dets_for_tracker.append([x1, y1, x2, y2, conf]) + if cls_id == 0: + police_prisoner_dets_roles.append("police") + elif cls_id == 1: + police_prisoner_dets_roles.append("prisoner") + + ppolice_prisoner_dets = np.array(police_prisoner_dets_for_tracker, dtype=np.float32) if len( + police_prisoner_dets_for_tracker) else np.empty((0, 5)) + + police_prisoner_dets_tracks = self.tracker.update( + ppolice_prisoner_dets, + [self.height, self.width], + [self.height, self.width] + ) + + # 重置当前帧的犯人track标记 + current_frame_prisoner_tids = set() + + # ========= 单帧统计变量 ========= + current_police_count = 0 + current_prisoner_count = 0 + + # ========= 警察和犯人检测 ========= + for t in police_prisoner_dets_tracks: + tid = t.track_id + + # IoU 匹配角色 + REVALIDATE_FRAME_INTERVAL = 10 + if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or ( + tid not in self.police_prisoner_track_role): + best_iou = 0 + best_role = "unknown" + + t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2] + + for i, box in enumerate(police_prisoner_dets_xyxy): + iou_val = self.compute_iou(t_box, box) + if iou_val > best_iou: + best_iou = iou_val + best_role = police_prisoner_dets_roles[i] + if best_iou > 0.1: + self.police_prisoner_track_role[tid] = best_role + else: + self.police_prisoner_track_role[tid] = "unknown" + + role = self.police_prisoner_track_role.get(tid, "unknown") + cls_id = -1 + if role == "police": + cls_id = 0 + elif role == "prisoner": + cls_id = 1 + + x1, y1, x2, y2 = map(int, t.tlbr) + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + + color = None + label = None + + if cls_id == 0: # police + current_police_count += 1 + color = (255, 0, 255) + label = "police" + + elif cls_id == 1: # prisoner + current_prisoner_count += 1 + color = (0, 0, 139) + label = "prisoner" + current_frame_prisoner_tids.add(tid) + # ===== 区域状态机更新 ===== + self._update_prisoner_route( + tid=tid, + point=(cx, cy), + timestamp=current_time_sec + ) + else: + color = (255, 255, 255) + label = "Unknown" + + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + # ========================================== + # 检查犯人违规(进入区域后离开) + # ========================================== + violation_alerts = self._check_prisoner_violation(current_time_sec) + + # 遍历所有状态,收集刚刚触发的 violation(那些在更新中被标记但尚未加入 violated_tracks 的) + for tid, state in self.prisoner_route_state.items(): + if state["violation"] and tid not in self.violated_tracks: + self.violated_tracks.add(tid) + violation_alerts.append({ + 'time': current_time_sec, + 'action': 'violation', + 'confidence': 1.0, + 'details': "Prisoner left zone" + }) + + current_frame_alerts.extend(violation_alerts) + + # ========================================== + # 犯人检测 + # ========================================== + if current_prisoner_count > 0: + self.prisoner_detection_frames += 1 + self.prisoner_missing_frames = 0 + if self.prisoner_detection_frames >= self.frame_thresh_prisoner: + self.prisoner_alert_active = True + else: + self.prisoner_missing_frames += 1 + if self.prisoner_detection_frames > 0: + if self.prisoner_missing_frames >= self.frame_buffer_prisoner: + self.prisoner_detection_frames = 0 + self.prisoner_alert_active = False + + # ========================================== + # 警察检测 + # ========================================== + if current_police_count > 0: + self.police_detection_frames += 1 + self.police_missing_frames = 0 + if self.police_detection_frames >= self.frame_thresh_police: + self.police_alert_active = True + else: + self.police_missing_frames += 1 + if self.police_detection_frames > 0: + if self.police_missing_frames >= self.frame_buffer_police: + self.police_detection_frames = 0 + self.police_alert_active = False + + alert_offset = 0 + + # A. 有犯人 + if self.prisoner_alert_active: + duration_seconds = self.prisoner_detection_frames / self.fps + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': 'prisoner', + 'confidence': 1.0, + 'details': f"Detected for {duration_seconds:.1f}s" + } + ) + self.draw_alert(frame, "prisoner", (0, 0, 255), offset_y=alert_offset) + alert_offset += 100 + + # C. 区域违规告警(离开区域) + for violation in violation_alerts: + self.draw_alert(frame, "ZONE VIOLATION!", (0, 0, 255), + sub_text=violation['details'], offset_y=alert_offset) + alert_offset += 100 + + # ========================= + # 绘制区域 ROI(始终显示) + # ========================= + self._draw_route_rois(frame) + + return { + "image": frame, + "alerts": current_frame_alerts + } + + +# ========================= 帧处理线程 ========================= +class FrameProcessorWorker(threading.Thread): + def __init__(self, + raw_frame_queue: "queue.Queue[Dict[str, Any]]", + ws_send_queue: "queue.Queue[Dict[str, Any]]", + stop_event: threading.Event): + super().__init__(daemon=True) + self.raw_queue = raw_frame_queue + self.ws_queue = ws_send_queue + self.stop_event = stop_event + + self.last_ts: Dict[int, float] = {} + + # 每个摄像头一个独立的 Kadian 检测器实例 + self.trajectory_detectors: Dict[int, TrajectoryDetector] = {} + + # 新增:维护每个摄像头每个action的最后推送时间 {camera_id: {action: last_push_time}} + self.last_alert_push_time: Dict[int, Dict[str, float]] = {} + + def _encode_image_to_base64(self, image) -> str: + ok, buf = cv2.imencode(".jpg", image) + if not ok: + raise RuntimeError("Failed to encode image to JPEG") + return base64.b64encode(buf.tobytes()).decode("ascii") + + def run(self): + target_interval = 1.0 / RTSP_TARGET_FPS + while not self.stop_event.is_set(): + try: + item = self.raw_queue.get(timeout=0.5) + except queue.Empty: + continue + + cam_id = item["camera_id"] + ts = item["timestamp"] + frame = item["frame"] + + # 抽帧控制 + if ts - self.last_ts.get(cam_id, 0) < target_interval: + self.raw_queue.task_done() + continue + self.last_ts[cam_id] = ts + + # 获取检测器实例 + if cam_id not in self.trajectory_detectors: + self.trajectory_detectors[cam_id] = TrajectoryDetector() + detector = self.trajectory_detectors[cam_id] + + # 执行检测 + result = detector.process_frame(frame.copy(), cam_id, ts) + + result_img = result["image"] + result_type = result["alerts"] + + # ========= 核心修改:过滤5秒内重复的action ========= + # 初始化当前摄像头的推送时间记录 + if cam_id not in self.last_alert_push_time: + self.last_alert_push_time[cam_id] = {} + + # 筛选出符合推送条件的action(5秒内未推送过) + push_actions = [] + current_time = time.time() + for alert in result_type: + action = alert['action'] + last_push = self.last_alert_push_time[cam_id].get(action, 0) + # 检查是否超过推送间隔 + if current_time - last_push >= ALERT_PUSH_INTERVAL: + push_actions.append(action) + # 更新该action的最后推送时间 + self.last_alert_push_time[cam_id][action] = current_time + + # 通过 WebSocket 发送帧结果 + try: + img_b64 = self._encode_image_to_base64(result_img) + except Exception as e: + print(f"[ERROR] Encode image failed: {e}") + img_b64 = None + + if img_b64 is not None: + msg = { + "msg_type": "frame", + "camera_id": 1, + "timestamp": ts, + "result_type": push_actions, + "image_base64": img_b64, + } + try: + self.ws_queue.put(msg, timeout=1.0) + # if push_actions and len(push_actions) > 0: + # self.ws_queue_2.put(msg, timeout=1.0) + except queue.Full: + print("[WARN] ws_send_queue full, drop frame message") + + self.raw_queue.task_done() \ No newline at end of file diff --git a/biz/trajectory/trajectory03_biz.py b/biz/trajectory/trajectory03_biz.py new file mode 100644 index 0000000..ad8bbf9 --- /dev/null +++ b/biz/trajectory/trajectory03_biz.py @@ -0,0 +1,562 @@ +# rtsp_service_kadian.py +# 融合 Kadian_Detect_1221.py + rtsp_service_ws.py +# 支持多路RTSP、抽帧、分段保存MP4、WebSocket推送图像与告警 +# 修改为单一区域监控:犯人离开指定区域即报警 + +import cv2 +import numpy as np +import os +import time +import threading +import queue +import yaml +import json +import base64 +import asyncio +import websockets +from dataclasses import dataclass +from typing import Dict, Any, Tuple, List +from datetime import datetime + +# -------------------------- Kadian 检测相关导入 -------------------------- +from algorithm.checkpoint.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机) +# from rtsp_service_ws_0108 import WS_PORT + +from yolox.tracker.byte_tracker import BYTETracker + +# ========================= 配置区 ========================= +# Kadian 模型路径与ROI(可根据实际情况修改) +detector_model_path = 'YOLO_Weight/prisoner_model.onnx' + +# 输入尺寸 +input_size = 640 + +# RTSP 服务配置 +RTSP_TARGET_FPS = 10.0 + +# 新增:告警推送频率限制(秒) +ALERT_PUSH_INTERVAL = 5.0 # 相同action 5秒内仅推送一次 + +ALERT_PUSH_URL = "http://123.57.151.210:10000/picenter/websocket/test/process" + + +class TrajectoryDetector: + def __init__(self): + # 模型加载 + self.police_prisoner_detector = YOLOv8_ONNX(detector_model_path, conf_threshold=0.5, iou_threshold=0.45, + input_size=input_size) + + # ByteTracker + class TrackerArgs: + track_thresh = 0.25 + track_buffer = 30 + match_thresh = 0.8 + mot20 = False + + self.police_prisoner_track_role = {} + + self.fps = RTSP_TARGET_FPS + + self.tracker = BYTETracker(TrackerArgs(), frame_rate=self.fps) + + # ========================================== + # 超参数设置 (Hyperparameters) + # ========================================== + self.TIME_THRESHOLD_POLICE = 1.0 # 警察判定时长 + self.TIME_TOLERANCE_POLICE = 0.5 # 警察失缓冲时间(防抖动) + + self.TIME_THRESHOLD_PRISONER = 1.0 # 犯人判定时长 + self.TIME_TOLERANCE_PRISONER = 1.0 # 犯人丢失缓冲时间(防抖动) + + # 警察检测帧数阈值 + self.frame_thresh_police = int(self.TIME_THRESHOLD_POLICE * self.fps) + self.frame_buffer_police = int(self.TIME_TOLERANCE_POLICE * self.fps) + + # 犯人检测帧数阈值 + self.frame_thresh_prisoner = int(self.TIME_THRESHOLD_PRISONER * self.fps) + self.frame_buffer_prisoner = int(self.TIME_TOLERANCE_PRISONER * self.fps) + + print(f"\n超参数设置:") + print(f" FPS: {self.fps:.2f}") + print(f" 判定 'police Detected' 需累计检测: {self.frame_thresh_police} 帧") + print(f" 警察丢失缓冲帧数: {self.frame_buffer_police} 帧") + print(f" 判定 'prisoner Detected' 需累计检测: {self.frame_thresh_prisoner} 帧") + print(f" 犯人丢失缓冲帧数: {self.frame_buffer_prisoner} 帧") + + # ========================================== + # 状态变量初始化 + # ========================================== + self.current_frame_idx = 0 + + # 警察检测状态变量 + self.police_detection_frames = 0 # 连续检测到警察的帧数 + self.police_missing_frames = 0 # 连续未检测到警察的帧数 + self.police_alert_active = False # 警察报警是否激活 + + # 犯人检测状态变量 + self.prisoner_detection_frames = 0 # 连续检测到犯人的帧数 + self.prisoner_missing_frames = 0 # 连续未检测到犯人的帧数 + self.prisoner_alert_active = False # 犯人报警是否激活 + + # ========================= + # 区域 ROI + 状态机初始化(修改为单一区域) + # ========================= + # ⚠️ 改为相对坐标(0-1区间),按 [x, y] 格式,x/y 范围 0~1 + # 示例:原 (50,100) 在 960x480 分辨率下 → x=50/960≈0.052, y=100/480≈0.208 + self.route_rois = [ + { + "name": "zone", # 单一区域,犯人离开即报警 + "polygon_rel": [(0.48, 0.18), (0.54, 0.18), (0.75, 1.0), (0.25, 1.0)] # 相对坐标,可自定义 + } + ] + + # 帧尺寸(动态更新) + self.width = 0 + self.height = 0 + + print(f"相对坐标 ROI: {self.route_rois}") + + # 每个犯人(track_id)一套状态 + self.prisoner_route_state = {} + + # 新增:记录所有曾经出现过的犯人track_id及其状态 + self.all_prisoner_tracks = {} + # 新增:记录已触发违规的track_id,避免重复告警 + self.violated_tracks = set() + + def _get_abs_polygon(self, rel_polygon): + """将相对坐标(0-1)转换为绝对像素坐标""" + return [ + (int(x * self.width), int(y * self.height)) + for x, y in rel_polygon + ] + + def compute_iou(self, boxA, boxB): + # box = [x1, y1, x2, y2] + xA = max(boxA[0], boxB[0]) + yA = max(boxA[1], boxB[1]) + xB = min(boxA[2], boxB[2]) + yB = min(boxB[3], boxB[3]) + + interW = max(0, xB - xA) + interH = max(0, yB - yA) + interArea = interW * interH + + boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1]) + boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1]) + + unionArea = boxAArea + boxBArea - interArea + if unionArea == 0: + return 0.0 + + return interArea / unionArea + + def draw_alert(self, frame, text, color=(0, 0, 255), sub_text=None, offset_y=0): + """在右上角绘制警告文字 (支持垂直偏移,防止文字重叠)""" + font_scale = 1.5 + thickness = 3 + font = cv2.FONT_HERSHEY_SIMPLEX + + (text_w, text_h), _ = cv2.getTextSize(text, font, font_scale, thickness) + x = self.width - text_w - 20 + y = 50 + text_h + offset_y # 增加 Y 轴偏移 + + cv2.rectangle(frame, (x - 10, y - text_h - 10), (x + text_w + 10, y + 10), (0, 0, 0), -1) + cv2.putText(frame, text, (x, y), font, font_scale, color, thickness) + + if sub_text: + cv2.putText(frame, sub_text, (x, y + 40), font, 0.7, (200, 200, 200), 2) + + def _point_in_polygon(self, point, polygon): + """ + 判断点是否在多边形内 + polygon: 绝对像素坐标的多边形 + """ + return cv2.pointPolygonTest( + np.array(polygon, dtype=np.int32), + point, + False + ) >= 0 + + def _draw_route_rois(self, frame): + """ + 在画面中绘制路线 ROI(动态转换为绝对坐标) + """ + for idx, roi in enumerate(self.route_rois): + # 相对坐标转绝对坐标 + abs_polygon = self._get_abs_polygon(roi["polygon_rel"]) + pts = np.array(abs_polygon, np.int32).reshape((-1, 1, 2)) + + # ROI 边框 + cv2.polylines( + frame, + [pts], + isClosed=True, + color=(0, 255, 255), + thickness=2 + ) + + # 标注名称 + text_pos = abs_polygon[0] + cv2.putText( + frame, + f"{idx + 1}:{roi['name']}", + (text_pos[0], text_pos[1] - 5), + cv2.FONT_HERSHEY_SIMPLEX, + 0.7, + (0, 255, 255), + 2 + ) + + def _update_prisoner_route(self, tid, point, timestamp): + """ + 区域监控状态机(修改为单一区域): + 只监控一个区域,如果犯人进入过该区域,后来离开(连续多帧不在区域内或消失),则触发违规。 + """ + # 初始化状态 + if tid not in self.prisoner_route_state: + self.prisoner_route_state[tid] = { + "entered_zone": False, # 是否曾进入区域 + "in_zone": False, # 当前是否在区域内 + "out_frames": 0, # 连续不在区域内的帧数 + "violation": False, # 是否已触发离开违规 + "last_seen": timestamp # 最后出现时间 + } + # 记录所有犯人track + self.all_prisoner_tracks[tid] = self.prisoner_route_state[tid] + + state = self.prisoner_route_state[tid] + state["last_seen"] = timestamp + + # 如果已经触发违规,不再处理(可保留但不重复触发) + if state["violation"]: + return + + # 获取当前唯一区域的多边形(绝对坐标) + current_roi_rel = self.route_rois[0]["polygon_rel"] + current_roi_abs = self._get_abs_polygon(current_roi_rel) + + # 判断点是否在区域内 + in_zone = self._point_in_polygon(point, current_roi_abs) + + if in_zone: + # 在区域内 + state["in_zone"] = True + state["out_frames"] = 0 + if not state["entered_zone"]: + state["entered_zone"] = True + else: + # 不在区域内 + if state["entered_zone"]: + # 曾进入过区域,开始计数离开帧数 + state["out_frames"] += 1 + # 如果离开帧数超过阈值,触发违规 + # 使用 frame_buffer_prisoner 作为离开判定缓冲(可自定义) + if state["out_frames"] >= self.frame_buffer_prisoner: + state["violation"] = True + state["in_zone"] = False + # 如果还未进入区域,忽略 + + def _check_prisoner_violation(self, current_time): + """ + 检查消失的犯人是否违规(离开区域): + 1. 曾进入过区域 + 2. 未触发过违规 + 3. 已经消失(超过track buffer时间) + """ + violations = [] + # 遍历所有曾经出现过的犯人track + for tid, state in list(self.all_prisoner_tracks.items()): + # 跳过已违规或未进入区域的track + if state["violation"] or not state["entered_zone"]: + continue + + # 检查是否已消失(超过track buffer时间,这里用2秒作为消失判定) + if current_time - state["last_seen"] > 2.0 and tid not in self.violated_tracks: + state["violation"] = True + self.violated_tracks.add(tid) + violations.append({ + 'time': current_time, + 'action': 'violation', + 'confidence': 1.0, + 'details': "Prisoner left zone (disappeared)" + }) + return violations + + def process_frame(self, frame, camera_id: int, timestamp: float) -> Dict[str, Any]: + h, w = frame.shape[:2] + self.width, self.height = w, h # 更新帧尺寸 + + self.current_frame_idx += 1 + current_time_sec = timestamp + + # ========= 警察和犯人检测 ========= + police_prisoner_results = self.police_prisoner_detector(frame) + + police_prisoner_dets_xyxy = [] + police_prisoner_dets_roles = [] + police_prisoner_dets_for_tracker = [] + + # ========= 当前帧所有警告列表 ========== + current_frame_alerts = [] # 每帧清空,重新收集 + + if police_prisoner_results: + for det in police_prisoner_results: + x1, y1, x2, y2, conf, cls_id = det # x1, y1, x2, y2为角点坐标,x1 y1为左上角,x2 y2为右下角 + police_prisoner_dets_xyxy.append([x1, y1, x2, y2]) + police_prisoner_dets_for_tracker.append([x1, y1, x2, y2, conf]) + if cls_id == 0: + police_prisoner_dets_roles.append("police") + elif cls_id == 1: + police_prisoner_dets_roles.append("prisoner") + + ppolice_prisoner_dets = np.array(police_prisoner_dets_for_tracker, dtype=np.float32) if len( + police_prisoner_dets_for_tracker) else np.empty((0, 5)) + + police_prisoner_dets_tracks = self.tracker.update( + ppolice_prisoner_dets, + [self.height, self.width], + [self.height, self.width] + ) + + # 重置当前帧的犯人track标记 + current_frame_prisoner_tids = set() + + # ========= 单帧统计变量 ========= + current_police_count = 0 + current_prisoner_count = 0 + + # ========= 警察和犯人检测 ========= + for t in police_prisoner_dets_tracks: + tid = t.track_id + + # IoU 匹配角色 + REVALIDATE_FRAME_INTERVAL = 10 + if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or ( + tid not in self.police_prisoner_track_role): + best_iou = 0 + best_role = "unknown" + + t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2] + + for i, box in enumerate(police_prisoner_dets_xyxy): + iou_val = self.compute_iou(t_box, box) + if iou_val > best_iou: + best_iou = iou_val + best_role = police_prisoner_dets_roles[i] + if best_iou > 0.1: + self.police_prisoner_track_role[tid] = best_role + else: + self.police_prisoner_track_role[tid] = "unknown" + + role = self.police_prisoner_track_role.get(tid, "unknown") + cls_id = -1 + if role == "police": + cls_id = 0 + elif role == "prisoner": + cls_id = 1 + + x1, y1, x2, y2 = map(int, t.tlbr) + cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 + + color = None + label = None + + if cls_id == 0: # police + current_police_count += 1 + color = (255, 0, 255) + label = "police" + + elif cls_id == 1: # prisoner + current_prisoner_count += 1 + color = (0, 0, 139) + label = "prisoner" + current_frame_prisoner_tids.add(tid) + # ===== 区域状态机更新 ===== + self._update_prisoner_route( + tid=tid, + point=(cx, cy), + timestamp=current_time_sec + ) + else: + color = (255, 255, 255) + label = "Unknown" + + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + # ========================================== + # 检查犯人违规(进入区域后离开) + # ========================================== + violation_alerts = self._check_prisoner_violation(current_time_sec) + + # 遍历所有状态,收集刚刚触发的 violation(那些在更新中被标记但尚未加入 violated_tracks 的) + for tid, state in self.prisoner_route_state.items(): + if state["violation"] and tid not in self.violated_tracks: + self.violated_tracks.add(tid) + violation_alerts.append({ + 'time': current_time_sec, + 'action': 'violation', + 'confidence': 1.0, + 'details': "Prisoner left zone" + }) + + current_frame_alerts.extend(violation_alerts) + + # ========================================== + # 犯人检测 + # ========================================== + if current_prisoner_count > 0: + self.prisoner_detection_frames += 1 + self.prisoner_missing_frames = 0 + if self.prisoner_detection_frames >= self.frame_thresh_prisoner: + self.prisoner_alert_active = True + else: + self.prisoner_missing_frames += 1 + if self.prisoner_detection_frames > 0: + if self.prisoner_missing_frames >= self.frame_buffer_prisoner: + self.prisoner_detection_frames = 0 + self.prisoner_alert_active = False + + # ========================================== + # 警察检测 + # ========================================== + if current_police_count > 0: + self.police_detection_frames += 1 + self.police_missing_frames = 0 + if self.police_detection_frames >= self.frame_thresh_police: + self.police_alert_active = True + else: + self.police_missing_frames += 1 + if self.police_detection_frames > 0: + if self.police_missing_frames >= self.frame_buffer_police: + self.police_detection_frames = 0 + self.police_alert_active = False + + alert_offset = 0 + + # A. 有犯人 + if self.prisoner_alert_active: + duration_seconds = self.prisoner_detection_frames / self.fps + current_frame_alerts.append( + { + 'time': current_time_sec, + 'action': 'prisoner', + 'confidence': 1.0, + 'details': f"Detected for {duration_seconds:.1f}s" + } + ) + self.draw_alert(frame, "prisoner", (0, 0, 255), offset_y=alert_offset) + alert_offset += 100 + + # C. 区域违规告警(离开区域) + for violation in violation_alerts: + self.draw_alert(frame, "ZONE VIOLATION!", (0, 0, 255), + sub_text=violation['details'], offset_y=alert_offset) + alert_offset += 100 + + # ========================= + # 绘制区域 ROI(始终显示) + # ========================= + self._draw_route_rois(frame) + + return { + "image": frame, + "alerts": current_frame_alerts + } + + +# ========================= 帧处理线程 ========================= +class FrameProcessorWorker(threading.Thread): + def __init__(self, + raw_frame_queue: "queue.Queue[Dict[str, Any]]", + ws_send_queue: "queue.Queue[Dict[str, Any]]", + stop_event: threading.Event): + super().__init__(daemon=True) + self.raw_queue = raw_frame_queue + self.ws_queue = ws_send_queue + self.stop_event = stop_event + + self.last_ts: Dict[int, float] = {} + + # 每个摄像头一个独立的 Kadian 检测器实例 + self.trajectory_detectors: Dict[int, TrajectoryDetector] = {} + + # 新增:维护每个摄像头每个action的最后推送时间 {camera_id: {action: last_push_time}} + self.last_alert_push_time: Dict[int, Dict[str, float]] = {} + + def _encode_image_to_base64(self, image) -> str: + ok, buf = cv2.imencode(".jpg", image) + if not ok: + raise RuntimeError("Failed to encode image to JPEG") + return base64.b64encode(buf.tobytes()).decode("ascii") + + def run(self): + target_interval = 1.0 / RTSP_TARGET_FPS + while not self.stop_event.is_set(): + try: + item = self.raw_queue.get(timeout=0.5) + except queue.Empty: + continue + + cam_id = item["camera_id"] + ts = item["timestamp"] + frame = item["frame"] + + # 抽帧控制 + if ts - self.last_ts.get(cam_id, 0) < target_interval: + self.raw_queue.task_done() + continue + self.last_ts[cam_id] = ts + + # 获取检测器实例 + if cam_id not in self.trajectory_detectors: + self.trajectory_detectors[cam_id] = TrajectoryDetector() + detector = self.trajectory_detectors[cam_id] + + # 执行检测 + result = detector.process_frame(frame.copy(), cam_id, ts) + + result_img = result["image"] + result_type = result["alerts"] + + # ========= 核心修改:过滤5秒内重复的action ========= + # 初始化当前摄像头的推送时间记录 + if cam_id not in self.last_alert_push_time: + self.last_alert_push_time[cam_id] = {} + + # 筛选出符合推送条件的action(5秒内未推送过) + push_actions = [] + current_time = time.time() + for alert in result_type: + action = alert['action'] + last_push = self.last_alert_push_time[cam_id].get(action, 0) + # 检查是否超过推送间隔 + if current_time - last_push >= ALERT_PUSH_INTERVAL: + push_actions.append(action) + # 更新该action的最后推送时间 + self.last_alert_push_time[cam_id][action] = current_time + + # 通过 WebSocket 发送帧结果 + try: + img_b64 = self._encode_image_to_base64(result_img) + except Exception as e: + print(f"[ERROR] Encode image failed: {e}") + img_b64 = None + + if img_b64 is not None: + msg = { + "msg_type": "frame", + "camera_id": 1, + "timestamp": ts, + "result_type": push_actions, + "image_base64": img_b64, + } + try: + self.ws_queue.put(msg, timeout=1.0) + # if push_actions and len(push_actions) > 0: + # self.ws_queue_2.put(msg, timeout=1.0) + except queue.Full: + print("[WARN] ws_send_queue full, drop frame message") + + self.raw_queue.task_done() \ No newline at end of file