完成ab门迁移
This commit is contained in:
@@ -6,6 +6,7 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import queue
|
import queue
|
||||||
import requests
|
import requests
|
||||||
|
from biz.base_frame_processor import BaseFrameProcessorWorker
|
||||||
|
|
||||||
# -------------------------- Kadian 检测相关导入 --------------------------
|
# -------------------------- Kadian 检测相关导入 --------------------------
|
||||||
from algorithm.common.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机)
|
from algorithm.common.npu_yolo_onnx_person_car_phone import YOLOv8_ONNX # 主检测模型(人/车/后备箱/手机)
|
||||||
@@ -16,10 +17,10 @@ from yolox.tracker.byte_tracker import BYTETracker
|
|||||||
|
|
||||||
# ========================= 配置区 =========================
|
# ========================= 配置区 =========================
|
||||||
# Kadian 模型路径与ROI(可根据实际情况修改)
|
# Kadian 模型路径与ROI(可根据实际情况修改)
|
||||||
detector_model_path = 'YOLO_Weight/prisoner_model.onnx'
|
detector_model_path = 'YOLO_Weight/bag_model.onnx'
|
||||||
|
|
||||||
# 输入尺寸
|
# 输入尺寸
|
||||||
input_size = 1280
|
input_size = 640
|
||||||
|
|
||||||
RTSP_TARGET_FPS = 10.0
|
RTSP_TARGET_FPS = 10.0
|
||||||
|
|
||||||
@@ -27,12 +28,15 @@ RTSP_TARGET_FPS = 10.0
|
|||||||
ALERT_PUSH_INTERVAL = 5.0 # 相同action 5秒内仅推送一次
|
ALERT_PUSH_INTERVAL = 5.0 # 相同action 5秒内仅推送一次
|
||||||
|
|
||||||
|
|
||||||
class ZoulangDetector:
|
class AbDetector:
|
||||||
def __init__(self):
|
def __init__(self, params=None):
|
||||||
|
# 摄像头额外参数
|
||||||
|
self.params = params if params is not None else {}
|
||||||
|
|
||||||
# 模型加载
|
# 模型加载
|
||||||
|
|
||||||
self.police_prisoner_detector = YOLOv8_ONNX(detector_model_path, conf_threshold=0.5, iou_threshold=0.45,
|
self.detector = YOLOv8_ONNX(detector_model_path, conf_threshold=0.5, iou_threshold=0.45,
|
||||||
input_size=input_size)
|
input_size=input_size)
|
||||||
|
|
||||||
# ByteTracker
|
# ByteTracker
|
||||||
class TrackerArgs:
|
class TrackerArgs:
|
||||||
@@ -41,9 +45,7 @@ class ZoulangDetector:
|
|||||||
match_thresh = 0.8
|
match_thresh = 0.8
|
||||||
mot20 = False
|
mot20 = False
|
||||||
|
|
||||||
|
self.track_role = {}
|
||||||
|
|
||||||
self.police_prisoner_track_role = {}
|
|
||||||
|
|
||||||
self.fps = RTSP_TARGET_FPS
|
self.fps = RTSP_TARGET_FPS
|
||||||
|
|
||||||
@@ -52,53 +54,30 @@ class ZoulangDetector:
|
|||||||
# ==========================================
|
# ==========================================
|
||||||
# 超参数设置 (Hyperparameters)
|
# 超参数设置 (Hyperparameters)
|
||||||
# ==========================================
|
# ==========================================
|
||||||
|
self.TIME_THRESHOLD_BLACKBAG = 1.0 # 黑包判定时长(秒)
|
||||||
|
self.TIME_TOLERANCE_BLACKBAG = 0.5 # 黑包丢失缓冲时间
|
||||||
|
|
||||||
# 1. 业务判定时间阈值
|
# 转换为帧数阈值
|
||||||
# self.TIME_THRESHOLD_NOBODY = 2.0 # 无人在场判定时长
|
self.frame_thresh_blackbag = int(self.TIME_THRESHOLD_BLACKBAG * self.fps)
|
||||||
|
self.frame_buffer_blackbag = int(self.TIME_TOLERANCE_BLACKBAG * self.fps)
|
||||||
self.TIME_THRESHOLD_POLICE = 1.0 # 警察判定时长
|
|
||||||
self.TIME_TOLERANCE_POLICE = 0.5 # 警察失缓冲时间(防抖动)
|
|
||||||
|
|
||||||
self.TIME_THRESHOLD_PRISONER = 1.0 # 犯人判定时长
|
|
||||||
self.TIME_TOLERANCE_PRISONER = 0.5 # 犯人丢失缓冲时间(防抖动)
|
|
||||||
|
|
||||||
# 无人在场帧数阈值
|
|
||||||
# self.frame_thresh_nobody = int(self.TIME_THRESHOLD_NOBODY * self.fps)
|
|
||||||
|
|
||||||
# 警察检测帧数阈值
|
|
||||||
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"\n超参数设置:")
|
||||||
print(f" FPS: {self.fps:.2f}")
|
print(f" FPS: {self.fps:.2f}")
|
||||||
# print(f" 判定 'Nobody' 需连续: {self.frame_thresh_nobody} 帧")
|
print(f" 判定 'BlackBag Detected' 需累计检测: {self.frame_thresh_blackbag} 帧")
|
||||||
print(f" 判定 'police Detected' 需累计检测: {self.frame_thresh_police} 帧")
|
print(f" 黑包丢失缓冲帧数: {self.frame_buffer_blackbag} 帧")
|
||||||
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.current_frame_idx = 0
|
||||||
|
|
||||||
# 无人在场检测状态变量
|
# 黑包检测状态
|
||||||
self.cnt_frame_nobody = 0
|
self.blackbag_detection_frames = 0
|
||||||
|
self.blackbag_missing_frames = 0
|
||||||
|
self.blackbag_alert_active = False
|
||||||
|
|
||||||
# 警察检测状态变量
|
# 人员统计变量
|
||||||
self.police_detection_frames = 0 # 连续检测到警察的帧数
|
self.current_person_count = 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 # 犯人报警是否激活
|
|
||||||
|
|
||||||
|
|
||||||
def compute_iou(self,boxA, boxB):
|
def compute_iou(self,boxA, boxB):
|
||||||
@@ -145,184 +124,120 @@ class ZoulangDetector:
|
|||||||
|
|
||||||
current_time_sec = timestamp
|
current_time_sec = timestamp
|
||||||
|
|
||||||
# ========= 警察和犯人检测 =========
|
# ========= 检测推理(黑包+人)=========
|
||||||
police_prisoner_results = self.police_prisoner_detector(frame)
|
detect_results = self.detector(frame)
|
||||||
|
|
||||||
police_prisoner_dets_xyxy = []
|
# 初始化检测结果存储
|
||||||
police_prisoner_dets_roles = []
|
dets_xyxy = []
|
||||||
police_prisoner_dets_for_tracker = []
|
dets_roles = []
|
||||||
|
dets_for_tracker = []
|
||||||
|
current_frame_alerts = []
|
||||||
|
|
||||||
# ========= 当前帧所有警告列表(关键改动)==========
|
# 解析检测结果(黑包cls_id=0,人员cls_id=1)
|
||||||
current_frame_alerts = [] # 每帧清空,重新收集
|
if detect_results:
|
||||||
|
for det in detect_results:
|
||||||
if police_prisoner_results:
|
x1, y1, x2, y2, conf, cls_id = det
|
||||||
for det in police_prisoner_results:
|
dets_xyxy.append([x1, y1, x2, y2])
|
||||||
x1, y1, x2, y2, conf, cls_id = det # x1, y1, x2, y2为角点坐标,x1 y1为左上角,x2 y2为右下角
|
dets_for_tracker.append([x1, y1, x2, y2, conf])
|
||||||
police_prisoner_dets_xyxy.append([x1, y1, x2, y2])
|
|
||||||
police_prisoner_dets_for_tracker.append([x1, y1, x2, y2, conf])
|
|
||||||
if cls_id == 0:
|
if cls_id == 0:
|
||||||
police_prisoner_dets_roles.append("police")
|
dets_roles.append("black_bag")
|
||||||
elif cls_id == 1:
|
elif cls_id == 1:
|
||||||
police_prisoner_dets_roles.append("prisoner")
|
dets_roles.append("person")
|
||||||
|
|
||||||
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))
|
dets = np.array(dets_for_tracker, dtype=np.float32) if len(dets_for_tracker) else np.empty((0, 5))
|
||||||
|
tracks = self.tracker.update(
|
||||||
police_prisoner_dets_tracks = self.tracker.update(
|
dets,
|
||||||
ppolice_prisoner_dets,
|
|
||||||
[self.height, self.width],
|
[self.height, self.width],
|
||||||
[self.height, self.width]
|
[self.height, self.width]
|
||||||
)
|
)
|
||||||
# ========= 单帧统计变量 =========
|
|
||||||
current_police_count = 0
|
|
||||||
current_prisoner_count = 0
|
|
||||||
|
|
||||||
# ========= 警察和犯人检测 =========
|
# ========= 单帧统计初始化 =========
|
||||||
for t in police_prisoner_dets_tracks:
|
self.current_person_count = 0
|
||||||
# print("t: {}".format(t))
|
current_blackbag_count = 0
|
||||||
|
|
||||||
|
# ========= 跟踪结果绘制与统计 =========
|
||||||
|
for t in tracks:
|
||||||
tid = t.track_id
|
tid = t.track_id
|
||||||
# cls_id = -1
|
|
||||||
|
|
||||||
# IoU 匹配角色
|
# IoU匹配跟踪ID和类别
|
||||||
|
REVALIDATE_FRAME_INTERVAL = 10
|
||||||
|
|
||||||
REVALIDATE_FRAME_INTERVAL = 10
|
|
||||||
if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or (
|
#if tid not in self.track_role:
|
||||||
tid not in self.police_prisoner_track_role):
|
if (self.current_frame_idx % REVALIDATE_FRAME_INTERVAL == 0) or (tid not in self.track_role):
|
||||||
best_iou = 0
|
best_iou = 0
|
||||||
best_role = "unknown"
|
best_role = "unknown"
|
||||||
|
t_box = list(map(float, t.tlbr))
|
||||||
t_box = list(map(float, t.tlbr)) # [x1,y1,x2,y2]
|
for i, box in enumerate(dets_xyxy):
|
||||||
|
|
||||||
for i, box in enumerate(police_prisoner_dets_xyxy):
|
|
||||||
iou_val = self.compute_iou(t_box, box)
|
iou_val = self.compute_iou(t_box, box)
|
||||||
if iou_val > best_iou:
|
if iou_val > best_iou:
|
||||||
best_iou = iou_val
|
best_iou = iou_val
|
||||||
best_role = police_prisoner_dets_roles[i]
|
best_role = dets_roles[i]
|
||||||
if best_iou > 0.1:
|
self.track_role[tid] = best_role if best_iou > 0.1 else "unknown"
|
||||||
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
|
|
||||||
# print("tid: {}, role: {}, cls: {}".format(tid, role,cls_id))
|
|
||||||
|
|
||||||
|
role = self.track_role.get(tid, "unknown")
|
||||||
x1, y1, x2, y2 = map(int, t.tlbr)
|
x1, y1, x2, y2 = map(int, t.tlbr)
|
||||||
|
color = (255, 255, 255)
|
||||||
|
label = "Unknown"
|
||||||
|
|
||||||
cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
|
# 人员检测(cls_id=1)
|
||||||
|
if role == "person":
|
||||||
color = None
|
self.current_person_count += 1
|
||||||
label = None
|
color = (255, 0, 255) # 紫色框
|
||||||
|
label = "Person"
|
||||||
if cls_id == 0: # Person
|
# 黑包检测(cls_id=0)
|
||||||
current_police_count += 1
|
elif role == "black_bag":
|
||||||
color = (255, 0, 255)
|
current_blackbag_count += 1
|
||||||
label = "police"
|
color = (0, 128, 0) # 绿色框
|
||||||
|
label = "Black Bag"
|
||||||
elif cls_id == 1: # Phone(主模型已支持)
|
|
||||||
current_prisoner_count += 1
|
|
||||||
color = (0, 0, 139)
|
|
||||||
label = "prisoner"
|
|
||||||
else:
|
|
||||||
color = (255, 255, 255)
|
|
||||||
label = "Unknown"
|
|
||||||
|
|
||||||
# label = f"ID:{tid} IN"
|
|
||||||
|
|
||||||
|
# 绘制检测框和标签
|
||||||
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
||||||
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# 犯人检测
|
# 黑包状态更新
|
||||||
# ==========================================
|
# ==========================================
|
||||||
if current_prisoner_count > 0:
|
if current_blackbag_count > 0:
|
||||||
# 检测到犯人框
|
self.blackbag_detection_frames += 1
|
||||||
self.prisoner_detection_frames += 1
|
self.blackbag_missing_frames = 0
|
||||||
self.prisoner_missing_frames = 0 # 重置丢失计数器
|
if self.blackbag_detection_frames >= self.frame_thresh_blackbag:
|
||||||
|
self.blackbag_alert_active = True
|
||||||
# 当检测累计达到阈值时,激活报警
|
|
||||||
if self.prisoner_detection_frames >= self.frame_thresh_prisoner:
|
|
||||||
self.prisoner_alert_active = True
|
|
||||||
else:
|
else:
|
||||||
# 未检测到犯人框
|
self.blackbag_missing_frames += 1
|
||||||
self.prisoner_missing_frames += 1
|
if self.blackbag_missing_frames >= self.frame_buffer_blackbag:
|
||||||
|
self.blackbag_detection_frames = 0
|
||||||
|
self.blackbag_alert_active = False
|
||||||
|
|
||||||
# 如果之前检测到手机,重置检测计数器
|
|
||||||
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
|
|
||||||
else:
|
|
||||||
# 从未检测到犯人,保持状态
|
|
||||||
pass
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# 警察检测
|
# 警告信息收集
|
||||||
# ==========================================
|
# ==========================================
|
||||||
if current_police_count > 0:
|
if self.blackbag_alert_active:
|
||||||
# 检测到犯人框
|
duration_seconds = self.blackbag_detection_frames / self.fps
|
||||||
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
|
|
||||||
else:
|
|
||||||
# 从未检测到犯人,保持状态
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
alert_offset = 0
|
|
||||||
|
|
||||||
# A. 有犯人
|
|
||||||
if self.prisoner_alert_active:
|
|
||||||
duration_seconds = self.prisoner_detection_frames / self.fps
|
|
||||||
current_frame_alerts.append(
|
current_frame_alerts.append(
|
||||||
{
|
{
|
||||||
'time': current_time_sec,
|
'time': current_time_sec,
|
||||||
'action': 'prisoner',
|
'action': 'Black Bag',
|
||||||
'confidence': 1.0, # 固定为1.0(规则判定)
|
|
||||||
'details': f"Detected for {duration_seconds:.1f}s"
|
'details': f"Detected for {duration_seconds:.1f}s"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
self.draw_alert(frame, "prisoner", (0, 0, 255), offset_y=alert_offset)
|
self.draw_alert(frame, "Black Bag Alert", (0, 0, 255), sub_text=f"Detected for {duration_seconds:.1f}s")
|
||||||
alert_offset += 100
|
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# 11. 统一显示当前帧所有警告(可替换原分层显示)
|
# 绘制信息
|
||||||
# ==========================================
|
# ==========================================
|
||||||
debug_info = f" prisoner: {current_prisoner_count}"
|
# 实时统计
|
||||||
|
debug_info = f"Person: {self.current_person_count} | BlackBag: {current_blackbag_count}"
|
||||||
cv2.putText(frame, debug_info, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
cv2.putText(frame, debug_info, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
||||||
|
|
||||||
# 统一警告显示区
|
# 警告信息
|
||||||
alert_y_start = 150
|
alert_y_start = 150
|
||||||
for i, alert in enumerate(current_frame_alerts):
|
for i, alert in enumerate(current_frame_alerts):
|
||||||
action = alert['action']
|
action = alert['action']
|
||||||
details = alert.get('details', '')
|
details = alert.get('details', '')
|
||||||
color = (0, 0, 255) # 默认红色警告
|
color = (0, 0, 255) # 红色警告
|
||||||
|
main_text = f"{action} ({details})"
|
||||||
if action == 'prisoner':
|
|
||||||
color = (255, 255, 255)
|
|
||||||
|
|
||||||
main_text = action
|
|
||||||
if details:
|
|
||||||
main_text += f" ({details})"
|
|
||||||
|
|
||||||
y_pos = alert_y_start + i * 50
|
y_pos = alert_y_start + i * 50
|
||||||
cv2.rectangle(frame, (20, y_pos - 40), (900, y_pos + 10), (0, 0, 0), -1)
|
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)
|
cv2.putText(frame, main_text, (30, y_pos), cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2)
|
||||||
@@ -334,114 +249,11 @@ class ZoulangDetector:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ========================= 帧处理线程 =========================
|
# ========================= 帧处理线程 =========================
|
||||||
class FrameProcessorWorker(threading.Thread):
|
class FrameProcessorWorker(BaseFrameProcessorWorker):
|
||||||
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] = {}
|
# 子类配置
|
||||||
|
DETECTOR_FACTORY = lambda params: AbDetector(params)
|
||||||
# 每个摄像头一个独立的 Kadian 检测器实例
|
POST_TYPE = 2
|
||||||
self.kadian_detectors: Dict[int, ZoulangDetector] = {}
|
TARGET_FPS = RTSP_TARGET_FPS
|
||||||
|
|
||||||
# 新增:维护每个摄像头每个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.kadian_detectors:
|
|
||||||
self.kadian_detectors[cam_id] = ZoulangDetector()
|
|
||||||
detector = self.kadian_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:
|
|
||||||
# 将abnormal_actions对象数组转换为字符串数组
|
|
||||||
#action_names = [action_info['action'] for action_info in push_actions]
|
|
||||||
|
|
||||||
msg = {
|
|
||||||
"msg_type": "frame",
|
|
||||||
"camera_id": item["camera_index"],
|
|
||||||
"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:
|
|
||||||
# 发送POST请求
|
|
||||||
post_msg = msg.copy()
|
|
||||||
post_msg['type'] = 2
|
|
||||||
try:
|
|
||||||
response = requests.post(ALERT_PUSH_URL, json=post_msg, timeout=5.0)
|
|
||||||
if response.status_code == 200:
|
|
||||||
print(f"[INFO] POST alert sent successfully for actions: {push_actions}")
|
|
||||||
else:
|
|
||||||
print(f"[WARN] POST alert failed with status: {response.status_code}")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"[ERROR] POST alert request failed: {e}")
|
|
||||||
except queue.Full:
|
|
||||||
print("[WARN] ws_send_queue full, drop frame message")
|
|
||||||
|
|
||||||
self.raw_queue.task_done()
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
from biz.checkpoint.checkpoint_biz import FrameProcessorWorker as CheckpointWorker
|
from biz.checkpoint.checkpoint_biz import FrameProcessorWorker as CheckpointWorker
|
||||||
from biz.prison.trajectory02_biz import FrameProcessorWorker as TrajectoryWorker
|
from biz.prison.trajectory02_biz import FrameProcessorWorker as TrajectoryWorker
|
||||||
from biz.prison.supervision_room_biz import FrameProcessorWorker as SupervisionWorker
|
from biz.prison.supervision_room_biz import FrameProcessorWorker as SupervisionWorker
|
||||||
|
from biz.prison.ab_biz import FrameProcessorWorker as AbWorker
|
||||||
|
|
||||||
# ... 其他导入
|
# ... 其他导入
|
||||||
|
|
||||||
PROCESSOR_MAP = {
|
PROCESSOR_MAP = {
|
||||||
"checkpoint": CheckpointWorker,
|
"checkpoint": CheckpointWorker,
|
||||||
"trajectory": TrajectoryWorker,
|
"trajectory": TrajectoryWorker,
|
||||||
"supervision_room": SupervisionWorker,
|
"supervision_room": SupervisionWorker,
|
||||||
|
"ab": AbWorker
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_processor(processor_type: str):
|
def get_processor(processor_type: str):
|
||||||
|
|||||||
Reference in New Issue
Block a user