Skip to content

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

WRIST module-attribute

WRIST = 0

TIP module-attribute

TIP = {'thumb': 4, 'index': 8, 'middle': 12, 'ring': 16, 'little': 20}

BASE module-attribute

BASE = {'thumb': 1, 'index': 5, 'middle': 9, 'ring': 13, 'little': 17}

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)}

ORDER module-attribute

ORDER = ('thumb', 'index', 'middle', 'ring', 'little')

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())

q instance-attribute

q: ndarray

ee instance-attribute

ee: ndarray

tcp instance-attribute

tcp: ndarray

tip_error_mm instance-attribute

tip_error_mm: ndarray

pair_error_mm instance-attribute

pair_error_mm: ndarray

ms_per_frame instance-attribute

ms_per_frame: float

scale instance-attribute

scale: float

fingers instance-attribute

fingers: dict[str, str]

mirrored instance-attribute

mirrored: bool

valid instance-attribute

valid: ndarray

info class-attribute instance-attribute

info: dict = field(default_factory=dict)

summary

summary() -> 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_side = robot_side or ('left' if 'left' in hand.name else 'right')

map instance-attribute

map = {n: n for n in names if n in TIP}

robot_fingers instance-attribute

robot_fingers = [n for n in ORDER if n in self.map] + [n for n in self.map if n not in ORDER]

fidx instance-attribute

fidx = {f.name: i for i, f in enumerate(hand.fingers)}

thumb instance-attribute

thumb = hand.fingers[hand.thumb].name

wrist_r instance-attribute

wrist_r = hand.wrist()

F_r instance-attribute

F_r = palm_frame(self.wrist_r, bases[nonthumb[0]], mid, bases[nonthumb[-1]])

nonthumb instance-attribute

nonthumb = nonthumb

palm_centre_r instance-attribute

palm_centre_r = np.mean([self.wrist_r] + [bases[n] for n in nonthumb], 0)

robot_len instance-attribute

robot_len = float(np.linalg.norm(bases[mf.name] - self.wrist_r)) + self._chain_length(mf)

human_length staticmethod

human_length(k: ndarray) -> float

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

run(keypoints: ndarray, valid: ndarray | None = None, side: str | None = None) -> RetargetResult

Retarget a (T, 21, 3) sequence (or one (21, 3) frame). Invalid frames are interpolated afterwards.

palm_pose

palm_pose(k: ndarray, mirror: bool = False) -> np.ndarray

The robot ee pose putting its palm frame on the human palm frame, palm centres together.

palm_frame

palm_frame(wrist: ndarray, index: ndarray, middle: ndarray, outer: ndarray) -> np.ndarray

4x4 palm frame: origin wrist, x to the middle base, y index -> outer base (made orthogonal), z = x cross y.

human_frame

human_frame(k: ndarray) -> np.ndarray

keypoints_from_robot

keypoints_from_robot(hand: HandModel, q: ndarray, T_world_ee: ndarray | None = None) -> np.ndarray

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.