fix(tracker): normalize joint reach by live shoulder width (body-size scale)

the users own fix idea: scale based on tracked body size. The shims
arm-reach gain (3.2/2.6) is a fixed multiplier tuned against one reference
reach magnitude - a players actual size/distance from camera changes the
raw reach the gain amplifies, so a fixed gain over- or under-stretches
limbs/cursor depending on who is in frame and how far away.

Added _body_scale(): derives a scale factor each frame from the live
shoulder width (MediaPipes world landmarks, already metric) against
TARGET_SHOULDER_WIDTH_METERS=0.40 (core/calibration.hs own T-pose target),
applied around the SHOULDER_CENTER pivot so scaling resizes the skeleton
around its own centre rather than shifting it on screen. Wired into both
map_pose_to_kinect_joints and SkeletonMapper.map via a shared _raw_joint_xyz
helper, so calibration and the live publish path stay consistent. 98/98
tests 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:37:12 -05:00
1 parent 4a9a651a4a
commit 148b3fcdef
1 file changed
+73 -23
+73 -23
View File
@@ -26,6 +26,16 @@ MIN_PLACED_Z_METERS = 0.8
MAX_PLACED_Z_METERS = 4.0
DEPTH_PROJECTION_SCALE = 3.501e-3
# Real-world (MediaPipe world-landmark, metric) shoulder width a Kinect-sized
# adult presents at - core/calibration.h's kTargetShoulderWidthMeters, reused
# here as a continuous per-frame normalizer instead of a one-time T-pose
# calibration step: the shim's fixed arm-reach gain (3.2/2.6) was tuned
# against one reach magnitude, so without this, a player closer/farther from
# the camera, or simply a different body size, gets a different raw reach -
# and the same fixed gain over- or under-stretches their limbs/cursor.
TARGET_SHOULDER_WIDTH_METERS = 0.40
MIN_SHOULDER_WIDTH_METERS = 0.05
# core/mapping.cpp's clamp_position() range, applied to every joint position
# before and after smoothing.
MIN_X = -2.2
@@ -116,6 +126,41 @@ def _kinect_joint_landmarks(
)
def _raw_joint_xyz(
u: float,
v: float,
world_z: float,
depth_size: tuple[int, int],
z0_meters: float,
depth_gain: float,
mirror: bool,
) -> tuple[float, float, float]:
width, height = depth_size
projected_u = 1.0 - min(max(u, 0.0), 1.0) if mirror else min(max(u, 0.0), 1.0)
projected_v = min(max(v, 0.0), 1.0)
z = min(max(z0_meters + depth_gain * world_z, MIN_PLACED_Z_METERS), MAX_PLACED_Z_METERS)
depth_x = round(projected_u * width)
depth_y = round(projected_v * height)
x = (depth_x - width / 2.0) * (320.0 / width) * DEPTH_PROJECTION_SCALE * z
y = -(depth_y - height / 2.0) * (240.0 / height) * DEPTH_PROJECTION_SCALE * z
return x, y, z
def _body_scale(pose: PoseResult) -> float:
"""Continuous per-frame stand-in for core/calibration.h's one-time T-pose
player_scale: derives a scale factor from the live shoulder width
(MediaPipe's world landmarks, already metric) against
TARGET_SHOULDER_WIDTH_METERS, so the shim's fixed arm-reach gain always
sees a reach normalized to one reference body size/distance, instead of
being over- or under-amplified for whoever's actually in frame."""
lw = pose.world_landmarks[11]
rw = pose.world_landmarks[12]
shoulder_width = math.sqrt((lw[0] - rw[0]) ** 2 + (lw[1] - rw[1]) ** 2 + (lw[2] - rw[2]) ** 2)
if shoulder_width < MIN_SHOULDER_WIDTH_METERS:
return 1.0
return TARGET_SHOULDER_WIDTH_METERS / shoulder_width
def map_pose_to_kinect_joints(
pose: PoseResult,
depth_size: tuple[int, int],
@@ -123,18 +168,23 @@ def map_pose_to_kinect_joints(
depth_gain: float = DEFAULT_DEPTH_GAIN,
mirror: bool = True,
) -> tuple[tuple[float, float, float, float, int], ...]:
"""Apply core/mapping's joint selection and exact Z0 placement formula."""
width, height = depth_size
"""Apply core/mapping's joint selection and exact Z0 placement formula,
then the body-size scale from _body_scale(), pivoted on SHOULDER_CENTER
(joint index 2) so a scale correction resizes around the body's own
centre rather than shifting its on-screen position."""
landmarks = tuple(_kinect_joint_landmarks(pose))
pivot_u, pivot_v, pivot_world_z, _pivot_vis = landmarks[2]
pivot_x, pivot_y, _pivot_z = _raw_joint_xyz(
pivot_u, pivot_v, pivot_world_z, depth_size, z0_meters, depth_gain, mirror
)
scale = _body_scale(pose)
joints = []
for u, v, world_z, visibility in _kinect_joint_landmarks(pose):
projected_u = 1.0 - min(max(u, 0.0), 1.0) if mirror else min(max(u, 0.0), 1.0)
projected_v = min(max(v, 0.0), 1.0)
z = min(max(z0_meters + depth_gain * world_z, MIN_PLACED_Z_METERS), MAX_PLACED_Z_METERS)
depth_x = round(projected_u * width)
depth_y = round(projected_v * height)
x = (depth_x - width / 2.0) * (320.0 / width) * DEPTH_PROJECTION_SCALE * z
y = -(depth_y - height / 2.0) * (240.0 / height) * DEPTH_PROJECTION_SCALE * z
joints.append((x, y, z, 1.0, tracking_state_for(visibility)))
for u, v, world_z, visibility in landmarks:
x, y, z = _raw_joint_xyz(u, v, world_z, depth_size, z0_meters, depth_gain, mirror)
scaled_x = pivot_x + (x - pivot_x) * scale
scaled_y = pivot_y + (y - pivot_y) * scale
joints.append((scaled_x, scaled_y, z, 1.0, tracking_state_for(visibility)))
return tuple(joints)
@@ -272,24 +322,24 @@ class SkeletonMapper:
depth_gain: float = DEFAULT_DEPTH_GAIN,
mirror: bool = True,
) -> tuple[tuple[float, float, float, float, int], ...]:
width, height = depth_size
landmarks = tuple(_kinect_joint_landmarks(pose))
pivot_u, pivot_v, pivot_world_z, _pivot_vis = landmarks[2]
pivot_x, pivot_y, _pivot_z = _raw_joint_xyz(
pivot_u, pivot_v, pivot_world_z, depth_size, z0_meters, depth_gain, mirror
)
scale = _body_scale(pose)
joints = []
for index, (u, v, world_z, visibility) in enumerate(_kinect_joint_landmarks(pose)):
for index, (u, v, world_z, visibility) in enumerate(landmarks):
tracked = visibility >= VIS_TRACKED
filt = self._filters[index]
if not tracked and filt.initialized:
joints.append((*filt.output, tracking_state_for(visibility)))
continue
projected_u = 1.0 - min(max(u, 0.0), 1.0) if mirror else min(max(u, 0.0), 1.0)
projected_v = min(max(v, 0.0), 1.0)
z = min(
max(z0_meters + depth_gain * world_z, MIN_PLACED_Z_METERS), MAX_PLACED_Z_METERS
)
depth_x = round(projected_u * width)
depth_y = round(projected_v * height)
raw_x = (depth_x - width / 2.0) * (320.0 / width) * DEPTH_PROJECTION_SCALE * z
raw_y = -(depth_y - height / 2.0) * (240.0 / height) * DEPTH_PROJECTION_SCALE * z
smoothed = filt.update((raw_x, raw_y, z, 1.0))
x, y, z = _raw_joint_xyz(u, v, world_z, depth_size, z0_meters, depth_gain, mirror)
scaled_x = pivot_x + (x - pivot_x) * scale
scaled_y = pivot_y + (y - pivot_y) * scale
smoothed = filt.update((scaled_x, scaled_y, z, 1.0))
joints.append((*smoothed, tracking_state_for(visibility)))
return tuple(joints)