panorama_camera.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. """
  2. 全景摄像头模块
  3. 负责获取视频流和物体检测
  4. """
  5. import os
  6. # 必须在导入cv2之前设置,防止FFmpeg多线程解码崩溃
  7. # pthread_frame.c:167 async_lock assertion
  8. os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'threads;1'
  9. import cv2
  10. import numpy as np
  11. import threading
  12. import queue
  13. import time
  14. from typing import Optional, List, Tuple, Dict, Any
  15. from dataclasses import dataclass
  16. from config import PANORAMA_CAMERA, DETECTION_CONFIG
  17. from dahua_sdk import DahuaSDK, PTZCommand
  18. from video_lock import ff_lock
  19. @dataclass
  20. class DetectedObject:
  21. """检测到的物体"""
  22. class_name: str # 类别名称
  23. confidence: float # 置信度
  24. bbox: Tuple[int, int, int, int] # 边界框 (x, y, width, height)
  25. center: Tuple[int, int] # 中心点坐标
  26. track_id: Optional[int] = None # 跟踪ID
  27. class PanoramaCamera:
  28. """全景摄像头类"""
  29. def __init__(self, sdk: DahuaSDK, camera_config: Dict = None):
  30. """
  31. 初始化全景摄像头
  32. Args:
  33. sdk: 大华SDK实例
  34. camera_config: 摄像头配置
  35. """
  36. self.sdk = sdk
  37. self.config = camera_config or PANORAMA_CAMERA
  38. self.login_handle = None
  39. self.play_handle = None
  40. self.connected = False
  41. # 视频流
  42. self.frame_queue = queue.Queue(maxsize=10)
  43. self.current_frame = None
  44. self.frame_lock = threading.Lock()
  45. self.rtsp_cap = None # RTSP视频捕获
  46. # 检测器
  47. self.detector = None
  48. # 控制标志
  49. self.running = False
  50. self.stream_thread = None
  51. # 断线重连
  52. self.auto_reconnect = True
  53. self.reconnect_interval = 5.0 # 重连间隔(秒)
  54. self.max_reconnect_attempts = 3 # 最大重连次数
  55. def connect(self) -> bool:
  56. """
  57. 连接摄像头
  58. Returns:
  59. 是否成功
  60. """
  61. login_handle, error = self.sdk.login(
  62. self.config['ip'],
  63. self.config['port'],
  64. self.config['username'],
  65. self.config['password']
  66. )
  67. if login_handle is None:
  68. print(f"连接全景摄像头失败: IP={self.config['ip']}, 错误码={error}")
  69. return False
  70. self.login_handle = login_handle
  71. self.connected = True
  72. print(f"成功连接全景摄像头: {self.config['ip']}")
  73. return True
  74. def disconnect(self):
  75. """断开连接"""
  76. self.stop_stream()
  77. if self.login_handle:
  78. self.sdk.logout(self.login_handle)
  79. self.login_handle = None
  80. self.connected = False
  81. def start_stream(self) -> bool:
  82. """
  83. 开始视频流
  84. Returns:
  85. 是否成功
  86. """
  87. if not self.connected:
  88. return False
  89. self.play_handle = self.sdk.real_play(
  90. self.login_handle,
  91. self.config['channel']
  92. )
  93. if self.play_handle is None:
  94. print("启动视频流失败")
  95. return False
  96. self.running = True
  97. self.stream_thread = threading.Thread(target=self._stream_worker, daemon=True)
  98. self.stream_thread.start()
  99. print("视频流已启动")
  100. return True
  101. def start_stream_rtsp(self, rtsp_url: str = None) -> bool:
  102. if rtsp_url is None:
  103. rtsp_url = self.config.get('rtsp_url') or f"rtsp://{self.config['username']}:{self.config['password']}@{self.config['ip']}:{self.config.get('rtsp_port', 554)}/h264/ch{self.config['channel']}/main/av_stream"
  104. try:
  105. self.rtsp_cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
  106. if not self.rtsp_cap.isOpened():
  107. print(f"无法打开RTSP流: {rtsp_url}")
  108. return False
  109. self.rtsp_cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
  110. self.running = True
  111. self.stream_thread = threading.Thread(target=self._rtsp_stream_worker, daemon=True)
  112. self.stream_thread.start()
  113. print(f"RTSP视频流已启动: {rtsp_url}")
  114. return True
  115. except Exception as e:
  116. print(f"RTSP流启动失败: {e}")
  117. return False
  118. def _stream_worker(self):
  119. """视频流工作线程 (SDK模式)"""
  120. retry_count = 0
  121. max_retries = 10
  122. while self.running:
  123. try:
  124. # 尝试从 SDK 帧缓冲区获取帧 (如果可用)
  125. frame_buffer = self.sdk.get_video_frame_buffer(self.config['channel'])
  126. if frame_buffer:
  127. frame_info = frame_buffer.get(timeout=0.1)
  128. if frame_info and frame_info.get('data'):
  129. # 解码帧数据 (如果需要)
  130. # 注意: SDK回调返回的是编码数据,需要解码
  131. # 这里暂时跳过,因为解码需要额外处理
  132. pass
  133. # RTSP 模式获取帧 (推荐方式)
  134. if self.rtsp_cap is not None and self.rtsp_cap.isOpened():
  135. with ff_lock:
  136. ret, frame = self.rtsp_cap.read()
  137. if ret and frame is not None:
  138. with self.frame_lock:
  139. self.current_frame = frame.copy()
  140. try:
  141. self.frame_queue.put(frame.copy(), block=False)
  142. except queue.Full:
  143. pass
  144. retry_count = 0 # 重置重试计数
  145. time.sleep(0.001) # 减少CPU占用
  146. continue
  147. # 如果 RTSP 不可用,尝试自动连接
  148. if retry_count < max_retries:
  149. rtsp_url = self._build_rtsp_url()
  150. try:
  151. if self.rtsp_cap is None:
  152. self.rtsp_cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
  153. self.rtsp_cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 减少缓冲延迟
  154. if self.rtsp_cap.isOpened():
  155. retry_count = 0
  156. continue
  157. except Exception as e:
  158. pass
  159. retry_count += 1
  160. time.sleep(1.0) # 重试间隔
  161. else:
  162. # 超过最大重试次数,使用模拟帧
  163. frame = np.zeros((1080, 1920, 3), dtype=np.uint8)
  164. with self.frame_lock:
  165. self.current_frame = frame
  166. try:
  167. self.frame_queue.put(frame, block=False)
  168. except queue.Full:
  169. pass
  170. time.sleep(0.1)
  171. except Exception as e:
  172. err_str = str(e)
  173. if 'async_lock' in err_str or 'Assertion' in err_str:
  174. print(f"视频流FFmpeg内部错误,重建连接: {e}")
  175. self._reconnect_rtsp()
  176. else:
  177. print(f"视频流错误: {e}")
  178. time.sleep(0.5)
  179. def _build_rtsp_url(self) -> str:
  180. return self.config.get('rtsp_url') or f"rtsp://{self.config['username']}:{self.config['password']}@{self.config['ip']}:{self.config.get('rtsp_port', 554)}/h264/ch{self.config['channel']}/main/av_stream"
  181. def _rtsp_stream_worker(self):
  182. """RTSP视频流工作线程"""
  183. import signal
  184. # 屏蔽SIGINT在此线程,由主线程处理
  185. if hasattr(signal, 'pthread_sigmask'):
  186. try:
  187. signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT})
  188. except (AttributeError, OSError):
  189. pass
  190. max_consecutive_errors = 50
  191. error_count = 0
  192. while self.running:
  193. try:
  194. if self.rtsp_cap is None or not self.rtsp_cap.isOpened():
  195. time.sleep(0.1)
  196. continue
  197. with ff_lock:
  198. ret, frame = self.rtsp_cap.read()
  199. if not ret or frame is None:
  200. error_count += 1
  201. if error_count > max_consecutive_errors:
  202. print(f"全景RTSP流连续{max_consecutive_errors}次读取失败,尝试重连...")
  203. self._reconnect_rtsp()
  204. error_count = 0
  205. time.sleep(0.01)
  206. continue
  207. error_count = 0
  208. with self.frame_lock:
  209. self.current_frame = frame.copy()
  210. try:
  211. self.frame_queue.put(frame, block=False)
  212. except queue.Full:
  213. pass
  214. except Exception as e:
  215. err_str = str(e)
  216. if 'async_lock' in err_str or 'Assertion' in err_str:
  217. print(f"全景RTSP流FFmpeg内部错误,3秒后重建连接: {e}")
  218. time.sleep(3)
  219. self._reconnect_rtsp()
  220. else:
  221. print(f"全景RTSP视频流错误: {e}")
  222. time.sleep(0.5)
  223. def _reconnect_rtsp(self):
  224. """重建RTSP连接"""
  225. rtsp_url = self._build_rtsp_url()
  226. if self.rtsp_cap is not None:
  227. try:
  228. self.rtsp_cap.release()
  229. except Exception:
  230. pass
  231. self.rtsp_cap = None
  232. time.sleep(1)
  233. try:
  234. self.rtsp_cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
  235. if self.rtsp_cap.isOpened():
  236. self.rtsp_cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
  237. print("全景RTSP流重连成功")
  238. else:
  239. print("全景RTSP流重连失败")
  240. self.rtsp_cap = None
  241. except Exception as e:
  242. print(f"全景RTSP流重连异常: {e}")
  243. self.rtsp_cap = None
  244. def stop_stream(self):
  245. """停止视频流"""
  246. self.running = False
  247. if self.stream_thread:
  248. self.stream_thread.join(timeout=2)
  249. if self.play_handle:
  250. self.sdk.stop_real_play(self.play_handle)
  251. self.play_handle = None
  252. if self.rtsp_cap:
  253. self.rtsp_cap.release()
  254. self.rtsp_cap = None
  255. def get_frame(self) -> Optional[np.ndarray]:
  256. """
  257. 获取当前帧
  258. Returns:
  259. 当前帧图像
  260. """
  261. with self.frame_lock:
  262. return self.current_frame.copy() if self.current_frame is not None else None
  263. def get_frame_from_queue(self, timeout: float = 0.1) -> Optional[np.ndarray]:
  264. """
  265. 从帧队列获取帧 (用于批量处理)
  266. Args:
  267. timeout: 等待超时时间
  268. Returns:
  269. 帧图像或None
  270. """
  271. try:
  272. return self.frame_queue.get(timeout=timeout)
  273. except:
  274. return None
  275. def get_frame_buffer(self, count: int = 5) -> List[np.ndarray]:
  276. """
  277. 获取帧缓冲 (用于运动检测等需要多帧的场景)
  278. Args:
  279. count: 获取帧数
  280. Returns:
  281. 帧列表
  282. """
  283. frames = []
  284. while len(frames) < count:
  285. frame = self.get_frame_from_queue(timeout=0.05)
  286. if frame is not None:
  287. frames.append(frame)
  288. else:
  289. break
  290. return frames
  291. def set_detector(self, detector):
  292. """设置物体检测器"""
  293. self.detector = detector
  294. def detect_objects(self, frame: np.ndarray = None) -> List[DetectedObject]:
  295. """
  296. 检测物体
  297. Args:
  298. frame: 输入帧,如果为None则使用当前帧
  299. Returns:
  300. 检测到的物体列表
  301. """
  302. if frame is None:
  303. frame = self.get_frame()
  304. if frame is None or self.detector is None:
  305. return []
  306. return self.detector.detect(frame)
  307. def get_detection_position(self, obj: DetectedObject,
  308. frame_size: Tuple[int, int]) -> Tuple[float, float]:
  309. """
  310. 获取检测物体在画面中的相对位置
  311. Args:
  312. obj: 检测到的物体
  313. frame_size: 画面尺寸 (width, height)
  314. Returns:
  315. 相对位置 (x_ratio, y_ratio) 范围0-1
  316. """
  317. width, height = frame_size
  318. x_ratio = obj.center[0] / width
  319. y_ratio = obj.center[1] / height
  320. return (x_ratio, y_ratio)
  321. class ObjectDetector:
  322. """
  323. 物体检测器
  324. 使用YOLO11模型进行人体检测
  325. 支持 YOLO (.pt), RKNN (.rknn), ONNX (.onnx) 模型
  326. """
  327. def __init__(self, model_path: str = None, use_gpu: bool = True, model_size: str = 'n',
  328. model_type: str = 'auto'):
  329. """
  330. 初始化检测器
  331. Args:
  332. model_path: 模型路径 (支持 .pt, .rknn, .onnx)
  333. use_gpu: 是否使用GPU
  334. model_size: 模型尺寸 ('n', 's', 'm', 'l', 'x') - 仅 YOLO 模型有效
  335. model_type: 模型类型 ('auto', 'yolo', 'rknn', 'onnx')
  336. """
  337. self.model = None
  338. self.rknn_detector = None
  339. self.model_path = model_path
  340. self.use_gpu = use_gpu
  341. self.model_size = model_size
  342. self.model_type = model_type
  343. self.config = DETECTION_CONFIG
  344. self.device = 'cuda:0' if use_gpu else 'cpu'
  345. # 根据扩展名自动判断模型类型
  346. if model_path:
  347. ext = os.path.splitext(model_path)[1].lower()
  348. if ext == '.rknn':
  349. self.model_type = 'rknn'
  350. elif ext == '.onnx':
  351. self.model_type = 'onnx'
  352. elif ext == '.pt':
  353. self.model_type = 'yolo'
  354. self._load_model()
  355. def _load_model(self):
  356. """加载检测模型"""
  357. if self.model_type == 'rknn':
  358. self._load_rknn_model()
  359. elif self.model_type == 'onnx':
  360. self._load_onnx_model()
  361. else:
  362. self._load_yolo_model()
  363. def _load_rknn_model(self):
  364. """加载 RKNN 模型"""
  365. if not self.model_path:
  366. raise ValueError("RKNN 模型需要指定 model_path")
  367. try:
  368. from rknnlite.api import RKNNLite
  369. self.rknn = RKNNLite()
  370. ret = self.rknn.load_rknn(self.model_path)
  371. if ret != 0:
  372. raise RuntimeError(f"加载 RKNN 模型失败: {self.model_path}")
  373. ret = self.rknn.init_runtime(core_mask=RKNNLite.NPU_CORE_0_1_2)
  374. if ret != 0:
  375. raise RuntimeError(f"初始化 RKNN 运行时失败")
  376. print(f"RKNN 模型加载成功: {self.model_path}")
  377. except ImportError:
  378. raise ImportError("未安装 rknnlite,请运行: pip install rknnlite2")
  379. def _load_onnx_model(self):
  380. """加载 ONNX 模型"""
  381. if not self.model_path:
  382. raise ValueError("ONNX 模型需要指定 model_path")
  383. try:
  384. import onnxruntime as ort
  385. self.session = ort.InferenceSession(self.model_path)
  386. self.input_name = self.session.get_inputs()[0].name
  387. self.output_name = self.session.get_outputs()[0].name
  388. print(f"ONNX 模型加载成功: {self.model_path}")
  389. except ImportError:
  390. raise ImportError("未安装 onnxruntime,请运行: pip install onnxruntime")
  391. def _load_yolo_model(self):
  392. """加载YOLO11检测模型"""
  393. try:
  394. from ultralytics import YOLO
  395. if self.model_path:
  396. self.model = YOLO(self.model_path)
  397. else:
  398. model_name = f'yolo11{self.model_size}.pt'
  399. self.model = YOLO(model_name)
  400. dummy = np.zeros((640, 640, 3), dtype=np.uint8)
  401. self.model(dummy, device=self.device, verbose=False)
  402. print(f"成功加载YOLO11检测模型 (device={self.device})")
  403. except ImportError:
  404. print("未安装ultralytics,请运行: pip install ultralytics")
  405. self._load_opencv_model()
  406. except Exception as e:
  407. print(f"加载YOLO11模型失败: {e}")
  408. self._load_opencv_model()
  409. def _load_opencv_model(self):
  410. """使用OpenCV加载模型"""
  411. pass
  412. def _letterbox(self, image, size=(640, 640)):
  413. """Letterbox 预处理"""
  414. h0, w0 = image.shape[:2]
  415. ih, iw = size
  416. scale = min(iw / w0, ih / h0)
  417. new_w, new_h = int(w0 * scale), int(h0 * scale)
  418. pad_w = (iw - new_w) // 2
  419. pad_h = (ih - new_h) // 2
  420. resized = cv2.resize(image, (new_w, new_h))
  421. canvas = np.full((ih, iw, 3), 114, dtype=np.uint8)
  422. canvas[pad_h:pad_h+new_h, pad_w:pad_w+new_w] = resized
  423. return canvas, scale, pad_w, pad_h, h0, w0
  424. def _detect_rknn(self, frame: np.ndarray) -> List[DetectedObject]:
  425. """使用 RKNN/ONNX 模型检测"""
  426. results = []
  427. try:
  428. canvas, scale, pad_w, pad_h, h0, w0 = self._letterbox(frame)
  429. if hasattr(self, 'rknn'):
  430. # RKNN
  431. img = canvas[..., ::-1].astype(np.float32) / 255.0
  432. blob = img[None, ...]
  433. outputs = self.rknn.inference(inputs=[blob])
  434. else:
  435. # ONNX
  436. img = canvas[..., ::-1].astype(np.float32) / 255.0
  437. img = img.transpose(2, 0, 1)
  438. blob = img[None, ...]
  439. outputs = self.session.run([self.output_name], {self.input_name: blob})
  440. output = outputs[0]
  441. if len(output.shape) == 3:
  442. output = output[0]
  443. num_boxes = output.shape[1]
  444. conf_threshold = self.config['confidence_threshold']
  445. for i in range(num_boxes):
  446. x_center = float(output[0, i])
  447. y_center = float(output[1, i])
  448. width = float(output[2, i])
  449. height = float(output[3, i])
  450. class_probs = output[4:, i]
  451. best_class = int(np.argmax(class_probs))
  452. confidence = float(class_probs[best_class])
  453. if confidence < conf_threshold:
  454. continue
  455. # 转换到原始图像坐标
  456. x1 = int(((x_center - width / 2) - pad_w) / scale)
  457. y1 = int(((y_center - height / 2) - pad_h) / scale)
  458. x2 = int(((x_center + width / 2) - pad_w) / scale)
  459. y2 = int(((y_center + height / 2) - pad_h) / scale)
  460. x1 = max(0, min(w0, x1))
  461. y1 = max(0, min(h0, y1))
  462. x2 = max(0, min(w0, x2))
  463. y2 = max(0, min(h0, y2))
  464. if x2 - x1 < 10 or y2 - y1 < 10:
  465. continue
  466. # 使用配置的类别映射获取类别名称
  467. class_map = self.config.get('class_map', {0: 'person', 3: '人'})
  468. cls_name = class_map.get(best_class, str(best_class))
  469. # 检查是否为目标类别
  470. if cls_name not in self.config['target_classes']:
  471. continue
  472. obj = DetectedObject(
  473. class_name=cls_name,
  474. confidence=confidence,
  475. bbox=(x1, y1, x2 - x1, y2 - y1),
  476. center=((x1 + x2) // 2, (y1 + y2) // 2)
  477. )
  478. results.append(obj)
  479. except Exception as e:
  480. print(f"RKNN/ONNX 检测错误: {e}")
  481. return results
  482. def detect(self, frame: np.ndarray) -> List[DetectedObject]:
  483. """
  484. 使用YOLO11检测物体
  485. Args:
  486. frame: 输入图像
  487. Returns:
  488. 检测结果列表
  489. """
  490. if frame is None:
  491. return []
  492. # 优先使用 RKNN/ONNX 模型
  493. if hasattr(self, 'rknn') and self.rknn is not None:
  494. return self._detect_rknn(frame)
  495. elif hasattr(self, 'session') and self.session is not None:
  496. return self._detect_rknn(frame)
  497. # 使用 YOLO 模型
  498. elif self.model is not None:
  499. return self._detect_yolo(frame)
  500. else:
  501. print("[错误] 没有可用的检测模型")
  502. return []
  503. def _detect_yolo(self, frame: np.ndarray) -> List[DetectedObject]:
  504. """使用 YOLO 模型检测"""
  505. results = []
  506. try:
  507. detections = self.model(
  508. frame,
  509. device=self.device,
  510. verbose=False,
  511. conf=self.config['confidence_threshold']
  512. )
  513. for det in detections:
  514. boxes = det.boxes
  515. if boxes is None:
  516. continue
  517. for i in range(len(boxes)):
  518. cls_id = int(boxes.cls[i])
  519. cls_name = det.names[cls_id]
  520. if cls_name not in self.config['target_classes']:
  521. continue
  522. conf = float(boxes.conf[i])
  523. xyxy = boxes.xyxy[i].cpu().numpy()
  524. x1, y1, x2, y2 = map(int, xyxy)
  525. width = x2 - x1
  526. height = y2 - y1
  527. if width < 10 or height < 10:
  528. continue
  529. center_x = x1 + width // 2
  530. center_y = y1 + height // 2
  531. obj = DetectedObject(
  532. class_name=cls_name,
  533. confidence=conf,
  534. bbox=(x1, y1, width, height),
  535. center=(center_x, center_y)
  536. )
  537. results.append(obj)
  538. except Exception as e:
  539. print(f"YOLO11检测错误: {e}")
  540. return results
  541. def detect_with_keypoints(self, frame: np.ndarray) -> List[DetectedObject]:
  542. """
  543. 使用YOLO11-pose检测人体并返回关键点
  544. Args:
  545. frame: 输入图像
  546. Returns:
  547. 带关键点的检测结果列表
  548. """
  549. return self.detect(frame)
  550. def detect_persons(self, frame: np.ndarray) -> List[DetectedObject]:
  551. """
  552. 检测人体
  553. Args:
  554. frame: 输入图像
  555. Returns:
  556. 检测到的人体列表
  557. """
  558. results = self.detect(frame)
  559. return [obj for obj in results if obj.class_name == 'person']
  560. def release(self):
  561. """释放模型资源"""
  562. if hasattr(self, 'rknn') and self.rknn:
  563. self.rknn.release()
  564. self.rknn = None
  565. self.model = None
  566. self.session = None
  567. class PersonTracker:
  568. """
  569. 人体跟踪器
  570. 使用简单的质心跟踪算法
  571. """
  572. def __init__(self, max_disappeared: int = 30):
  573. """
  574. 初始化跟踪器
  575. Args:
  576. max_disappeared: 最大消失帧数
  577. """
  578. self.max_disappeared = max_disappeared
  579. self.next_id = 0
  580. self.objects = {} # id -> center
  581. self.disappeared = {} # id -> disappeared count
  582. def update(self, detections: List[DetectedObject]) -> List[DetectedObject]:
  583. """
  584. 更新跟踪状态
  585. Args:
  586. detections: 当前帧检测结果
  587. Returns:
  588. 带有跟踪ID的检测结果
  589. """
  590. # 如果没有检测结果
  591. if len(detections) == 0:
  592. # 标记所有已跟踪对象为消失
  593. for obj_id in list(self.disappeared.keys()):
  594. self.disappeared[obj_id] += 1
  595. if self.disappeared[obj_id] > self.max_disappeared:
  596. self._deregister(obj_id)
  597. return []
  598. # 计算当前检测中心点
  599. input_centers = np.array([d.center for d in detections])
  600. # 如果没有已跟踪对象
  601. if len(self.objects) == 0:
  602. for det in detections:
  603. self._register(det)
  604. else:
  605. # 计算距离矩阵
  606. object_ids = list(self.objects.keys())
  607. object_centers = np.array([self.objects[obj_id] for obj_id in object_ids])
  608. # 计算欧氏距离
  609. distances = np.linalg.norm(
  610. object_centers[:, np.newaxis] - input_centers,
  611. axis=2
  612. )
  613. # 匈牙利算法匹配 (简化版: 贪心匹配)
  614. rows = distances.min(axis=1).argsort()
  615. cols = distances.argmin(axis=1)[rows]
  616. used_rows = set()
  617. used_cols = set()
  618. for (row, col) in zip(rows, cols):
  619. if row in used_rows or col in used_cols:
  620. continue
  621. obj_id = object_ids[row]
  622. self.objects[obj_id] = input_centers[col]
  623. self.disappeared[obj_id] = 0
  624. detections[col].track_id = obj_id
  625. used_rows.add(row)
  626. used_cols.add(col)
  627. # 处理未匹配的已跟踪对象
  628. unused_rows = set(range(len(object_ids))) - used_rows
  629. for row in unused_rows:
  630. obj_id = object_ids[row]
  631. self.disappeared[obj_id] += 1
  632. if self.disappeared[obj_id] > self.max_disappeared:
  633. self._deregister(obj_id)
  634. # 处理未匹配的新检测
  635. unused_cols = set(range(len(input_centers))) - used_cols
  636. for col in unused_cols:
  637. self._register(detections[col])
  638. return [d for d in detections if d.track_id is not None]
  639. def _register(self, detection: DetectedObject):
  640. """注册新对象"""
  641. detection.track_id = self.next_id
  642. self.objects[self.next_id] = detection.center
  643. self.disappeared[self.next_id] = 0
  644. self.next_id += 1
  645. def _deregister(self, obj_id: int):
  646. """注销对象"""
  647. del self.objects[obj_id]
  648. del self.disappeared[obj_id]