coordinator.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. """
  2. 联动控制器
  3. 协调全景摄像头和球机的工作
  4. """
  5. import time
  6. import threading
  7. import queue
  8. import logging
  9. from typing import Optional, List, Dict, Tuple, Callable
  10. from dataclasses import dataclass, field
  11. from enum import Enum
  12. import numpy as np
  13. from config import COORDINATOR_CONFIG, SYSTEM_CONFIG, PTZ_CONFIG, DETECTION_CONFIG
  14. from panorama_camera import PanoramaCamera, ObjectDetector, PersonTracker, DetectedObject
  15. from ptz_camera import PTZCamera, PTZController
  16. from ocr_recognizer import NumberDetector, PersonInfo
  17. from ptz_person_tracker import PTZPersonDetector, PTZAutoZoomController
  18. logger = logging.getLogger(__name__)
  19. class TrackingState(Enum):
  20. """跟踪状态"""
  21. IDLE = 0 # 空闲
  22. SEARCHING = 1 # 搜索目标
  23. TRACKING = 2 # 跟踪中
  24. ZOOMING = 3 # 变焦中
  25. OCR_PROCESSING = 4 # OCR处理中
  26. @dataclass
  27. class TrackingTarget:
  28. """跟踪目标"""
  29. track_id: int # 跟踪ID
  30. position: Tuple[float, float] # 位置比例 (x_ratio, y_ratio)
  31. last_update: float # 最后更新时间
  32. person_info: Optional[PersonInfo] = None # 人员信息
  33. priority: int = 0 # 优先级
  34. area: int = 0 # 目标面积(像素²)
  35. confidence: float = 0.0 # 置信度
  36. center_distance: float = 1.0 # 到画面中心的距离比例(0-1)
  37. score: float = 0.0 # 综合得分
  38. class TargetSelector:
  39. """
  40. 目标选择策略类
  41. 支持按面积、置信度、混合模式排序,支持优先级切换
  42. """
  43. def __init__(self, config: Dict = None):
  44. """
  45. 初始化目标选择器
  46. Args:
  47. config: 目标选择配置
  48. """
  49. self.config = config or {
  50. 'strategy': 'area',
  51. 'area_weight': 0.6,
  52. 'confidence_weight': 0.4,
  53. 'min_area_threshold': 5000,
  54. 'prefer_center': True,
  55. 'center_weight': 0.2,
  56. 'switch_on_lost': True,
  57. 'stickiness': 0.3,
  58. }
  59. self.current_target_id: Optional[int] = None
  60. self.current_target_score: float = 0.0
  61. def calculate_score(self, target: TrackingTarget, frame_size: Tuple[int, int] = None) -> float:
  62. """
  63. 计算目标综合得分
  64. Args:
  65. target: 跟踪目标
  66. frame_size: 帧尺寸(w, h),用于计算中心距离
  67. Returns:
  68. 综合得分(0-1)
  69. """
  70. strategy = self.config.get('strategy', 'area')
  71. area_weight = self.config.get('area_weight', 0.6)
  72. conf_weight = self.config.get('confidence_weight', 0.4)
  73. min_area = self.config.get('min_area_threshold', 5000)
  74. prefer_center = self.config.get('prefer_center', False)
  75. center_weight = self.config.get('center_weight', 0.2)
  76. # 归一化面积得分 (对数缩放,避免大目标得分过高)
  77. import math
  78. area_score = min(1.0, math.log10(max(target.area, 1)) / 5.0) # 100000像素² ≈ 1.0
  79. # 小面积惩罚
  80. if target.area < min_area:
  81. area_score *= 0.5
  82. # 置信度得分直接使用
  83. conf_score = target.confidence
  84. # 中心距离得分 (距离中心越近得分越高)
  85. center_score = 1.0 - target.center_distance
  86. # 根据策略计算综合得分
  87. if strategy == 'area':
  88. score = area_score * 0.8 + conf_score * 0.2
  89. elif strategy == 'confidence':
  90. score = conf_score * 0.8 + area_score * 0.2
  91. else: # hybrid
  92. score = area_score * area_weight + conf_score * conf_weight
  93. # 加入中心距离权重
  94. if prefer_center:
  95. score = score * (1 - center_weight) + center_score * center_weight
  96. return score
  97. def select_target(self, targets: Dict[int, TrackingTarget],
  98. frame_size: Tuple[int, int] = None) -> Optional[TrackingTarget]:
  99. """
  100. 从多个目标中选择最优目标
  101. Args:
  102. targets: 目标字典 {track_id: TrackingTarget}
  103. frame_size: 帧尺寸
  104. Returns:
  105. 最优目标
  106. """
  107. if not targets:
  108. self.current_target_id = None
  109. return None
  110. stickiness = self.config.get('stickiness', 0.3)
  111. switch_on_lost = self.config.get('switch_on_lost', True)
  112. # 计算所有目标得分
  113. scored_targets = []
  114. for track_id, target in targets.items():
  115. target.score = self.calculate_score(target, frame_size)
  116. scored_targets.append((track_id, target, target.score))
  117. # 按得分排序
  118. scored_targets.sort(key=lambda x: x[2], reverse=True)
  119. # 检查当前目标是否仍在列表中
  120. if self.current_target_id is not None:
  121. current_exists = self.current_target_id in targets
  122. if current_exists:
  123. # 应用粘性:当前目标得分需要显著低于最优目标才切换
  124. best_id, best_target, best_score = scored_targets[0]
  125. current_target = targets[self.current_target_id]
  126. # 粘性阈值: 当前目标得分 > 最优得分 * (1 - stickiness) 时保持
  127. stickiness_threshold = best_score * (1 - stickiness)
  128. if current_target.score > stickiness_threshold:
  129. return current_target
  130. # 选择得分最高的目标
  131. best_id, best_target, best_score = scored_targets[0]
  132. self.current_target_id = best_id
  133. self.current_target_score = best_score
  134. logger.debug(
  135. f"[目标选择] 选择目标ID={best_id} 得分={best_score:.3f} "
  136. f"面积={best_target.area} 置信度={best_target.confidence:.2f}"
  137. )
  138. return best_target
  139. def get_sorted_targets(self, targets: Dict[int, TrackingTarget],
  140. frame_size: Tuple[int, int] = None) -> List[Tuple[TrackingTarget, float]]:
  141. """
  142. 获取按得分排序的目标列表
  143. Args:
  144. targets: 目标字典
  145. frame_size: 帧尺寸
  146. Returns:
  147. 排序后的目标列表 [(target, score), ...]
  148. """
  149. scored = []
  150. for target in targets.values():
  151. target.score = self.calculate_score(target, frame_size)
  152. scored.append((target, target.score))
  153. scored.sort(key=lambda x: x[1], reverse=True)
  154. return scored
  155. def set_strategy(self, strategy: str):
  156. """设置选择策略"""
  157. self.config['strategy'] = strategy
  158. logger.info(f"[目标选择] 策略已切换为: {strategy}")
  159. def set_stickiness(self, stickiness: float):
  160. """设置目标粘性"""
  161. self.config['stickiness'] = max(0.0, min(1.0, stickiness))
  162. logger.info(f"[目标选择] 粘性已设置为: {self.config['stickiness']}")
  163. class Coordinator:
  164. """
  165. 联动控制器
  166. 协调全景摄像头和球机实现联动抓拍
  167. """
  168. def __init__(self, panorama_camera: PanoramaCamera,
  169. ptz_camera: PTZCamera,
  170. detector: ObjectDetector = None,
  171. number_detector: NumberDetector = None,
  172. calibrator = None):
  173. """
  174. 初始化联动控制器
  175. Args:
  176. panorama_camera: 全景摄像头
  177. ptz_camera: 球机
  178. detector: 物体检测器
  179. number_detector: 编号检测器
  180. calibrator: 校准器 (用于坐标转换)
  181. """
  182. self.panorama = panorama_camera
  183. self.ptz = ptz_camera
  184. self.detector = detector
  185. self.number_detector = number_detector
  186. self.calibrator = calibrator
  187. self.config = COORDINATOR_CONFIG
  188. # 功能开关 - 从 SYSTEM_CONFIG 读取
  189. self.enable_ptz_camera = SYSTEM_CONFIG.get('enable_ptz_camera', True)
  190. self.enable_ptz_tracking = SYSTEM_CONFIG.get('enable_ptz_tracking', True)
  191. self.enable_calibration = SYSTEM_CONFIG.get('enable_calibration', True)
  192. self.enable_detection = SYSTEM_CONFIG.get('enable_detection', True)
  193. self.enable_ocr = SYSTEM_CONFIG.get('enable_ocr', True)
  194. # 球机端人体检测与自动对焦
  195. self.enable_ptz_detection = PTZ_CONFIG.get('enable_ptz_detection', False)
  196. self.auto_zoom_config = PTZ_CONFIG.get('auto_zoom', {})
  197. self.ptz_detector = None
  198. self.auto_zoom_controller = None
  199. # 跟踪器
  200. self.tracker = PersonTracker()
  201. # 状态
  202. self.state = TrackingState.IDLE
  203. self.state_lock = threading.Lock()
  204. # 跟踪目标
  205. self.tracking_targets: Dict[int, TrackingTarget] = {}
  206. self.targets_lock = threading.Lock()
  207. # 当前跟踪目标
  208. self.current_target: Optional[TrackingTarget] = None
  209. # 回调函数
  210. self.on_person_detected: Optional[Callable] = None
  211. self.on_number_recognized: Optional[Callable] = None
  212. self.on_tracking_started: Optional[Callable] = None
  213. self.on_tracking_stopped: Optional[Callable] = None
  214. # 控制标志
  215. self.running = False
  216. self.coordinator_thread = None
  217. # OCR频率控制
  218. self.last_ocr_time = 0
  219. self.ocr_interval = 1.0 # OCR间隔(秒),避免过于频繁调用API
  220. # PTZ优化 - 避免频繁发送相同位置的命令
  221. self.last_ptz_position = None
  222. self.ptz_position_threshold = self.config.get('ptz_position_threshold', 0.03)
  223. # 目标选择器
  224. self.target_selector = TargetSelector(
  225. self.config.get('target_selection', {})
  226. )
  227. # 结果队列
  228. self.result_queue = queue.Queue()
  229. # 性能统计
  230. self.stats = {
  231. 'frames_processed': 0,
  232. 'persons_detected': 0,
  233. 'ocr_attempts': 0,
  234. 'ocr_success': 0,
  235. 'start_time': None,
  236. 'last_frame_time': None,
  237. }
  238. self.stats_lock = threading.Lock()
  239. def set_calibrator(self, calibrator):
  240. """设置校准器"""
  241. self.calibrator = calibrator
  242. def _transform_position(self, x_ratio: float, y_ratio: float) -> Tuple[float, float, int]:
  243. """
  244. 将全景坐标转换为PTZ角度
  245. Args:
  246. x_ratio: X方向比例
  247. y_ratio: Y方向比例
  248. Returns:
  249. (pan, tilt, zoom)
  250. """
  251. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  252. # 使用校准结果进行转换
  253. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  254. zoom = 8 # 默认变倍
  255. else:
  256. # 使用默认估算
  257. pan, tilt, zoom = self.ptz.calculate_ptz_position(x_ratio, y_ratio)
  258. return (pan, tilt, zoom)
  259. def start(self) -> bool:
  260. """
  261. 启动联动系统
  262. Returns:
  263. 是否成功
  264. """
  265. # 连接全景摄像头
  266. if not self.panorama.connect():
  267. print("连接全景摄像头失败")
  268. return False
  269. # 连接 PTZ 球机 (可选)
  270. if self.enable_ptz_camera:
  271. if not self.ptz.connect():
  272. print("连接球机失败")
  273. self.panorama.disconnect()
  274. return False
  275. else:
  276. print("PTZ 球机功能已禁用")
  277. # 启动视频流(优先RTSP,SDK回调不可用时回退)
  278. if not self.panorama.start_stream_rtsp():
  279. print("RTSP视频流启动失败,尝试SDK方式...")
  280. if not self.panorama.start_stream():
  281. print("启动视频流失败")
  282. self.panorama.disconnect()
  283. if self.enable_ptz_camera:
  284. self.ptz.disconnect()
  285. return False
  286. # 启动联动线程
  287. self.running = True
  288. self.coordinator_thread = threading.Thread(target=self._coordinator_worker, daemon=True)
  289. self.coordinator_thread.start()
  290. print("联动系统已启动")
  291. return True
  292. def stop(self):
  293. """停止联动系统"""
  294. self.running = False
  295. if self.coordinator_thread:
  296. self.coordinator_thread.join(timeout=3)
  297. self.panorama.disconnect()
  298. if self.enable_ptz_camera:
  299. self.ptz.disconnect()
  300. # 打印统计信息
  301. self._print_stats()
  302. print("联动系统已停止")
  303. def _update_stats(self, key: str, value: int = 1):
  304. """更新统计信息"""
  305. with self.stats_lock:
  306. if key in self.stats:
  307. self.stats[key] += value
  308. def _print_stats(self):
  309. """打印统计信息"""
  310. with self.stats_lock:
  311. if self.stats['start_time'] and self.stats['frames_processed'] > 0:
  312. elapsed = time.time() - self.stats['start_time']
  313. fps = self.stats['frames_processed'] / elapsed
  314. print("\n=== 性能统计 ===")
  315. print(f"运行时长: {elapsed:.1f}秒")
  316. print(f"处理帧数: {self.stats['frames_processed']}")
  317. print(f"平均帧率: {fps:.1f} fps")
  318. print(f"检测人体: {self.stats['persons_detected']}次")
  319. print(f"OCR尝试: {self.stats['ocr_attempts']}次")
  320. print(f"OCR成功: {self.stats['ocr_success']}次")
  321. print("================\n")
  322. def get_stats(self) -> dict:
  323. """获取统计信息"""
  324. with self.stats_lock:
  325. return self.stats.copy()
  326. def _coordinator_worker(self):
  327. """联动工作线程"""
  328. last_detection_time = 0
  329. # 优先使用 detection_fps,默认每秒2帧
  330. detection_fps = self.config.get('detection_fps', 2)
  331. detection_interval = 1.0 / detection_fps # 根据FPS计算间隔
  332. # 初始化统计
  333. with self.stats_lock:
  334. self.stats['start_time'] = time.time()
  335. while self.running:
  336. try:
  337. current_time = time.time()
  338. # 获取当前帧
  339. frame = self.panorama.get_frame()
  340. if frame is None:
  341. time.sleep(0.01)
  342. continue
  343. # 更新帧统计
  344. self._update_stats('frames_processed')
  345. frame_size = (frame.shape[1], frame.shape[0])
  346. # 周期性检测
  347. if current_time - last_detection_time >= detection_interval:
  348. last_detection_time = current_time
  349. # 检测人体
  350. detections = self._detect_persons(frame)
  351. # 更新检测统计
  352. if detections:
  353. self._update_stats('persons_detected', len(detections))
  354. # 更新跟踪
  355. tracked = self.tracker.update(detections)
  356. # 更新跟踪目标
  357. self._update_tracking_targets(tracked, frame_size)
  358. # 处理检测结果
  359. if tracked:
  360. self._process_detections(tracked, frame, frame_size)
  361. # 处理当前跟踪目标
  362. self._process_current_target(frame, frame_size)
  363. # 清理过期目标
  364. self._cleanup_expired_targets()
  365. time.sleep(0.01)
  366. except Exception as e:
  367. print(f"联动处理错误: {e}")
  368. time.sleep(0.1)
  369. def _detect_persons(self, frame: np.ndarray) -> List[DetectedObject]:
  370. """检测人体"""
  371. if not self.enable_detection or self.detector is None:
  372. return []
  373. return self.detector.detect_persons(frame)
  374. def _update_tracking_targets(self, detections: List[DetectedObject],
  375. frame_size: Tuple[int, int]):
  376. """更新跟踪目标"""
  377. current_time = time.time()
  378. frame_w, frame_h = frame_size
  379. center_x, center_y = frame_w / 2, frame_h / 2
  380. with self.targets_lock:
  381. # 更新现有目标
  382. for det in detections:
  383. if det.track_id is None:
  384. continue
  385. x_ratio = det.center[0] / frame_w
  386. y_ratio = det.center[1] / frame_h
  387. # 计算面积
  388. _, _, width, height = det.bbox
  389. area = width * height
  390. # 计算到画面中心的距离比例
  391. dx = abs(det.center[0] - center_x) / center_x
  392. dy = abs(det.center[1] - center_y) / center_y
  393. center_distance = (dx + dy) / 2 # 归一化到0-1
  394. if det.track_id in self.tracking_targets:
  395. # 更新位置
  396. target = self.tracking_targets[det.track_id]
  397. target.position = (x_ratio, y_ratio)
  398. target.last_update = current_time
  399. target.area = area
  400. target.confidence = det.confidence
  401. target.center_distance = center_distance
  402. else:
  403. # 新目标
  404. if len(self.tracking_targets) < self.config['max_tracking_targets']:
  405. self.tracking_targets[det.track_id] = TrackingTarget(
  406. track_id=det.track_id,
  407. position=(x_ratio, y_ratio),
  408. last_update=current_time,
  409. area=area,
  410. confidence=det.confidence,
  411. center_distance=center_distance
  412. )
  413. def _process_detections(self, detections: List[DetectedObject],
  414. frame: np.ndarray, frame_size: Tuple[int, int]):
  415. """处理检测结果"""
  416. if self.on_person_detected:
  417. for det in detections:
  418. self.on_person_detected(det, frame)
  419. def _process_current_target(self, frame: np.ndarray, frame_size: Tuple[int, int]):
  420. """处理当前跟踪目标"""
  421. with self.targets_lock:
  422. if not self.tracking_targets:
  423. self._set_state(TrackingState.IDLE)
  424. self.current_target = None
  425. return
  426. # 使用目标选择器选择最优目标
  427. self.current_target = self.target_selector.select_target(
  428. self.tracking_targets, frame_size
  429. )
  430. if self.current_target:
  431. # 移动球机到目标位置 (仅在 PTZ 跟踪启用时)
  432. if self.enable_ptz_tracking and self.enable_ptz_camera:
  433. self._set_state(TrackingState.TRACKING)
  434. x_ratio, y_ratio = self.current_target.position
  435. # 检查位置是否变化超过阈值
  436. should_move = True
  437. if self.last_ptz_position is not None:
  438. last_x, last_y = self.last_ptz_position
  439. if (abs(x_ratio - last_x) < self.ptz_position_threshold and
  440. abs(y_ratio - last_y) < self.ptz_position_threshold):
  441. should_move = False
  442. if should_move:
  443. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  444. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  445. if self.ptz.ptz_config.get('pan_flip', False):
  446. pan = (pan + 180) % 360
  447. zoom = self.ptz.ptz_config.get('default_zoom', 8)
  448. self.ptz.goto_exact_position(pan, tilt, zoom)
  449. else:
  450. self.ptz.track_target(x_ratio, y_ratio)
  451. self.last_ptz_position = (x_ratio, y_ratio)
  452. # 执行OCR识别 (仅在 OCR 启用时)
  453. if self.enable_ocr:
  454. self._perform_ocr(frame, self.current_target)
  455. def _perform_ocr(self, frame: np.ndarray, target: TrackingTarget):
  456. """执行OCR识别"""
  457. if not self.enable_ocr or self.number_detector is None:
  458. return
  459. # 频率控制 - 避免过于频繁调用OCR API
  460. current_time = time.time()
  461. if current_time - self.last_ocr_time < self.ocr_interval:
  462. return
  463. self.last_ocr_time = current_time
  464. # 更新OCR尝试统计
  465. self._update_stats('ocr_attempts')
  466. # 计算人体边界框 (基于位置估算)
  467. frame_h, frame_w = frame.shape[:2]
  468. # 人体占画面比例 (可配置,默认宽20%、高40%)
  469. person_width_ratio = self.config.get('person_width_ratio', 0.2)
  470. person_height_ratio = self.config.get('person_height_ratio', 0.4)
  471. person_width = int(frame_w * person_width_ratio)
  472. person_height = int(frame_h * person_height_ratio)
  473. x_ratio, y_ratio = target.position
  474. center_x = int(x_ratio * frame_w)
  475. center_y = int(y_ratio * frame_h)
  476. # 计算边界框,确保不超出画面范围
  477. x1 = max(0, center_x - person_width // 2)
  478. y1 = max(0, center_y - person_height // 2)
  479. x2 = min(frame_w, x1 + person_width)
  480. y2 = min(frame_h, y1 + person_height)
  481. # 更新实际宽高 (可能因边界裁剪而变小)
  482. actual_width = x2 - x1
  483. actual_height = y2 - y1
  484. person_bbox = (x1, y1, actual_width, actual_height)
  485. # 检测编号
  486. self._set_state(TrackingState.OCR_PROCESSING)
  487. person_info = self.number_detector.detect_number(frame, person_bbox)
  488. person_info.person_id = target.track_id
  489. # 更新OCR成功统计
  490. if person_info.number_text:
  491. self._update_stats('ocr_success')
  492. # 更新目标信息
  493. with self.targets_lock:
  494. if target.track_id in self.tracking_targets:
  495. self.tracking_targets[target.track_id].person_info = person_info
  496. # 回调
  497. if self.on_number_recognized and person_info.number_text:
  498. self.on_number_recognized(person_info)
  499. # 放入结果队列
  500. self.result_queue.put(person_info)
  501. def _cleanup_expired_targets(self):
  502. """清理过期目标"""
  503. current_time = time.time()
  504. timeout = self.config['tracking_timeout']
  505. with self.targets_lock:
  506. expired_ids = [
  507. target_id for target_id, target in self.tracking_targets.items()
  508. if current_time - target.last_update > timeout
  509. ]
  510. for target_id in expired_ids:
  511. del self.tracking_targets[target_id]
  512. if self.current_target and self.current_target.track_id == target_id:
  513. self.current_target = None
  514. def _set_state(self, state: TrackingState):
  515. """设置状态"""
  516. with self.state_lock:
  517. self.state = state
  518. def get_state(self) -> TrackingState:
  519. """获取状态"""
  520. with self.state_lock:
  521. return self.state
  522. def get_results(self) -> List[PersonInfo]:
  523. """
  524. 获取识别结果
  525. Returns:
  526. 人员信息列表
  527. """
  528. results = []
  529. while not self.result_queue.empty():
  530. try:
  531. results.append(self.result_queue.get_nowait())
  532. except queue.Empty:
  533. break
  534. return results
  535. def get_tracking_targets(self) -> List[TrackingTarget]:
  536. """获取当前跟踪目标"""
  537. with self.targets_lock:
  538. return list(self.tracking_targets.values())
  539. def force_track_position(self, x_ratio: float, y_ratio: float, zoom: int = None):
  540. """
  541. 强制跟踪指定位置
  542. Args:
  543. x_ratio: X方向比例
  544. y_ratio: Y方向比例
  545. zoom: 变倍
  546. """
  547. if self.enable_ptz_tracking and self.enable_ptz_camera:
  548. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  549. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  550. if self.ptz.ptz_config.get('pan_flip', False):
  551. pan = (pan + 180) % 360
  552. self.ptz.goto_exact_position(pan, tilt, zoom or self.ptz.ptz_config.get('default_zoom', 8))
  553. else:
  554. self.ptz.move_to_target(x_ratio, y_ratio, zoom)
  555. def capture_snapshot(self) -> Optional[np.ndarray]:
  556. """
  557. 抓拍快照
  558. Returns:
  559. 快照图像
  560. """
  561. return self.panorama.get_frame()
  562. class EventDrivenCoordinator(Coordinator):
  563. """事件驱动联动控制器,当全景摄像头检测到事件时触发联动"""
  564. def __init__(self, *args, **kwargs):
  565. super().__init__(*args, **kwargs)
  566. self.event_types = {
  567. 'intruder': True,
  568. 'crossline': True,
  569. 'motion': True,
  570. }
  571. self.event_queue = queue.Queue()
  572. def on_event(self, event_type: str, event_data: dict):
  573. if not self.event_types.get(event_type, False):
  574. return
  575. self.event_queue.put({'type': event_type, 'data': event_data, 'time': time.time()})
  576. def _coordinator_worker(self):
  577. while self.running:
  578. try:
  579. try:
  580. event = self.event_queue.get(timeout=0.1)
  581. self._process_event(event)
  582. except queue.Empty:
  583. pass
  584. frame = self.panorama.get_frame()
  585. if frame is not None:
  586. frame_size = (frame.shape[1], frame.shape[0])
  587. detections = self._detect_persons(frame)
  588. if detections:
  589. tracked = self.tracker.update(detections)
  590. self._update_tracking_targets(tracked, frame_size)
  591. self._process_current_target(frame, frame_size)
  592. self._cleanup_expired_targets()
  593. except Exception as e:
  594. print(f"事件处理错误: {e}")
  595. time.sleep(0.1)
  596. def _process_event(self, event: dict):
  597. event_type = event['type']
  598. event_data = event['data']
  599. print(f"处理事件: {event_type}")
  600. if event_type == 'intruder' and 'position' in event_data:
  601. x_ratio, y_ratio = event_data['position']
  602. self.force_track_position(x_ratio, y_ratio)
  603. @dataclass
  604. class PTZCommand:
  605. """PTZ控制命令"""
  606. pan: float
  607. tilt: float
  608. zoom: int
  609. x_ratio: float = 0.0
  610. y_ratio: float = 0.0
  611. use_calibration: bool = True
  612. class AsyncCoordinator(Coordinator):
  613. """
  614. 异步联动控制器 — 检测线程与PTZ控制线程分离
  615. 改进:
  616. 1. 检测线程:持续读取全景帧 + YOLO推理
  617. 2. PTZ控制线程:通过命令队列接收目标位置,独立控制球机
  618. 3. 两线程通过 queue 通信,互不阻塞
  619. 4. PTZ位置确认:移动后等待球机到位并验证帧
  620. """
  621. PTZ_CONFIRM_WAIT = 0.3 # PTZ命令后等待稳定的秒数
  622. PTZ_CONFIRM_TIMEOUT = 2.0 # PTZ位置确认超时
  623. PTZ_COMMAND_COOLDOWN = 0.15 # PTZ命令最小间隔秒数
  624. def __init__(self, *args, **kwargs):
  625. super().__init__(*args, **kwargs)
  626. # PTZ命令队列(检测→PTZ)
  627. self._ptz_queue: queue.Queue = queue.Queue(maxsize=10)
  628. # 线程
  629. self._detection_thread = None
  630. self._ptz_thread = None
  631. # PTZ确认回调
  632. self._on_ptz_confirmed: Optional[Callable] = None
  633. # 上次PTZ命令时间
  634. self._last_ptz_time = 0.0
  635. def start(self) -> bool:
  636. """启动联动(覆盖父类,启动双线程)"""
  637. if not self.panorama.connect():
  638. print("连接全景摄像头失败")
  639. return False
  640. if self.enable_ptz_camera:
  641. if not self.ptz.connect():
  642. print("连接球机失败")
  643. self.panorama.disconnect()
  644. return False
  645. # 启动球机RTSP流(用于球机端人体检测)
  646. if self.enable_ptz_detection:
  647. if not self.ptz.start_stream_rtsp():
  648. print("球机RTSP流启动失败,禁用球机端检测功能")
  649. self.enable_ptz_detection = False
  650. else:
  651. # 初始化球机端人体检测器
  652. self._init_ptz_detector()
  653. else:
  654. print("PTZ球机功能已禁用")
  655. if not self.panorama.start_stream_rtsp():
  656. print("RTSP视频流启动失败,尝试SDK方式...")
  657. if not self.panorama.start_stream():
  658. print("启动视频流失败")
  659. self.panorama.disconnect()
  660. if self.enable_ptz_camera:
  661. self.ptz.disconnect()
  662. return False
  663. self.running = True
  664. # 启动检测线程
  665. self._detection_thread = threading.Thread(
  666. target=self._detection_worker, name="detection-worker", daemon=True)
  667. self._detection_thread.start()
  668. # 启动PTZ控制线程
  669. if self.enable_ptz_camera and self.enable_ptz_tracking:
  670. self._ptz_thread = threading.Thread(
  671. target=self._ptz_worker, name="ptz-worker", daemon=True)
  672. self._ptz_thread.start()
  673. print("异步联动系统已启动 (检测线程 + PTZ控制线程)")
  674. return True
  675. def stop(self):
  676. """停止联动"""
  677. self.running = False
  678. # 清空PTZ队列,让工作线程退出
  679. while not self._ptz_queue.empty():
  680. try:
  681. self._ptz_queue.get_nowait()
  682. except queue.Empty:
  683. break
  684. if self._detection_thread:
  685. self._detection_thread.join(timeout=3)
  686. if self._ptz_thread:
  687. self._ptz_thread.join(timeout=3)
  688. # 停止父类线程(如果有的话)
  689. if self.coordinator_thread:
  690. self.coordinator_thread.join(timeout=1)
  691. self.panorama.disconnect()
  692. if self.enable_ptz_camera:
  693. self.ptz.disconnect()
  694. self._print_stats()
  695. print("异步联动系统已停止")
  696. def _detection_worker(self):
  697. """检测线程:持续读帧 + YOLO推理 + 发送PTZ命令 + 打印检测日志"""
  698. last_detection_time = 0
  699. # 优先使用 detection_fps,默认每秒2帧
  700. detection_fps = self.config.get('detection_fps', 2)
  701. detection_interval = 1.0 / detection_fps # 根据FPS计算间隔
  702. ptz_cooldown = self.config.get('ptz_command_cooldown', 0.5)
  703. ptz_threshold = self.config.get('ptz_position_threshold', 0.03)
  704. frame_count = 0
  705. last_log_time = time.time()
  706. log_interval = 5.0 # 每5秒打印一次帧率统计
  707. detection_run_count = 0
  708. detection_person_count = 0
  709. last_no_detect_log_time = 0
  710. no_detect_log_interval = 30.0
  711. with self.stats_lock:
  712. self.stats['start_time'] = time.time()
  713. if self.detector is None:
  714. logger.warning("[检测线程] ⚠️ 人体检测器未初始化! 检测功能不可用, 请检查 YOLO 模型是否正确加载")
  715. elif not self.enable_detection:
  716. logger.warning("[检测线程] ⚠️ 人体检测已禁用 (enable_detection=False)")
  717. else:
  718. logger.info(f"[检测线程] ✓ 人体检测器已就绪, 检测帧率={detection_fps}fps(间隔={detection_interval:.2f}s), PTZ冷却={ptz_cooldown}s")
  719. while self.running:
  720. try:
  721. current_time = time.time()
  722. frame = self.panorama.get_frame()
  723. if frame is None:
  724. time.sleep(0.01)
  725. continue
  726. frame_count += 1
  727. self._update_stats('frames_processed')
  728. frame_size = (frame.shape[1], frame.shape[0])
  729. if current_time - last_log_time >= log_interval:
  730. elapsed = current_time - last_log_time
  731. fps = frame_count / elapsed if elapsed > 0 else 0
  732. state_str = self.state.name if hasattr(self.state, 'name') else str(self.state)
  733. stats_parts = [f"帧率={fps:.1f}fps", f"处理帧={frame_count}", f"状态={state_str}"]
  734. if self.detector is None:
  735. stats_parts.append("检测器=未加载")
  736. elif not self.enable_detection:
  737. stats_parts.append("检测=已禁用")
  738. else:
  739. stats_parts.append(f"检测轮次={detection_run_count}(有人={detection_person_count})")
  740. with self.targets_lock:
  741. target_count = len(self.tracking_targets)
  742. stats_parts.append(f"跟踪目标={target_count}")
  743. logger.info(f"[检测线程] {', '.join(stats_parts)}")
  744. frame_count = 0
  745. last_log_time = current_time
  746. # 周期性检测(约1次/秒)
  747. if current_time - last_detection_time >= detection_interval:
  748. last_detection_time = current_time
  749. detection_run_count += 1
  750. # YOLO 人体检测
  751. detections = self._detect_persons(frame)
  752. if detections:
  753. self._update_stats('persons_detected', len(detections))
  754. detection_person_count += 1
  755. # 更新跟踪
  756. tracked = self.tracker.update(detections)
  757. self._update_tracking_targets(tracked, frame_size)
  758. # 打印检测日志
  759. if tracked:
  760. for t in tracked:
  761. # tracked 是 DetectedObject,使用 center 计算位置
  762. x_ratio = t.center[0] / frame_size[0]
  763. y_ratio = t.center[1] / frame_size[1]
  764. _, _, w, h = t.bbox
  765. area = w * h
  766. logger.info(
  767. f"[检测] ✓ 目标ID={t.track_id} "
  768. f"位置=({x_ratio:.3f}, {y_ratio:.3f}) "
  769. f"面积={area} 置信度={t.confidence:.2f}"
  770. )
  771. elif detections:
  772. # 有检测但没跟踪上
  773. for d in detections:
  774. logger.debug(f"[检测] 未跟踪: {d.class_name} @ {d.center}")
  775. else:
  776. if current_time - last_no_detect_log_time >= no_detect_log_interval:
  777. logger.info(
  778. f"[检测] · YOLO检测运行正常, 本轮未检测到人员 "
  779. f"(累计检测{detection_run_count}轮, 检测到人{detection_person_count}轮)"
  780. )
  781. last_no_detect_log_time = current_time
  782. if tracked:
  783. self._process_detections(tracked, frame, frame_size)
  784. # 选择跟踪目标并发送PTZ命令
  785. target = self._select_tracking_target()
  786. if target and self.enable_ptz_tracking and self.enable_ptz_camera:
  787. self._send_ptz_command_with_log(target, frame_size)
  788. elif not tracked and self.current_target:
  789. # 目标消失,切回IDLE
  790. self._set_state(TrackingState.IDLE)
  791. logger.info("[检测] 目标丢失,球机进入IDLE状态")
  792. self.current_target = None
  793. self._cleanup_expired_targets()
  794. time.sleep(0.01)
  795. except Exception as e:
  796. logger.error(f"检测线程错误: {e}")
  797. time.sleep(0.1)
  798. def _init_ptz_detector(self):
  799. """初始化球机端人体检测器"""
  800. try:
  801. model_path = DETECTION_CONFIG.get('model_path')
  802. model_type = DETECTION_CONFIG.get('model_type', 'auto')
  803. conf_threshold = DETECTION_CONFIG.get('person_threshold', 0.5)
  804. if model_path:
  805. self.ptz_detector = PTZPersonDetector(
  806. model_path=model_path,
  807. model_type=model_type,
  808. confidence_threshold=conf_threshold
  809. )
  810. self.auto_zoom_controller = PTZAutoZoomController(
  811. ptz_camera=self.ptz,
  812. detector=self.ptz_detector,
  813. config=self.auto_zoom_config
  814. )
  815. print(f"[AsyncCoordinator] 球机端人体检测器初始化成功")
  816. else:
  817. print("[AsyncCoordinator] 未配置球机检测模型路径,禁用球机端检测")
  818. self.enable_ptz_detection = False
  819. except Exception as e:
  820. print(f"[AsyncCoordinator] 球机端检测器初始化失败: {e}")
  821. self.enable_ptz_detection = False
  822. def _ptz_worker(self):
  823. """PTZ控制线程:从队列接收命令并控制球机"""
  824. while self.running:
  825. try:
  826. try:
  827. cmd = self._ptz_queue.get(timeout=0.1)
  828. except queue.Empty:
  829. continue
  830. self._execute_ptz_command(cmd)
  831. except Exception as e:
  832. print(f"PTZ控制线程错误: {e}")
  833. time.sleep(0.05)
  834. def _select_tracking_target(self) -> Optional[TrackingTarget]:
  835. """选择当前跟踪目标"""
  836. with self.targets_lock:
  837. if not self.tracking_targets:
  838. self._set_state(TrackingState.IDLE)
  839. self.current_target = None
  840. return None
  841. # 使用目标选择器选择最优目标
  842. self.current_target = self.target_selector.select_target(
  843. self.tracking_targets
  844. )
  845. return self.current_target
  846. def _send_ptz_command(self, target: TrackingTarget, frame_size: Tuple[int, int]):
  847. """将跟踪目标转化为PTZ命令放入队列"""
  848. x_ratio, y_ratio = target.position
  849. # 检查位置变化是否超过阈值
  850. if self.last_ptz_position is not None:
  851. last_x, last_y = self.last_ptz_position
  852. if abs(x_ratio - last_x) < self.ptz_position_threshold and \
  853. abs(y_ratio - last_y) < self.ptz_position_threshold:
  854. return
  855. # 冷却检查
  856. current_time = time.time()
  857. if current_time - self._last_ptz_time < self.PTZ_COMMAND_COOLDOWN:
  858. return
  859. cmd = PTZCommand(
  860. pan=0, tilt=0, zoom=0,
  861. x_ratio=x_ratio, y_ratio=y_ratio,
  862. use_calibration=self.enable_calibration
  863. )
  864. try:
  865. self._ptz_queue.put_nowait(cmd)
  866. self.last_ptz_position = (x_ratio, y_ratio)
  867. except queue.Full:
  868. pass # 丢弃命令,下一个检测周期会重发
  869. def _send_ptz_command_with_log(self, target: TrackingTarget, frame_size: Tuple[int, int]):
  870. """发送PTZ命令并打印日志"""
  871. x_ratio, y_ratio = target.position
  872. # 计算PTZ角度(用于日志)
  873. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  874. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  875. zoom = self.ptz.ptz_config.get('default_zoom', 8)
  876. coord_type = "校准坐标"
  877. else:
  878. pan, tilt, zoom = self.ptz.calculate_ptz_position(x_ratio, y_ratio)
  879. coord_type = "估算坐标"
  880. logger.info(
  881. f"[PTZ] 发送命令: 目标ID={target.track_id} "
  882. f"全景位置=({x_ratio:.3f}, {y_ratio:.3f}) → "
  883. f"PTZ角度=(pan={pan:.1f}°, tilt={tilt:.1f}°, zoom={zoom}) [{coord_type}]"
  884. )
  885. # 检查位置变化是否超过阈值
  886. ptz_threshold = self.config.get('ptz_position_threshold', 0.03)
  887. if self.last_ptz_position is not None:
  888. last_x, last_y = self.last_ptz_position
  889. dx = abs(x_ratio - last_x)
  890. dy = abs(y_ratio - last_y)
  891. if dx < ptz_threshold and dy < ptz_threshold:
  892. logger.debug(f"[PTZ] 位置变化太小(dx={dx:.4f}, dy={dy:.4f}),跳过")
  893. return
  894. # 冷却检查
  895. current_time = time.time()
  896. ptz_cooldown = self.config.get('ptz_command_cooldown', 0.5)
  897. if current_time - self._last_ptz_time < ptz_cooldown:
  898. logger.debug(f"[PTZ] 冷却中,跳过 (间隔={current_time - self._last_ptz_time:.2f}s < {ptz_cooldown}s)")
  899. return
  900. cmd = PTZCommand(
  901. pan=0, tilt=0, zoom=0,
  902. x_ratio=x_ratio, y_ratio=y_ratio,
  903. use_calibration=self.enable_calibration
  904. )
  905. try:
  906. self._ptz_queue.put_nowait(cmd)
  907. self.last_ptz_position = (x_ratio, y_ratio)
  908. self._update_stats('ptz_commands_sent' if 'ptz_commands_sent' in self.stats else 'persons_detected')
  909. except queue.Full:
  910. logger.warning("[PTZ] 命令队列满,丢弃本次命令")
  911. def _execute_ptz_command(self, cmd: PTZCommand):
  912. """执行PTZ命令(在PTZ线程中)"""
  913. self._last_ptz_time = time.time()
  914. if cmd.use_calibration and self.calibrator and self.calibrator.is_calibrated():
  915. pan, tilt = self.calibrator.transform(cmd.x_ratio, cmd.y_ratio)
  916. if self.ptz.ptz_config.get('pan_flip', False):
  917. pan = (pan + 180) % 360
  918. zoom = self.ptz.ptz_config.get('default_zoom', 8)
  919. else:
  920. pan, tilt, zoom = self.ptz.calculate_ptz_position(cmd.x_ratio, cmd.y_ratio)
  921. self._set_state(TrackingState.TRACKING)
  922. logger.info(
  923. f"[PTZ] 执行: pan={pan:.1f}° tilt={tilt:.1f}° zoom={zoom} "
  924. f"(全景位置=({cmd.x_ratio:.3f}, {cmd.y_ratio:.3f}))"
  925. )
  926. success = self.ptz.goto_exact_position(pan, tilt, zoom)
  927. if success:
  928. time.sleep(self.PTZ_CONFIRM_WAIT)
  929. # 球机端人体检测与自动对焦
  930. if self.enable_ptz_detection and self.auto_zoom_config.get('enabled', False):
  931. self._auto_zoom_person(pan, tilt, zoom)
  932. else:
  933. self._confirm_ptz_position(cmd.x_ratio, cmd.y_ratio)
  934. logger.info(f"[PTZ] 到位确认完成: pan={pan:.1f}° tilt={tilt:.1f}°")
  935. else:
  936. logger.warning(f"[PTZ] 命令执行失败: pan={pan:.1f}° tilt={tilt:.1f}° zoom={zoom}")
  937. def _auto_zoom_person(self, initial_pan: float, initial_tilt: float, initial_zoom: int):
  938. """
  939. 自动对焦人体
  940. 在球机画面中检测人体,自动调整zoom使人体居中且大小合适
  941. """
  942. if self.auto_zoom_controller is None:
  943. return
  944. logger.info("[AutoZoom] 开始自动对焦...")
  945. try:
  946. success, final_zoom = self.auto_zoom_controller.auto_focus_loop(
  947. get_frame_func=self.ptz.get_frame,
  948. max_attempts=self.auto_zoom_config.get('max_adjust_attempts', 3)
  949. )
  950. if success:
  951. logger.info(f"[AutoZoom] 自动对焦成功: zoom={final_zoom}")
  952. else:
  953. logger.warning("[AutoZoom] 自动对焦未能定位人体")
  954. except Exception as e:
  955. logger.error(f"[AutoZoom] 自动对焦异常: {e}")
  956. def _confirm_ptz_position(self, x_ratio: float, y_ratio: float):
  957. """PTZ位置确认:读取球机帧验证目标是否可见"""
  958. if not hasattr(self.ptz, 'get_frame') or self.ptz.get_frame() is None:
  959. return
  960. ptz_frame = self.ptz.get_frame()
  961. if ptz_frame is None:
  962. return
  963. # 未来可以在这里添加球机帧目标验证逻辑
  964. # 例如:在球机帧中检测目标是否在画面中心附近
  965. def on_ptz_confirmed(self, callback: Callable):
  966. """注册PTZ位置确认回调"""
  967. self._on_ptz_confirmed = callback