You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
783 lines
39 KiB
Diff
783 lines
39 KiB
Diff
--- a/postprocess.py
|
|
+++ b/postprocess.py
|
|
@@ -12,7 +12,7 @@
|
|
|
|
from . import config, state, visual_control
|
|
from .zoom_controller import (
|
|
- ZoomFrame, controller_for_track, expire_calibration, flush_zoom_blocks, record_zoom_blocks, telemetry_tick,
|
|
+ ZoomFrame, _episode_telemetry, controller_for_track, expire_calibration, flush_zoom_blocks, record_zoom_blocks, telemetry_tick,
|
|
)
|
|
from . import motion_detector
|
|
from .utils import (
|
|
@@ -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],
|
|
+ *,
|
|
+ roi_scale: float | None = None,
|
|
) -> float | None:
|
|
"""Return Laplacian focus energy from a bounded ROI around the target.
|
|
|
|
@@ -708,7 +711,7 @@
|
|
if not np.isfinite([x1, y1, x2, y2]).all() or x2 <= x1 or y2 <= y1:
|
|
return None
|
|
|
|
- scale = max(1.0, float(getattr(config, "AUTOFOCUS_ROI_SCALE", 2.5)))
|
|
+ scale = max(1.0, float(getattr(config, "AUTOFOCUS_ROI_SCALE", 2.5) if roi_scale is None else roi_scale))
|
|
cx = (x1 + x2) * 0.5
|
|
cy = (y1 + y2) * 0.5
|
|
roi_w = max(16.0, (x2 - x1) * scale)
|
|
@@ -741,6 +744,221 @@
|
|
return score if math.isfinite(score) else None
|
|
|
|
|
|
+@dataclass(frozen=True, slots=True)
|
|
+class _PTZ17ZoomTuning:
|
|
+ focus_max_hfov_deg: float = 18.0
|
|
+ focus_min_side_px: int = 20
|
|
+ focus_core_only_threshold_scale: float = 8.0
|
|
+ focus_unknown_retry_s: float = 0.60
|
|
+ focus_unknown_max_gap_s: float = 0.25
|
|
+ focus_recovery_samples: int = 2
|
|
+ frame_max_age_s: float = 0.25
|
|
+ detector_max_age_s: float = 0.65
|
|
+ first_pulse_s: float = 0.34
|
|
+ hard_stop_s: float = 0.40
|
|
+ startup_native: int = 10
|
|
+ projected_margin_min: float = 0.16
|
|
+ edge_margin_min: float = 0.30
|
|
+ center_max_norm: float = 0.12
|
|
+ image_rate_max_norm_s: float = 0.04
|
|
+
|
|
+
|
|
+_PTZ17_ZOOM = _PTZ17ZoomTuning()
|
|
+
|
|
+
|
|
+def _record_ptz17_decision(st: dict, at: float, **fields: Any) -> None:
|
|
+ """Publish decision metadata to the existing bounded asynchronous queue."""
|
|
+ generation, started_at = visual_control.owner(st)[1:3]
|
|
+ if started_at <= 0.0 or not math.isfinite(started_at):
|
|
+ return
|
|
+ values = {
|
|
+ key: None if isinstance(value, float) and not math.isfinite(value) else value
|
|
+ for key, value in fields.items()
|
|
+ }
|
|
+ _episode_telemetry(config, _PTZ17_CAM_IDX, (generation, started_at),
|
|
+ "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:
|
|
+ """Assess target detail when the wider crop has little contrast."""
|
|
+ core = _focus_quality_score(frame_bgr, bbox, roi_scale=1.0)
|
|
+ if core is None:
|
|
+ return None
|
|
+ expanded = _focus_quality_score(frame_bgr, bbox)
|
|
+ if expanded is not None:
|
|
+ return min(core, expanded)
|
|
+ threshold = max(0.0, float(getattr(config, "AUTOFOCUS_BLUR_THRESHOLD", 35.0)))
|
|
+ if core >= threshold * _PTZ17_ZOOM.focus_core_only_threshold_scale:
|
|
+ return core
|
|
+ return None
|
|
+
|
|
+
|
|
+def _ptz17_focus_hold(
|
|
+ st: dict, *, hfov_deg: float, side_px: int, frame_is_current: bool, captured_at: float,
|
|
+) -> str | None:
|
|
+ """Keep the lens at its current FOV while the settled target is blurry."""
|
|
+ owner = visual_control.owner(st)
|
|
+ if owner[2] <= 0.0 or not 0.0 < hfov_deg <= _PTZ17_ZOOM.focus_max_hfov_deg:
|
|
+ st.pop("ptz17_focus_hold", None)
|
|
+ st.pop("ptz17_focus_recovery", None)
|
|
+ return None
|
|
+ previous = st.get("ptz17_focus_hold")
|
|
+ reason = previous[1] if previous is not None and previous[0] == owner else None
|
|
+ if not frame_is_current or not math.isfinite(captured_at):
|
|
+ return reason
|
|
+ focus_state = st.get("focus_state")
|
|
+ if focus_state == "disabled":
|
|
+ reason = None
|
|
+ elif side_px >= _PTZ17_ZOOM.focus_min_side_px:
|
|
+ if focus_state == "sharp" and reason is not None:
|
|
+ recovery = st.get("ptz17_focus_recovery")
|
|
+ votes = 1
|
|
+ if recovery is not None and recovery[0] == owner:
|
|
+ gap_s = captured_at - recovery[1]
|
|
+ if gap_s <= 0.0:
|
|
+ return reason
|
|
+ if gap_s <= _PTZ17_ZOOM.focus_unknown_max_gap_s:
|
|
+ votes += recovery[2]
|
|
+ st["ptz17_focus_recovery"] = (owner, captured_at, votes)
|
|
+ if votes >= _PTZ17_ZOOM.focus_recovery_samples:
|
|
+ reason = None
|
|
+ elif focus_state in {
|
|
+ "blur_suspected", "blur_fast_suspected", "blur_cooldown", "blur_wait",
|
|
+ "unassessable", "requested", "settling",
|
|
+ }:
|
|
+ reason = str(focus_state)
|
|
+ if focus_state != "sharp" or side_px < _PTZ17_ZOOM.focus_min_side_px or reason is None:
|
|
+ st.pop("ptz17_focus_recovery", None)
|
|
+ if reason is not None:
|
|
+ st["ptz17_focus_hold"] = (owner, reason)
|
|
+ else:
|
|
+ st.pop("ptz17_focus_hold", None)
|
|
+ return reason
|
|
+
|
|
+
|
|
+def _ptz17_unassessable_retry_ready(st: dict, captured_at: float) -> bool:
|
|
+ """Authorize one recovery attempt per track owner after sustained low contrast."""
|
|
+ owner = visual_control.owner(st)
|
|
+ evidence = st.get("ptz17_focus_unknown")
|
|
+ if owner[2] <= 0.0 or not math.isfinite(captured_at):
|
|
+ return False
|
|
+ if evidence is None or evidence[0] != owner:
|
|
+ st["ptz17_focus_unknown"] = (owner, captured_at, captured_at)
|
|
+ return False
|
|
+ if st.get("ptz17_focus_unknown_attempt") == owner:
|
|
+ return False
|
|
+ gap_s = captured_at - evidence[2]
|
|
+ if gap_s <= 0.0:
|
|
+ return False
|
|
+ since = captured_at if gap_s > _PTZ17_ZOOM.focus_unknown_max_gap_s else evidence[1]
|
|
+ st["ptz17_focus_unknown"] = (owner, since, captured_at)
|
|
+ return captured_at - since >= _PTZ17_ZOOM.focus_unknown_retry_s
|
|
+
|
|
+
|
|
+def _ptz17_first_useful_timing(
|
|
+ st: dict, timing: tuple[float, float], *, native: int, normal_onset: bool,
|
|
+ frame_age_s: float, detector_age_s: float, hfov_deg: float, wide_hfov_deg: float,
|
|
+ approach_margin: float | None, edge_margin: float, center_error: float, image_rate: float,
|
|
+) -> tuple[float, float]:
|
|
+ """Select the longer measured-response candidate within existing deadlines."""
|
|
+ tuning = _PTZ17_ZOOM
|
|
+ if (
|
|
+ not normal_onset or timing[0] <= 0.0 or native != tuning.startup_native
|
|
+ or st.get("zoom_no_effect_blocked")
|
|
+ or st.get("zoom_stop_obligation_active") or st.get("lens_move_stop_required")
|
|
+ or not 0.0 <= frame_age_s <= tuning.frame_max_age_s
|
|
+ or not 0.0 <= detector_age_s <= tuning.detector_max_age_s
|
|
+ or not 0.0 < hfov_deg <= wide_hfov_deg < 180.0
|
|
+ or hfov_deg < wide_hfov_deg * max(.85, float(getattr(config, "ZOOM_RESPONSE_WIDE_RATIO", .85)))
|
|
+ or approach_margin is None or not tuning.projected_margin_min <= approach_margin <= .5
|
|
+ or not tuning.edge_margin_min <= edge_margin <= .5
|
|
+ or not 0.0 <= center_error <= tuning.center_max_norm
|
|
+ or not 0.0 <= image_rate <= tuning.image_rate_max_norm_s
|
|
+ ):
|
|
+ return timing
|
|
+ if st.get("zoom_step_pending"):
|
|
+ # Revalidate only the original request created by the PTZ17 profile.
|
|
+ # The existing pulse gate owns the pending timer and expiry.
|
|
+ marker = st.get("ptz17_first_timing_owner")
|
|
+ if marker == (visual_control.owner(st), st.get("zoom_step_pending_id")):
|
|
+ return (float(st["zoom_step_pending_pulse_sec"]), float(st["zoom_step_pending_hard_sec"]))
|
|
+ return timing
|
|
+ if int(st.get("zoom_step_request_id", 0) or 0) != 0:
|
|
+ return timing
|
|
+ pulse = min(tuning.first_pulse_s, float(getattr(config, "ZOOM_RESPONSE_MAX_PULSE_SEC", .34)))
|
|
+ hard = min(tuning.hard_stop_s, float(getattr(config, "ZOOM_RESPONSE_MAX_HARD_STOP_SEC", .40)))
|
|
+ return (pulse, hard) if timing[0] < pulse <= hard and timing[1] <= hard else timing
|
|
+
|
|
+
|
|
def _clear_focus_low_evidence(st: dict) -> None:
|
|
st["focus_quality_low_frames"] = 0
|
|
st["focus_quality_low_since"] = None
|
|
@@ -761,6 +979,7 @@
|
|
center_err: float,
|
|
hfov_deg: float,
|
|
validation_age: float,
|
|
+ allow_unassessable_retry: bool = False,
|
|
) -> 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 +1052,31 @@
|
|
_clear_focus_low_evidence(st)
|
|
st["focus_last_hfov_deg"] = float(hfov_deg)
|
|
|
|
- score = _focus_quality_score(frame_bgr, bbox)
|
|
+ score = (
|
|
+ _ptz17_target_focus_score(frame_bgr, bbox)
|
|
+ if cam_idx == _PTZ17_CAM_IDX and float(hfov_deg) <= _PTZ17_ZOOM.focus_max_hfov_deg
|
|
+ else _focus_quality_score(frame_bgr, bbox)
|
|
+ )
|
|
st["focus_quality_last_eval_at"] = float(now)
|
|
st["focus_quality_score"] = score
|
|
+ unknown_retry = False
|
|
if score is None:
|
|
st["focus_state"] = "unassessable"
|
|
_clear_focus_low_evidence(st)
|
|
- return False
|
|
-
|
|
+ unknown_retry = (
|
|
+ allow_unassessable_retry and cam_idx == _PTZ17_CAM_IDX
|
|
+ and 0.0 < hfov_deg <= _PTZ17_ZOOM.focus_max_hfov_deg
|
|
+ and 0.0 <= validation_age <= _PTZ17_ZOOM.detector_max_age_s
|
|
+ and _ptz17_unassessable_retry_ready(st, now)
|
|
+ )
|
|
+ if not unknown_retry:
|
|
+ return False
|
|
+ else:
|
|
+ st.pop("ptz17_focus_unknown", None)
|
|
+
|
|
+ if unknown_retry:
|
|
+ return _queue_focus_recovery(cam_idx, st, now, reason="unassessable")
|
|
+ assert score is not None
|
|
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 +1106,10 @@
|
|
if ema >= threshold:
|
|
# Keep the independent raw-drop evidence while the deliberately slow
|
|
# EMA still describes the previously sharp scene.
|
|
- st["focus_state"] = "blur_fast_suspected" if raw_drop else "sharp"
|
|
+ ptz17_instant_low = (
|
|
+ allow_unassessable_retry and cam_idx == _PTZ17_CAM_IDX and score < threshold
|
|
+ )
|
|
+ st["focus_state"] = "blur_fast_suspected" if raw_drop or ptz17_instant_low else "sharp"
|
|
st["focus_quality_low_frames"] = 0
|
|
st["focus_quality_low_since"] = None
|
|
else:
|
|
@@ -916,12 +1155,19 @@
|
|
if not confirmed_low:
|
|
return False
|
|
|
|
+ return _queue_focus_recovery(cam_idx, st, now, reason="quality", score=score, ema=ema)
|
|
+
|
|
+
|
|
+def _queue_focus_recovery(
|
|
+ cam_idx: int, st: dict, now: float, *, reason: str,
|
|
+ score: float | None = None, ema: float | None = None,
|
|
+) -> bool:
|
|
request_cd = max(0.5, float(getattr(config, "AUTOFOCUS_REQUEST_COOLDOWN_SEC", 15.0)))
|
|
last_request = float(st.get("focus_last_request_at", 0.0) or 0.0)
|
|
if last_request > 0.0 and (float(now) - last_request) < request_cd:
|
|
st["focus_state"] = "blur_cooldown"
|
|
return False
|
|
- if not bool(call_autofocus(cam_idx, reason="quality")):
|
|
+ if not bool(call_autofocus(cam_idx, reason=reason)):
|
|
if str(st.get("focus_state", "")) not in {
|
|
"unsupported",
|
|
"unsupported_backoff",
|
|
@@ -931,6 +1177,8 @@
|
|
st["focus_state"] = "blur_wait"
|
|
return False
|
|
|
|
+ if reason == "unassessable":
|
|
+ st["ptz17_focus_unknown_attempt"] = visual_control.owner(st)
|
|
st["focus_state"] = "requested"
|
|
st["focus_last_request_at"] = float(now)
|
|
st["focus_settle_until"] = float(now) + max(
|
|
@@ -938,12 +1186,11 @@
|
|
float(getattr(config, "AUTOFOCUS_SETTLE_HOLD_SEC", 0.80)),
|
|
)
|
|
_clear_focus_low_evidence(st)
|
|
- logger.warning(
|
|
- "[FOCUS/QUALITY] cam=%s autofocus requested score=%.1f ema=%.1f",
|
|
- cam_idx,
|
|
- float(score),
|
|
- float(ema),
|
|
- )
|
|
+ if score is not None and ema is not None:
|
|
+ logger.warning("[FOCUS/QUALITY] cam=%s autofocus requested score=%.1f ema=%.1f",
|
|
+ cam_idx, score, ema)
|
|
+ else:
|
|
+ logger.warning("[FOCUS/QUALITY] cam=%s autofocus requested reason=%s", cam_idx, reason)
|
|
return True
|
|
|
|
|
|
@@ -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))
|
|
+ ptz17_focus_reason = None
|
|
+ ptz17_focus_checked = False
|
|
+ if (cam_idx == _PTZ17_CAM_IDX and float(hfov_deg) <= _PTZ17_ZOOM.focus_max_hfov_deg
|
|
+ and not pulse_active and not st.get("zoom_step_pending")):
|
|
+ # Check the settled image before authorizing a fresh pulse. The
|
|
+ # existing autofocus worker and controller keep their own limits.
|
|
+ focus_wall_now = time.time()
|
|
+ lens_settled = (
|
|
+ not st.get("lens_move_put_inflight") and not st.get("zoom_stop_obligation_active")
|
|
+ and not st.get("lens_move_stop_required")
|
|
+ and (st.get("last_ptz_physical_cmd") or (0.0, 0.0, 0.0))[2] == 0.0
|
|
+ )
|
|
+ if (lens_settled and zoom_cmd >= 0.0 and visual_only
|
|
+ and visual_control.current(cam_idx, visual_sample, focus_wall_now,
|
|
+ max_age=_PTZ17_ZOOM.frame_max_age_s)):
|
|
+ _maybe_request_autofocus(
|
|
+ cam_idx, st, frame_bgr, (x1, y1, x2, y2), now=float(now),
|
|
+ pan_cmd=float(pan_speed) + float(pan_rate_ff),
|
|
+ tilt_cmd=float(tilt_speed) + float(tilt_rate_ff),
|
|
+ zoom_cmd=0.0, center_err=float(center_err), hfov_deg=float(hfov_deg),
|
|
+ validation_age=float(visual_control_validation_age),
|
|
+ allow_unassessable_retry=True,
|
|
+ )
|
|
+ ptz17_focus_checked = True
|
|
+ # A small but measurable crop can veto another zoom step.
|
|
+ # The higher size threshold for requesting autofocus still applies.
|
|
+ if (st.get("focus_state") == "target_too_small"
|
|
+ and min(x2 - x1, y2 - y1) >= _PTZ17_ZOOM.focus_min_side_px
|
|
+ and 0.0 <= visual_control_validation_age <= _PTZ17_ZOOM.detector_max_age_s):
|
|
+ target_score = _ptz17_target_focus_score(frame_bgr, (x1, y1, x2, y2))
|
|
+ st["ptz17_small_focus_at"] = now
|
|
+ st["focus_quality_score"] = target_score
|
|
+ threshold = float(getattr(config, "AUTOFOCUS_BLUR_THRESHOLD", 35.0))
|
|
+ st["focus_state"] = (
|
|
+ "unassessable" if target_score is None else
|
|
+ "sharp" if target_score >= threshold else "blur_suspected"
|
|
+ )
|
|
+ ptz17_focus_reason = _ptz17_focus_hold(
|
|
+ st, hfov_deg=float(hfov_deg), side_px=min(x2 - x1, y2 - y1),
|
|
+ frame_is_current=ptz17_focus_checked, captured_at=float(now),
|
|
+ )
|
|
+ if ptz17_focus_reason is not None and zoom_cmd > 0.0:
|
|
+ zoom_cmd = 0.0
|
|
+ zoom_zero_reason = "focus_hold"
|
|
if probe_start_candidate and identify_start_candidate:
|
|
pulse_kind = "probe_identify"
|
|
elif probe_start_candidate:
|
|
@@ -9645,8 +10032,30 @@
|
|
zoom_cmd = 0.0
|
|
st["zoom_useful_pulse_reason"] = "wait_response_framing"
|
|
zoom_zero_reason = "response_framing"
|
|
+ ptz17_timing_changed = False
|
|
+ if cam_idx == _PTZ17_CAM_IDX:
|
|
+ original_response_timing = response_timing
|
|
+ response_timing = _ptz17_first_useful_timing(
|
|
+ st, response_timing, native=timing_native,
|
|
+ normal_onset=(zoom_cmd > 0.0 and useful_reason == "useful" and useful_optics_fresh
|
|
+ and pulse_family == "normal"
|
|
+ and fast_approach and normal_zoom_quality_ok
|
|
+ and current_track_source == "visual" and not predictive_start
|
|
+ and bool(visual_only and visual_control.current(
|
|
+ cam_idx, visual_sample, zoom_gate_now,
|
|
+ max_age=_PTZ17_ZOOM.frame_max_age_s,
|
|
+ ))),
|
|
+ frame_age_s=zoom_gate_now - now,
|
|
+ detector_age_s=zoom_gate_now - last_visual_zoom_validation,
|
|
+ hfov_deg=float(hfov_deg), wide_hfov_deg=float(shared_wide_hfov),
|
|
+ approach_margin=approach_margin, edge_margin=float(edge_risk_margin),
|
|
+ center_error=max(abs(float(measured_dx)), abs(float(measured_dy))),
|
|
+ image_rate=max(float(vel), float(fast_drift)),
|
|
+ )
|
|
+ ptz17_timing_changed = response_timing != original_response_timing
|
|
gate_stop = [] if decision_request else None
|
|
continuation_trace = [] if decision_request else None
|
|
+ ptz17_zoom_before_gate = float(zoom_cmd) if cam_idx == _PTZ17_CAM_IDX else None
|
|
zoom_cmd = _gate_zoom_in_pulse(
|
|
st,
|
|
# Keep new-request expiry tied to the frame that authorized
|
|
@@ -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"]
|
|
+ if (ptz17_timing_changed and zoom_cmd > 0.0
|
|
+ and st["zoom_step_request_id"] > request_id_before_gate):
|
|
+ st["ptz17_first_timing_owner"] = (visual_control.owner(st), st["zoom_step_pending_id"])
|
|
if gate_stop:
|
|
zoom_zero_reason = gate_stop[-1]
|
|
decision_generation = st.get("ptz_generation")
|
|
+ ptz17_request_after_gate = (
|
|
+ int(st.get("zoom_step_pending_id", 0) or 0) if cam_idx == _PTZ17_CAM_IDX else 0
|
|
+ )
|
|
if (
|
|
probe_start_candidate
|
|
and zoom_cmd > 0.0
|
|
@@ -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
|
|
|
|
- _maybe_request_autofocus(
|
|
- cam_idx,
|
|
- st,
|
|
- frame_bgr,
|
|
- (x1, y1, x2, y2),
|
|
- now=float(now),
|
|
- pan_cmd=float(pan_speed) + float(pan_rate_ff),
|
|
- tilt_cmd=float(tilt_speed) + float(tilt_rate_ff),
|
|
- zoom_cmd=float(zoom_cmd),
|
|
- center_err=float(center_err),
|
|
- hfov_deg=float(hfov_deg),
|
|
- validation_age=(
|
|
- float(visual_control_validation_age)
|
|
- if current_track_source == "visual"
|
|
- else 0.0
|
|
- ),
|
|
- )
|
|
+ if not ptz17_focus_checked:
|
|
+ _maybe_request_autofocus(
|
|
+ cam_idx,
|
|
+ st,
|
|
+ frame_bgr,
|
|
+ (x1, y1, x2, y2),
|
|
+ now=float(now),
|
|
+ pan_cmd=float(pan_speed) + float(pan_rate_ff),
|
|
+ tilt_cmd=float(tilt_speed) + float(tilt_rate_ff),
|
|
+ zoom_cmd=float(zoom_cmd),
|
|
+ center_err=float(center_err),
|
|
+ hfov_deg=float(hfov_deg),
|
|
+ validation_age=(
|
|
+ float(visual_control_validation_age)
|
|
+ if current_track_source == "visual"
|
|
+ else 0.0
|
|
+ ),
|
|
+ )
|
|
|
|
st["fallback_done"] = False
|
|
|
|
@@ -10191,7 +10607,7 @@
|
|
stop_ptz(cam_idx, source='stop', generation=int(st.get('ptz_generation', 0) or 0))
|
|
return
|
|
zoom_decision = None
|
|
- if decision_request and zoom_cmd <= 0.0:
|
|
+ if (decision_request or cam_idx == _PTZ17_CAM_IDX) and zoom_cmd <= 0.0:
|
|
blockers = tuple(name for name, blocked in (
|
|
("quality", not zoom_quality_ok),
|
|
("framing", not zoom_safe),
|
|
@@ -10210,7 +10626,36 @@
|
|
("stale", visual_control_stale),
|
|
("brake", final_guard_active),
|
|
) if blocked)
|
|
- zoom_decision = (source_seq, decision_request, zoom_zero_reason, "+".join(blockers) or "-")
|
|
+ if decision_request:
|
|
+ zoom_decision = (source_seq, decision_request, zoom_zero_reason, "+".join(blockers) or "-")
|
|
+ if (cam_idx == _PTZ17_CAM_IDX and st.get("mode") == "TRACK"
|
|
+ and st.get("tracking_active") and source_seq is not None
|
|
+ and int(source_seq) > 0):
|
|
+ _record_ptz17_decision(
|
|
+ st, time.time(), frame_seq=int(source_seq), frame_utc=float(now),
|
|
+ frame_age_at_gate_s=zoom_gate_now - now, source=current_track_source,
|
|
+ bbox_px=[int(x1), int(y1), int(x2), int(y2)],
|
|
+ detector_age_at_gate_s=zoom_gate_now - last_visual_zoom_validation,
|
|
+ detector_conf=float(st.get("visual_tracker_last_detector_conf", 0.0) or 0.0),
|
|
+ tracker_score=float(st.get("visual_tracker_score", 0.0) or 0.0),
|
|
+ hfov_deg=float(hfov_deg), optics_age_s=float(optics_age),
|
|
+ center_error_norm=max(abs(float(measured_dx)), abs(float(measured_dy))),
|
|
+ edge_margin_norm=float(edge_risk_margin), approach_margin_norm=approach_margin,
|
|
+ image_rate_norm_s=max(float(vel), float(fast_drift)),
|
|
+ zoom_requested=bool(raw_zoom_needed), zoom_before_gate=ptz17_zoom_before_gate,
|
|
+ zoom_command=float(zoom_cmd), proposed_native=int(timing_native),
|
|
+ reason=zoom_zero_reason if zoom_cmd <= 0.0 else "positive_command",
|
|
+ blockers=list(blockers) if zoom_cmd <= 0.0 else [],
|
|
+ pulse_kind=str(pulse_kind), nominal_s=float(response_timing[0]),
|
|
+ hard_limit_s=float(response_timing[1]), timing_profile_changed=ptz17_timing_changed,
|
|
+ request_before_gate=decision_request, request_after_gate=ptz17_request_after_gate,
|
|
+ request_created=bool(ptz17_request_after_gate > request_id_before_gate),
|
|
+ focus_state=st.get("focus_state"), focus_hold=ptz17_focus_reason,
|
|
+ focus_sample_current=bool(st.get("focus_quality_last_eval_at") == now or (
|
|
+ ptz17_focus_checked and st.get("ptz17_small_focus_at") == now
|
|
+ )),
|
|
+ focus_score=st.get("focus_quality_score"),
|
|
+ )
|
|
if predictive_decision is not None:
|
|
with _zoom_command_lock(cam_idx):
|
|
telemetry_tick(
|
|
@@ -10222,6 +10667,8 @@
|
|
if getattr(config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", "logs/ptz_zoom"):
|
|
blocked = {}
|
|
if zoom_cmd <= 0.0 and raw_zoom_needed:
|
|
+ if ptz17_focus_reason is not None:
|
|
+ blocked["focus_hold"] = {"state": ptz17_focus_reason}
|
|
if not stable_ok:
|
|
blocked["centering"] = {"error": center_err, "limit": lock_gate, "stable_frames": st.get("stable_frames")}
|
|
if not zoom_safe:
|