diff --git a/integration/ptz17_focus_zoom/README.md b/integration/ptz17_focus_zoom/README.md index 607a02b..9411b26 100644 --- a/integration/ptz17_focus_zoom/README.md +++ b/integration/ptz17_focus_zoom/README.md @@ -1,5 +1,15 @@ # PTZ17 · зум и контроль резкости +## Диагностика HOLD и повторного захвата + +Событие `recovery_frame` в очереди JSONL PTZ17 связывает причину отказа или восстановления с поколением трека, номером кадра, временем наблюдения и размером изображения обработки. Координаты `xyxy` отсчитываются от левого верхнего угла. Для промежуточного OSTrack и привязанного к нему YOLO время наблюдения соответствует получению декодированного кадра. Для отдельного детекторного прохода оно соответствует входу в обработчик. Поле `input_kind` различает эти источники. Номера кадров OSTrack и управляющего прохода записываются отдельно. + +Телеметрия содержит последнюю принятую рамку, опору сопоставления, рамку и оценку OSTrack, кандидатов YOLO с оценками и классами, результат сопоставления, выбранную рамку и результат переинициализации. Записываются счётчики голосов повторного захвата и разрешения зума. При геометрическом HOLD журнал различает отсутствие сопоставления, отбрасывание по площади относительно принятой рамки, ожидание подтверждения и отказ переинициализации. При позднем YOLO событие отмечает, что кадр использован для верификации или привёл к штатному STOP из-за конфликта. + +Списки содержат до 12 рамок, которые дошли до соответствующих проверок контроллера. Общие количества и выбранная рамка записываются отдельно. Сырые результаты YOLO до фильтрации представлены счётчиком. Для событий, ожидающих подтверждения, `yolo_match` показывает возможное совпадение, а `yolo_selected` остаётся пустым. Писатель использует существующую ограниченную очередь. Переполнение увеличивает `dropped_before` у следующих событий. + +Для офлайн-проверки `test_recovery_telemetry.py` укажите пути `PTZ17_TEST_RUNTIME`, `PTZ17_TEST_BASELINE` и `PTZ17_TEST_CANDIDATE`. Базовая версия для этих тестов — предыдущий коммит PR №14. Тесты сравнивают команды Pan/Tilt/Zoom, защитные STOP, вызовы тревог и состояние контроллера на одинаковых входах со смоделированными детекциями. Полевое время от HOLD до повторного разрешения зума остаётся измерить после сбора новых событий. + Локальный кандидат поверх установленного PR №13. Прямое и обратное применение патча проверены побайтно. SHA-256 базовой и кандидатной версий в `pins.json`. На широком угле кандидат проверяет, может ли уже разрешённый первый обычный импульс native 10 начаться с длительностью до 340 мс и жёстким пределом 400 мс. Для этого нужны текущий кадр OSTrack, свежее подтверждение YOLO, свежая оптика, допустимая скорость в кадре и полная штатная проекция рамки. Повторное разрешение допускает только исходный запрос того же владельца. При запрете базового контроллера запрос остаётся нулевым. Функции модели, тревог и транспорт STOP сохранены. diff --git a/integration/ptz17_focus_zoom/pins.json b/integration/ptz17_focus_zoom/pins.json index 4a896fa..d553369 100644 --- a/integration/ptz17_focus_zoom/pins.json +++ b/integration/ptz17_focus_zoom/pins.json @@ -1,6 +1,6 @@ { "base_sha256": "6cf484271ef3659028b0b4d43041addb05102f91d2758ab472d968a52d24e911", - "candidate_sha256": "fc0deb0c54fbd94a916493ee0c3159b02407876b32deebbbc3d819cc58a17500", - "patch_sha256": "e4a03e3dd4cd0f99995bc37acab29d063553cb32966fb13bdd93a46b8f7c5a9f", + "candidate_sha256": "f04d8a8d7a4105fe6812afaed00655dc42cbf7e6fb014e01d9f5efb306db13ca", + "patch_sha256": "555d329a1c437187e254330384ec1dab60dc3fb8ac859c710c9cef517ac59dd5", "installed": false } diff --git a/integration/ptz17_focus_zoom/server.patch b/integration/ptz17_focus_zoom/server.patch index b1ac814..227ada5 100644 --- a/integration/ptz17_focus_zoom/server.patch +++ b/integration/ptz17_focus_zoom/server.patch @@ -9,7 +9,15 @@ ) from . import motion_detector from .utils import ( -@@ -690,6 +690,8 @@ +@@ -46,6 +46,7 @@ + from .tracking_policy import should_allow_offsector_park, should_enforce_track_sector_limits + from .bt_tracker import ByteTracker + from .visual_tracker import ( ++ VisualTrackResult, + bbox_center_distance_norm, + bbox_iou, + visual_tracker_manager, +@@ -690,6 +691,8 @@ def _focus_quality_score( frame_bgr: np.ndarray, bbox: Tuple[int, int, int, int] | list[int], @@ -18,7 +26,7 @@ ) -> float | None: """Return Laplacian focus energy from a bounded ROI around the target. -@@ -708,7 +710,7 @@ +@@ -708,7 +711,7 @@ if not np.isfinite([x1, y1, x2, y2]).all() or x2 <= x1 or y2 <= y1: return None @@ -27,7 +35,7 @@ cx = (x1 + x2) * 0.5 cy = (y1 + y2) * 0.5 roi_w = max(16.0, (x2 - x1) * scale) -@@ -741,6 +743,154 @@ +@@ -741,6 +744,221 @@ return score if math.isfinite(score) else None @@ -66,6 +74,73 @@ + "decision_frame", at, **values) + + ++_PTZ17_RECOVERY_BOX_LIMIT = 12 ++_PTZ17_RECOVERY_REASON_LIMIT = 160 ++_DetectorBox = tuple[list[int], float, int] ++ ++ ++def _recovery_box_px(box: tuple[int, ...] | list[int] | None) -> list[int] | None: ++ """Snapshot xyxy pixels from the top left of the processing image, x right and y down.""" ++ if box is None or len(box) != 4: ++ return None ++ try: ++ return [int(value) for value in box] ++ except (TypeError, ValueError, OverflowError): ++ return None ++ ++ ++def _recovery_detector_box(candidate: _DetectorBox | None) -> dict[str, Any] | None: ++ if candidate is None: ++ return None ++ box, score, class_id = candidate ++ confidence = float(score) ++ return dict(bbox_px=_recovery_box_px(box), confidence=confidence if math.isfinite(confidence) else None, ++ class_id=int(class_id)) ++ ++ ++def _record_ptz17_recovery( ++ cam_idx: int, st: dict, observation_utc: float, source_seq: int | None, ++ frame_w: int, frame_h: int, visual_result: VisualTrackResult, *, ++ input_kind: str, outcome: str, trusted_bbox: tuple[int, ...] | list[int] | None, ++ reference_bbox: tuple[int, ...] | list[int] | None, ++ validation_targets: list[_DetectorBox], reacquire_targets: list[_DetectorBox], ++ raw_box_count: int, expected_track_class: int | None = None, detector_match: _DetectorBox | None = None, ++ selected: _DetectorBox | None = None, seeded: VisualTrackResult | None = None, ++) -> None: ++ """Record already available inputs and the recovery outcome off the control thread.""" ++ if (cam_idx != _PTZ17_CAM_IDX or st.get("mode") != "TRACK" or not st.get("tracking_active") ++ or not getattr(config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", "logs/ptz_zoom")): ++ return ++ _, generation, started_at, rearm_epoch = visual_control.owner(st) ++ if started_at <= 0.0 or not math.isfinite(started_at) or not math.isfinite(observation_utc): ++ return ++ ++ def tracker_fields(result: VisualTrackResult) -> dict[str, Any]: ++ score = float(result.score) ++ return dict(bbox_px=_recovery_box_px(result.bbox), ok=bool(result.ok), ++ score=score if math.isfinite(score) else None, class_id=int(result.target_class), ++ frame_seq=int(result.source_seq) if result.source_seq is not None else None, ++ reason=str(result.reason)[:_PTZ17_RECOVERY_REASON_LIMIT]) ++ ++ _episode_telemetry( ++ config, cam_idx, (generation, started_at), "recovery_frame", time.time(), ++ observation_utc=observation_utc, frame_seq=int(source_seq) if source_seq is not None else None, ++ frame_size_px=[frame_w, frame_h], input_kind=input_kind, outcome=outcome, ++ expected_track_class=expected_track_class, ++ trusted_bbox_px=_recovery_box_px(trusted_bbox), association_reference_bbox_px=_recovery_box_px(reference_bbox), ++ ostrack=tracker_fields(visual_result), reinitialized_ostrack=tracker_fields(seeded) if seeded is not None else None, ++ yolo_raw_count=raw_box_count, ++ yolo_validation_count=len(validation_targets), yolo_reacquire_count=len(reacquire_targets), ++ yolo_validation=[_recovery_detector_box(box) for box in validation_targets[:_PTZ17_RECOVERY_BOX_LIMIT]], ++ yolo_reacquire=[_recovery_detector_box(box) for box in reacquire_targets[:_PTZ17_RECOVERY_BOX_LIMIT]], ++ yolo_match=_recovery_detector_box(detector_match), yolo_selected=_recovery_detector_box(selected), ++ reacquire_votes=int(st.get("visual_reacquire_votes", 0) or 0), ++ reacquire_vote_frame_seq=int(st.get("visual_reacquire_source_seq", -1)), ++ zoom_rearm_epoch=rearm_epoch, zoom_rearm_required=bool(st.get("zoom_rearm_required")), ++ zoom_rearm_votes=int(st.get("zoom_rearm_votes", 0) or 0), ++ ) ++ ++ +def _ptz17_target_focus_score( + frame_bgr: np.ndarray, bbox: tuple[int, int, int, int] | list[int], +) -> float | None: @@ -182,7 +257,7 @@ def _clear_focus_low_evidence(st: dict) -> None: st["focus_quality_low_frames"] = 0 st["focus_quality_low_since"] = None -@@ -761,6 +911,7 @@ +@@ -761,6 +979,7 @@ center_err: float, hfov_deg: float, validation_age: float, @@ -190,7 +265,7 @@ ) -> bool: """Measure settled target detail and queue one bounded autofocus request.""" if not bool(getattr(config, "AUTOFOCUS_QUALITY_ENABLE", True)) or not bool( -@@ -833,14 +984,31 @@ +@@ -833,14 +1052,31 @@ _clear_focus_low_evidence(st) st["focus_last_hfov_deg"] = float(hfov_deg) @@ -225,7 +300,7 @@ alpha = clipf(float(getattr(config, "AUTOFOCUS_SCORE_EMA_ALPHA", 0.35)), 0.01, 1.0) previous_ema = st.get("focus_quality_ema") ema = float(score) if not isinstance(previous_ema, (int, float)) else ( -@@ -870,7 +1038,10 @@ +@@ -870,7 +1106,10 @@ if ema >= threshold: # Keep the independent raw-drop evidence while the deliberately slow # EMA still describes the previously sharp scene. @@ -237,7 +312,7 @@ st["focus_quality_low_frames"] = 0 st["focus_quality_low_since"] = None else: -@@ -916,12 +1087,19 @@ +@@ -916,12 +1155,19 @@ if not confirmed_low: return False @@ -258,7 +333,7 @@ if str(st.get("focus_state", "")) not in { "unsupported", "unsupported_backoff", -@@ -931,6 +1109,8 @@ +@@ -931,6 +1177,8 @@ st["focus_state"] = "blur_wait" return False @@ -267,7 +342,7 @@ st["focus_state"] = "requested" st["focus_last_request_at"] = float(now) st["focus_settle_until"] = float(now) + max( -@@ -938,12 +1118,11 @@ +@@ -938,12 +1186,11 @@ float(getattr(config, "AUTOFOCUS_SETTLE_HOLD_SEC", 0.80)), ) _clear_focus_low_evidence(st) @@ -285,7 +360,231 @@ return True -@@ -9537,6 +9716,50 @@ +@@ -6757,11 +7004,20 @@ + detector_targets: list[tuple[list[int], float, int]], + validation_targets: list[tuple[list[int], float, int]], width: int, height: int, + steering_targets: list[tuple[list[float], float, int]] | None = None, ++ *, recovery_targets: list[_DetectorBox] | None = None, raw_box_count: int = 0, + ) -> None: + """Validate the same image without rewinding live control or loss recovery.""" + result = sample.result + bbox = result.bbox + if not result.ok or bbox is None: ++ if recovery_targets is not None: ++ _record_ptz17_recovery( ++ cam_idx, st, float(sample.captured_at), sample.source_seq, width, height, result, ++ input_kind="paired_detector", outcome="paired_tracker_unusable", ++ trusted_bbox=st.get("last_bbox"), reference_bbox=bbox, ++ validation_targets=validation_targets, reacquire_targets=recovery_targets, ++ raw_box_count=raw_box_count, ++ ) + return + now = float(sample.captured_at) + target_class = int(result.target_class) +@@ -6783,6 +7039,14 @@ + visual_tracker_manager.reset(cam_idx, reason='paired detector identity mismatch') + stop_ptz(cam_idx, source='stop', generation=int(st.get('ptz_generation', 0) or 0)) + alarms.ingest_detection(cam_idx, False) ++ if recovery_targets is not None: ++ _record_ptz17_recovery( ++ cam_idx, st, now, sample.source_seq, width, height, result, ++ input_kind="paired_detector", outcome="paired_identity_stop", ++ trusted_bbox=st.get("last_bbox"), reference_bbox=bbox, ++ validation_targets=candidates, reacquire_targets=recovery_targets, ++ raw_box_count=raw_box_count, expected_track_class=target_class, ++ ) + return + st['visual_tracker_mismatch_frames'] = 0 + if matched is not None and now >= float(st.get('visual_tracker_last_detector_at', 0.) or 0.): +@@ -6802,6 +7066,14 @@ + detector_conf=max(alarm_conf, identity_conf), + alarm_eligible=alarm_eligible, cam_idx=cam_idx) + alarms.ingest_detection(cam_idx, alarm_eligible) ++ if recovery_targets is not None: ++ _record_ptz17_recovery( ++ cam_idx, st, now, sample.source_seq, width, height, result, ++ input_kind="paired_detector", outcome="validation_only", ++ trusted_bbox=st.get("last_bbox"), reference_bbox=bbox, ++ validation_targets=candidates, reacquire_targets=recovery_targets, ++ raw_box_count=raw_box_count, expected_track_class=target_class, detector_match=matched, ++ ) + + + @visual_control.serialized_control +@@ -7259,8 +7531,14 @@ + ) + + if evidence_only: +- _consume_paired_detector_evidence(cam_idx, st, visual_sample, detector_targets, +- validation_targets, w, h, steering_targets) ++ trace_recovery = cam_idx == _PTZ17_CAM_IDX and ( ++ not visual_sample.result.ok or visual_sample.result.reason == "recovered" ++ or st.get("loss_since") is not None or st.get("zoom_rearm_reason") == "geometry_reject" ++ ) ++ _consume_paired_detector_evidence( ++ cam_idx, st, visual_sample, detector_targets, validation_targets, w, h, steering_targets, ++ recovery_targets=reacquire_targets if trace_recovery else None, raw_box_count=raw_box_count, ++ ) + return + + if bt_active: +@@ -7580,6 +7858,7 @@ + # its own gate, but a bad backend result/config override must still + # be unable to move the camera by following a remote cloud edge. + previous_control_bbox = st.get("last_bbox") ++ recovery_input_kind = "visual_control" if visual_only else "detector_control" + gross_visual_jump = False + visual_control_jump = 0.0 + visual_control_limit = float( +@@ -7622,6 +7901,9 @@ + ("center jump", "area ratio", "validated area ratio") + ) + ) ++ geometry_match = validated_geometry_target = None ++ seeded_geometry = None ++ geometry_outcome = "geometry_no_box" + if (gross_visual_jump or geometric_reject) and visual_result.bbox is not None: + # A large motion is safe only when the *current* detector agrees + # with that new location. A backend result that passed its own +@@ -7643,6 +7925,8 @@ + ), + allow_target_class_compatibility=True, + ) ++ geometry_match = validated_geometry_target ++ geometry_outcome = "geometry_no_match" + if ( + validated_geometry_target is not None + and isinstance(previous_control_bbox, (tuple, list)) +@@ -7673,6 +7957,7 @@ + ) + ): + validated_geometry_target = None ++ geometry_outcome = "geometry_trusted_area_rejected" + if geometric_reject and validated_geometry_target is not None: + validated_geometry_target = _confirm_loss_reacquire_candidate( + st, +@@ -7682,6 +7967,7 @@ + h, + source_seq=source_seq, + ) ++ geometry_outcome = "geometry_confirmation_pending" + if validated_geometry_target is not None: + preserved_track_class = ( + int(visual_result.target_class) +@@ -7701,6 +7987,7 @@ + now=now, + source_seq=source_seq, + ) ++ geometry_outcome = "geometry_seed_failed" + if seeded_geometry.ok and seeded_geometry.bbox is not None: + logger.info( + "[OSTRACK/IDENTITY] cam=%s detector validated geometry jump; reinitialized", +@@ -7714,6 +8001,16 @@ + _arm_zoom_revalidation( + st, now, "geometry_reinitialized", cam_idx=cam_idx + ) ++ if cam_idx == _PTZ17_CAM_IDX: ++ _record_ptz17_recovery( ++ cam_idx, st, now, source_seq, w, h, visual_result, ++ input_kind=recovery_input_kind, outcome="geometry_reinitialized", ++ trusted_bbox=previous_control_bbox, reference_bbox=visual_result.bbox, ++ validation_targets=validation_targets, reacquire_targets=reacquire_targets, ++ raw_box_count=raw_box_count, detector_match=geometry_match, ++ selected=validated_geometry_target, seeded=seeded_geometry, ++ expected_track_class=expected_track_class, ++ ) + visual_result = seeded_geometry + reference_bbox = seeded_geometry.bbox + gross_visual_jump = False +@@ -7743,6 +8040,16 @@ + source="stop", + generation=int(st.get("ptz_generation", 0) or 0), + ) ++ if cam_idx == _PTZ17_CAM_IDX: ++ _record_ptz17_recovery( ++ cam_idx, st, now, source_seq, w, h, visual_result, ++ input_kind=recovery_input_kind, outcome=geometry_outcome, ++ trusted_bbox=previous_control_bbox, reference_bbox=visual_result.bbox, ++ validation_targets=validation_targets, reacquire_targets=reacquire_targets, ++ raw_box_count=raw_box_count, ++ detector_match=geometry_match, selected=validated_geometry_target, ++ seeded=seeded_geometry, expected_track_class=expected_track_class, ++ ) + logger.warning( + "[OSTRACK/IDENTITY] cam=%s HOLD rejected geometry reason=%s; awaiting recovery", + cam_idx, +@@ -7771,6 +8078,16 @@ + source="stop", + generation=int(st.get("ptz_generation", 0) or 0), + ) ++ if cam_idx == _PTZ17_CAM_IDX: ++ _record_ptz17_recovery( ++ cam_idx, st, now, source_seq, w, h, visual_result, ++ input_kind=recovery_input_kind, outcome="control_jump_stop", ++ trusted_bbox=previous_control_bbox, reference_bbox=visual_result.bbox, ++ validation_targets=validation_targets, reacquire_targets=reacquire_targets, ++ raw_box_count=raw_box_count, detector_match=geometry_match, ++ selected=validated_geometry_target, seeded=seeded_geometry, ++ expected_track_class=expected_track_class, ++ ) + logger.warning( + "[OSTRACK/IDENTITY] cam=%s STOP rejected geometry reason=%s", + cam_idx, +@@ -7782,6 +8099,7 @@ + + matched_validation = None + matched_reacquire = None ++ widened_candidate = None + if reference_bbox is not None: + matched_validation = _find_visual_validation_target( + reference_bbox, +@@ -8062,6 +8380,17 @@ + det_xyxy.append(tuple(visual_result.bbox)) + visual_track_label_index = len(det_labels) + det_labels.append("target track") ++ if cam_idx == _PTZ17_CAM_IDX and not geometry_reinitialized and ( ++ visual_result.reason == "recovered" or st.get("loss_since") is not None ++ ): ++ _record_ptz17_recovery( ++ cam_idx, st, now, source_seq, w, h, visual_result, ++ input_kind=recovery_input_kind, outcome="visual_recovered", ++ trusted_bbox=previous_control_bbox, reference_bbox=reference_bbox, ++ validation_targets=validation_targets, reacquire_targets=reacquire_targets, ++ raw_box_count=raw_box_count, detector_match=matched_validation, ++ expected_track_class=expected_track_class, ++ ) + else: + ptz17_validation_timeout = bool( + visual_only and _ptz17_validation_timeout_steering( +@@ -8105,6 +8434,7 @@ + _arm_zoom_revalidation( + st, now, "visual_reacquire", cam_idx=cam_idx + ) ++ seeded = None + if matched_reacquire is not None: + preserved_track_class = ( + int(visual_result.target_class) +@@ -8148,6 +8478,19 @@ + target_source = "none" + st["visual_tracker_score"] = float(visual_result.score) + st["visual_tracker_last_reason"] = visual_result.reason ++ if cam_idx == _PTZ17_CAM_IDX: ++ _record_ptz17_recovery( ++ cam_idx, st, now, source_seq, w, h, visual_result, ++ input_kind=recovery_input_kind, ++ outcome=target_source if matched_reacquire is not None else ++ "loss_confirmation_pending" if widened_candidate is not None else "loss_no_match", ++ trusted_bbox=previous_control_bbox, reference_bbox=reference_bbox, ++ validation_targets=validation_targets, reacquire_targets=reacquire_targets, ++ raw_box_count=raw_box_count, ++ detector_match=widened_candidate if widened_candidate is not None else matched_reacquire, ++ selected=matched_reacquire, seeded=seeded, ++ expected_track_class=expected_track_class, ++ ) + elif best_target is not None: + # ACQUIRE has already approved this target. Seed the appearance + # tracker on the same frame before entering full TRACK. +@@ -9537,6 +9880,50 @@ max(0.0, float(getattr(config, min_key, 0.10))), ) zoom_cmd = min(max_effective, max(float(zoom_cmd), min_effective)) @@ -336,7 +635,7 @@ if probe_start_candidate and identify_start_candidate: pulse_kind = "probe_identify" elif probe_start_candidate: -@@ -9645,8 +9868,30 @@ +@@ -9645,8 +10032,30 @@ zoom_cmd = 0.0 st["zoom_useful_pulse_reason"] = "wait_response_framing" zoom_zero_reason = "response_framing" @@ -367,7 +666,7 @@ zoom_cmd = _gate_zoom_in_pulse( st, # Keep new-request expiry tied to the frame that authorized -@@ -9667,9 +9912,15 @@ +@@ -9667,9 +10076,15 @@ ) if predictive_start and zoom_cmd > 0.0 and st["zoom_step_request_id"] > request_id_before_gate: st["predictive_zoom_request"] = st["zoom_step_pending_id"] @@ -383,7 +682,7 @@ if ( probe_start_candidate and zoom_cmd > 0.0 -@@ -9693,23 +9944,24 @@ +@@ -9693,23 +10108,24 @@ elif zoom_cmd < 0.0 and float(size_est) <= max(0.05, zoomed_size_thr * 0.75): st["was_zoomed_in"] = False @@ -425,7 +724,7 @@ st["fallback_done"] = False -@@ -10191,7 +10443,7 @@ +@@ -10191,7 +10607,7 @@ stop_ptz(cam_idx, source='stop', generation=int(st.get('ptz_generation', 0) or 0)) return zoom_decision = None @@ -434,7 +733,7 @@ blockers = tuple(name for name, blocked in ( ("quality", not zoom_quality_ok), ("framing", not zoom_safe), -@@ -10210,7 +10462,36 @@ +@@ -10210,7 +10626,36 @@ ("stale", visual_control_stale), ("brake", final_guard_active), ) if blocked) @@ -472,7 +771,7 @@ if predictive_decision is not None: with _zoom_command_lock(cam_idx): telemetry_tick( -@@ -10222,6 +10503,8 @@ +@@ -10222,6 +10667,8 @@ if getattr(config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", "logs/ptz_zoom"): blocked = {} if zoom_cmd <= 0.0 and raw_zoom_needed: diff --git a/integration/ptz17_focus_zoom/test_recovery_telemetry.py b/integration/ptz17_focus_zoom/test_recovery_telemetry.py new file mode 100644 index 0000000..678bb63 --- /dev/null +++ b/integration/ptz17_focus_zoom/test_recovery_telemetry.py @@ -0,0 +1,231 @@ +"""Offline comparison of TRACK recovery with and without PTZ17 telemetry.""" +from contextlib import ExitStack +import importlib.util +import json +import math +import os +from pathlib import Path +import sys +from types import SimpleNamespace +from unittest.mock import Mock, patch + +import numpy as np +import pytest +import torch + + +HERE = Path(__file__).resolve().parent +TRUSTED = (620, 350, 640, 364) +REJECTED = (570, 325, 680, 382) +GOOD = [622, 351, 643, 365] +WRONG = [590, 335, 670, 372] +MOVED = [760, 350, 780, 364] + + +@pytest.fixture(scope="module") +def versions(): + runtime = Path(os.environ["PTZ17_TEST_RUNTIME"]) + package = "_ptz17_recovery_runtime" + paths = [runtime / "__init__.py", Path(os.environ["PTZ17_TEST_BASELINE"]), + Path(os.environ["PTZ17_TEST_CANDIDATE"])] + modules = [] + for name, path in zip((package, package + ".baseline", package + ".candidate"), paths, strict=True): + spec = importlib.util.spec_from_file_location( + name, path, submodule_search_locations=[str(runtime)] if name == package else None, + ) + assert spec and spec.loader + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + modules.append(module) + return modules[1:] + + +class Boxes: + def __init__(self, observations): + self.xyxy = torch.tensor([box for box, _, _ in observations], dtype=torch.float32) + self.conf = torch.tensor([confidence for _, confidence, _ in observations]) + self.cls = torch.tensor([class_id for _, _, class_id in observations]) + + def __len__(self): + return len(self.conf) + + +def exercise(module, scenario, *, cam=6, enabled=True): + st = module.state._default_ptz_state(cam) + st.update(tracking_active=True, mode="TRACK", ptz_generation=31, track_started_at=90.0, + last_seen=99.8, last_zoom_seen=99.8, last_bbox=TRUSTED, track_source="visual", + hfov_deg=59.0, zoom_status_at=99.95, abs_zoom=1.0, identity_class=0, + visual_tracker_last_detector_at=99.9, visual_tracker_last_zoom_detector_at=99.9, + visual_tracker_last_detector_conf=0.80) + result_type = module.visual_tracker_manager.update.__globals__["VisualTrackResult"] + frame = np.zeros((720, 1280, 3), dtype=np.uint8) + operations, telemetry = [], [] + + def emit(_config, _cam, owner, name, at, **fields): + json.dumps(fields, allow_nan=False) + if name == "recovery_frame": + telemetry.append((owner, at, fields)) + + with ExitStack() as stack: + stack.enter_context(patch.dict(module.state.ptz_states, {cam: st})) + for key, value in { + "PTZ_VISUAL_CONTROL_ENABLE": True, "PREDICTIVE_ZOOM_TELEMETRY_DIR": "offline" if enabled else "", + "DETECT_CONF_BY_CLASS": {0: .65, 1: .65}, "DETECT_CONF": .65, + "DRONE_ONLY": False, "MOTION_DETECT_ENABLE": False, "BT_ENABLE": True, + "VISUAL_TRACKER_MATCH_CENTER_NORM": .10, "VISUAL_TRACKER_LOSS_REACQUIRE_CENTER_BASE": .15, + "VISUAL_TRACKER_LOSS_REACQUIRE_CONFIRM_MAX_GAP_SEC": .65, + }.items(): + stack.enter_context(patch.object(module.config, key, value, create=True)) + clock = stack.enter_context(patch.object(module.time, "time")) + update = stack.enter_context(patch.object(module.visual_tracker_manager, "update")) + for name in ("_publish_with_boxes", "notify_detected", "call_autofocus", "return_to_preset"): + stack.enter_context(patch.object(module, name)) + for name in ("stop_ptz", "move_ptz_bounded"): + stack.enter_context(patch.object(module, name, side_effect=lambda *a, key=name, **kw: + operations.append((key, a, kw)))) + stack.enter_context(patch.object(module.alarms, "is_armed", return_value=False)) + stack.enter_context(patch.object(module.alarms, "ingest_detection", side_effect=lambda *a: + operations.append(("alarm", a, {})))) + stack.enter_context(patch.object(module.visual_tracker_manager, "record_detector_validation")) + stack.enter_context(patch.object(module.visual_tracker_manager, "reset")) + stack.enter_context(patch.object(module.visual_tracker_manager, "should_refresh", return_value=False)) + stack.enter_context(patch.object(module, "_episode_telemetry", side_effect=emit)) + zoom_module = sys.modules[module.record_zoom_blocks.__module__] + stack.enter_context(patch.object(zoom_module, "_episode_telemetry", side_effect=emit)) + + def start(cam_idx, _image, box, *, target_class, detector_conf, now, source_seq): + operations.append(("seed", (cam_idx, tuple(box), target_class, detector_conf, now, source_seq), {})) + ok = scenario != "seed_failed" + return result_type(ok, tuple(box) if ok else None, .8, target_class, detector_conf, + 0, "initialized" if ok else "no template", source_seq) + + stack.enter_context(patch.object(module.visual_tracker_manager, "start", side_effect=start)) + + def frame_step(seq, at, tracker_box, reason, observations=(), *, paired=False, visual_only=False): + ok = reason in ("tracked", "recovered") + result = result_type(ok, tracker_box, .80 if ok else .29, 0, .80, 40, reason, seq) + update.return_value = result + clock.return_value = at + sample = module.visual_control.VisualSample(module.visual_control.owner(st), seq, at - .010, result) + module.postprocess_control(cam, frame, SimpleNamespace(boxes=Boxes(observations)), True, seq, + visual_sample=sample if paired or visual_only else None, + visual_only=visual_only) + + if scenario == "hold_then_strict": + frame_step(41, 100.0, REJECTED, "validated area ratio 11.34", + [(GOOD, .50, 0), (WRONG, .95, 0)]) + frame_step(42, 100.2, REJECTED, "low score", [(GOOD, .51, 0)]) + elif scenario == "wide_two_frames": + frame_step(41, 100.0, None, "low score", [(MOVED, .70, 0)]) + frame_step(42, 100.3, None, "low score", [(MOVED, .71, 0)]) + elif scenario == "seed_failed": + frame_step(41, 100.0, None, "low score", [(GOOD, .81, 0)]) + elif scenario == "visual_recovers": + frame_step(41, 100.0, REJECTED, "validated area ratio 11.34", visual_only=True) + frame_step(42, 100.1, TRUSTED, "recovered", visual_only=True) + elif scenario == "paired_hold": + frame_step(41, 100.0, REJECTED, "validated area ratio 11.34", visual_only=True) + frame_step(42, 100.1, TRUSTED, "tracked", [(GOOD, .81, 0)], paired=True) + elif scenario == "geometry_two_frames": + frame_step(41, 100.0, TRUSTED, "center jump", [(GOOD, .81, 0)]) + frame_step(42, 100.2, TRUSTED, "center jump", [(GOOD, .82, 0)]) + elif scenario == "ordinary_track": + frame_step(41, 100.0, TRUSTED, "tracked", [(GOOD, .81, 0)]) + else: + raise ValueError(scenario) + state_evidence = {key: st.get(key) for key in ( + "track_source", "zoom_state", "zoom_rearm_epoch", "zoom_rearm_votes", "zoom_prev_cmd", + "visual_reacquire_votes", "identity_state", "alarm_gate_eligible", "last_bbox", + )} + return operations, state_evidence, [fields for _, _, fields in telemetry] + + +@pytest.mark.parametrize("scenario,outcomes", [ + ("hold_then_strict", ["geometry_trusted_area_rejected", "detector_reacquire"]), + ("wide_two_frames", ["loss_confirmation_pending", "detector_reacquire"]), + ("seed_failed", ["detector_fallback"]), + ("visual_recovers", ["geometry_no_match", "visual_recovered"]), + ("paired_hold", ["geometry_no_match", "validation_only"]), + ("geometry_two_frames", ["geometry_confirmation_pending", "geometry_reinitialized"]), + ("ordinary_track", []), +]) +def test_command_and_alarm_parity(versions, scenario, outcomes): + base, candidate = versions + previous, previous_state, _ = exercise(base, scenario) + actual, actual_state, rows = exercise(candidate, scenario) + assert (actual, actual_state) == (previous, previous_state) + assert [row["outcome"] for row in rows] == outcomes + if scenario == "hold_then_strict": + reject, recover = rows + assert reject["trusted_bbox_px"] == list(TRUSTED) + assert reject["association_reference_bbox_px"] == list(REJECTED) + assert reject["ostrack"]["bbox_px"] == list(REJECTED) + assert reject["yolo_match"]["bbox_px"] == WRONG and reject["yolo_selected"] is None + assert {tuple(box["bbox_px"]) for box in reject["yolo_reacquire"]} == {tuple(WRONG), tuple(GOOD)} + assert recover["yolo_selected"]["bbox_px"] == GOOD + assert recover["reinitialized_ostrack"]["bbox_px"] == GOOD + if scenario == "wide_two_frames": + assert [row["reacquire_votes"] for row in rows] == [1, 2] + assert rows[0]["yolo_selected"] is None + assert rows[1]["yolo_selected"]["bbox_px"] == MOVED + if scenario == "paired_hold": + paired = rows[-1] + assert paired["frame_seq"] == paired["ostrack"]["frame_seq"] == 42 + assert paired["observation_utc"] == pytest.approx(100.09) + assert paired["input_kind"] == "paired_detector" + assert paired["yolo_match"]["bbox_px"] == GOOD + if scenario == "geometry_two_frames": + assert [row["reacquire_votes"] for row in rows] == [1, 2] + assert rows[-1]["ostrack"]["ok"] is False + assert rows[-1]["reinitialized_ostrack"]["ok"] is True + assert rows[-1]["reinitialized_ostrack"]["bbox_px"] == GOOD + + +@pytest.mark.parametrize("cam,enabled", [(8, True), (6, False)]) +def test_scope_and_disable(versions, cam, enabled): + assert exercise(versions[1], "hold_then_strict", cam=cam, enabled=enabled)[2] == [] + + +def test_bounds_and_finite_json(versions): + module = versions[1] + valid = module.visual_tracker_manager.update.__globals__["VisualTrackResult"] + result = valid(False, (620, 350, 640, 364), math.nan, 0, .8, 40, "x" * 900, 17) + candidates = [([k, 4, k + 20, 17], math.inf if k == 0 else .8, 0) for k in range(33)] + selected = candidates[-1] + state = dict(mode="TRACK", tracking_active=True, track_started_at=90.0, ptz_generation=7) + sink = Mock() + with patch.object(module, "_episode_telemetry", sink): + module._record_ptz17_recovery( + 6, state, 100.0, 17, 1280, 720, result, input_kind="detector_control", + outcome="geometry_hold", trusted_bbox=TRUSTED, reference_bbox=result.bbox, + validation_targets=candidates, reacquire_targets=candidates, raw_box_count=33, selected=selected, + ) + assert sink.call_args.args[1:4] == (6, (7, 90.0), "recovery_frame") + row = sink.call_args.kwargs + assert len(row["yolo_validation"]) == len(row["yolo_reacquire"]) == 12 + assert row["yolo_validation_count"] == row["yolo_reacquire_count"] == 33 + assert row["yolo_selected"]["bbox_px"] == selected[0] + assert row["ostrack"]["score"] is None and row["yolo_validation"][0]["confidence"] is None + assert len(row["ostrack"]["reason"]) == 160 + json.dumps(row, allow_nan=False) + + +def test_full_queue_drops_telemetry_without_waiting(versions, tmp_path): + module = versions[1] + log_module = sys.modules[module.record_zoom_blocks.__module__] + with patch.object(log_module, "Thread"): + writer = log_module.EpisodeTelemetryLogger(tmp_path, capacity=1) + result_type = module.visual_tracker_manager.update.__globals__["VisualTrackResult"] + result = result_type(False, REJECTED, .29, 0, .8, 40, "area ratio 11.34", 17) + state = dict(mode="TRACK", tracking_active=True, track_started_at=90.0, ptz_generation=7) + with patch.object(module.config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", str(tmp_path), create=True): + with patch.dict(log_module._telemetry_loggers, {str(tmp_path): writer}): + for _ in range(2): + module._record_ptz17_recovery( + 6, state, 100.0, 17, 1280, 720, result, input_kind="visual_control", + outcome="geometry_no_match", trusted_bbox=TRUSTED, reference_bbox=REJECTED, + validation_targets=[], reacquire_targets=[], raw_box_count=0, + ) + assert writer.queue.qsize() == 1 + assert writer.dropped == 1