diff --git a/tracker-native/scripts/puppet.py b/tracker-native/scripts/puppet.py new file mode 100644 index 0000000..db05aa7 --- /dev/null +++ b/tracker-native/scripts/puppet.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Synthetic Kinect-skeleton puppet for validating the DEA mapping WITHOUT a +webcam or the game. + +It reimplements core/mapping (landmark u,v,world_z -> NUI-20 joint xyz in Kinect +camera space) exactly, and generates a parametric standing figure with +controllable left/right hands, so we can: + * sweep a hand across the frame and see the HAND joint's skeleton-space range + (the INPUT to the game's menu hand->cursor transform), and + * feed controlled full-body poses for gameplay-accuracy checks. + +Keep this in sync with core/mapping/mapping.cpp (constants below). +""" +from __future__ import annotations +import numpy as np + +# --- constants mirrored from core/mapping/mapping.cpp --- +SCALE = 3.501e-3 +MINPZ, MAXZ = 0.8, 4.0 +MINX, MAXX = -2.2, 2.2 +MINY, MAXY = -1.6, 1.6 +DW, DH = 640, 480 +NUI = ['HIP_CENTER', 'SPINE', 'SHOULDER_CENTER', 'HEAD', 'SHOULDER_L', 'ELBOW_L', + 'WRIST_L', 'HAND_L', 'SHOULDER_R', 'ELBOW_R', 'WRIST_R', 'HAND_R', + 'HIP_L', 'KNEE_L', 'ANKLE_L', 'FOOT_L', 'HIP_R', 'KNEE_R', 'ANKLE_R', + 'FOOT_R'] + + +def joint_xyz(u, v, world_z, z0=2.5, gain=1.0, mirror=True): + """Map one landmark (normalized u,v in image, world_z meters) to a Kinect + skeleton position, identical to SkeletonMapper::map + depth_image_to_skeleton.""" + z = min(max(z0 + gain * world_z, MINPZ), MAXZ) + up = (1.0 - u) if mirror else u + dx = round(up * DW) + dy = round(v * DH) + x = (dx - DW / 2) * (320.0 / DW) * SCALE * z + y = -(dy - DH / 2) * (240.0 / DH) * SCALE * z + return np.array([min(max(x, MINX), MAXX), min(max(y, MINY), MAXY), + min(max(z, 0.0), MAXZ)]) + + +def standing_pose(lhand=(0.33, 0.55, -0.10), rhand=(0.67, 0.55, -0.10)): + lm = [None] * 33 + + def P(i, u, v, wz=0.0, vis=1.0): + lm[i] = (u, v, vis, wz) + P(0, 0.50, 0.12); P(1, 0.48, 0.10); P(2, 0.47, 0.10); P(3, 0.46, 0.10) + P(4, 0.52, 0.10); P(5, 0.53, 0.10); P(6, 0.54, 0.10) + P(7, 0.45, 0.12); P(8, 0.55, 0.12); P(9, 0.48, 0.15); P(10, 0.52, 0.15) + P(11, 0.40, 0.28); P(12, 0.60, 0.28) + P(13, 0.35, 0.42); P(14, 0.65, 0.42) + lu, lv, lwz = lhand; ru, rv, rwz = rhand + P(15, lu, lv, lwz); P(16, ru, rv, rwz) + P(17, lu - 0.01, lv + 0.02, lwz); P(18, ru + 0.01, rv + 0.02, rwz) + P(19, lu + 0.01, lv + 0.02, lwz); P(20, ru - 0.01, rv + 0.02, rwz) + P(21, lu, lv + 0.01, lwz); P(22, ru, rv + 0.01, rwz) + P(23, 0.44, 0.55); P(24, 0.56, 0.55) + P(25, 0.44, 0.75); P(26, 0.56, 0.75) + P(27, 0.44, 0.92); P(28, 0.56, 0.92) + P(29, 0.43, 0.94); P(30, 0.57, 0.94); P(31, 0.44, 0.96); P(32, 0.56, 0.96) + return lm + + +def _mid(a, b): + return ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2, min(a[2], b[2]), + (a[3] + b[3]) / 2) + + +def _hand(lm, imcp, pmcp, wrist): + h = _mid(lm[imcp], lm[pmcp]) + return h if h[2] >= 0.5 else lm[wrist] + + +def map_nui(lm, z0=2.5, gain=1.0, mirror=True): + j = [None] * 20 + j[0] = _mid(lm[23], lm[24]); j[2] = _mid(lm[11], lm[12]) + j[1] = tuple(j[0][k] * 0.55 + j[2][k] * 0.45 for k in range(4)) + j[3] = lm[0] + j[4] = lm[11]; j[5] = lm[13]; j[6] = lm[15]; j[7] = _hand(lm, 19, 17, 15) + j[8] = lm[12]; j[9] = lm[14]; j[10] = lm[16]; j[11] = _hand(lm, 20, 18, 16) + j[12] = lm[23]; j[13] = lm[25]; j[14] = lm[27]; j[15] = lm[31] + j[16] = lm[24]; j[17] = lm[26]; j[18] = lm[28]; j[19] = lm[32] + return {NUI[i]: joint_xyz(j[i][0], j[i][1], j[i][3], z0, gain, mirror) + for i in range(20)} + + +def hand_sweep(n=9, z0=2.5, gain=1.0, mirror=True, world_z=-0.1): + out = [] + for v in np.linspace(0.20, 0.85, n): + for u in np.linspace(0.20, 0.80, n): + sk = map_nui(standing_pose(rhand=(u, v, world_z)), z0, gain, mirror) + out.append((u, v, sk['HAND_R'])) + return out + + +if __name__ == '__main__': + sk = map_nui(standing_pose()) + for nm in NUI: + print(f"{nm:16s} {np.round(sk[nm], 3)}")