--- a/ptz_tracker_modular/alarms.py +++ b/ptz_tracker_modular/alarms.py @@ -10,2 +10,3 @@ import threading +from itertools import count from datetime import datetime, timezone @@ -29,2 +30,7 @@ cameras_io = None # type: ignore + +try: + from . import alarm_boundary as _boundary +except ImportError: + import alarm_boundary as _boundary @@ -81,2 +87,3 @@ _http_queue: Optional[asyncio.Queue] = None +_http_revisions = count(1) def _ensure_http_queue() -> asyncio.Queue: @@ -266,3 +273,3 @@ online = bool(cam_id in detected) - armed = bool((_H.get(cam_id) or {}).get("armed", False)) + armed = is_armed(cam_id) cameras_payload.append(_build_swagger_camera(mac_n, alarm=armed, status=online)) @@ -277,3 +284,16 @@ +def _camera_for_mac(mac): + try: + normalized = _norm_mac(mac) + found = [int(c) for c in config.CAMERA_CONFIG if _get_cam_mac(int(c)) == normalized] + return found[0] if len(found) == 1 else None + except Exception: + return None + + def _dispatch_ws(minimal: Dict[str, Any]) -> None: + if _boundary.enabled(): + minimal = {**minimal, "_cc10_contexts": _boundary.capture_contexts(config.CAMERA_CONFIG)} + token = minimal["_cc10_contexts"] + minimal = {**_boundary.filter_ws(minimal), "_cc10_contexts": token} q = _ensure_ws_queue() @@ -291,3 +311,6 @@ loop = _state.MAIN_LOOP - item = {"url": url, "json": body} + # Keep the original ordering when a failed snapshot is retried later. + contexts = _boundary.capture_contexts(config.CAMERA_CONFIG) + item = {"url": url, "json": _boundary.filter_http(body, contexts, _camera_for_mac), + "_created_at": next(_http_revisions), "_cc10_contexts": contexts} if loop is not None: @@ -316,3 +339,3 @@ """Публичный геттор текущего состояния тревоги (armed) по камере.""" - return bool((_H.get(int(cam_idx)) or {}).get("armed", False)) + return bool((_H.get(int(cam_idx)) or {}).get("armed", False)) and _boundary.arm_allowed(int(cam_idx)) @@ -332,2 +355,3 @@ """ + detected = bool(detected) and _boundary.arm_allowed(cam_idx) changed = False @@ -335,2 +359,4 @@ st = _hyst_state(cam_idx) + if _boundary.enabled() and not detected: + st["off"] = ALARM_COUNT_OFF if detected: @@ -347,3 +373,3 @@ changed = True - aggregate_alarm = any(bool(v.get("armed", False)) for v in _H.values()) + aggregate_alarm = any(is_armed(c) for c in _H) @@ -383,2 +409,4 @@ return + + alarm = bool(alarm) and _boundary.arm_allowed(_camera_for_mac(mac_n)) @@ -465,3 +493,4 @@ - data_bit = bool(last.get("data", False)) if isinstance(last, dict) else bool(last) + guarded_last = _boundary.filter_ws(last) + data_bit = bool(guarded_last.get("data", False)) if isinstance(guarded_last, dict) else bool(guarded_last) to_send = {"type": "video_alarm", "data": data_bit} @@ -489,2 +518,9 @@ +def _http_error_detail(response: Any) -> str: + detail = str(response.text or "") + if ALARM_HTTP_API_KEY: + detail = detail.replace(ALARM_HTTP_API_KEY, "[redacted]") + return " ".join(detail.split())[:500] + + async def alarm_http_sender() -> None: @@ -499,22 +535,6 @@ from requests.adapters import HTTPAdapter - try: - from urllib3.util.retry import Retry # type: ignore - except Exception: - Retry = None # type: ignore - sess = requests.Session() sess.trust_env = False - if Retry is not None: - adapter = HTTPAdapter( - pool_connections=16, - pool_maxsize=16, - max_retries=Retry( - total=2, - backoff_factor=0.3, - status_forcelist=[500, 502, 503, 504], - raise_on_status=False, - ), - ) - else: - adapter = HTTPAdapter(pool_connections=16, pool_maxsize=16, max_retries=0) + # One retry policy owns POST attempts; do not multiply transport retries. + adapter = HTTPAdapter(pool_connections=16, pool_maxsize=16, max_retries=0) sess.mount("http://", adapter) @@ -527,7 +547,20 @@ - def _merge_http_bucket(bucket: List[Dict[str, Any]]) -> Tuple[str, Dict[str, Any]]: - # Все элементы в bucket — с одинаковым URL. Берём ПОСЛЕДНИЙ body. - url = bucket[0].get("url", "") - last_body = bucket[-1].get("json") or {} - return url, last_body + latest_by_url: Dict[str, int] = {} + max_retries = max(0, min(5, int(getattr(config, "ALARM_HTTP_MAX_RETRIES", 2)))) + retry_base = max(0.0, float(getattr(config, "ALARM_HTTP_RETRY_BASE_SEC", 1.0))) + + async def retry_failed(failed: Dict[str, Any], response: Any = None) -> None: + attempts = int(failed.get("_retries", 0)) + if attempts >= max_retries: + logger.error("[ALARM][HTTP-BULK][RETRY-EXHAUSTED] url=%s attempts=%d; waiting for fresh state", + failed.get("url", ""), attempts + 1) + return + delay = min(8.0, retry_base * (2 ** attempts)) + if response is not None: + try: + delay = max(delay, min(30.0, float(response.headers.get("Retry-After", 0)))) + except (ValueError, TypeError, AttributeError): + pass + await asyncio.sleep(delay) + q.put_nowait({**failed, "_retries": attempts + 1}) @@ -535,4 +568,6 @@ item = await q.get() - try: - bucket = [item] + item.setdefault("_created_at", next(_http_revisions)) + selected = item + bucket = [item] + try: t0 = asyncio.get_running_loop().time() @@ -541,2 +576,3 @@ nxt = q.get_nowait() + nxt.setdefault("_created_at", next(_http_revisions)) if nxt.get("url") != bucket[0].get("url"): @@ -550,3 +586,7 @@ - url_raw, body = _merge_http_bucket(bucket) + # A retry can be queued behind a newer alarm OFF. Queue position + # must never make an old ON overwrite that newer observation. + _, selected = max(enumerate(bucket), key=lambda pair: (pair[1]["_created_at"], pair[0])) + url_raw = selected.get("url", "") + body = selected.get("json") or {} base = _sanitize_http_base(ALARM_HTTP_BASE) @@ -559,2 +599,7 @@ + revision = selected["_created_at"] + if revision < latest_by_url.get(url, 0): + continue + latest_by_url[url] = revision + cams_cnt = len(body.get("cameras") or []) @@ -563,3 +608,3 @@ def _post(): - return sess.post(url, json=body, headers=headers, timeout=3.0, verify=verify_arg) + return sess.post(url, json=_boundary.filter_http(body, selected.get("_cc10_contexts"), _camera_for_mac), headers=headers, timeout=3.0, verify=verify_arg) @@ -569,15 +614,11 @@ else: - logger.warning("[ALARM][HTTP-BULK][FAIL] %s HTTP %s", url, resp.status_code) - logger.debug("[ALARM][HTTP-BULK][RESP] %s", (resp.text or "")[:200]) - # вернуть последний элемент на ретрай - try: - q.put_nowait(bucket[-1]) - except Exception: - pass + logger.warning("[ALARM][HTTP-BULK][FAIL] %s HTTP %s response=%s", + url, resp.status_code, _http_error_detail(resp)) + if resp.status_code in (408, 429) or 500 <= resp.status_code < 600: + await retry_failed(selected, resp) + # Repeating an unchanged 400/401/403/404/422 cannot repair it. + # A fresh event or periodic heartbeat may still be delivered. except Exception as exc: logger.error("[ALARM][HTTP-BULK] error: %s", exc) - try: - q.put_nowait(item) - except Exception: - pass + await retry_failed(selected) finally: @@ -627,3 +668,3 @@ sess.trust_env = False - if Retry is not None: + if Retry is not None and not _boundary.enabled(): adapter = HTTPAdapter( @@ -688,2 +729,3 @@ body = _build_swagger_payload(cameras) + contexts = _boundary.capture_contexts(config.CAMERA_CONFIG) cams_cnt = len(cameras) @@ -692,3 +734,3 @@ def _post(): - return sess.post(url, json=body, headers=headers, timeout=3.0, verify=verify_arg) + return sess.post(url, json=_boundary.filter_http(body, contexts, _camera_for_mac), headers=headers, timeout=3.0, verify=verify_arg) @@ -698,4 +740,4 @@ else: - logger.warning("[ALARM][HEARTBEAT][FAIL] %s HTTP %s", url, resp.status_code) - logger.debug("[ALARM][HEARTBEAT][RESP] %s", (resp.text or "")[:200]) + logger.warning("[ALARM][HEARTBEAT][FAIL] %s HTTP %s response=%s", + url, resp.status_code, _http_error_detail(resp)) else: --- a/ptz_tracker_modular/events.py +++ b/ptz_tracker_modular/events.py @@ -11,3 +11,3 @@ -from . import config, state +from . import config, state, alarm_boundary as _boundary from .event_logging import start_record_event, stop_record_event @@ -100,2 +100,5 @@ """ + if _boundary.enabled(): + contexts = _boundary.capture_contexts(config.CAMERA_CONFIG) + payload = {**_boundary.filter_ws({**payload, "_cc10_contexts": contexts}), "_cc10_contexts": contexts} if state.alarm_queue is not None and state.MAIN_LOOP is not None: @@ -132,2 +135,3 @@ try: + guarded_payload = _boundary.filter_ws(payload) # Строго минимальный словарь только с нужными ключами @@ -135,4 +139,4 @@ minimal = { - "type": payload.get("type"), - "data": bool(payload.get("data")), + "type": guarded_payload.get("type"), + "data": bool(guarded_payload.get("data")), } @@ -140,3 +144,3 @@ # на случай если кто-то положил уже готовую строку/другую структуру - minimal = payload + minimal = guarded_payload @@ -169,3 +173,3 @@ """ - _dispatch_alarm({"type": "freq_alarm", "data": bool(state_on)}) + _dispatch_alarm({"type": "freq_alarm", "data": bool(state_on) and _boundary.arm_allowed(cam_idx)}) --- a/ptz_tracker_modular/main.py +++ b/ptz_tracker_modular/main.py @@ -217,2 +217,8 @@ async def main() -> None: + from ptz_tracker_modular import inspection_handoff + if inspection_handoff.enabled(): + from ptz_tracker_modular.inspection_startup import configure_runtime + inspection = configure_runtime() + else: + inspection = None # --- Инициализация общего состояния --- @@ -479,3 +485,3 @@ # ---- Опциональный smoke-тест тревоги (по первому MAC) ---- - if os.getenv("ALARM_SMOKE", "0") in ("1", "true", "TRUE", "yes", "YES"): + if getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is not True and os.getenv("ALARM_SMOKE", "0") in ("1", "true", "TRUE", "yes", "YES"): async def _smoke(): @@ -528,2 +534,5 @@ + if inspection is not None: + tasks.append(watch(inspection.recognition_loop(), "unknown_recognition")) + try: --- a/ptz_tracker_modular/postprocess.py +++ b/ptz_tracker_modular/postprocess.py @@ -11,2 +11,3 @@ from . import config, state, visual_control +from . import unknown_inspection as inspection from . import motion_detector @@ -47,2 +48,3 @@ logger = logging.getLogger("PTZTracker") +_inspection_move_emit = move_ptz_bounded @@ -52,3 +54,4 @@ """Keep class filtering on the detector result's actual device.""" - return torch.as_tensor(config.TARGET_CLASSES, device=classes.device, dtype=classes.dtype) + return torch.as_tensor((0,) if _inspection_enabled() else config.TARGET_CLASSES, + device=classes.device, dtype=classes.dtype) @@ -57,2 +60,4 @@ """Return the detector class name retained by candidate/visual tracking.""" + if getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is True: + return "unknown airborne object" try: @@ -540,3 +545,10 @@ st["ptz_move_seq"] = int(st.get("ptz_move_seq", 0) or 0) + 1 + inspection_on = getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is True + old_owner = visual_control.owner(st) if inspection_on else None st["zoom_rearm_epoch"] = int(st.get("zoom_rearm_epoch", 0) or 0) + 1 + session = st.get("inspection_session") + if inspection_on and isinstance(session, inspection.Session) and session.owner == old_owner: + session.owner = visual_control.owner(st) + session.recent.clear() + session.revoke_semantics("zoom_revalidation") st["zoom_rearm_required"] = True @@ -650,2 +662,9 @@ """Require strong detector evidence after any optical narrowing.""" + if _inspection_enabled(): + cfg = getattr(config, "UNKNOWN_INSPECTION_SETTINGS", None) + session = st.get("inspection_session") + return bool(isinstance(cfg, inspection.Settings) and isinstance(session, inspection.Session) + and session.live(time.monotonic()) and inspection.finite(confidence) + and confidence >= cfg.proposal_min + and 0 <= time.time()-session.last_at <= .65) followup = bool( @@ -3972,2 +3991,3 @@ frame_h: int, + *, inspection_state: dict | None = None, ) -> tuple[bool, float]: @@ -3990,2 +4010,8 @@ detector_conf = float(matched[1]) if matched is not None else 0.0 + if _inspection_enabled(): + st = inspection_state or {} + session = st.get("inspection_session") + allowed = st.get("inspection_authorized") is True and isinstance(session, inspection.Session) and matched is not None and session.contract == getattr(config, "UNKNOWN_INSPECTION_SETTINGS", None) and session.alarm_allowed( + time.time(), time.monotonic(), visual_control.owner(st), tuple(target_bbox), (frame_h, frame_w)) + return bool(allowed), session.semantic_score if allowed else 0. threshold = float(getattr(config, "ALARM_CONF", 0.50)) @@ -4016,2 +4042,9 @@ """Keep localization, class identity and protection as separate states.""" + if _inspection_enabled(): + session = st.get("inspection_session") + st["tracking_state"] = "inspecting" + st["identity_state"] = session.decision if isinstance(session, inspection.Session) else "unresolved" + st["identity_class"] = -1 + st["protection_state"] = "pending" if alarm_eligible else "safe" + return source = str(target_source or "none") @@ -4093,2 +4126,7 @@ def _format_visual_tracking_label(st: dict, *, tracker_score: float) -> str: + if _inspection_enabled(): + session = st.get("inspection_session") + if isinstance(session, inspection.Session): + return f"{session.decision}: {session.semantic_name or 'unknown object'}; inspecting" + return "unknown object; inspection unavailable" identity_state = str(st.get("identity_state", "unknown")) @@ -4569,2 +4607,4 @@ """Break continuous zoom-in into short feedback-controlled steps.""" + if _inspection_enabled(): + zoom_cmd = min(float(zoom_cmd), .10) pulse_until = float(st.get("zoom_step_until", 0.0) or 0.0) @@ -5454,3 +5494,3 @@ -def _enter_acquire_locked(cam_idx: int, now: float) -> bool: +def _legacy_enter_acquire_locked(cam_idx: int, now: float) -> bool: st = state.ensure_cam_state(cam_idx, is_ptz=True) @@ -5978,3 +6018,3 @@ -def _enter_track_locked(cam_idx: int) -> None: +def _legacy_enter_track_locked(cam_idx: int) -> None: st = state.ensure_cam_state(cam_idx, is_ptz=cam_idx in config.PTZ_CAM_IDS) @@ -6541,3 +6581,3 @@ alarm_eligible, alarm_conf = _alarm_eligible_for_target( - bbox, target_class, detector_targets, width, height) + bbox, target_class, detector_targets, width, height, inspection_state=st) identity_conf = float(matched[1]) if matched is not None and int(matched[2]) == target_class else 0. @@ -6548,3 +6588,3 @@ alarm_eligible=alarm_eligible, cam_idx=cam_idx) - alarms.ingest_detection(cam_idx, alarm_eligible) + alarms.ingest_detection(cam_idx, bool(alarm_eligible) and (not _inspection_enabled() or _inspection_alarm_ready(st))) @@ -6596,2 +6636,4 @@ if not visual_only: + if _inspection_enabled(): + eligible = bool(eligible) and _inspection_alarm_ready(st) alarms.ingest_detection(cam_idx, eligible) @@ -6805,2 +6847,8 @@ + if _inspection_enabled() and not _inspection_before_frame( + cam_idx, st, res, frame_bgr, source_seq, is_ptz, visual_only, visual_sample): + return + if _inspection_enabled(): + boxes_t = getattr(res, "boxes", None) + _cfg = config.CAMERA_CONFIG.get(int(cam_idx), {}) if hasattr(config, "CAMERA_CONFIG") else {} @@ -6874,3 +6922,3 @@ mask = torch.isin(clss, _targets_tensor_like(clss)) - if getattr(config, "DRONE_ONLY", False): + if not _inspection_enabled() and getattr(config, "DRONE_ONLY", False): drone_id = int(getattr(config, "DRONE_CLASS_ID", 1)) @@ -6887,3 +6935,3 @@ conf_by_cls = getattr(config, "DETECT_CONF_BY_CLASS", None) - if isinstance(conf_by_cls, dict) and conf_by_cls: + if not _inspection_enabled() and isinstance(conf_by_cls, dict) and conf_by_cls: for k, v in conf_by_cls.items(): @@ -6896,2 +6944,5 @@ + if _inspection_enabled(): + conf_thr = torch.full_like(confs, config.UNKNOWN_INSPECTION_SETTINGS.proposal_min) + # === Motion-aided detection: boost conf для боксов внутри motion ROI === @@ -6902,2 +6953,3 @@ is_ptz + and not _inspection_enabled() and bool(getattr(config, "MOTION_DETECT_ENABLE", False)) @@ -6924,3 +6976,3 @@ # optional drone size filter (fractions of frame) - if getattr(config, "DRONE_SIZE_FILTER", False): + if not _inspection_enabled() and getattr(config, "DRONE_SIZE_FILTER", False): drone_ids_cfg = getattr(config, "DRONE_CLASS_ID", 1) @@ -7943,3 +7995,3 @@ (int(x1), int(y1), int(x2), int(y2)), - int(best_cls), detector_targets, w, h, + int(best_cls), detector_targets, w, h, inspection_state=st, ) @@ -10272 +10324,231 @@ return response + + +def _enter_acquire_locked(cam_idx: int, now: float): + st = state.ensure_cam_state(cam_idx, is_ptz=True) + old_owner = visual_control.owner(st) + count = int(st.get("zoom_no_effect_count", 0) or 0) + blocked = bool(st.get("zoom_no_effect_blocked", False)) + result = _legacy_enter_acquire_locked(cam_idx, now) + if _inspection_enabled(): + st["zoom_no_effect_count"] = max(count, int(st.get("zoom_no_effect_count", 0) or 0)) + st["zoom_no_effect_blocked"] = blocked or bool(st.get("zoom_no_effect_blocked", False)) + session = st.get("inspection_session") + if isinstance(session, inspection.Session) and session.owner == old_owner: + new_owner = visual_control.owner(st) + if new_owner != old_owner: + session.owner = new_owner + session.recent.clear() + session.revoke_semantics("known_owner_transition") + return result + + +def _enter_track_locked(cam_idx: int): + st = state.ensure_cam_state(cam_idx, is_ptz=True) + old_owner = visual_control.owner(st) + count = int(st.get("zoom_no_effect_count", 0) or 0) + blocked = bool(st.get("zoom_no_effect_blocked", False)) + result = _legacy_enter_track_locked(cam_idx) + if _inspection_enabled(): + st["zoom_no_effect_count"] = max(count, int(st.get("zoom_no_effect_count", 0) or 0)) + st["zoom_no_effect_blocked"] = blocked or bool(st.get("zoom_no_effect_blocked", False)) + session = st.get("inspection_session") + if isinstance(session, inspection.Session) and session.owner == old_owner: + new_owner = visual_control.owner(st) + if new_owner != old_owner: + session.owner = new_owner + session.recent.clear() + session.revoke_semantics("known_owner_transition") + return result + + +def _inspection_enabled(): + return getattr(config, 'UNKNOWN_INSPECTION_ENABLE', False) is True + + +def _inspection_alarm_ready(st): + from .inspection_runtime import current_detector + session = st.get('inspection_session') + cfg = getattr(config, 'UNKNOWN_INSPECTION_SETTINGS', None) + return bool(isinstance(cfg, inspection.Settings) and cfg.enabled + and isinstance(session, inspection.Session) and session.contract == cfg + and st.get('inspection_authorized') is True + and st.get('mode') not in {'manual', 'MANUAL', 'RETURNING', 'RETURN', 'GOTO'} + and current_detector(st, time.monotonic()) + and _inspection_common_ready(st) + and session.alarm_allowed(time.time(), time.monotonic(), visual_control.owner(st), + tuple(st.get('last_bbox') or session.bbox or ()), session.shape)) + + +def _inspection_common_ready(st): + """Independent monotonic two-frame veto; no binding is no alarm.""" + guard = st.get('inspection_common_alarm_guard') + try: + return callable(guard) and guard() is True + except Exception: + return False + + +def _inspection_before_frame(cam, st, result, frame, seq, is_ptz, visual_only, sample): + """No inference here: only bounded metadata and current-frame admission.""" + cfg = getattr(config, 'UNKNOWN_INSPECTION_SETTINGS', None) + session = st.get('inspection_session') + now, mono = time.time(), time.monotonic() + + def deny(reason): + if isinstance(session, inspection.Session) and session.owner != visual_control.owner(st): + return False # an old callback cannot STOP or clear a newer owner + st['alarm_gate_eligible'] = False + st['alarm_detector_conf'] = 0. + st['inspection_reason'] = reason + if isinstance(session, inspection.Session): + session.revoke_semantics(reason) + if st.get('inspection_stop_reason') != reason: + stop_ptz(cam, source='stop', generation=int(st.get('ptz_generation', 0) or 0)) + st['inspection_stop_reason'] = reason + if not visual_only: + alarms.ingest_detection(cam, False) + return False + + if not is_ptz: + return deny('inspection_ptz_only') + if not isinstance(cfg, inspection.Settings) or not cfg.enabled: + return deny('missing_validated_contract') + if isinstance(session, inspection.Session) and session.contract is not None and session.contract != cfg: + session.phase = 'closed' + return deny('model_contract_changed') + if st.get('inspection_authorized') is not True: + return deny('inspection_not_authorized') + if st.get('mode') in {'manual', 'MANUAL', 'RETURNING', 'RETURN', 'GOTO'}: + return deny('camera_not_authorized_for_inspection') + incoming = getattr(result, 'inspection_observation', None) + if isinstance(incoming, inspection.Observation) and incoming.owner != visual_control.owner(st): + return False + from .inspection_runtime import provenance, check_stamp + reason = provenance(st, result, sample, camera=cam, seq=seq, + owner=visual_control.owner(st), mono=mono, visual_only=visual_only) + if reason: + return deny(reason) + if isinstance(session, inspection.Session) and session.phase == 'closed': + # A new SEARCH owner may inspect another object. A closed callback itself + # never renews its session/budget. Same-object retry is deliberately not + # automatic in this first candidate; the discovery scheduler must retire + # its bounded suppression entry explicitly. + another = (isinstance(incoming, inspection.Observation) + and any(inspection.valid_box(b, incoming.shape) + and not inspection.same_object(session.bbox, b, incoming.shape) + for b, _, _ in incoming.detections)) + if (another and not st.get('tracking_active') + and st.get('mode') in {'IDLE', 'SWEEP', 'auto'}): + session = None + st.pop('inspection_session', None) + else: + return deny('inspection_closed') + if isinstance(session, inspection.Session): + if session.decision == 'target' and not 0 <= now-session.semantic_at <= .65: + session.revoke_semantics('semantic_evidence_expired') + if session.owner != visual_control.owner(st): + session.phase = 'closed' + return deny('owner_changed') + if not session.live(mono): + session.phase = 'closed' + return deny('inspection_timeout') + if session.decision == 'non_target': + session.phase = 'closed' + deny('confirmed_non_target') + _end_track_and_park(cam, reason='inspection confirmed non-target') + return False + if session.phase == 'closed': + return deny('inspection_closed') + if visual_only: + if not isinstance(session, inspection.Session) or session.phase != 'inspection': + return deny('no_inspection_owner') + if (sample is None or not sample.result.ok or sample.result.bbox is None + or not 0 <= now-session.last_at <= .65 + or tuple(frame.shape[:2]) != session.shape + or not inspection.same_object(session.bbox, sample.result.bbox, session.shape)): + return deny('unverified_visual_continuity') + st['inspection_control_deadline'] = min( + mono + max(0., .25-(now-sample.captured_at)), st['inspection_input_mono']+.25) + return True + observation = getattr(result, 'inspection_observation', None) + if not isinstance(observation, inspection.Observation) or observation.source_seq != seq: + return deny('missing_original_frame_metadata') + if observation.shape != tuple(frame.shape[:2]) or observation.owner != visual_control.owner(st): + return deny('foreign_observation') + if session is None: + st['inspection_serial'] = int(st.get('inspection_serial', 0) or 0)+1 + session = inspection.Session(cam, st['inspection_serial'], visual_control.owner(st), + mono, mono+cfg.budget_sec) + st['inspection_session'] = session + st['purpose'] = 'inspection' + admitted = session.observe(observation, cfg, now, mono) + recognition = getattr(result, 'inspection_recognition', None) + if admitted and isinstance(recognition, inspection.Recognition): + rec_stamp = getattr(result, 'inspection_recognition_stamp', None) + if check_stamp(rec_stamp, camera=cam, seq=recognition.source_seq, + owner=recognition.owner, expected_owner=visual_control.owner(st), + binding=st.get('inspection_source_binding'), now=mono, age=.65): + session.recognize(recognition, cfg, now, mono) + if session.semantic_seq == recognition.source_seq and session.semantic_votes: + st['inspection_semantic_stamp'] = rec_stamp + else: + session.revoke_semantics('stale_or_foreign_recognition_stamp') + if not admitted: + return deny(session.reason) + if session.decision == 'non_target': + session.phase = 'closed' + deny('confirmed_non_target') + _end_track_and_park(cam, reason='inspection confirmed non-target') + return False + # Old-frame evidence may validate identity, but must not replay a MOVE. + if sample is None and (not 0 <= now-observation.observed_at <= .25 + or not 0 <= mono-st['inspection_input_mono'] <= .25): + return deny('too_old_for_motion') + st['inspection_stop_reason'] = '' + st['inspection_reason'] = session.reason + st['purpose'] = 'target' if session.decision == 'target' else 'inspection' + st['inspection_control_deadline'] = min( + mono + max(0., .25-(now-observation.observed_at)), st['inspection_input_mono']+.25) + # The legacy ranker must never select a stronger neighboring proposal over + # the object whose temporal admission we just checked. + if getattr(result, 'boxes', None) is not None: + raw_boxes = result.boxes.xyxy.detach().cpu().tolist() + indices = [i for i, b in enumerate(raw_boxes) if tuple(b) == session.bbox] + if not indices: + return deny('proposal_result_mismatch') + result.boxes = result.boxes[indices[:1]] + return True + + +def move_ptz_bounded(cam_idx, pan_speed, tilt_speed, zoom=0., **kwargs): + """Last-boundary recheck; the existing physical worker is still authoritative.""" + if _inspection_enabled(): + from .inspection_runtime import current_detector + st = state.ptz_states.get(cam_idx, {}) + session = st.get('inspection_session') + now, mono = time.time(), time.monotonic() + if isinstance(session, inspection.Session) and session.decision == 'target': + stamp = st.get('inspection_semantic_stamp') + from .inspection_runtime import check_stamp + if (not 0 <= now-session.semantic_at <= .65 + or not check_stamp(stamp, camera=cam_idx, seq=session.semantic_seq, + owner=session.owner, expected_owner=visual_control.owner(st), + binding=st.get('inspection_source_binding'), now=mono, age=.65)): + session.revoke_semantics('semantic_evidence_expired_at_emit') + elif mono >= session.deadline_mono and not _inspection_common_ready(st): + session.revoke_semantics('independent_semantic_evidence_expired_at_emit') + valid = (isinstance(session, inspection.Session) + and st.get('inspection_authorized') is True + and st.get('mode') not in {'manual', 'MANUAL', 'RETURNING', 'RETURN', 'GOTO'} + and session.live(mono) and session.phase == 'inspection' + and session.decision != 'non_target' + and session.owner == visual_control.owner(st) + and current_detector(st, mono) + and 0 <= now-session.last_at <= .65 + and mono < float(st.get('inspection_control_deadline', 0.) or 0.)) + if not valid: + stop_ptz(cam_idx, source='stop', generation=int(st.get('ptz_generation', 0) or 0)) + return + zoom = min(float(zoom), .10) + return _inspection_move_emit(cam_idx, pan_speed, tilt_speed, zoom, **kwargs) --- a/ptz_tracker_modular/streaming.py +++ b/ptz_tracker_modular/streaming.py @@ -25,2 +25,4 @@ from .postprocess import postprocess_control +from . import inspection_handoff +from uuid import uuid4 from .visual_tracker import visual_tracker_manager @@ -60,3 +62,3 @@ _cpp_stream_map_missing_logged: str = "" -_visual_feed_pending: Dict[int, Tuple[np.ndarray, int, float]] = {} +_visual_feed_pending: Dict[int, Tuple[np.ndarray, int, float, Any]] = {} _visual_feed_last_served: Dict[int, float] = {} @@ -228,3 +230,3 @@ waiting = [] - for cam_id, (_, _, queued_at) in _visual_feed_pending.items(): + for cam_id, (_, _, queued_at, _) in _visual_feed_pending.items(): st = state.ptz_states.get(int(cam_id), {}) @@ -256,3 +258,3 @@ break - frame_bgr, source_seq, queued_at = _visual_feed_pending.pop(cam_id) + frame_bgr, source_seq, queued_at, ticket = _visual_feed_pending.pop(cam_id) captured_at = _visual_feed_captured_at.pop(cam_id, time.time()) @@ -274,2 +276,3 @@ captured_at=captured_at, + inspection_ticket=ticket, ), @@ -299,2 +302,3 @@ source_seq: int, + *, inspection_ticket=None, ) -> None: @@ -315,4 +319,6 @@ queued_at = float(previous[2]) if previous is not None else time.perf_counter() - _visual_feed_pending[int(cam_id)] = (frame_bgr, int(source_seq), queued_at) - _visual_feed_captured_at[int(cam_id)] = time.time() + _visual_feed_pending[int(cam_id)] = (frame_bgr, int(source_seq), queued_at, inspection_ticket) + _visual_feed_captured_at[int(cam_id)] = ( + inspection_ticket.stamp.decode_utc_ns/1e9 if inspection_ticket else time.time() + ) if _visual_feed_worker_task is None or _visual_feed_worker_task.done(): @@ -776,2 +782,4 @@ st = state.ptz_states.get(cam_id, {}) + if getattr(config, "UNKNOWN_INSPECTION_ENABLE", False) is True: + continue # UNKNOWN publishes from semantic eligibility, never recording. has_target_now = bool(st.get("rec_active", False)) @@ -1910,2 +1918,4 @@ """Replace a queued stale frame with the newest frame from the same camera.""" + if inspection_handoff.enabled(): + return inspection_handoff.require_configured().refresh(item) cam_idx, frame_bgr, is_ptz, source_seq, fut, queued_at = item @@ -1967,2 +1977,3 @@ source_seq: int = 0, + *, inspection_ticket=None, ) -> None: @@ -1971,5 +1982,7 @@ async with self._cond: - self._buf.append( - (cam_idx, frame_bgr, is_ptz, int(source_seq), fut, time.perf_counter()) - ) + item = cam_idx, frame_bgr, is_ptz, int(source_seq), fut, time.perf_counter() + if inspection_handoff.enabled(): + if not inspection_handoff.require_configured().bind_item(item, inspection_ticket): + return + self._buf.append(item) self._cond.notify() @@ -2034,2 +2047,6 @@ ]) + if inspection_handoff.enabled(): + batch = inspection_handoff.require_configured().prune(batch) + if not batch: + continue frames = [b[1] for b in batch] @@ -2037,3 +2054,6 @@ infer_t0 = time.perf_counter() - results = await loop.run_in_executor(None, run, frames) + detect = run + if inspection_handoff.enabled(): + detect = inspection_handoff.require_configured().detect + results = await loop.run_in_executor(None, detect, frames) forward_ms = (time.perf_counter() - infer_t0) * 1000.0 @@ -2044,3 +2064,3 @@ ) - if bool(getattr(config, "SAHI_ENABLE", False)): + if bool(getattr(config, "SAHI_ENABLE", False)) and not inspection_handoff.enabled(): results = await loop.run_in_executor( @@ -2108,3 +2128,6 @@ + handoff = inspection_handoff.require_configured() if inspection_handoff.enabled() else None while True: + source_cameras = set() + stream_epoch = uuid4().hex if handoff else None try: @@ -2196,2 +2219,10 @@ + inspection_ptz = handoff is not None and idx in handoff.bridge.crops.cameras + epoch = handoff.camera_epoch(idx) if inspection_ptz else None + if inspection_ptz: + if not handoff.begin_source(idx, f"cpp_{idx_raw}", stream_epoch, epoch): + continue + source_cameras.add(idx) + + ticket = None now_pc = time.perf_counter() @@ -2212,2 +2243,4 @@ if img is not None: + if inspection_ptz: + ticket = handoff.accepted(idx, img, source_seq, epoch) _publish_frontend_raw_preview(idx, img, pst, source_seq, priority=4) @@ -2223,8 +2256,15 @@ - if getattr(config, "DROP_DECODE_WHEN_BUSY", False) and pst["proc_busy"]: + if getattr(config, "DROP_DECODE_WHEN_BUSY", False) and pst["proc_busy"] and ( + handoff is None or inspection_ptz + ): img_busy = img if img is not None else _decode_image(encoded) if img_busy is not None: - _feed_visual_tracker_frame(idx, img_busy, pst, source_seq) - pst["pending_frame"] = img_busy - pst["pending_source_seq"] = int(source_seq) + if handoff is None: + pst["pending_frame"] = img_busy + pst["pending_source_seq"] = int(source_seq) + elif inspection_ptz: + if img is None: + ticket = handoff.accepted(idx, img_busy, source_seq, epoch) + handoff.pending(ticket) + _feed_visual_tracker_frame(idx, img_busy, pst, source_seq, inspection_ticket=ticket) if can_publish and _frontend_raw_preview_due(idx, pst): @@ -2236,2 +2276,4 @@ img = _decode_image(encoded) + if img is not None and inspection_ptz: + ticket = handoff.accepted(idx, img, source_seq, epoch) if img is None: @@ -2242,3 +2284,3 @@ - if skip_infer_for_non_ptz: + if skip_infer_for_non_ptz or (handoff is not None and not inspection_ptz): # Still keep the UI alive for non-PTZ/panorama streams. Detection @@ -2250,2 +2292,4 @@ + if inspection_ptz and ticket is None: + continue now = time.time() @@ -2284,3 +2328,3 @@ if pst["proc_busy"]: - _feed_visual_tracker_frame(idx, img, pst, source_seq) + _feed_visual_tracker_frame(idx, img, pst, source_seq, inspection_ticket=ticket) skipped_busy_total += 1 @@ -2294,3 +2338,3 @@ if (now_pc - last_submit) < infer_min_dt: - _feed_visual_tracker_frame(idx, img, pst, source_seq) + _feed_visual_tracker_frame(idx, img, pst, source_seq, inspection_ticket=ticket) if publish_raw_allowed: @@ -2306,3 +2350,3 @@ - async def process_frame(cam_idx: int, frame, frame_seq: int) -> None: + async def process_frame(cam_idx: int, frame, frame_seq: int, frame_ticket=None) -> None: try: @@ -2313,2 +2357,3 @@ source_seq=frame_seq, + inspection_ticket=frame_ticket, ) @@ -2318,2 +2363,3 @@ pst_done = state.ptz_states[cam_idx] + next_ticket = pst_done.pop("pending_inspection_ticket", None) next_frame = pst_done.pop("pending_frame", None) @@ -2331,3 +2377,3 @@ pst_done["proc_busy"] = True - asyncio.create_task(process_frame(cam_idx, next_frame, next_seq)) + asyncio.create_task(process_frame(cam_idx, next_frame, next_seq, next_ticket)) else: @@ -2341,3 +2387,3 @@ - asyncio.create_task(process_frame(idx, img, source_seq)) + asyncio.create_task(process_frame(idx, img, source_seq, ticket)) @@ -2345,3 +2391,7 @@ logger.error("[CPP WS] error: %s", exc) - await asyncio.sleep(0.5) + finally: + if handoff is not None: + for camera in source_cameras: + handoff.end_source(camera, stream_epoch) + await asyncio.sleep(0.5) @@ -2475,2 +2525,8 @@ with ExitStack() as locks: + if inspection_handoff.enabled(): + handoff = inspection_handoff.require_configured() + for cid in sorted(ids): + locks.enter_context(visual_control.control_lock(cid)) + for cid in ids: + handoff.end_source(cid, reason='camera_address_changed') for cid in ids: @@ -2565,3 +2621,3 @@ def _update_visual_feed(cam_id: int, frame_bgr: np.ndarray, source_seq: int, - captured_at: Optional[float] = None) -> None: + captured_at: Optional[float] = None, *, inspection_ticket=None) -> None: with visual_control.control_lock(cam_id): @@ -2572,2 +2628,10 @@ token = visual_control.owner(st) + handoff = inspection_handoff.require_configured() if inspection_handoff.enabled() else None + if handoff is not None and ( + not handoff.current(inspection_ticket, age=.25) + or inspection_ticket.pixels is not frame_bgr + or inspection_ticket.stamp.camera_id != cam_id + or inspection_ticket.stamp.source_seq != source_seq + ): + return result = visual_control_probe.call_tracker_update(visual_tracker_manager.update, cam_id, frame_bgr, source_seq=source_seq) @@ -2582,3 +2646,8 @@ return - sample = visual_control.VisualSample(token, int(source_seq), captured_at, result) + if handoff is not None: + sample = handoff.visual_sample(inspection_ticket, result) + if sample is None: + return + else: + sample = visual_control.VisualSample(token, int(source_seq), captured_at, result) postprocess_control(cam_id, frame_bgr, None, True, source_seq, @@ -2592,2 +2661,4 @@ """Pair YOLO with an exact visual observation, independently of GPU latency.""" + if inspection_handoff.enabled(): + return inspection_handoff.require_configured().prepare(item) cam_id, frame, is_ptz, source_seq, future, queued_at = item @@ -2609,2 +2680,4 @@ def _postprocess_inference_item(cam_id, frame, res, is_ptz, source_seq, future): + if inspection_handoff.enabled(): + return inspection_handoff.require_configured().process_item(cam_id, frame, res, is_ptz, source_seq, future) sample = getattr(future, 'visual_sample', None) --- a/ptz_tracker_modular/visual_control_probe.py +++ b/ptz_tracker_modular/visual_control_probe.py @@ -87,4 +87,4 @@ @wraps(function) - def wrapped(camera, frame, st, source_seq): - result = function(camera, frame, st, source_seq) + def wrapped(camera, frame, st, source_seq, **kwargs): + result = function(camera, frame, st, source_seq, **kwargs) try: @@ -103,3 +103,3 @@ @wraps(function) - def wrapped(camera, frame, source_seq, captured_at=None): + def wrapped(camera, frame, source_seq, captured_at=None, **kwargs): start = _perf() @@ -117,3 +117,3 @@ try: - return function(camera, frame, source_seq, captured_at=captured_at) + return function(camera, frame, source_seq, captured_at=captured_at, **kwargs) finally: