diff --git a/tests/test_tracker.py b/tests/test_tracker.py index 20ec28d..91569fa 100644 --- a/tests/test_tracker.py +++ b/tests/test_tracker.py @@ -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) diff --git a/tracker/publisher.py b/tracker/publisher.py index 876db8d..a62f72e 100644 --- a/tracker/publisher.py +++ b/tracker/publisher.py @@ -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: diff --git a/tracker/tracker.py b/tracker/tracker.py index 7989a68..4817aea 100644 --- a/tracker/tracker.py +++ b/tracker/tracker.py @@ -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()