@ -1,5 +1,14 @@
--- 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 (
@@ -690,6 +690,8 @@
def _focus_quality_score(
frame_bgr: np.ndarray,
@ -18,7 +27,7 @@
cx = (x1 + x2) * 0.5
cy = (y1 + y2) * 0.5
roi_w = max(16.0, (x2 - x1) * scale)
@@ -741,6 +743,1 41 @@
@@ -741,6 +743,1 5 4 @@
return score if math.isfinite(score) else None
@ -44,6 +53,19 @@
+_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)
+
+
+def _ptz17_target_focus_score(
+ frame_bgr: np.ndarray, bbox: tuple[int, int, int, int] | list[int],
+) -> float | None:
@ -160,7 +182,7 @@
def _clear_focus_low_evidence(st: dict) -> None:
st["focus_quality_low_frames"] = 0
st["focus_quality_low_since"] = None
@@ -761,6 + 898 ,7 @@
@@ -761,6 + 911 ,7 @@
center_err: float,
hfov_deg: float,
validation_age: float,
@ -168,7 +190,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 +9 71 ,31 @@
@@ -833,14 +9 84 ,31 @@
_clear_focus_low_evidence(st)
st["focus_last_hfov_deg"] = float(hfov_deg)
@ -203,7 +225,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 +10 25 ,10 @@
@@ -870,7 +10 38 ,10 @@
if ema >= threshold:
# Keep the independent raw-drop evidence while the deliberately slow
# EMA still describes the previously sharp scene.
@ -215,7 +237,7 @@
st["focus_quality_low_frames"] = 0
st["focus_quality_low_since"] = None
else:
@@ -916,12 +10 74 ,19 @@
@@ -916,12 +10 8 7,19 @@
if not confirmed_low:
return False
@ -236,7 +258,7 @@
if str(st.get("focus_state", "")) not in {
"unsupported",
"unsupported_backoff",
@@ -931,6 +1 096 ,8 @@
@@ -931,6 +1 1 09,8 @@
st["focus_state"] = "blur_wait"
return False
@ -245,7 +267,7 @@
st["focus_state"] = "requested"
st["focus_last_request_at"] = float(now)
st["focus_settle_until"] = float(now) + max(
@@ -938,12 +11 05 ,11 @@
@@ -938,12 +11 18 ,11 @@
float(getattr(config, "AUTOFOCUS_SETTLE_HOLD_SEC", 0.80)),
)
_clear_focus_low_evidence(st)
@ -263,7 +285,7 @@
return True
@@ -9537,6 +97 03,48 @@
@@ -9537,6 +97 16,50 @@
max(0.0, float(getattr(config, min_key, 0.10))),
)
zoom_cmd = min(max_effective, max(float(zoom_cmd), min_effective))
@ -297,6 +319,8 @@
+ 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
@ -312,7 +336,7 @@
if probe_start_candidate and identify_start_candidate:
pulse_kind = "probe_identify"
elif probe_start_candidate:
@@ -9645, 6 +9853,27 @@
@@ -9645, 8 +9868,30 @@
zoom_cmd = 0.0
st["zoom_useful_pulse_reason"] = "wait_response_framing"
zoom_zero_reason = "response_framing"
@ -339,8 +363,11 @@
+ 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(
@@ -9667,6 +9896,9 @@
st,
# Keep new-request expiry tied to the frame that authorized
@@ -9667,9 +9912,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"]
@ -350,7 +377,13 @@
if gate_stop:
zoom_zero_reason = gate_stop[-1]
decision_generation = st.get("ptz_generation")
@@ -9693,23 +9925,24 @@
+ 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 +9944,24 @@
elif zoom_cmd < 0.0 and float(size_est) <= max(0.05, zoomed_size_thr * 0.75):
st["was_zoomed_in"] = False
@ -392,7 +425,54 @@
st["fallback_done"] = False
@@ -10222,6 +10455,8 @@
@@ -10191,7 +10443,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 +10462,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 +10503,8 @@
if getattr(config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", "logs/ptz_zoom"):
blocked = {}
if zoom_cmd <= 0.0 and raw_zoom_needed: