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.
180 lines
8.1 KiB
Diff
180 lines
8.1 KiB
Diff
--- a/ptz_tracker_modular/streaming.py
|
|
+++ b/ptz_tracker_modular/streaming.py
|
|
@@ -3,3 +3,3 @@
|
|
from . import visual_control
|
|
-_visual_feed_captured_at: dict[int, float] = {}
|
|
+from typing import NamedTuple
|
|
|
|
@@ -60,3 +60,11 @@
|
|
_cpp_stream_map_missing_logged: str = ""
|
|
-_visual_feed_pending: Dict[int, Tuple[np.ndarray, int, float]] = {}
|
|
+class _VisualReceipt(NamedTuple):
|
|
+ source: object
|
|
+ owner: tuple
|
|
+ utc: float
|
|
+ monotonic: float
|
|
+
|
|
+
|
|
+_visual_source = None
|
|
+_visual_feed_pending: Dict[int, Tuple[np.ndarray, int, float, _VisualReceipt]] = {}
|
|
_visual_feed_last_served: Dict[int, float] = {}
|
|
@@ -228,3 +236,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,4 +264,3 @@
|
|
break
|
|
- frame_bgr, source_seq, queued_at = _visual_feed_pending.pop(cam_id)
|
|
- captured_at = _visual_feed_captured_at.pop(cam_id, time.time())
|
|
+ frame_bgr, source_seq, queued_at, receipt = _visual_feed_pending.pop(cam_id)
|
|
st = state.ptz_states.get(cam_id)
|
|
@@ -273,3 +280,4 @@
|
|
source_seq=int(source_seq),
|
|
- captured_at=captured_at,
|
|
+ captured_at=receipt.utc,
|
|
+ receipt=receipt,
|
|
),
|
|
@@ -299,2 +307,3 @@
|
|
source_seq: int,
|
|
+ *, receipt: Optional[_VisualReceipt],
|
|
) -> None:
|
|
@@ -302,6 +311,5 @@
|
|
global _visual_feed_worker_task
|
|
- if not _visual_feed_is_active(cam_id, st):
|
|
+ if receipt is None or not _visual_feed_is_active(cam_id, st):
|
|
_visual_feed_pending.pop(cam_id, None)
|
|
_visual_feed_last_served.pop(cam_id, None)
|
|
- _visual_feed_captured_at.pop(cam_id, None)
|
|
return
|
|
@@ -315,4 +323,3 @@
|
|
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, receipt)
|
|
if _visual_feed_worker_task is None or _visual_feed_worker_task.done():
|
|
@@ -2092,2 +2099,3 @@
|
|
async def cpp_ws_loop() -> None:
|
|
+ global _visual_source
|
|
frames_total = 0
|
|
@@ -2119,2 +2127,3 @@
|
|
logger.info("[CPP WS] connected]")
|
|
+ source = _visual_source = object()
|
|
_cpp_stream_map_logged.clear()
|
|
@@ -2208,5 +2217,14 @@
|
|
img = None
|
|
+ receipt = None
|
|
+
|
|
+ def decode():
|
|
+ owner = visual_control.owner(pst) if idx in config.PTZ_CAM_IDS else None
|
|
+ pixels = _decode_image(encoded)
|
|
+ if pixels is None or owner is None:
|
|
+ return pixels, None
|
|
+ return pixels, _VisualReceipt(source, owner, time.time(), time.monotonic())
|
|
+
|
|
frontend_due = can_publish and _frontend_raw_preview_due(idx, pst, now_pc)
|
|
if frontend_due:
|
|
- img = _decode_image(encoded)
|
|
+ img, receipt = decode()
|
|
if img is not None:
|
|
@@ -2224,5 +2242,5 @@
|
|
if getattr(config, "DROP_DECODE_WHEN_BUSY", False) and pst["proc_busy"]:
|
|
- img_busy = img if img is not None else _decode_image(encoded)
|
|
+ img_busy, receipt = (img, receipt) if img is not None else decode()
|
|
if img_busy is not None:
|
|
- _feed_visual_tracker_frame(idx, img_busy, pst, source_seq)
|
|
+ _feed_visual_tracker_frame(idx, img_busy, pst, source_seq, receipt=receipt)
|
|
pst["pending_frame"] = img_busy
|
|
@@ -2235,3 +2253,3 @@
|
|
if img is None:
|
|
- img = _decode_image(encoded)
|
|
+ img, receipt = decode()
|
|
if img is None:
|
|
@@ -2284,3 +2302,3 @@
|
|
if pst["proc_busy"]:
|
|
- _feed_visual_tracker_frame(idx, img, pst, source_seq)
|
|
+ _feed_visual_tracker_frame(idx, img, pst, source_seq, receipt=receipt)
|
|
skipped_busy_total += 1
|
|
@@ -2294,3 +2312,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, receipt=receipt)
|
|
if publish_raw_allowed:
|
|
@@ -2345,3 +2363,5 @@
|
|
logger.error("[CPP WS] error: %s", exc)
|
|
- await asyncio.sleep(0.5)
|
|
+ finally:
|
|
+ _visual_source = None
|
|
+ await asyncio.sleep(0.5)
|
|
|
|
@@ -2563,12 +2583,23 @@
|
|
|
|
+def _visual_receipt_current(st, receipt, max_age):
|
|
+ return (receipt is not None and _visual_source is not None
|
|
+ and receipt.source is _visual_source
|
|
+ and receipt.owner == visual_control.owner(st)
|
|
+ and 0 <= time.monotonic() - receipt.monotonic <= max_age
|
|
+ and 0 <= time.time() - receipt.utc <= max_age)
|
|
+
|
|
+
|
|
@visual_control_probe.trace_feed_update
|
|
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, *, receipt: _VisualReceipt) -> None:
|
|
with visual_control.control_lock(cam_id):
|
|
st = state.ptz_states.get(cam_id)
|
|
- if st is None:
|
|
+ if (st is None or not _visual_receipt_current(st, receipt, .25)
|
|
+ or not _visual_feed_is_active(cam_id, st)):
|
|
return
|
|
- captured_at = time.time() if captured_at is None else float(captured_at)
|
|
- token = visual_control.owner(st)
|
|
+ captured_at = receipt.utc
|
|
+ token = receipt.owner
|
|
result = visual_control_probe.call_tracker_update(visual_tracker_manager.update, cam_id, frame_bgr, source_seq=source_seq)
|
|
+ if not _visual_receipt_current(st, receipt, .25):
|
|
+ return
|
|
_store_visual_feed_result(cam_id, source_seq, result)
|
|
@@ -2589,2 +2620,3 @@
|
|
visual_control.retain(cam_id, frame_bgr, sample)
|
|
+ st['visual_paired_receipt'] = (sample, receipt)
|
|
|
|
@@ -2605,3 +2637,7 @@
|
|
paired_frame, sample = saved
|
|
- future.visual_sample = sample
|
|
+ provenance = st.get('visual_paired_receipt')
|
|
+ if (provenance is None or provenance[0] is not sample
|
|
+ or not _visual_receipt_current(st, provenance[1], .25)):
|
|
+ return item
|
|
+ future.visual_sample, future.visual_receipt = provenance
|
|
return cam_id, paired_frame, is_ptz, sample.source_seq, future, queued_at
|
|
@@ -2612,3 +2648,7 @@
|
|
return postprocess_control(cam_id, frame, res, is_ptz, source_seq)
|
|
- return postprocess_control(cam_id, frame, res, is_ptz, source_seq,
|
|
- visual_sample=sample)
|
|
+ with visual_control.control_lock(cam_id):
|
|
+ st = state.ptz_states.get(cam_id)
|
|
+ if st is None or not _visual_receipt_current(st, future.visual_receipt, .65):
|
|
+ return
|
|
+ return postprocess_control(cam_id, frame, res, is_ptz, source_seq,
|
|
+ visual_sample=sample)
|
|
--- 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:
|