perf(tracker): decouple capture from inference, matching win_main.cpp

Latency bug: Tracker._inference_worker read the camera and ran pose
inference in the same synchronous loop, so a slow model frame backs up the
camera driver queue and lag grows without bound - the same bug win_main.cpp
fixed natively (dedicated capture thread, condition-variable wake, latest-
frame-wins, no fixed poll).

Added _capture_worker on its own thread, storing only the latest frame
behind a lock+condvar; _inference_worker now waits for and consumes the
newest capture, never a queued one. run()/stop() start and join both
threads. Updated the regression test to drive capture then inference
synchronously (both are plain methods, no real threading needed for a
deterministic single-frame test). Full suite: 98/98 passing.

Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
This commit is contained in:
kinect-botandsisyphus-dev-ai committed 2026-11-30 13:22:40 -05:00
1 parent a90b2af6e9
commit b513519b55
2 files changed
+44 -3

No files matched your search

+1
View File
@@ -134,6 +134,7 @@ def test_inference_worker_mirrors_frame_for_pose_and_publish():
publisher = FrameContractPublisher(depth_size=(160, 120), color_size=(160, 120))
try:
tracker = Tracker(OneFrameSource(), SpyProvider(), publisher)
tracker._capture_worker()
tracker._inference_worker()
assert len(processed_frames) == 1
+43 -3
View File
@@ -96,18 +96,52 @@ class Tracker:
self._last_update_count = -1
self._last_update_time = time.monotonic()
def _inference_worker(self) -> None:
t0 = time.monotonic()
# Dedicated capture thread (matching tracker-native's win_main.cpp):
# decouples camera reads from inference so a slow model doesn't back up
# the driver's frame queue and grow latency unboundedly. The inference
# loop always consumes the newest captured frame, never a queued one.
self._cap_lock = threading.Lock()
self._cap_cv = threading.Condition(self._cap_lock)
self._cap_frame = None
self._cap_seq = 0
self._cap_thread: Optional[threading.Thread] = None
def _capture_worker(self) -> None:
while not self._stop.is_set():
try:
frame = self.source.read()
except Exception:
logger.exception("frame source read failed; marking worker crashed")
logger.exception("capture read failed; marking worker crashed")
self._worker_crashed.set()
return
if frame is None:
self._source_done.set()
with self._cap_cv:
self._cap_cv.notify_all()
return
with self._cap_cv:
self._cap_frame = frame
self._cap_seq += 1
self._cap_cv.notify_all()
def _inference_worker(self) -> None:
t0 = time.monotonic()
last_seq = 0
while not self._stop.is_set():
with self._cap_cv:
got = self._cap_cv.wait_for(
lambda: self._cap_seq != last_seq or self._stop.is_set() or self._source_done.is_set(),
timeout=1.0,
)
if self._stop.is_set():
return
if not got:
continue
if self._cap_seq == last_seq:
# Woken only by source_done with no new frame ever captured.
return
frame = self._cap_frame
last_seq = self._cap_seq
import cv2
# Mirror horizontally before pose inference, matching
@@ -186,6 +220,8 @@ class Tracker:
# looks identical to a silently stuck one. Print a status line every
# status_interval seconds (None disables it, e.g. for tests) so a
# terminal run shows live confirmation instead of nothing.
self._cap_thread = threading.Thread(target=self._capture_worker, name="capture", daemon=True)
self._cap_thread.start()
self._worker = threading.Thread(target=self._inference_worker, name="inference", daemon=True)
self._worker.start()
@@ -229,8 +265,12 @@ class Tracker:
def stop(self) -> None:
self._stop.set()
with self._cap_cv:
self._cap_cv.notify_all()
if self._worker is not None:
self._worker.join(timeout=1.0)
if self._cap_thread is not None:
self._cap_thread.join(timeout=1.0)
def measure_fps(provider: PoseProvider, frame, iterations: int = 60) -> float: