Files
kinect/tests/test_joint_mapping_py.py
T
kinect-botandsisyphus-dev-ai a90b2af6e9 fix(tracker): port core/mapping's hand averaging + Holt smoothing exactly
"i don't know what calibration you're talking about because i never had to
callibrate on the windows version" and "i'm having the problem from earlier
[...] difficult to move the cursor down" - both pointed at the same thing:
calibration was a workaround for a real pipeline discrepancy, not the fix.
On Windows the user only ever used the hardcoded default gains (3.2/2.6, no
calibration run) and it worked; if the Linux tracker needs calibration to
reach the same place with the same defaults, the two pipelines aren't
actually equivalent. Went back to core/mapping.cpp (the C++ module
joint_mapping.py's own docstring already claims to mirror "exactly") and
found two real, concrete divergences from that claim:

1. HAND joint averaging. core/mapping.cpp's hand_landmark() averages four
   points (wrist + index/pinky knuckles + thumb, each 0.25 weight) - the
   individual finger landmarks are MediaPipe's noisiest output, so this is a
   deliberate accuracy/jitter fix (~35% less jitter, lands on the palm
   centre matching real Kinect). joint_mapping.py's _kinect_joint_landmarks
   only averaged index+pinky (2 points, no wrist, no thumb) - a different
   hand position every frame than the Windows tracker computes for the same
   physical hand. Ported hand_landmark() exactly (same weights, same
   visibility-gated fallback to the wrist alone).

2. No smoothing at all. tracker-native/src/frame_pipeline.cpp runs every
   published joint through core/mapping's SkeletonMapper, which owns one
   Holt (double-exponential) smoothing filter per joint with jitter-radius
   damping, prediction, and a max-deviation clamp - applied to literally
   every frame of the main publish loop, not just during calibration. The
   Python tracker published a fresh, completely unsmoothed position every
   frame - a real behavioral gap (jitter/prediction/damping characteristics
   the Windows cursor feel was built around), not cosmetic.

Ported both into tracker/joint_mapping.py: HoltSmoothingFilter (the exact
filter math) and SkeletonMapper (owns one filter per joint, holds the last
smoothed position when a joint's visibility drops below VIS_TRACKED instead
of publishing a fresh low-confidence one - matching
core/mapping.cpp::SkeletonMapper::map exactly). FrameContractPublisher now
owns one SkeletonMapper instance persisting across publish() calls (matching
frame_pipeline.cpp owning one SkeletonMapper for the tracker session),
replacing the stateless per-frame map_pose_to_kinect_joints() call (still
kept, now only used by calibration.py and as the first-frame reference).

Verified against the C++ test suite's own fixtures, not just "looks
similar": tests/test_joint_mapping_py.py ports
test_mapping.cpp::HoltFilterMatchesPrecomputedGoldenSequence's exact raw/
expected values (1e-7 precision match) and
::OccludedJointIsInferredAndHoldsLastSmoothedPosition. Full suite: 98/98
passing (tests/test_tracker.py's existing fixed-pose comparison test still
matches exactly, since a fresh filter's first update is a pure passthrough -
no smoothing history yet - proven by a dedicated new test).

This does not touch the cursor arm-gain calibration added earlier - that
remains available but optional, same as it always was on Windows.

Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
2026-11-30 13:14:14 -05:00

86 lines
3.4 KiB
Python

"""Parity tests for tracker/joint_mapping.py's HoltSmoothingFilter/SkeletonMapper
against core/mapping.cpp's behavior, reusing tests/test_mapping.cpp's own golden
values and fixtures so the two implementations are provably the same, not just
similarly-shaped."""
import os
import sys
import pytest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from tracker.contract import JointTrackingState # noqa: E402
from tracker.fake_pose import make_fake_pose # noqa: E402
from tracker.joint_mapping import ( # noqa: E402
SkeletonMapper,
SmoothingParameters,
HoltSmoothingFilter,
map_pose_to_kinect_joints,
)
def test_holt_filter_matches_precomputed_golden_sequence():
"""tests/test_mapping.cpp::HoltFilterMatchesPrecomputedGoldenSequence's
exact fixture and expected values, ported verbatim."""
filt = HoltSmoothingFilter(SmoothingParameters(0.6, 0.4, 0.4, 0.04, 0.07))
raw_x = [0.0, 0.02, 0.04, 0.10, 0.12]
expected_x = [
0.000000000,
0.004640000,
0.020531200,
0.062115328,
0.102407352,
]
for x, expected in zip(raw_x, expected_x):
output = filt.update((x, 0.0, 2.5, 1.0))
assert output[0] == pytest.approx(expected, abs=1e-7)
assert output[1] == pytest.approx(0.0)
assert output[2] == pytest.approx(2.5)
assert output[3] == pytest.approx(1.0)
def test_skeleton_mapper_holds_last_smoothed_position_when_joint_occluded():
"""tests/test_mapping.cpp::OccludedJointIsInferredAndHoldsLastSmoothedPosition,
ported to the Python PoseResult/SkeletonMapper shape: a wrist landmark
dropping below the visibility threshold must hold the filter's last
smoothed output, not jump to a fresh (and meaningless) low-confidence
projection."""
mapper = SkeletonMapper()
pose = make_fake_pose(frame_index=0, image_width=160, image_height=120)
visible_joints = mapper.map(pose, (160, 120), mirror=False)
last_wrist = visible_joints[6] # WRIST_LEFT
occluded_landmarks = list(pose.landmarks)
occluded_landmarks[15] = (0.0, 0.0, -100.0, 0.0) # left wrist, zero visibility
pose.landmarks = occluded_landmarks
pose.world_landmarks = list(pose.world_landmarks)
pose.visibility = list(pose.visibility)
pose.visibility[15] = 0.0
occluded_joints = mapper.map(pose, (160, 120), mirror=False)
assert occluded_joints[6][0] == pytest.approx(last_wrist[0])
assert occluded_joints[6][1] == pytest.approx(last_wrist[1])
assert occluded_joints[6][2] == pytest.approx(last_wrist[2])
assert occluded_joints[6][3] == pytest.approx(1.0)
def test_skeleton_mapper_smooths_first_frame_as_a_passthrough():
"""A fresh filter's first update equals the raw (clamped) input - no
smoothing history exists yet - so map_pose_to_kinect_joints() (the
stateless function calibration.py still uses) matches a fresh
SkeletonMapper's first frame exactly."""
pose = make_fake_pose(frame_index=0, image_width=160, image_height=120)
stateless = map_pose_to_kinect_joints(pose, (160, 120), mirror=False)
mapper = SkeletonMapper()
first_frame = mapper.map(pose, (160, 120), mirror=False)
for stateless_joint, mapped_joint in zip(stateless, first_frame):
assert mapped_joint[0] == pytest.approx(stateless_joint[0], abs=1e-6)
assert mapped_joint[1] == pytest.approx(stateless_joint[1], abs=1e-6)
assert mapped_joint[2] == pytest.approx(stateless_joint[2], abs=1e-6)