100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
"""MediaPipe-33 -> Kinect-20 joint placement, mirroring core/mapping exactly
|
|
(plan todos 6 and 21).
|
|
|
|
Reduces MediaPipe's 33 landmarks to the 20 Kinect joints core/mapping.cpp
|
|
selects, places each joint at `clamp(Z0 + depthGain * worldZ, 0.8, 4.0)`
|
|
(nui-contracts.md section 1.5), then back-projects through Kinect's documented
|
|
depth-camera inverse transform (nui-contracts.md section 1.1). This gives each
|
|
joint a real, distinct depth instead of a flat per-frame constant, so the C++
|
|
synth rasterizer (which interpolates depth from these projected joints) emits
|
|
an articulated body surface.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from .contract import JointTrackingState
|
|
from .pose import PoseResult
|
|
|
|
VIS_TRACKED = 0.5
|
|
VIS_INFERRED = 0.1
|
|
DEFAULT_Z0_METERS = 2.5
|
|
DEFAULT_DEPTH_GAIN = 1.0
|
|
MIN_PLACED_Z_METERS = 0.8
|
|
MAX_PLACED_Z_METERS = 4.0
|
|
DEPTH_PROJECTION_SCALE = 3.501e-3
|
|
|
|
|
|
def tracking_state_for(visibility: float) -> int:
|
|
if visibility >= VIS_TRACKED:
|
|
return JointTrackingState.TRACKED
|
|
if visibility >= VIS_INFERRED:
|
|
return JointTrackingState.INFERRED
|
|
return JointTrackingState.NOT_TRACKED
|
|
|
|
|
|
def _mix_landmarks(
|
|
first: tuple[float, float, float, float],
|
|
second: tuple[float, float, float, float],
|
|
amount: float,
|
|
) -> tuple[float, float, float, float]:
|
|
return (
|
|
first[0] + (second[0] - first[0]) * amount,
|
|
first[1] + (second[1] - first[1]) * amount,
|
|
first[2] + (second[2] - first[2]) * amount,
|
|
min(first[3], second[3]),
|
|
)
|
|
|
|
|
|
def _kinect_joint_landmarks(
|
|
pose: PoseResult,
|
|
) -> tuple[tuple[float, float, float, float], ...]:
|
|
source = tuple(
|
|
(
|
|
pose.landmarks[index][0],
|
|
pose.landmarks[index][1],
|
|
pose.world_landmarks[index][2],
|
|
pose.visibility[index],
|
|
)
|
|
for index in range(len(pose.landmarks))
|
|
)
|
|
hips = _mix_landmarks(source[23], source[24], 0.5)
|
|
shoulders = _mix_landmarks(source[11], source[12], 0.5)
|
|
ears = _mix_landmarks(source[7], source[8], 0.5)
|
|
head = _mix_landmarks(shoulders, ears, 1.3) if ears[3] >= VIS_TRACKED else source[0]
|
|
left_hand = _mix_landmarks(source[19], source[17], 0.5)
|
|
right_hand = _mix_landmarks(source[20], source[18], 0.5)
|
|
return (
|
|
hips,
|
|
_mix_landmarks(hips, shoulders, 0.45),
|
|
shoulders,
|
|
head,
|
|
source[11], source[13], source[15],
|
|
left_hand if left_hand[3] >= VIS_TRACKED else source[15],
|
|
source[12], source[14], source[16],
|
|
right_hand if right_hand[3] >= VIS_TRACKED else source[16],
|
|
source[23], source[25], source[27], source[31],
|
|
source[24], source[26], source[28], source[32],
|
|
)
|
|
|
|
|
|
def map_pose_to_kinect_joints(
|
|
pose: PoseResult,
|
|
depth_size: tuple[int, int],
|
|
z0_meters: float = DEFAULT_Z0_METERS,
|
|
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
|
|
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)))
|
|
return tuple(joints)
|