coordinator.py 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. """
  2. 联动控制器
  3. 协调全景摄像头和球机的工作
  4. """
  5. import time
  6. import threading
  7. import queue
  8. import logging
  9. import math
  10. from typing import Optional, List, Dict, Tuple, Callable
  11. from dataclasses import dataclass, field
  12. from enum import Enum
  13. import numpy as np
  14. import cv2
  15. from config import COORDINATOR_CONFIG, SYSTEM_CONFIG, PTZ_CONFIG, DETECTION_CONFIG
  16. from panorama_camera import PanoramaCamera, ObjectDetector, DetectedObject
  17. from ptz_camera import PTZCamera, PTZController
  18. from ocr_recognizer import NumberDetector, PersonInfo
  19. from ptz_person_tracker import PTZPersonDetector, PTZAutoZoomController
  20. from paired_image_saver import PairedImageSaver, get_paired_saver
  21. logger = logging.getLogger(__name__)
  22. class TrackingState(Enum):
  23. """跟踪状态"""
  24. IDLE = 0 # 空闲
  25. SEARCHING = 1 # 搜索目标
  26. TRACKING = 2 # 跟踪中
  27. ZOOMING = 3 # 变焦中
  28. OCR_PROCESSING = 4 # OCR处理中
  29. @dataclass
  30. class TrackingTarget:
  31. """跟踪目标"""
  32. track_id: int # 跟踪ID
  33. position: Tuple[float, float] # 位置比例 (x_ratio, y_ratio)
  34. last_update: float # 最后更新时间
  35. person_info: Optional[PersonInfo] = None # 人员信息
  36. priority: int = 0 # 优先级
  37. area: int = 0 # 目标面积(像素²)
  38. confidence: float = 0.0 # 置信度
  39. center_distance: float = 1.0 # 到画面中心的距离比例(0-1)
  40. score: float = 0.0 # 综合得分
  41. class TargetSelector:
  42. """
  43. 目标选择策略类
  44. 支持按面积、置信度、混合模式排序,支持优先级切换
  45. """
  46. def __init__(self, config: Dict = None):
  47. """
  48. 初始化目标选择器
  49. Args:
  50. config: 目标选择配置
  51. """
  52. self.config = config or {
  53. 'strategy': 'area',
  54. 'area_weight': 0.6,
  55. 'confidence_weight': 0.4,
  56. 'min_area_threshold': 5000,
  57. 'prefer_center': True,
  58. 'center_weight': 0.2,
  59. 'switch_on_lost': True,
  60. 'stickiness': 0.3,
  61. }
  62. self.current_target_id: Optional[int] = None
  63. self.current_target_score: float = 0.0
  64. def calculate_score(self, target: TrackingTarget, frame_size: Tuple[int, int] = None) -> float:
  65. """
  66. 计算目标综合得分
  67. Args:
  68. target: 跟踪目标
  69. frame_size: 帧尺寸(w, h),用于计算中心距离
  70. Returns:
  71. 综合得分(0-1)
  72. """
  73. strategy = self.config.get('strategy', 'area')
  74. area_weight = self.config.get('area_weight', 0.6)
  75. conf_weight = self.config.get('confidence_weight', 0.4)
  76. min_area = self.config.get('min_area_threshold', 5000)
  77. prefer_center = self.config.get('prefer_center', False)
  78. center_weight = self.config.get('center_weight', 0.2)
  79. # 归一化面积得分 (对数缩放,避免大目标得分过高)
  80. import math
  81. area_score = min(1.0, math.log10(max(target.area, 1)) / 5.0) # 100000像素² ≈ 1.0
  82. # 小面积惩罚
  83. if target.area < min_area:
  84. area_score *= 0.5
  85. # 置信度得分直接使用
  86. conf_score = target.confidence
  87. # 中心距离得分 (距离中心越近得分越高)
  88. center_score = 1.0 - target.center_distance
  89. # 根据策略计算综合得分
  90. if strategy == 'area':
  91. score = area_score * 0.8 + conf_score * 0.2
  92. elif strategy == 'confidence':
  93. score = conf_score * 0.8 + area_score * 0.2
  94. else: # hybrid
  95. score = area_score * area_weight + conf_score * conf_weight
  96. # 加入中心距离权重
  97. if prefer_center:
  98. score = score * (1 - center_weight) + center_score * center_weight
  99. return score
  100. def select_target(self, targets: Dict[int, TrackingTarget],
  101. frame_size: Tuple[int, int] = None) -> Optional[TrackingTarget]:
  102. """
  103. 从多个目标中选择最优目标
  104. Args:
  105. targets: 目标字典 {track_id: TrackingTarget}
  106. frame_size: 帧尺寸
  107. Returns:
  108. 最优目标
  109. """
  110. if not targets:
  111. self.current_target_id = None
  112. return None
  113. stickiness = self.config.get('stickiness', 0.3)
  114. switch_on_lost = self.config.get('switch_on_lost', True)
  115. # 计算所有目标得分
  116. scored_targets = []
  117. for track_id, target in targets.items():
  118. target.score = self.calculate_score(target, frame_size)
  119. scored_targets.append((track_id, target, target.score))
  120. # 按得分排序
  121. scored_targets.sort(key=lambda x: x[2], reverse=True)
  122. # 检查当前目标是否仍在列表中
  123. if self.current_target_id is not None:
  124. current_exists = self.current_target_id in targets
  125. if current_exists:
  126. # 应用粘性:当前目标得分需要显著低于最优目标才切换
  127. best_id, best_target, best_score = scored_targets[0]
  128. current_target = targets[self.current_target_id]
  129. # 粘性阈值: 当前目标得分 > 最优得分 * (1 - stickiness) 时保持
  130. stickiness_threshold = best_score * (1 - stickiness)
  131. if current_target.score > stickiness_threshold:
  132. return current_target
  133. # 选择得分最高的目标
  134. best_id, best_target, best_score = scored_targets[0]
  135. self.current_target_id = best_id
  136. self.current_target_score = best_score
  137. logger.debug(
  138. f"[目标选择] 选择目标ID={best_id} 得分={best_score:.3f} "
  139. f"面积={best_target.area} 置信度={best_target.confidence:.2f}"
  140. )
  141. return best_target
  142. def get_sorted_targets(self, targets: Dict[int, TrackingTarget],
  143. frame_size: Tuple[int, int] = None) -> List[Tuple[TrackingTarget, float]]:
  144. """
  145. 获取按得分排序的目标列表
  146. Args:
  147. targets: 目标字典
  148. frame_size: 帧尺寸
  149. Returns:
  150. 排序后的目标列表 [(target, score), ...]
  151. """
  152. scored = []
  153. for target in targets.values():
  154. target.score = self.calculate_score(target, frame_size)
  155. scored.append((target, target.score))
  156. scored.sort(key=lambda x: x[1], reverse=True)
  157. return scored
  158. def set_strategy(self, strategy: str):
  159. """设置选择策略"""
  160. self.config['strategy'] = strategy
  161. logger.info(f"[目标选择] 策略已切换为: {strategy}")
  162. def set_stickiness(self, stickiness: float):
  163. """设置目标粘性"""
  164. self.config['stickiness'] = max(0.0, min(1.0, stickiness))
  165. logger.info(f"[目标选择] 粘性已设置为: {self.config['stickiness']}")
  166. class Coordinator:
  167. """
  168. 联动控制器
  169. 协调全景摄像头和球机实现联动抓拍
  170. """
  171. def __init__(self, panorama_camera: PanoramaCamera,
  172. ptz_camera: PTZCamera,
  173. detector: ObjectDetector = None,
  174. number_detector: NumberDetector = None,
  175. calibrator = None):
  176. """
  177. 初始化联动控制器
  178. Args:
  179. panorama_camera: 全景摄像头
  180. ptz_camera: 球机
  181. detector: 物体检测器
  182. number_detector: 编号检测器
  183. calibrator: 校准器 (用于坐标转换)
  184. """
  185. self.panorama = panorama_camera
  186. self.ptz = ptz_camera
  187. self.detector = detector
  188. self.number_detector = number_detector
  189. self.calibrator = calibrator
  190. self.config = COORDINATOR_CONFIG
  191. # 功能开关 - 从 SYSTEM_CONFIG 读取
  192. self.enable_ptz_camera = SYSTEM_CONFIG.get('enable_ptz_camera', True)
  193. self.enable_ptz_tracking = SYSTEM_CONFIG.get('enable_ptz_tracking', True)
  194. self.enable_calibration = SYSTEM_CONFIG.get('enable_calibration', True)
  195. self.enable_detection = SYSTEM_CONFIG.get('enable_detection', True)
  196. self.enable_ocr = SYSTEM_CONFIG.get('enable_ocr', True)
  197. # 球机端人体检测与自动对焦
  198. self.enable_ptz_detection = PTZ_CONFIG.get('enable_ptz_detection', False)
  199. self.auto_zoom_config = PTZ_CONFIG.get('auto_zoom', {})
  200. self.ptz_detector = None
  201. self.auto_zoom_controller = None
  202. # 状态
  203. self.state = TrackingState.IDLE
  204. self.state_lock = threading.Lock()
  205. # 跟踪目标
  206. self.tracking_targets: Dict[int, TrackingTarget] = {}
  207. self.targets_lock = threading.Lock()
  208. # 当前跟踪目标
  209. self.current_target: Optional[TrackingTarget] = None
  210. # 回调函数
  211. self.on_person_detected: Optional[Callable] = None
  212. self.on_number_recognized: Optional[Callable] = None
  213. self.on_tracking_started: Optional[Callable] = None
  214. self.on_tracking_stopped: Optional[Callable] = None
  215. # 控制标志
  216. self.running = False
  217. self.coordinator_thread = None
  218. # OCR频率控制
  219. self.last_ocr_time = 0
  220. self.ocr_interval = 1.0 # OCR间隔(秒),避免过于频繁调用API
  221. # PTZ优化 - 避免频繁发送相同位置的命令
  222. self.last_ptz_position = None
  223. self.ptz_position_threshold = self.config.get('ptz_position_threshold', 0.03)
  224. # 目标选择器
  225. self.target_selector = TargetSelector(
  226. self.config.get('target_selection', {})
  227. )
  228. # 结果队列
  229. self.result_queue = queue.Queue()
  230. # 性能统计
  231. self.stats = {
  232. 'frames_processed': 0,
  233. 'persons_detected': 0,
  234. 'ocr_attempts': 0,
  235. 'ocr_success': 0,
  236. 'start_time': None,
  237. 'last_frame_time': None,
  238. }
  239. self.stats_lock = threading.Lock()
  240. def set_calibrator(self, calibrator):
  241. """设置校准器"""
  242. self.calibrator = calibrator
  243. def _transform_position(self, x_ratio: float, y_ratio: float) -> Tuple[float, float, int]:
  244. """
  245. 将全景坐标转换为PTZ角度
  246. Args:
  247. x_ratio: X方向比例
  248. y_ratio: Y方向比例
  249. Returns:
  250. (pan, tilt, zoom)
  251. """
  252. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  253. # 使用校准结果进行转换
  254. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  255. zoom = 8 # 默认变倍
  256. else:
  257. # 使用默认估算
  258. pan, tilt, zoom = self.ptz.calculate_ptz_position(x_ratio, y_ratio)
  259. return (pan, tilt, zoom)
  260. def start(self) -> bool:
  261. """
  262. 启动联动系统
  263. Returns:
  264. 是否成功
  265. """
  266. # 连接全景摄像头
  267. if not self.panorama.connect():
  268. print("连接全景摄像头失败")
  269. return False
  270. # 连接 PTZ 球机 (可选)
  271. if self.enable_ptz_camera:
  272. if not self.ptz.connect():
  273. print("连接球机失败")
  274. self.panorama.disconnect()
  275. return False
  276. else:
  277. print("PTZ 球机功能已禁用")
  278. # 启动视频流(优先RTSP,SDK回调不可用时回退)
  279. if not self.panorama.start_stream_rtsp():
  280. print("RTSP视频流启动失败,尝试SDK方式...")
  281. if not self.panorama.start_stream():
  282. print("启动视频流失败")
  283. self.panorama.disconnect()
  284. if self.enable_ptz_camera:
  285. self.ptz.disconnect()
  286. return False
  287. # 启动联动线程
  288. self.running = True
  289. self.coordinator_thread = threading.Thread(target=self._coordinator_worker, daemon=True)
  290. self.coordinator_thread.start()
  291. print("联动系统已启动")
  292. return True
  293. def stop(self):
  294. """停止联动系统"""
  295. self.running = False
  296. if self.coordinator_thread:
  297. self.coordinator_thread.join(timeout=3)
  298. self.panorama.disconnect()
  299. if self.enable_ptz_camera:
  300. self.ptz.disconnect()
  301. # 打印统计信息
  302. self._print_stats()
  303. print("联动系统已停止")
  304. def _update_stats(self, key: str, value: int = 1):
  305. """更新统计信息"""
  306. with self.stats_lock:
  307. if key in self.stats:
  308. self.stats[key] += value
  309. def _print_stats(self):
  310. """打印统计信息"""
  311. with self.stats_lock:
  312. if self.stats['start_time'] and self.stats['frames_processed'] > 0:
  313. elapsed = time.time() - self.stats['start_time']
  314. fps = self.stats['frames_processed'] / elapsed
  315. print("\n=== 性能统计 ===")
  316. print(f"运行时长: {elapsed:.1f}秒")
  317. print(f"处理帧数: {self.stats['frames_processed']}")
  318. print(f"平均帧率: {fps:.1f} fps")
  319. print(f"检测人体: {self.stats['persons_detected']}次")
  320. print(f"OCR尝试: {self.stats['ocr_attempts']}次")
  321. print(f"OCR成功: {self.stats['ocr_success']}次")
  322. print("================\n")
  323. def get_stats(self) -> dict:
  324. """获取统计信息"""
  325. with self.stats_lock:
  326. return self.stats.copy()
  327. def _coordinator_worker(self):
  328. """联动工作线程"""
  329. last_detection_time = 0
  330. # 从 DETECTION_CONFIG 获取检测帧率,默认每秒2帧
  331. detection_fps = self.config.get('detection_fps', DETECTION_CONFIG.get('detection_fps', 2))
  332. detection_interval = 1.0 / detection_fps # 根据FPS计算间隔
  333. # 初始化统计
  334. with self.stats_lock:
  335. self.stats['start_time'] = time.time()
  336. while self.running:
  337. try:
  338. current_time = time.time()
  339. # 获取当前帧
  340. frame = self.panorama.get_frame()
  341. if frame is None:
  342. time.sleep(0.01)
  343. continue
  344. # 更新帧统计
  345. self._update_stats('frames_processed')
  346. frame_size = (frame.shape[1], frame.shape[0])
  347. # 周期性检测
  348. if current_time - last_detection_time >= detection_interval:
  349. last_detection_time = current_time
  350. # 检测人体
  351. detections = self._detect_persons(frame)
  352. # 更新检测统计
  353. if detections:
  354. self._update_stats('persons_detected', len(detections))
  355. # 更新跟踪目标(track_id 在此方法内分配)
  356. self._update_tracking_targets(detections, frame_size)
  357. # 处理检测结果
  358. if detections:
  359. self._process_detections(detections, frame, frame_size)
  360. # 处理当前跟踪目标
  361. self._process_current_target(frame, frame_size)
  362. # 清理过期目标
  363. self._cleanup_expired_targets()
  364. time.sleep(0.01)
  365. except Exception as e:
  366. print(f"联动处理错误: {e}")
  367. time.sleep(0.1)
  368. def _detect_persons(self, frame: np.ndarray) -> List[DetectedObject]:
  369. """检测人体"""
  370. if not self.enable_detection or self.detector is None:
  371. return []
  372. return self.detector.detect_persons(frame)
  373. def _update_tracking_targets(self, detections: List[DetectedObject],
  374. frame_size: Tuple[int, int]):
  375. """更新跟踪目标(跨帧匹配,支持粘性跟踪)
  376. 改进:不再每轮清空目标,而是使用位置匹配关联连续帧的目标
  377. """
  378. current_time = time.time()
  379. frame_w, frame_h = frame_size
  380. center_x, center_y = frame_w / 2, frame_h / 2
  381. # 获取人员置信度阈值
  382. person_threshold = DETECTION_CONFIG.get('person_threshold', 0.8)
  383. # 过滤有效人员
  384. valid_detections = []
  385. for det in detections:
  386. if det.class_name != 'person':
  387. continue
  388. if det.confidence < person_threshold:
  389. continue
  390. valid_detections.append(det)
  391. with self.targets_lock:
  392. # 匹配阈值:位置距离小于此值认为是同一目标
  393. MATCH_THRESHOLD = 0.15 # 画面比例
  394. # 已匹配的检测索引
  395. matched_det_indices = set()
  396. # 步骤1:尝试匹配现有目标
  397. for track_id, target in list(self.tracking_targets.items()):
  398. best_match_idx = None
  399. best_match_dist = MATCH_THRESHOLD
  400. for idx, det in enumerate(valid_detections):
  401. if idx in matched_det_indices:
  402. continue
  403. det_x = det.center[0] / frame_w
  404. det_y = det.center[1] / frame_h
  405. # 计算位置距离
  406. dist = math.sqrt(
  407. (det_x - target.position[0]) ** 2 +
  408. (det_y - target.position[1]) ** 2
  409. )
  410. if dist < best_match_dist:
  411. best_match_dist = dist
  412. best_match_idx = idx
  413. if best_match_idx is not None:
  414. # 找到匹配,更新目标
  415. det = valid_detections[best_match_idx]
  416. matched_det_indices.add(best_match_idx)
  417. x_ratio = det.center[0] / frame_w
  418. y_ratio = det.center[1] / frame_h
  419. _, _, width, height = det.bbox
  420. area = width * height
  421. dx = abs(det.center[0] - center_x) / center_x
  422. dy = abs(det.center[1] - center_y) / center_y
  423. center_distance = (dx + dy) / 2
  424. # 更新目标属性
  425. self.tracking_targets[track_id] = TrackingTarget(
  426. track_id=track_id,
  427. position=(x_ratio, y_ratio),
  428. last_update=current_time,
  429. area=area,
  430. confidence=det.confidence,
  431. center_distance=center_distance,
  432. person_info=target.person_info # 保留之前识别的信息
  433. )
  434. # 步骤2:为未匹配的检测创建新目标
  435. for idx, det in enumerate(valid_detections):
  436. if idx in matched_det_indices:
  437. continue
  438. x_ratio = det.center[0] / frame_w
  439. y_ratio = det.center[1] / frame_h
  440. _, _, width, height = det.bbox
  441. area = width * height
  442. dx = abs(det.center[0] - center_x) / center_x
  443. dy = abs(det.center[1] - center_y) / center_y
  444. center_distance = (dx + dy) / 2
  445. # 分配全局唯一track_id
  446. with self._track_id_lock:
  447. new_track_id = self._next_track_id
  448. self._next_track_id += 1
  449. det.track_id = new_track_id # 更新检测对象的track_id
  450. self.tracking_targets[new_track_id] = TrackingTarget(
  451. track_id=new_track_id,
  452. position=(x_ratio, y_ratio),
  453. last_update=current_time,
  454. area=area,
  455. confidence=det.confidence,
  456. center_distance=center_distance
  457. )
  458. def _process_detections(self, detections: List[DetectedObject],
  459. frame: np.ndarray, frame_size: Tuple[int, int]):
  460. """处理检测结果"""
  461. if self.on_person_detected:
  462. for det in detections:
  463. self.on_person_detected(det, frame)
  464. def _process_current_target(self, frame: np.ndarray, frame_size: Tuple[int, int]):
  465. """处理当前跟踪目标"""
  466. with self.targets_lock:
  467. if not self.tracking_targets:
  468. self._set_state(TrackingState.IDLE)
  469. self.current_target = None
  470. return
  471. # 使用目标选择器选择最优目标
  472. self.current_target = self.target_selector.select_target(
  473. self.tracking_targets, frame_size
  474. )
  475. if self.current_target:
  476. # 移动球机到目标位置 (仅在 PTZ 跟踪启用时)
  477. if self.enable_ptz_tracking and self.enable_ptz_camera:
  478. self._set_state(TrackingState.TRACKING)
  479. x_ratio, y_ratio = self.current_target.position
  480. # 检查位置是否变化超过阈值
  481. should_move = True
  482. if self.last_ptz_position is not None:
  483. last_x, last_y = self.last_ptz_position
  484. if (abs(x_ratio - last_x) < self.ptz_position_threshold and
  485. abs(y_ratio - last_y) < self.ptz_position_threshold):
  486. should_move = False
  487. if should_move:
  488. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  489. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  490. if self.ptz.ptz_config.get('pan_flip', False):
  491. pan = (pan + 180) % 360
  492. zoom = self.ptz.ptz_config.get('default_zoom', 8)
  493. self.ptz.goto_exact_position(pan, tilt, zoom)
  494. else:
  495. self.ptz.track_target(x_ratio, y_ratio)
  496. self.last_ptz_position = (x_ratio, y_ratio)
  497. # 执行OCR识别 (仅在 OCR 启用时)
  498. if self.enable_ocr:
  499. self._perform_ocr(frame, self.current_target)
  500. def _perform_ocr(self, frame: np.ndarray, target: TrackingTarget):
  501. """执行OCR识别"""
  502. if not self.enable_ocr or self.number_detector is None:
  503. return
  504. # 频率控制 - 避免过于频繁调用OCR API
  505. current_time = time.time()
  506. if current_time - self.last_ocr_time < self.ocr_interval:
  507. return
  508. self.last_ocr_time = current_time
  509. # 更新OCR尝试统计
  510. self._update_stats('ocr_attempts')
  511. # 计算人体边界框 (基于位置估算)
  512. frame_h, frame_w = frame.shape[:2]
  513. # 人体占画面比例 (可配置,默认宽20%、高40%)
  514. person_width_ratio = self.config.get('person_width_ratio', 0.2)
  515. person_height_ratio = self.config.get('person_height_ratio', 0.4)
  516. person_width = int(frame_w * person_width_ratio)
  517. person_height = int(frame_h * person_height_ratio)
  518. x_ratio, y_ratio = target.position
  519. center_x = int(x_ratio * frame_w)
  520. center_y = int(y_ratio * frame_h)
  521. # 计算边界框,确保不超出画面范围
  522. x1 = max(0, center_x - person_width // 2)
  523. y1 = max(0, center_y - person_height // 2)
  524. x2 = min(frame_w, x1 + person_width)
  525. y2 = min(frame_h, y1 + person_height)
  526. # 更新实际宽高 (可能因边界裁剪而变小)
  527. actual_width = x2 - x1
  528. actual_height = y2 - y1
  529. person_bbox = (x1, y1, actual_width, actual_height)
  530. # 检测编号
  531. self._set_state(TrackingState.OCR_PROCESSING)
  532. person_info = self.number_detector.detect_number(frame, person_bbox)
  533. person_info.person_id = target.track_id
  534. # 更新OCR成功统计
  535. if person_info.number_text:
  536. self._update_stats('ocr_success')
  537. # 更新目标信息
  538. with self.targets_lock:
  539. if target.track_id in self.tracking_targets:
  540. self.tracking_targets[target.track_id].person_info = person_info
  541. # 回调
  542. if self.on_number_recognized and person_info.number_text:
  543. self.on_number_recognized(person_info)
  544. # 放入结果队列
  545. self.result_queue.put(person_info)
  546. def _cleanup_expired_targets(self):
  547. """清理过期目标"""
  548. current_time = time.time()
  549. timeout = self.config['tracking_timeout']
  550. with self.targets_lock:
  551. expired_ids = [
  552. target_id for target_id, target in self.tracking_targets.items()
  553. if current_time - target.last_update > timeout
  554. ]
  555. for target_id in expired_ids:
  556. del self.tracking_targets[target_id]
  557. if self.current_target and self.current_target.track_id == target_id:
  558. self.current_target = None
  559. def _set_state(self, state: TrackingState):
  560. """设置状态"""
  561. with self.state_lock:
  562. self.state = state
  563. def get_state(self) -> TrackingState:
  564. """获取状态"""
  565. with self.state_lock:
  566. return self.state
  567. def get_results(self) -> List[PersonInfo]:
  568. """
  569. 获取识别结果
  570. Returns:
  571. 人员信息列表
  572. """
  573. results = []
  574. while not self.result_queue.empty():
  575. try:
  576. results.append(self.result_queue.get_nowait())
  577. except queue.Empty:
  578. break
  579. return results
  580. def get_tracking_targets(self) -> List[TrackingTarget]:
  581. """获取当前跟踪目标"""
  582. with self.targets_lock:
  583. return list(self.tracking_targets.values())
  584. def force_track_position(self, x_ratio: float, y_ratio: float, zoom: int = None):
  585. """
  586. 强制跟踪指定位置
  587. Args:
  588. x_ratio: X方向比例
  589. y_ratio: Y方向比例
  590. zoom: 变倍
  591. """
  592. if self.enable_ptz_tracking and self.enable_ptz_camera:
  593. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  594. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  595. if self.ptz.ptz_config.get('pan_flip', False):
  596. pan = (pan + 180) % 360
  597. self.ptz.goto_exact_position(pan, tilt, zoom or self.ptz.ptz_config.get('default_zoom', 8))
  598. else:
  599. self.ptz.move_to_target(x_ratio, y_ratio, zoom)
  600. def capture_snapshot(self) -> Optional[np.ndarray]:
  601. """
  602. 抓拍快照
  603. Returns:
  604. 快照图像
  605. """
  606. return self.panorama.get_frame()
  607. class EventDrivenCoordinator(Coordinator):
  608. """事件驱动联动控制器,当全景摄像头检测到事件时触发联动"""
  609. def __init__(self, *args, **kwargs):
  610. super().__init__(*args, **kwargs)
  611. self.event_types = {
  612. 'intruder': True,
  613. 'crossline': True,
  614. 'motion': True,
  615. }
  616. self.event_queue = queue.Queue()
  617. def on_event(self, event_type: str, event_data: dict):
  618. if not self.event_types.get(event_type, False):
  619. return
  620. self.event_queue.put({'type': event_type, 'data': event_data, 'time': time.time()})
  621. def _coordinator_worker(self):
  622. while self.running:
  623. try:
  624. try:
  625. event = self.event_queue.get(timeout=0.1)
  626. self._process_event(event)
  627. except queue.Empty:
  628. pass
  629. frame = self.panorama.get_frame()
  630. if frame is not None:
  631. frame_size = (frame.shape[1], frame.shape[0])
  632. detections = self._detect_persons(frame)
  633. if detections:
  634. # 为检测结果分配临时序号
  635. for idx, det in enumerate(detections):
  636. det.track_id = idx
  637. self._update_tracking_targets(detections, frame_size)
  638. self._process_current_target(frame, frame_size)
  639. self._cleanup_expired_targets()
  640. except Exception as e:
  641. print(f"事件处理错误: {e}")
  642. time.sleep(0.1)
  643. def _process_event(self, event: dict):
  644. event_type = event['type']
  645. event_data = event['data']
  646. print(f"处理事件: {event_type}")
  647. if event_type == 'intruder' and 'position' in event_data:
  648. x_ratio, y_ratio = event_data['position']
  649. self.force_track_position(x_ratio, y_ratio)
  650. @dataclass
  651. class PTZCommand:
  652. """PTZ控制命令"""
  653. pan: float
  654. tilt: float
  655. zoom: int
  656. x_ratio: float = 0.0
  657. y_ratio: float = 0.0
  658. use_calibration: bool = True
  659. track_id: Optional[int] = None # 跟踪目标ID(用于配对图片保存)
  660. batch_id: Optional[str] = None # 批次ID(用于配对图片保存)
  661. person_index: int = -1 # 人员在批次中的序号(用于配对图片保存)
  662. class AsyncCoordinator(Coordinator):
  663. """
  664. 异步联动控制器 — 检测线程与PTZ控制线程分离
  665. 改进:
  666. 1. 检测线程:持续读取全景帧 + YOLO推理
  667. 2. PTZ控制线程:通过命令队列接收目标位置,独立控制球机
  668. 3. 两线程通过 queue 通信,互不阻塞
  669. 4. PTZ位置确认:移动后等待球机到位并验证帧
  670. """
  671. PTZ_CONFIRM_WAIT = 0.3 # PTZ命令后等待稳定的秒数
  672. PTZ_CONFIRM_TIMEOUT = 2.0 # PTZ位置确认超时
  673. PTZ_COMMAND_COOLDOWN = 0.15 # PTZ命令最小间隔秒数
  674. def __init__(self, *args, **kwargs):
  675. super().__init__(*args, **kwargs)
  676. # PTZ命令队列(检测→PTZ)
  677. self._ptz_queue: queue.Queue = queue.Queue(maxsize=10)
  678. # 线程
  679. self._detection_thread = None
  680. self._ptz_thread = None
  681. # PTZ确认回调
  682. self._on_ptz_confirmed: Optional[Callable] = None
  683. # 上次PTZ命令时间(添加线程锁保护)
  684. self._last_ptz_time = 0.0
  685. self._last_ptz_time_lock = threading.Lock()
  686. # 跨帧跟踪:全局track_id计数器
  687. self._next_track_id = 1
  688. self._track_id_lock = threading.Lock()
  689. # 配对图片保存器
  690. self._enable_paired_saving = DETECTION_CONFIG.get('enable_paired_saving', False)
  691. self._paired_saver: Optional[PairedImageSaver] = None
  692. self._current_batch_id: Optional[str] = None
  693. self._person_ptz_index: Dict[int, int] = {} # track_id -> person_index
  694. if self._enable_paired_saving:
  695. save_dir = DETECTION_CONFIG.get('paired_image_dir', '/home/admin/dsh/paired_images')
  696. time_window = DETECTION_CONFIG.get('paired_time_window', 5.0)
  697. self._paired_saver = get_paired_saver(base_dir=save_dir, time_window=time_window)
  698. logger.info(f"[AsyncCoordinator] 配对图片保存已启用: 目录={save_dir}, 时间窗口={time_window}s")
  699. def start(self) -> bool:
  700. """启动联动(覆盖父类,启动双线程)"""
  701. if not self.panorama.connect():
  702. print("连接全景摄像头失败")
  703. return False
  704. if self.enable_ptz_camera:
  705. if not self.ptz.connect():
  706. print("连接球机失败")
  707. self.panorama.disconnect()
  708. return False
  709. # 启动球机RTSP流(用于球机端人体检测)
  710. if self.enable_ptz_detection:
  711. if not self.ptz.start_stream_rtsp():
  712. print("球机RTSP流启动失败,禁用球机端检测功能")
  713. self.enable_ptz_detection = False
  714. else:
  715. # 初始化球机端人体检测器
  716. self._init_ptz_detector()
  717. else:
  718. print("PTZ球机功能已禁用")
  719. if not self.panorama.start_stream_rtsp():
  720. print("RTSP视频流启动失败,尝试SDK方式...")
  721. if not self.panorama.start_stream():
  722. print("启动视频流失败")
  723. self.panorama.disconnect()
  724. if self.enable_ptz_camera:
  725. self.ptz.disconnect()
  726. return False
  727. self.running = True
  728. # 启动检测线程
  729. self._detection_thread = threading.Thread(
  730. target=self._detection_worker, name="detection-worker", daemon=True)
  731. self._detection_thread.start()
  732. # 启动PTZ控制线程
  733. if self.enable_ptz_camera and self.enable_ptz_tracking:
  734. self._ptz_thread = threading.Thread(
  735. target=self._ptz_worker, name="ptz-worker", daemon=True)
  736. self._ptz_thread.start()
  737. print("异步联动系统已启动 (检测线程 + PTZ控制线程)")
  738. return True
  739. def stop(self):
  740. """停止联动"""
  741. self.running = False
  742. # 清空PTZ队列,让工作线程退出
  743. while not self._ptz_queue.empty():
  744. try:
  745. self._ptz_queue.get_nowait()
  746. except queue.Empty:
  747. break
  748. if self._detection_thread:
  749. self._detection_thread.join(timeout=3)
  750. if self._ptz_thread:
  751. self._ptz_thread.join(timeout=3)
  752. # 停止父类线程(如果有的话)
  753. if self.coordinator_thread:
  754. self.coordinator_thread.join(timeout=1)
  755. # 关闭配对保存器
  756. if self._paired_saver is not None:
  757. self._paired_saver.close()
  758. self._paired_saver = None
  759. self.panorama.disconnect()
  760. if self.enable_ptz_camera:
  761. self.ptz.disconnect()
  762. self._print_stats()
  763. print("异步联动系统已停止")
  764. def _detection_worker(self):
  765. """检测线程:持续读帧 + YOLO推理 + 发送PTZ命令 + 打印检测日志"""
  766. last_detection_time = 0
  767. # 从 DETECTION_CONFIG 获取检测帧率,默认每秒2帧
  768. detection_fps = self.config.get('detection_fps', DETECTION_CONFIG.get('detection_fps', 2))
  769. detection_interval = 1.0 / detection_fps # 根据FPS计算间隔
  770. ptz_cooldown = self.config.get('ptz_command_cooldown', 0.5)
  771. ptz_threshold = self.config.get('ptz_position_threshold', 0.03)
  772. frame_count = 0
  773. last_log_time = time.time()
  774. log_interval = 5.0 # 每5秒打印一次帧率统计
  775. detection_run_count = 0
  776. detection_person_count = 0
  777. last_no_detect_log_time = 0
  778. no_detect_log_interval = 30.0
  779. with self.stats_lock:
  780. self.stats['start_time'] = time.time()
  781. if self.detector is None:
  782. logger.warning("[检测线程] ⚠️ 人体检测器未初始化! 检测功能不可用, 请检查 YOLO 模型是否正确加载")
  783. elif not self.enable_detection:
  784. logger.warning("[检测线程] ⚠️ 人体检测已禁用 (enable_detection=False)")
  785. else:
  786. logger.info(f"[检测线程] ✓ 人体检测器已就绪, 检测帧率={detection_fps}fps(间隔={detection_interval:.2f}s), PTZ冷却={ptz_cooldown}s")
  787. while self.running:
  788. try:
  789. current_time = time.time()
  790. frame = self.panorama.get_frame()
  791. if frame is None:
  792. time.sleep(0.01)
  793. continue
  794. frame_count += 1
  795. self._update_stats('frames_processed')
  796. frame_size = (frame.shape[1], frame.shape[0])
  797. if current_time - last_log_time >= log_interval:
  798. elapsed = current_time - last_log_time
  799. fps = frame_count / elapsed if elapsed > 0 else 0
  800. state_str = self.state.name if hasattr(self.state, 'name') else str(self.state)
  801. stats_parts = [f"帧率={fps:.1f}fps", f"处理帧={frame_count}", f"状态={state_str}"]
  802. if self.detector is None:
  803. stats_parts.append("检测器=未加载")
  804. elif not self.enable_detection:
  805. stats_parts.append("检测=已禁用")
  806. else:
  807. stats_parts.append(f"检测轮次={detection_run_count}(有人={detection_person_count})")
  808. with self.targets_lock:
  809. target_count = len(self.tracking_targets)
  810. stats_parts.append(f"跟踪目标={target_count}")
  811. logger.info(f"[检测线程] {', '.join(stats_parts)}")
  812. frame_count = 0
  813. last_log_time = current_time
  814. # 周期性检测(约1次/秒)
  815. if current_time - last_detection_time >= detection_interval:
  816. last_detection_time = current_time
  817. detection_run_count += 1
  818. # YOLO 人体检测
  819. detections = self._detect_persons(frame)
  820. if detections:
  821. self._update_stats('persons_detected', len(detections))
  822. detection_person_count += 1
  823. # 更新跟踪目标(track_id 在此方法内分配)
  824. self._update_tracking_targets(detections, frame_size)
  825. # 配对图片保存:创建新批次(在 _update_tracking_targets 之后,使用正确的 track_id)
  826. if detections and self._enable_paired_saving and self._paired_saver is not None:
  827. self._create_detection_batch(frame, detections, frame_size)
  828. # 打印检测日志(使用连续序号,与图片标记一致)
  829. if detections:
  830. person_threshold = DETECTION_CONFIG.get('person_threshold', 0.8)
  831. person_idx = 0
  832. for t in detections:
  833. # detections 是 DetectedObject,使用 center 计算位置
  834. x_ratio = t.center[0] / frame_size[0]
  835. y_ratio = t.center[1] / frame_size[1]
  836. _, _, w, h = t.bbox
  837. area = w * h
  838. # 只对达到阈值的人员打印日志并分配序号
  839. if t.class_name == 'person' and t.confidence >= person_threshold:
  840. logger.info(
  841. f"[检测] ✓ person_{person_idx} "
  842. f"位置=({x_ratio:.3f}, {y_ratio:.3f}) "
  843. f"面积={area} 置信度={t.confidence:.2f}"
  844. )
  845. person_idx += 1
  846. else:
  847. logger.debug(
  848. f"[检测] · 目标ID={t.track_id}({t.class_name}) "
  849. f"位置=({x_ratio:.3f}, {y_ratio:.3f}) "
  850. f"置信度={t.confidence:.2f}(低于阈值{person_threshold})"
  851. )
  852. else:
  853. if current_time - last_no_detect_log_time >= no_detect_log_interval:
  854. logger.info(
  855. f"[检测] · YOLO检测运行正常, 本轮未检测到人员 "
  856. f"(累计检测{detection_run_count}轮, 检测到人{detection_person_count}轮)"
  857. )
  858. last_no_detect_log_time = current_time
  859. if detections:
  860. self._process_detections(detections, frame, frame_size)
  861. # 为每个检测到的人发送PTZ命令(不再只选一个)
  862. if self.enable_ptz_tracking and self.enable_ptz_camera:
  863. targets = self._get_all_valid_targets()
  864. for target in targets:
  865. self._send_ptz_command_with_log(target, frame_size)
  866. elif not detections and self.current_target:
  867. # 目标消失,切回IDLE
  868. self._set_state(TrackingState.IDLE)
  869. logger.info("[检测] 目标丢失,球机进入IDLE状态")
  870. self.current_target = None
  871. self._cleanup_expired_targets()
  872. time.sleep(0.01)
  873. except Exception as e:
  874. logger.error(f"检测线程错误: {e}")
  875. time.sleep(0.1)
  876. def _init_ptz_detector(self):
  877. """初始化球机端人体检测器"""
  878. try:
  879. model_path = DETECTION_CONFIG.get('model_path')
  880. model_type = DETECTION_CONFIG.get('model_type', 'auto')
  881. conf_threshold = DETECTION_CONFIG.get('person_threshold', 0.5)
  882. if model_path:
  883. self.ptz_detector = PTZPersonDetector(
  884. model_path=model_path,
  885. model_type=model_type,
  886. confidence_threshold=conf_threshold
  887. )
  888. self.auto_zoom_controller = PTZAutoZoomController(
  889. ptz_camera=self.ptz,
  890. detector=self.ptz_detector,
  891. config=self.auto_zoom_config
  892. )
  893. print(f"[AsyncCoordinator] 球机端人体检测器初始化成功")
  894. else:
  895. print("[AsyncCoordinator] 未配置球机检测模型路径,禁用球机端检测")
  896. self.enable_ptz_detection = False
  897. except Exception as e:
  898. print(f"[AsyncCoordinator] 球机端检测器初始化失败: {e}")
  899. self.enable_ptz_detection = False
  900. def _create_detection_batch(self, frame: np.ndarray,
  901. detections: List[DetectedObject],
  902. frame_size: Tuple[int, int]):
  903. """
  904. 创建检测批次,用于配对图片保存
  905. Args:
  906. frame: 全景帧
  907. detections: 检测到的人员列表
  908. frame_size: 帧尺寸
  909. """
  910. if self._paired_saver is None:
  911. return
  912. # 过滤有效人员(必须是 person 且置信度 >= 阈值)
  913. person_threshold = DETECTION_CONFIG.get('person_threshold', 0.8)
  914. valid_persons = []
  915. for det in detections:
  916. # 只处理 class_name 为 person 的目标,排除安全帽、反光衣等
  917. if det.class_name == 'person' and det.confidence >= person_threshold:
  918. valid_persons.append(det)
  919. if not valid_persons:
  920. logger.debug(f"[配对保存] 无有效人员(阈值={person_threshold}),跳过批次创建")
  921. return
  922. # 构建人员信息列表(只包含有效人员)
  923. persons = []
  924. self._person_ptz_index = {} # 重置索引映射
  925. for i, det in enumerate(valid_persons):
  926. x_ratio = det.center[0] / frame_size[0]
  927. y_ratio = det.center[1] / frame_size[1]
  928. person_info = {
  929. 'track_id': det.track_id,
  930. 'position': (x_ratio, y_ratio),
  931. 'bbox': (det.bbox[0], det.bbox[1],
  932. det.bbox[0] + det.bbox[2],
  933. det.bbox[1] + det.bbox[3]),
  934. 'confidence': det.confidence
  935. }
  936. persons.append(person_info)
  937. self._person_ptz_index[det.track_id] = i
  938. # 创建新批次
  939. batch_id = self._paired_saver.start_new_batch(frame, persons)
  940. if batch_id:
  941. self._current_batch_id = batch_id
  942. logger.info(f"[配对保存] 创建批次: {batch_id}, 有效人员={len(persons)}/{len(detections)}")
  943. def _save_ptz_image_for_person(self, track_id: int,
  944. ptz_frame: np.ndarray,
  945. ptz_position: Tuple[float, float, int]):
  946. """
  947. 保存球机聚焦图片到对应批次
  948. Args:
  949. track_id: 人员跟踪ID
  950. ptz_frame: 球机帧
  951. ptz_position: PTZ位置 (pan, tilt, zoom)
  952. """
  953. if (self._paired_saver is None or
  954. self._current_batch_id is None or
  955. track_id not in self._person_ptz_index):
  956. return
  957. person_index = self._person_ptz_index[track_id]
  958. self._paired_saver.save_ptz_image(
  959. batch_id=self._current_batch_id,
  960. person_index=person_index,
  961. ptz_frame=ptz_frame,
  962. ptz_position=ptz_position,
  963. ptz_bbox=getattr(self, '_last_ptz_bbox', None)
  964. )
  965. def _save_ptz_image_for_person_batch(self, batch_id: str, person_index: int,
  966. ptz_frame: np.ndarray,
  967. ptz_position: Tuple[float, float, int]):
  968. """
  969. 保存球机聚焦图片到指定批次(直接使用 batch_id,不依赖当前批次)
  970. Args:
  971. batch_id: 批次ID
  972. person_index: 人员序号
  973. ptz_frame: 球机帧
  974. ptz_position: PTZ位置 (pan, tilt, zoom)
  975. """
  976. if self._paired_saver is None:
  977. return
  978. self._paired_saver.save_ptz_image(
  979. batch_id=batch_id,
  980. person_index=person_index,
  981. ptz_frame=ptz_frame,
  982. ptz_position=ptz_position,
  983. ptz_bbox=getattr(self, '_last_ptz_bbox', None)
  984. )
  985. def _ptz_worker(self):
  986. """PTZ控制线程:从队列接收命令并控制球机"""
  987. while self.running:
  988. try:
  989. try:
  990. cmd = self._ptz_queue.get(timeout=0.1)
  991. except queue.Empty:
  992. continue
  993. # 执行PTZ命令(batch_id 和 person_index 已在命令中)
  994. self._execute_ptz_command(cmd)
  995. except Exception as e:
  996. print(f"PTZ控制线程错误: {e}")
  997. time.sleep(0.05)
  998. def _select_tracking_target(self) -> Optional[TrackingTarget]:
  999. """选择当前跟踪目标"""
  1000. with self.targets_lock:
  1001. if not self.tracking_targets:
  1002. self._set_state(TrackingState.IDLE)
  1003. self.current_target = None
  1004. return None
  1005. # 使用目标选择器选择最优目标
  1006. self.current_target = self.target_selector.select_target(
  1007. self.tracking_targets
  1008. )
  1009. return self.current_target
  1010. def _get_all_valid_targets(self) -> List[TrackingTarget]:
  1011. """
  1012. 获取所有有效的检测目标(用于多目标PTZ定位)
  1013. 返回按优先级排序的目标列表
  1014. """
  1015. with self.targets_lock:
  1016. if not self.tracking_targets:
  1017. self._set_state(TrackingState.IDLE)
  1018. self.current_target = None
  1019. return []
  1020. # 按得分排序所有目标
  1021. targets = list(self.tracking_targets.values())
  1022. targets.sort(key=lambda t: t.score, reverse=True)
  1023. if targets:
  1024. self._set_state(TrackingState.TRACKING)
  1025. self.current_target = targets[0] # 第一个作为当前目标
  1026. return targets
  1027. def _send_ptz_command(self, target: TrackingTarget, frame_size: Tuple[int, int]):
  1028. """将跟踪目标转化为PTZ命令放入队列"""
  1029. x_ratio, y_ratio = target.position
  1030. # 检查位置变化是否超过阈值
  1031. if self.last_ptz_position is not None:
  1032. last_x, last_y = self.last_ptz_position
  1033. if abs(x_ratio - last_x) < self.ptz_position_threshold and \
  1034. abs(y_ratio - last_y) < self.ptz_position_threshold:
  1035. return
  1036. # 冷却检查(线程安全)
  1037. current_time = time.time()
  1038. with self._last_ptz_time_lock:
  1039. if current_time - self._last_ptz_time < self.PTZ_COMMAND_COOLDOWN:
  1040. return
  1041. cmd = PTZCommand(
  1042. pan=0, tilt=0, zoom=0,
  1043. x_ratio=x_ratio, y_ratio=y_ratio,
  1044. use_calibration=self.enable_calibration
  1045. )
  1046. try:
  1047. self._ptz_queue.put_nowait(cmd)
  1048. self.last_ptz_position = (x_ratio, y_ratio)
  1049. except queue.Full:
  1050. pass # 丢弃命令,下一个检测周期会重发
  1051. def _send_ptz_command_with_log(self, target: TrackingTarget, frame_size: Tuple[int, int]):
  1052. """发送PTZ命令并打印日志"""
  1053. x_ratio, y_ratio = target.position
  1054. # 冷却检查(线程安全)
  1055. current_time = time.time()
  1056. with self._last_ptz_time_lock:
  1057. if current_time - self._last_ptz_time < self.PTZ_COMMAND_COOLDOWN:
  1058. return
  1059. # 位置变化阈值检查
  1060. if self.last_ptz_position is not None:
  1061. last_x, last_y = self.last_ptz_position
  1062. if abs(x_ratio - last_x) < self.ptz_position_threshold and \
  1063. abs(y_ratio - last_y) < self.ptz_position_threshold:
  1064. return
  1065. # 计算PTZ角度(用于日志)
  1066. if self.enable_calibration and self.calibrator and self.calibrator.is_calibrated():
  1067. pan, tilt = self.calibrator.transform(x_ratio, y_ratio)
  1068. zoom = self.ptz.ptz_config.get('default_zoom', 8)
  1069. coord_type = "校准坐标"
  1070. else:
  1071. pan, tilt, zoom = self.ptz.calculate_ptz_position(x_ratio, y_ratio)
  1072. coord_type = "估算坐标"
  1073. # 获取当前批次信息和人员序号
  1074. batch_id = self._current_batch_id if self._enable_paired_saving else None
  1075. person_index = self._person_ptz_index.get(target.track_id, -1) if self._enable_paired_saving else -1
  1076. cmd = PTZCommand(
  1077. pan=0, tilt=0, zoom=0,
  1078. x_ratio=x_ratio, y_ratio=y_ratio,
  1079. use_calibration=self.enable_calibration,
  1080. track_id=target.track_id, # 传递跟踪ID
  1081. batch_id=batch_id, # 传递批次ID
  1082. person_index=person_index # 传递人员序号
  1083. )
  1084. try:
  1085. self._ptz_queue.put_nowait(cmd)
  1086. self.last_ptz_position = (x_ratio, y_ratio) # 更新位置记录
  1087. self._update_stats('ptz_commands_sent' if 'ptz_commands_sent' in self.stats else 'persons_detected')
  1088. logger.info(
  1089. f"[PTZ] 命令已发送: 目标ID={target.track_id} "
  1090. f"全景位置=({x_ratio:.3f}, {y_ratio:.3f}) → "
  1091. f"PTZ角度=(pan={pan:.1f}°, tilt={tilt:.1f}°, zoom={zoom}) [{coord_type}]"
  1092. )
  1093. except queue.Full:
  1094. logger.warning("[PTZ] 命令队列满,丢弃本次命令")
  1095. def _execute_ptz_command(self, cmd: PTZCommand):
  1096. """
  1097. 执行PTZ命令(在PTZ线程中)
  1098. Args:
  1099. cmd: PTZ命令(包含 batch_id, person_index, track_id 用于配对保存)
  1100. """
  1101. # 更新最后执行时间(线程安全)
  1102. with self._last_ptz_time_lock:
  1103. self._last_ptz_time = time.time()
  1104. # 从命令中提取配对保存相关信息
  1105. track_id = cmd.track_id
  1106. batch_id = cmd.batch_id
  1107. person_index = cmd.person_index
  1108. if cmd.use_calibration and self.calibrator and self.calibrator.is_calibrated():
  1109. pan, tilt = self.calibrator.transform(cmd.x_ratio, cmd.y_ratio)
  1110. if self.ptz.ptz_config.get('pan_flip', False):
  1111. pan = (pan + 180) % 360
  1112. zoom = self.ptz.ptz_config.get('default_zoom', 8)
  1113. else:
  1114. pan, tilt, zoom = self.ptz.calculate_ptz_position(cmd.x_ratio, cmd.y_ratio)
  1115. self._set_state(TrackingState.TRACKING)
  1116. logger.info(
  1117. f"[PTZ] 执行: pan={pan:.1f}° tilt={tilt:.1f}° zoom={zoom} "
  1118. f"(全景位置=({cmd.x_ratio:.3f}, {cmd.y_ratio:.3f}), "
  1119. f"batch={batch_id}, person={person_index})"
  1120. )
  1121. success = self.ptz.goto_exact_position(pan, tilt, zoom)
  1122. if success:
  1123. # 等待球机物理移动到位(增加额外等待确保画面清晰)
  1124. time.sleep(self.PTZ_CONFIRM_WAIT)
  1125. # 球机端人体检测与自动对焦
  1126. final_pan, final_tilt, final_zoom = pan, tilt, zoom
  1127. if self.enable_ptz_detection and self.auto_zoom_config.get('enabled', False):
  1128. auto_zoom_result = self._auto_zoom_person(pan, tilt, zoom)
  1129. if auto_zoom_result != zoom:
  1130. final_zoom = auto_zoom_result
  1131. # 自动变焦后再次等待画面稳定
  1132. time.sleep(0.5)
  1133. # 获取清晰的球机画面(尝试多次获取最新帧)
  1134. ptz_frame = self._get_clear_ptz_frame()
  1135. # 保存球机图片到配对批次(使用命令中的 batch_id 和 person_index)
  1136. if self._enable_paired_saving and batch_id is not None and person_index >= 0 and ptz_frame is not None:
  1137. # 使用球机端检测器检测人体并标记
  1138. ptz_frame_marked = self._mark_ptz_frame_with_detection(ptz_frame, person_index=person_index)
  1139. self._save_ptz_image_for_person_batch(batch_id, person_index, ptz_frame_marked, (final_pan, final_tilt, final_zoom))
  1140. elif self._enable_paired_saving:
  1141. logger.warning(f"[配对保存] 跳过球机图保存: batch_id={batch_id}, person_index={person_index}, frame={ptz_frame is not None}")
  1142. logger.info(f"[PTZ] 到位确认完成: pan={final_pan:.1f}° tilt={final_tilt:.1f}° zoom={final_zoom}")
  1143. else:
  1144. logger.warning(f"[PTZ] 命令执行失败: pan={pan:.1f}° tilt={tilt:.1f}° zoom={zoom}")
  1145. def _auto_zoom_person(self, initial_pan: float, initial_tilt: float, initial_zoom: int) -> int:
  1146. """
  1147. 自动对焦人体
  1148. 在球机画面中检测人体,自动调整zoom使人体居中且大小合适
  1149. Returns:
  1150. 最终的 zoom 值
  1151. """
  1152. if self.auto_zoom_controller is None:
  1153. return initial_zoom
  1154. logger.info("[AutoZoom] 开始自动对焦...")
  1155. try:
  1156. success, final_zoom = self.auto_zoom_controller.auto_focus_loop(
  1157. get_frame_func=self.ptz.get_frame,
  1158. max_attempts=self.auto_zoom_config.get('max_adjust_attempts', 3)
  1159. )
  1160. if success:
  1161. logger.info(f"[AutoZoom] 自动对焦成功: zoom={final_zoom}")
  1162. return final_zoom
  1163. else:
  1164. logger.warning("[AutoZoom] 自动对焦未能定位人体")
  1165. return initial_zoom
  1166. except Exception as e:
  1167. logger.error(f"[AutoZoom] 自动对焦异常: {e}")
  1168. return initial_zoom
  1169. def _get_clear_ptz_frame(self, max_attempts: int = 5, wait_interval: float = 0.1) -> Optional[np.ndarray]:
  1170. """
  1171. 获取清晰的球机画面
  1172. 尝试多次获取,丢弃模糊/过渡帧
  1173. Args:
  1174. max_attempts: 最大尝试次数
  1175. wait_interval: 每次等待间隔
  1176. Returns:
  1177. 清晰的球机帧或 None
  1178. """
  1179. best_frame = None
  1180. best_score = -1
  1181. for i in range(max_attempts):
  1182. frame = self.ptz.get_frame()
  1183. if frame is not None:
  1184. # 使用拉普拉斯算子评估图像清晰度
  1185. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  1186. laplacian_var = cv2.Laplacian(gray, cv2.CV_64F).var()
  1187. if laplacian_var > best_score:
  1188. best_score = laplacian_var
  1189. best_frame = frame.copy()
  1190. # 如果清晰度足够高,直接返回
  1191. if laplacian_var > 100: # 清晰度阈值
  1192. return frame
  1193. time.sleep(wait_interval)
  1194. return best_frame
  1195. def _mark_ptz_frame_with_detection(self, frame: np.ndarray, person_index: int) -> np.ndarray:
  1196. """
  1197. 在球机帧上标记检测到的人体
  1198. Args:
  1199. frame: 球机帧
  1200. person_index: 人员序号
  1201. Returns:
  1202. 标记后的帧
  1203. """
  1204. marked_frame = frame.copy()
  1205. h, w = marked_frame.shape[:2]
  1206. # 重置保存的bbox
  1207. self._last_ptz_bbox = None
  1208. # 使用球机端检测器检测人体
  1209. if self.ptz_detector is not None:
  1210. try:
  1211. persons = self.ptz_detector.detect(frame)
  1212. if persons:
  1213. # 找到最大的人体(假设是目标)
  1214. largest_person = max(persons, key=lambda p: p.area)
  1215. x1, y1, x2, y2 = largest_person.bbox
  1216. # 保存bbox供后续使用
  1217. self._last_ptz_bbox = (x1, y1, x2, y2)
  1218. # 绘制边界框(红色,区别于全景的绿色)
  1219. cv2.rectangle(marked_frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
  1220. # 绘制标签
  1221. label = f"person_{person_index} ({largest_person.confidence:.2f})"
  1222. (label_w, label_h), _ = cv2.getTextSize(
  1223. label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2
  1224. )
  1225. # 标签背景(红色)
  1226. cv2.rectangle(
  1227. marked_frame,
  1228. (x1, y1 - label_h - 8),
  1229. (x1 + label_w, y1),
  1230. (0, 0, 255),
  1231. -1
  1232. )
  1233. # 标签文字(白色)
  1234. cv2.putText(
  1235. marked_frame, label,
  1236. (x1, y1 - 4),
  1237. cv2.FONT_HERSHEY_SIMPLEX, 0.7,
  1238. (255, 255, 255), 2
  1239. )
  1240. logger.info(f"[配对保存] 球机图标记: person_{person_index}, "
  1241. f"位置=({x1},{y1},{x2},{y2}), 置信度={largest_person.confidence:.2f}")
  1242. else:
  1243. # 未检测到人体,在画面中心添加提示
  1244. cv2.putText(
  1245. marked_frame, f"person_{person_index} (no detection)",
  1246. (w // 2 - 100, h // 2),
  1247. cv2.FONT_HERSHEY_SIMPLEX, 0.8,
  1248. (0, 0, 255), 2
  1249. )
  1250. except Exception as e:
  1251. logger.error(f"[配对保存] 球机图检测标记失败: {e}")
  1252. return marked_frame
  1253. def _confirm_ptz_position(self, x_ratio: float, y_ratio: float):
  1254. """PTZ位置确认:读取球机帧验证目标是否可见"""
  1255. if not hasattr(self.ptz, 'get_frame') or self.ptz.get_frame() is None:
  1256. return
  1257. ptz_frame = self.ptz.get_frame()
  1258. if ptz_frame is None:
  1259. return
  1260. # 未来可以在这里添加球机帧目标验证逻辑
  1261. # 例如:在球机帧中检测目标是否在画面中心附近
  1262. def on_ptz_confirmed(self, callback: Callable):
  1263. """注册PTZ位置确认回调"""
  1264. self._on_ptz_confirmed = callback