hylo.manip.retarget¶
Package: hylo-manip · Core contracts
retarget ¶
Hand motion retargeting: human hand keypoints (the 21-point MediaPipe layout) -> joint angles of a robot hand and the pose of its palm.
Frames. Both hands get a palm frame from the same recipe: origin at the wrist, x towards the middle finger's base (knuckle), y across the knuckles from the index towards the outermost finger, z = x cross y (out of the palm on a right hand, anatomically the same side on both). The robot's wrist point, finger bases and fingertips come from its model (HandModel); for a three-finger hand the outermost finger is its middle finger.
Per frame the finger joints q minimise, in the robot palm frame, sum_i w_tip |tip_i(q) - o - a Rv(k_tip_i - k_wrist)|^2 wrist-to-tip vectors + sum_j w_ij |(tip_j(q) - tip_thumb(q)) - a Rv(k_tip_j - k_tip_thumb)|^2 thumb-to-finger vectors + w_smooth |q - q_prev|^2 + w_rest |q - q_open|^2 within the joint limits (bounded least squares, analytic Jacobians from MuJoCo). a scales the human hand to the robot's (middle finger plus palm length, measured once per sequence), Rv expresses human palm-frame vectors in the robot palm frame; w_ij grows as the human thumb and finger close in, so pinches stay pinches. Robot fingers map to human fingers by name (thumb, index, middle, ring, little); a robot with fewer fingers uses the first ones.
The palm pose in the world aligns the robot palm frame with the human one, palm centres (wrist and knuckles) together. A left human hand on a right robot hand (or the reverse) is mirrored: finger shapes reflected through the palm plane, the palm's normal kept (the thumb then sits on the other side).
rt = Retargeter(hand_model("allegro_right"))
out = rt.run(keypoints) # (T, 21, 3) metres, any frame (camera or world)
out.q (T, n), out.ee (T, 4, 4) palm (ee frame) poses, out.tcp, out.tip_error_mm (T,), out.ms_per_frame
CHAIN
module-attribute
¶
CHAIN = {'thumb': (1, 2, 3, 4), 'index': (5, 6, 7, 8), 'middle': (9, 10, 11, 12), 'ring': (13, 14, 15, 16), 'little': (17, 18, 19, 20)}
RetargetResult
dataclass
¶
RetargetResult(q: ndarray, ee: ndarray, tcp: ndarray, tip_error_mm: ndarray, pair_error_mm: ndarray, ms_per_frame: float, scale: float, fingers: dict[str, str], mirrored: bool, valid: ndarray, info: dict = dict())
Retargeter ¶
Retargeter(hand: HandModel, *, scale: float | None = None, w_tip: float = 1.0, w_pair: float = 1.0, w_smooth: float = 0.02, w_rest: float = 0.002, pinch_dist: float = 0.03, robot_side: str | None = None)
Keypoints -> robot hand. scale: robot/human size (None: measured per sequence). side of the human hand
("right"/"left"; None: same as the robot's) decides mirroring.
robot_side
instance-attribute
¶
robot_fingers
instance-attribute
¶
F_r
instance-attribute
¶
palm_centre_r
instance-attribute
¶
robot_len
instance-attribute
¶
targets ¶
targets(k: ndarray, scale: float, mirror: bool) -> tuple[dict[str, np.ndarray], dict[str, np.ndarray]]
Robot-palm-frame (ee coordinates) targets: tips relative to nothing (absolute in ee), and thumb-to-finger vectors.
solve ¶
solve(k: ndarray, q0: ndarray, q_prev: ndarray | None, scale: float, mirror: bool = False) -> tuple[np.ndarray, float, float]
run ¶
Retarget a (T, 21, 3) sequence (or one (21, 3) frame). Invalid frames are interpolated afterwards.
palm_pose ¶
The robot ee pose putting its palm frame on the human palm frame, palm centres together.
palm_frame ¶
4x4 palm frame: origin wrist, x to the middle base, y index -> outer base (made orthogonal), z = x cross y.
keypoints_from_robot ¶
A 21-point "human" hand made from the robot hand itself at joints q (tests and demos with a known answer): wrist at the robot's wrist point, each mapped finger's base, joint anchors and tip at the robot's; fingers the robot lacks are placed beside its outermost finger, straight.