fix(tracker): mirror the capture for pose inference, matching tracker-native

The in-game skeleton/cursor tracking was left/right reversed. Root cause:
tracker-native's win_main.cpp physically mirrors the captured frame
(pixel-level horizontal flip) before pose inference, but publishes the raw
unmirrored frame as the color stream, and runs its mapper with
cal_mapping_cfg.mirror=false (verified by reading win_main.cpp and
core/mapping.cpp directly). The shim's unconditional L/R joint swap
(fallback/windows-nui/src/sensor.cpp) then corrects MediaPipe's resulting
left/right mislabeling.

The Python tracker never physically mirrored the capture - it only applied a
coordinate-level flip (1-u) in joint_mapping.py, with FrameContractPublisher
defaulting mirror=True. That left joint identity already correct on its own
(no image mirror -> no MediaPipe mislabeling -> no swap needed), so the
shim's unconditional swap then flipped it backwards: the exact "tracking is
backwards" symptom.

Fix: Tracker._inference_worker now mirrors the frame (cv2.flip) before
handing it to the pose provider, exactly like win_main.cpp, while keeping
the raw frame for self._latest_frame (the published color stream).
FrameContractPublisher's mirror default changes to False to match
cal_mapping_cfg.mirror=false, so the physical flip isn't double-applied.

New regression test calls the real Tracker._inference_worker directly (not
a reimplementation) with an asymmetric synthetic frame and asserts the pose
provider receives the mirrored version while the stored/published frame
stays unmirrored. Updated test_publish_and_read_back_fake_pose to compute
its independent expectation with the publisher's actual mirror setting
instead of assuming it matches joint_mapping's own default. Full suite:
11/11 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 12:36:55 -05:00
1 parent b0c418bd16
commit 5157651cd7
3 files changed
+75 -3

No files matched your search

+52 -1
View File
@@ -61,7 +61,7 @@ def test_publish_and_read_back_fake_pose(publisher):
assert frame.frame_number == 1
assert frame.skeleton.skeleton_tracking_id == 1
mapped = map_pose_to_kinect_joints(pose, (160, 120))
mapped = map_pose_to_kinect_joints(pose, (160, 120), mirror=publisher.mirror)
for j in range(c.JOINT_COUNT):
joint = frame.skeleton.joints[j]
assert joint.position.x == pytest.approx(mapped[j][0], abs=1e-5)
@@ -88,6 +88,57 @@ def test_publish_and_read_back_fake_pose(publisher):
assert int(color_out[:, :, 0].max()) == 200
def test_inference_worker_mirrors_frame_for_pose_but_not_for_publish():
"""tracker-native's win_main.cpp mirrors the capture pixel-for-pixel before
pose inference, but publishes the raw (unmirrored) frame as the color
stream; the shim's L/R joint swap then corrects MediaPipe's resulting
left/right mislabeling. The Python tracker must match that exactly - running
pose on the raw frame instead leaves joint identity already correct on its
own, so the shim's swap then flips tracking backwards in-game. Calls the
real Tracker._inference_worker (not a reimplementation) directly, bypassing
run()'s threaded pacing so the assertion is deterministic."""
import cv2
width, height = 10, 4
frame = np.zeros((height, width, 3), dtype=np.uint8)
frame[:, : width // 2] = 10
frame[:, width // 2 :] = 200
class OneFrameSource:
def __init__(self):
self._served = False
def read(self):
if self._served:
return None
self._served = True
return frame
def release(self):
pass
processed_frames = []
class SpyProvider:
def process(self, frame_bgr, timestamp_ms=None):
processed_frames.append(frame_bgr.copy())
return None
def close(self):
pass
publisher = FrameContractPublisher(depth_size=(160, 120), color_size=(160, 120))
try:
tracker = Tracker(OneFrameSource(), SpyProvider(), publisher)
tracker._inference_worker()
assert len(processed_frames) == 1
np.testing.assert_array_equal(processed_frames[0], cv2.flip(frame, 1))
np.testing.assert_array_equal(tracker._latest_frame, frame)
finally:
publisher.close()
def test_joint_depth_uses_z0_placement_and_has_body_relief(publisher):
pose = make_fake_pose(frame_index=0, image_width=160, image_height=120)
publisher.publish(pose, None)
+7 -1
View File
@@ -78,7 +78,7 @@ class FrameContractPublisher:
create: bool = True,
z0_meters: float = DEFAULT_Z0_METERS,
depth_gain: float = DEFAULT_DEPTH_GAIN,
mirror: bool = True,
mirror: bool = False,
) -> None:
self.depth_w, self.depth_h = depth_size
self.color_w, self.color_h = color_size
@@ -94,6 +94,12 @@ class FrameContractPublisher:
self._create = create
self.z0_meters = z0_meters
self.depth_gain = depth_gain
# False by default: tracker.py's capture thread already mirrors the
# frame pixel-for-pixel before pose inference (matching
# tracker-native's win_main.cpp), so this coordinate-level flip would
# double-mirror joint positions if left on. Only set True for a caller
# that feeds already-unmirrored pose data and wants this flag to do the
# mirroring instead.
self.mirror = mirror
self.contract = FrameContract.from_buffer(self._buf)
if create:
+16 -1
View File
@@ -108,9 +108,24 @@ class Tracker:
if frame is None:
self._source_done.set()
return
import cv2
# Mirror horizontally for pose inference only, matching
# tracker-native's win_main.cpp exactly: the published color frame
# stays the raw, unmirrored capture (below), but the pose model
# sees a mirrored frame, which flips which side MediaPipe calls
# "left"/"right" for the subject. core/mapping's own coordinate
# mirror stays off (FrameContractPublisher's mirror=False) since
# this physical flip already does that job; the shim's unconditional
# L/R swap (fallback/windows-nui/src/sensor.cpp) corrects MediaPipe's
# resulting mislabeling, same as the Windows tracker. Running pose on
# the raw (unmirrored) frame instead - as this tracker used to -
# leaves joint identity already correct on its own, so the shim's
# swap then flips it backwards: left/right tracking reversed in-game.
mirrored = cv2.flip(frame, 1)
ts_ms = int((time.monotonic() - t0) * 1000)
try:
pose = self.provider.process(frame, timestamp_ms=ts_ms)
pose = self.provider.process(mirrored, timestamp_ms=ts_ms)
except Exception:
logger.exception("pose inference failed; marking worker crashed")
self._worker_crashed.set()