Log PTZ17 zoom decisions and focus evidence per frame

feature/ptz17-focus-aware-zoom
Ярослав Карташов 1 week ago
parent 25c94c1e8d
commit f90c7da682

@ -29,6 +29,8 @@
## Воспроизводимый эталон первых 20 секунд ## Воспроизводимый эталон первых 20 секунд
Для полевого сравнения PR №14 пишет `decision_frame` только для PTZ17 через существующую ограниченную асинхронную очередь. Событие связывает номер и время кадра с состоянием проверки, полем зрения, оценкой резкости, запросом зума, длительностью и итоговой командой. Ненулевые команды и идентификаторы проверяются по журналу доставки и замерам оптики. Для первого резкого кадра учитывается только измерение с текущего кадра и размер рамки от 20 px. Эталон предыдущей версии располагает лишь редкими строками оценки резкости и выборочными JPEG.
`replay_ptz17_reference.py` читает зафиксированные команды, завершения запросов и выборки свежего HFOV из `reference_fixture.json`. Запуск `python replay_ptz17_reference.py` восстанавливает `reference_replay.json`. В журнале время подтверждения и первое наблюдаемое изменение HFOV могут отличаться из-за округления меток и порядка записи. Время измеримого сужения определяется отдельно от подтверждения конкретного запроса. `replay_ptz17_reference.py` читает зафиксированные команды, завершения запросов и выборки свежего HFOV из `reference_fixture.json`. Запуск `python replay_ptz17_reference.py` восстанавливает `reference_replay.json`. В журнале время подтверждения и первое наблюдаемое изменение HFOV могут отличаться из-за округления меток и порядка записи. Время измеримого сужения определяется отдельно от подтверждения конкретного запроса.
| TRACK, UTC+7 | Первый старт → первое подтверждение | Первый подтверждённый запрос | Свежие замеры HFOV за 20 с | | TRACK, UTC+7 | Первый старт → первое подтверждение | Первый подтверждённый запрос | Свежие замеры HFOV за 20 с |

@ -1,6 +1,6 @@
{ {
"base_sha256": "6cf484271ef3659028b0b4d43041addb05102f91d2758ab472d968a52d24e911", "base_sha256": "6cf484271ef3659028b0b4d43041addb05102f91d2758ab472d968a52d24e911",
"candidate_sha256": "26edb69b9d0bd0ae6ad4b9c18ff2f793bcb2a2cf035b38293a3e07d6fc833f8b", "candidate_sha256": "fc0deb0c54fbd94a916493ee0c3159b02407876b32deebbbc3d819cc58a17500",
"patch_sha256": "6a563f751871dcadc3c4daf730cc3cfb1ff6fd8edf1f379a2cee01f99302d4de", "patch_sha256": "e4a03e3dd4cd0f99995bc37acab29d063553cb32966fb13bdd93a46b8f7c5a9f",
"installed": false "installed": false
} }

@ -1,5 +1,14 @@
--- a/postprocess.py --- a/postprocess.py
+++ b/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 @@ @@ -690,6 +690,8 @@
def _focus_quality_score( def _focus_quality_score(
frame_bgr: np.ndarray, frame_bgr: np.ndarray,
@ -18,7 +27,7 @@
cx = (x1 + x2) * 0.5 cx = (x1 + x2) * 0.5
cy = (y1 + y2) * 0.5 cy = (y1 + y2) * 0.5
roi_w = max(16.0, (x2 - x1) * scale) roi_w = max(16.0, (x2 - x1) * scale)
@@ -741,6 +743,141 @@ @@ -741,6 +743,154 @@
return score if math.isfinite(score) else None return score if math.isfinite(score) else None
@ -44,6 +53,19 @@
+_PTZ17_ZOOM = _PTZ17ZoomTuning() +_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( +def _ptz17_target_focus_score(
+ frame_bgr: np.ndarray, bbox: tuple[int, int, int, int] | list[int], + frame_bgr: np.ndarray, bbox: tuple[int, int, int, int] | list[int],
+) -> float | None: +) -> float | None:
@ -160,7 +182,7 @@
def _clear_focus_low_evidence(st: dict) -> None: def _clear_focus_low_evidence(st: dict) -> None:
st["focus_quality_low_frames"] = 0 st["focus_quality_low_frames"] = 0
st["focus_quality_low_since"] = None st["focus_quality_low_since"] = None
@@ -761,6 +898,7 @@ @@ -761,6 +911,7 @@
center_err: float, center_err: float,
hfov_deg: float, hfov_deg: float,
validation_age: float, validation_age: float,
@ -168,7 +190,7 @@
) -> bool: ) -> bool:
"""Measure settled target detail and queue one bounded autofocus request.""" """Measure settled target detail and queue one bounded autofocus request."""
if not bool(getattr(config, "AUTOFOCUS_QUALITY_ENABLE", True)) or not bool( if not bool(getattr(config, "AUTOFOCUS_QUALITY_ENABLE", True)) or not bool(
@@ -833,14 +971,31 @@ @@ -833,14 +984,31 @@
_clear_focus_low_evidence(st) _clear_focus_low_evidence(st)
st["focus_last_hfov_deg"] = float(hfov_deg) 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) alpha = clipf(float(getattr(config, "AUTOFOCUS_SCORE_EMA_ALPHA", 0.35)), 0.01, 1.0)
previous_ema = st.get("focus_quality_ema") previous_ema = st.get("focus_quality_ema")
ema = float(score) if not isinstance(previous_ema, (int, float)) else ( ema = float(score) if not isinstance(previous_ema, (int, float)) else (
@@ -870,7 +1025,10 @@ @@ -870,7 +1038,10 @@
if ema >= threshold: if ema >= threshold:
# Keep the independent raw-drop evidence while the deliberately slow # Keep the independent raw-drop evidence while the deliberately slow
# EMA still describes the previously sharp scene. # EMA still describes the previously sharp scene.
@ -215,7 +237,7 @@
st["focus_quality_low_frames"] = 0 st["focus_quality_low_frames"] = 0
st["focus_quality_low_since"] = None st["focus_quality_low_since"] = None
else: else:
@@ -916,12 +1074,19 @@ @@ -916,12 +1087,19 @@
if not confirmed_low: if not confirmed_low:
return False return False
@ -236,7 +258,7 @@
if str(st.get("focus_state", "")) not in { if str(st.get("focus_state", "")) not in {
"unsupported", "unsupported",
"unsupported_backoff", "unsupported_backoff",
@@ -931,6 +1096,8 @@ @@ -931,6 +1109,8 @@
st["focus_state"] = "blur_wait" st["focus_state"] = "blur_wait"
return False return False
@ -245,7 +267,7 @@
st["focus_state"] = "requested" st["focus_state"] = "requested"
st["focus_last_request_at"] = float(now) st["focus_last_request_at"] = float(now)
st["focus_settle_until"] = float(now) + max( st["focus_settle_until"] = float(now) + max(
@@ -938,12 +1105,11 @@ @@ -938,12 +1118,11 @@
float(getattr(config, "AUTOFOCUS_SETTLE_HOLD_SEC", 0.80)), float(getattr(config, "AUTOFOCUS_SETTLE_HOLD_SEC", 0.80)),
) )
_clear_focus_low_evidence(st) _clear_focus_low_evidence(st)
@ -263,7 +285,7 @@
return True return True
@@ -9537,6 +9703,48 @@ @@ -9537,6 +9716,50 @@
max(0.0, float(getattr(config, min_key, 0.10))), max(0.0, float(getattr(config, min_key, 0.10))),
) )
zoom_cmd = min(max_effective, max(float(zoom_cmd), min_effective)) 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 min(x2 - x1, y2 - y1) >= _PTZ17_ZOOM.focus_min_side_px
+ and 0.0 <= visual_control_validation_age <= _PTZ17_ZOOM.detector_max_age_s): + 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)) + 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)) + threshold = float(getattr(config, "AUTOFOCUS_BLUR_THRESHOLD", 35.0))
+ st["focus_state"] = ( + st["focus_state"] = (
+ "unassessable" if target_score is None else + "unassessable" if target_score is None else
@ -312,7 +336,7 @@
if probe_start_candidate and identify_start_candidate: if probe_start_candidate and identify_start_candidate:
pulse_kind = "probe_identify" pulse_kind = "probe_identify"
elif probe_start_candidate: elif probe_start_candidate:
@@ -9645,6 +9853,27 @@ @@ -9645,8 +9868,30 @@
zoom_cmd = 0.0 zoom_cmd = 0.0
st["zoom_useful_pulse_reason"] = "wait_response_framing" st["zoom_useful_pulse_reason"] = "wait_response_framing"
zoom_zero_reason = "response_framing" zoom_zero_reason = "response_framing"
@ -339,8 +363,11 @@
+ ptz17_timing_changed = response_timing != original_response_timing + ptz17_timing_changed = response_timing != original_response_timing
gate_stop = [] if decision_request else None gate_stop = [] if decision_request else None
continuation_trace = [] 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( 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: 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"] st["predictive_zoom_request"] = st["zoom_step_pending_id"]
@ -350,7 +377,13 @@
if gate_stop: if gate_stop:
zoom_zero_reason = gate_stop[-1] zoom_zero_reason = gate_stop[-1]
decision_generation = st.get("ptz_generation") 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): elif zoom_cmd < 0.0 and float(size_est) <= max(0.05, zoomed_size_thr * 0.75):
st["was_zoomed_in"] = False st["was_zoomed_in"] = False
@ -392,7 +425,54 @@
st["fallback_done"] = False 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"): if getattr(config, "PREDICTIVE_ZOOM_TELEMETRY_DIR", "logs/ptz_zoom"):
blocked = {} blocked = {}
if zoom_cmd <= 0.0 and raw_zoom_needed: if zoom_cmd <= 0.0 and raw_zoom_needed:

Loading…
Cancel
Save