Skip to content

Turn any video into a Mixamo-rig animation - GVHMR estimator, spec-driven retarget, FK apply in Blender via MCP. Works with any Mixamo character; built to be operated end-to-end by an AI agent.

pipeline: planted feet from physics and the image, fingers from zoomed hand crops

Sachaa5111c3744a7
diff --git a/pipeline/bl_motion.py b/pipeline/bl_motion.py
index 47859c6..b1a946b 100644
--- a/pipeline/bl_motion.py
+++ b/pipeline/bl_motion.py
@@ -314,7 +314,9 @@ def cmd_render(args) -> None:
a = math.radians(35.0)
cam.location = Vector((cx + dist * math.sin(a), cy - dist * math.cos(a), 1.55))
cam.rotation_euler = (math.radians(81.0), 0.0, a)
- scene.render.resolution_percentage = 100
+ # --scale renders more pixels of the same shot (framing is resolution-
+ # independent): the zoomed hand and foot crops of render_review.py
+ scene.render.resolution_percentage = int(round(100 * args.scale))
out = rpath(args.out)
out.mkdir(parents=True, exist_ok=True)
scene.render.filepath = str(out / "f####")
@@ -342,6 +344,7 @@ def main() -> None:
r.add_argument("--out", required=True)
r.add_argument("--frames", help="first:last (1-based, inclusive)")
r.add_argument("--distance", type=float, default=4.6)
+ r.add_argument("--scale", type=float, default=1.0, help="resolution multiplier (2 = twice the pixels per side)")
args = ap.parse_args(argv)
{"dump-rig": dump_rig, "apply": cmd_apply, "render": cmd_render}[args.cmd](args)
diff --git a/pipeline/detect_hands.py b/pipeline/detect_hands.py
new file mode 100644
index 0000000..4c8156d
--- /dev/null
+++ b/pipeline/detect_hands.py
@@ -0,0 +1,160 @@
+"""Hand keypoints (21 per hand) for a plate, from zoomed crops.
+
+ python pipeline/detect_hands.py --video plates/<p>/<p>.mp4 [--kp plates/<p>/smplx.npz]
+ [--out plates/<p>/hands2d.npz]
+
+GVHMR estimates no fingers, and on a full-body 1280x720 plate a hand is
+30-50 px: a hand detector run on the whole frame sees a blob. So each
+hand is cropped around its wrist first — the ViTPose wrist and elbow of
+the plate's estimate place and size the crop (the hand lies beyond the
+wrist, along the forearm, within about one forearm length) — and the
+crop is upscaled before MediaPipe's Hand Landmarker looks at it. The
+landmarks are mapped back to the plate's pixels.
+
+Runs in any Python with `mediapipe` and `opencv-python` (it does not need
+the GVHMR venv). The model (hand_landmarker.task) is looked up in
+tools/models/, then downloaded from Google's model store if missing.
+Solo plates only.
+
+Writes hands2d.npz, hands in (left, right) order — the performer's own:
+ kp (T, 2, 21, 3) x px, y px, relative depth (px scale) in the plate
+ world (T, 2, 21, 3) metric 3D landmarks, hand-centred, camera axes
+ score (T, 2) handedness confidence of the kept detection, 0 = none
+ crop (T, 2, 3) crop centre x, y and half-size, px
+ fps
+"""
+
+from __future__ import annotations
+
+import argparse
+from pathlib import Path
+
+import cv2
+import numpy as np
+
+import detect_feet as DF
+
+MODEL_URL = ("https://storage.googleapis.com/mediapipe-models/hand_landmarker/"
+ "hand_landmarker/float16/latest/hand_landmarker.task")
+MODEL_PATHS = [DF.REPO / "tools" / "models" / "hand_landmarker.task"]
+# ViTPose COCO-17: wrist, elbow per side (performer's left, right)
+ARMS = ((9, 7), (10, 8))
+CROP_PX = 256
+
+
+def model_path(arg=None) -> Path:
+ for p in ([DF.rpath(arg)] if arg else []) + MODEL_PATHS:
+ if p.exists() and DF._is_task_file(p):
+ return p
+ dst = MODEL_PATHS[0]
+ dst.parent.mkdir(parents=True, exist_ok=True)
+ print(f"downloading {MODEL_URL} -> {dst}")
+ import urllib.request
+ urllib.request.urlretrieve(MODEL_URL, dst)
+ return dst
+
+
+def hand_boxes(kp2d: np.ndarray, min_conf: float = 0.3):
+ """(T, 2, 3) crop centre and half-size per hand, NaN where the wrist is
+ not seen. Centre: a third of a forearm beyond the wrist; half-size: 0.9
+ forearm (at least 36 px). The forearm's screen length is smoothed over
+ time — it shrinks when the arm points at the camera, and a crop that
+ shrinks with it would cut the hand off."""
+ T = kp2d.shape[0]
+ out = np.full((T, 2, 3), np.nan)
+ for k, (w, e) in enumerate(ARMS):
+ fore = kp2d[:, w, :2] - kp2d[:, e, :2]
+ length = np.linalg.norm(fore, axis=1)
+ ok = (kp2d[:, w, 2] >= min_conf) & (kp2d[:, e, 2] >= min_conf)
+ ref = np.percentile(length[ok], 75) if ok.any() else 60.0
+ # never under 60 % of the arm's usual length on screen
+ length = np.maximum(length, 0.6 * ref)
+ c = kp2d[:, w, :2] + fore / np.maximum(np.linalg.norm(fore, axis=1, keepdims=True), 1e-6) * length[:, None] / 3.0
+ half = np.maximum(0.9 * length, 36.0)
+ out[ok, k, :2] = c[ok]
+ out[ok, k, 2] = half[ok]
+ return out
+
+
+def crop(img: np.ndarray, cx: float, cy: float, half: float) -> np.ndarray:
+ """Square crop, padded with edge pixels where it leaves the frame,
+ upscaled to CROP_PX."""
+ x0, y0, s = int(round(cx - half)), int(round(cy - half)), int(round(2 * half))
+ H, W = img.shape[:2]
+ pad = max(0, -x0, -y0, x0 + s - W, y0 + s - H)
+ if pad:
+ img = cv2.copyMakeBorder(img, pad, pad, pad, pad, cv2.BORDER_REPLICATE)
+ x0, y0 = x0 + pad, y0 + pad
+ return cv2.resize(img[y0:y0 + s, x0:x0 + s], (CROP_PX, CROP_PX), interpolation=cv2.INTER_CUBIC)
+
+
+def detect(video: Path, kp2d: np.ndarray, model: Path):
+ import mediapipe as mp
+ from mediapipe.tasks import python as mp_python
+ from mediapipe.tasks.python import vision
+
+ opts = vision.HandLandmarkerOptions(
+ base_options=mp_python.BaseOptions(model_asset_path=str(model)),
+ running_mode=vision.RunningMode.IMAGE, num_hands=2,
+ min_hand_detection_confidence=0.3, min_hand_presence_confidence=0.3)
+ cap = cv2.VideoCapture(str(video))
+ fps = cap.get(cv2.CAP_PROP_FPS) or 24.0
+ boxes = hand_boxes(kp2d)
+ T = kp2d.shape[0]
+ kp = np.zeros((T, 2, 21, 3))
+ world = np.zeros((T, 2, 21, 3))
+ score = np.zeros((T, 2))
+ with vision.HandLandmarker.create_from_options(opts) as lm:
+ for t in range(T):
+ ok, bgr = cap.read()
+ if not ok:
+ break
+ rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
+ for k in range(2):
+ cx, cy, half = boxes[t, k]
+ if not np.isfinite(half):
+ continue
+ img = mp.Image(image_format=mp.ImageFormat.SRGB, data=np.ascontiguousarray(crop(rgb, cx, cy, half)))
+ res = lm.detect(img)
+ if not res.hand_landmarks:
+ continue
+ # the wrist we cropped around, in normalised crop coordinates
+ w = kp2d[t, ARMS[k][0], :2]
+ wx, wy = (w[0] - (cx - half)) / (2 * half), (w[1] - (cy - half)) / (2 * half)
+ dist = [np.hypot(h[0].x - wx, h[0].y - wy) for h in res.hand_landmarks]
+ i = int(np.argmin(dist))
+ if dist[i] > 0.35: # another hand in the crop, or a false palm
+ continue
+ for j, p in enumerate(res.hand_landmarks[i]):
+ kp[t, k, j] = (cx - half + p.x * 2 * half, cy - half + p.y * 2 * half, p.z * 2 * half)
+ for j, p in enumerate(res.hand_world_landmarks[i]):
+ world[t, k, j] = (p.x, p.y, p.z)
+ score[t, k] = res.handedness[i][0].score if res.handedness else 0.5
+ cap.release()
+ return kp, world, score, boxes, float(fps)
+
+
+def main():
+ ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
+ ap.add_argument("--video", required=True)
+ ap.add_argument("--kp", help="npz with the plate's ViTPose kp2d (default: smplx_raw.npz, else smplx.npz, next to the video)")
+ ap.add_argument("--out", help="default: hands2d.npz next to the video")
+ ap.add_argument("--model")
+ args = ap.parse_args()
+ video = DF.rpath(args.video)
+ if args.kp:
+ kp_path = DF.rpath(args.kp)
+ else:
+ kp_path = next((p for p in (video.with_name("smplx_raw.npz"), video.with_name("smplx.npz")) if p.exists()), None)
+ if kp_path is None:
+ raise SystemExit("no smplx_raw.npz / smplx.npz next to the video: run estimate_pose_gvhmr.py first, or pass --kp")
+ kp2d = np.load(kp_path)["kp2d"]
+ out = DF.rpath(args.out) if args.out else video.with_name("hands2d.npz")
+ kp, world, score, boxes, fps = detect(video, kp2d, model_path(args.model))
+ np.savez_compressed(out, kp=kp, world=world, score=score, crop=boxes, fps=np.array(fps))
+ found = (score > 0).mean(0) * 100
+ print(f"wrote {out}: {kp.shape[0]} frames, left hand found on {found[0]:.0f}%, right on {found[1]:.0f}%")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/pipeline/estimate_pose_gvhmr.py b/pipeline/estimate_pose_gvhmr.py
index c77d7ae..0e1e1df 100644
--- a/pipeline/estimate_pose_gvhmr.py
+++ b/pipeline/estimate_pose_gvhmr.py
@@ -534,40 +534,44 @@ def project(p: np.ndarray, K: np.ndarray, wh: tuple[int, int]) -> np.ndarray:
return np.array([u, v, 0.0])
-def foot_keypoints(video: Path, out: Path, args):
- """Heel/toe 2D keypoints for the refinement (detect_feet.py, MediaPipe).
+def run_mediapipe(script: str, video: Path, out: Path, args, extra=()) -> bool:
+ """Run one of the MediaPipe detectors (detect_feet.py, detect_hands.py).
MediaPipe does not live in the GVHMR venv, so the detector runs in
whichever Python has it: this one, --feet-python, or `python` on PATH.
- Cached next to the plate; returns None (refinement without feet) when
- no detector is available or the plate has two performers.
- """
+ Returns whether `out` was written."""
import importlib.util
import shutil
import subprocess
- if out.exists():
- return np.load(out)["kp"]
- if args.person:
- print("foot keypoints: skipped (multi-person plate; MediaPipe tracks one pose)")
- return None
- script = REPO / "pipeline" / "detect_feet.py"
if importlib.util.find_spec("mediapipe") is not None:
cmd = [sys.executable]
else:
exe = args.feet_python or shutil.which("python") or shutil.which("python3")
cmd = [exe] if exe else None
if cmd is None:
- print("foot keypoints: no Python with mediapipe found (pass --feet-python); refining without feet")
- return None
- res = subprocess.run(cmd + [str(script), "--video", str(video), "--out", str(out)],
+ print(f"{script}: no Python with mediapipe found (pass --feet-python)")
+ return False
+ res = subprocess.run(cmd + [str(REPO / "pipeline" / script), "--video", str(video), "--out", str(out), *extra],
capture_output=True, text=True)
if res.returncode != 0 or not out.exists():
- print("foot keypoints: detect_feet.py failed; refining without feet\n" + res.stderr[-800:])
- return None
+ print(f"{script} failed\n" + res.stderr[-800:])
+ return False
if res.stdout.strip():
print(res.stdout.strip().splitlines()[-1])
- return np.load(out)["kp"]
+ return True
+
+
+def foot_keypoints(video: Path, out: Path, args):
+ """Heel/toe 2D keypoints for the refinement (detect_feet.py). Cached
+ next to the plate; None (refinement without feet) when no detector is
+ available or the plate has two performers."""
+ if out.exists():
+ return np.load(out)["kp"]
+ if args.person:
+ print("foot keypoints: skipped (multi-person plate; MediaPipe tracks one pose)")
+ return None
+ return np.load(out)["kp"] if run_mediapipe("detect_feet.py", video, out, args) else None
def main() -> None:
@@ -588,7 +592,8 @@ def main() -> None:
help="where to write the SMPL-X parameters for retarget_smplx.py "
"(default: smplx.npz — or smplx_<person>.npz — next to --out)")
ap.add_argument("--feet-python", default=None,
- help="a Python with mediapipe for detect_feet.py (default: this one, else `python` on PATH)")
+ help="a Python with mediapipe for detect_feet.py and detect_hands.py "
+ "(default: this one, else `python` on PATH)")
ap.add_argument("--no-refine", action="store_true",
help="skip the 2D-keypoint refinement (refine_smplx.py); the raw GVHMR "
"parameters are always kept beside it as *_raw.npz")
@@ -636,6 +641,14 @@ def main() -> None:
print(f"wrote {smplx_out} (refined on 2D keypoints: error {st['reproj_before_cm']:.1f} -> "
f"{st['reproj_after_cm']:.1f} cm, p95 {st['reproj_p95_before_cm']:.1f} -> "
f"{st['reproj_p95_after_cm']:.1f} cm{feet_msg})")
+ # Fingers: GVHMR estimates none. detect_hands.py zooms on each wrist of
+ # this estimate and runs a hand detector there; retarget_smplx.py reads
+ # hands2d.npz next to smplx.npz.
+ hands = smplx_out.with_name("hands2d.npz")
+ if args.person:
+ print("hand keypoints: skipped (multi-person plate)")
+ elif not hands.exists():
+ run_mediapipe("detect_hands.py", video, hands, args, ("--kp", str(raw_out)))
if args.smplx_only:
return
diff --git a/pipeline/render_review.py b/pipeline/render_review.py
index 53dd96b..9918be9 100644
--- a/pipeline/render_review.py
+++ b/pipeline/render_review.py
@@ -12,6 +12,12 @@ Writes into the clip folder, per view (front, three_quarter):
over the video: where the character and the performer
disagree, you see it. Without --legacy the bottom row
shows this clip from two angles.
+ review_details.mp4 the hands and the feet, zoomed: for each, the plate
+ and the overlay side by side, following the wrist or
+ the ankle. On a full-body plate a hand is 30-50 px;
+ this is where fingers, fists and foot placement can
+ be judged. The plate camera's view is rendered at
+ twice the plate's resolution for it.
Frames are rendered with bl_motion.py in background Blender and removed
afterwards (--keep-frames to keep them). The legacy clip, if given, is
re-keyed from its curves.json onto the same rig and rendered with the
@@ -118,6 +124,66 @@ def encode(path: Path, frames_iter, fps: int, crf: int = 26):
container.close()
+def part_tracks(kp2d: np.ndarray, min_conf: float = 0.3):
+ """Crop boxes for the detail video, per source frame: (T, 4, 3) centre
+ x, y and half-size in plate pixels for left hand, right hand, left
+ foot, right foot. Sizes are fixed per clip (from the usual forearm and
+ shin lengths on screen), centres follow the wrist (a third of a
+ forearm beyond it) and the ankle (a little below it), smoothed so the
+ view does not shake with the detector."""
+ T = kp2d.shape[0]
+ out = np.zeros((T, 4, 3))
+
+ def usual(a, b):
+ ok = (kp2d[:, a, 2] >= min_conf) & (kp2d[:, b, 2] >= min_conf)
+ d = np.linalg.norm(kp2d[:, a, :2] - kp2d[:, b, :2], axis=1)
+ return float(np.percentile(d[ok], 75)) if ok.sum() > 3 else 80.0
+
+ def track(x, conf):
+ t = np.arange(T)
+ good = conf >= min_conf
+ if good.sum() < 2:
+ return np.full(T, np.nanmean(x) if np.isfinite(x).any() else 0.0)
+ y = np.interp(t, t[good], x[good])
+ k = np.exp(-0.5 * (np.arange(-6, 7) / 2.0) ** 2)
+ return np.convolve(np.pad(y, 6, mode="edge"), k / k.sum(), mode="valid")
+
+ for k, (w, e) in enumerate(((9, 7), (10, 8))):
+ fore = usual(w, e)
+ d = kp2d[:, w, :2] - kp2d[:, e, :2]
+ c = kp2d[:, w, :2] + d / np.maximum(np.linalg.norm(d, axis=1, keepdims=True), 1e-6) * fore / 3.0
+ conf = np.minimum(kp2d[:, w, 2], kp2d[:, e, 2])
+ out[:, k, 0], out[:, k, 1], out[:, k, 2] = track(c[:, 0], conf), track(c[:, 1], conf), max(0.55 * fore, 30.0)
+ for k, (a, kn) in enumerate(((15, 13), (16, 14))):
+ shin = usual(a, kn)
+ conf = kp2d[:, a, 2]
+ out[:, 2 + k, 0] = track(kp2d[:, a, 0], conf)
+ out[:, 2 + k, 1] = track(kp2d[:, a, 1] + 0.12 * shin, conf)
+ out[:, 2 + k, 2] = max(0.42 * shin, 36.0)
+ return out
+
+
+def tag(img, text):
+ """A small caption at the bottom-left: the zoomed tiles are too small
+ for the grid's title bands."""
+ (tw, th), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.45, 1)
+ h = img.shape[0]
+ band = img[h - th - 14:h - 4, 4:tw + 16].astype(np.float32)
+ img[h - th - 14:h - 4, 4:tw + 16] = (band * 0.35 + np.array([28, 24, 22], np.float32) * 0.65).astype(np.uint8)
+ cv2.putText(img, text, (10, h - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 255, 255), 1, cv2.LINE_AA)
+ return img
+
+
+def zoom(img, cx, cy, half, size, scale=1.0):
+ """Square crop of `img` (which is `scale` times the plate's resolution)
+ around a plate-pixel box, resized to size x size; edge pixels pad it
+ where it leaves the frame."""
+ M = np.array([[size / (2 * half * scale), 0, -(cx - half) * size / (2 * half)],
+ [0, size / (2 * half * scale), -(cy - half) * size / (2 * half)]])
+ return cv2.warpAffine(img, M, (size, size), flags=cv2.INTER_AREA if scale > 1 else cv2.INTER_CUBIC,
+ borderMode=cv2.BORDER_REPLICATE)
+
+
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
ap.add_argument("--spec", required=True)
@@ -129,6 +195,7 @@ def main():
ap.add_argument("--blender")
ap.add_argument("--keep-frames", action="store_true")
ap.add_argument("--reuse-frames", action="store_true", help="skip rendering where frames exist")
+ ap.add_argument("--no-details", action="store_true", help="skip review_details.mp4")
args = ap.parse_args()
spec = json.loads(rpath(args.spec).read_text(encoding="utf-8"))
@@ -151,7 +218,17 @@ def main():
shutil.rmtree(d, ignore_errors=True)
return render(blender, scene, flag, path, view, d)
- cam = frames_of("--motion", motion, "camera", "new_camera")
+ details = not args.no_details
+ cam_scale = 2 if details else 1
+
+ def frames_cam(name):
+ d = work / name
+ if args.reuse_frames and any(d.glob("f*.png")):
+ return sorted(d.glob("f*.png"))
+ shutil.rmtree(d, ignore_errors=True)
+ return render(blender, scene, "--motion", motion, "camera", d, ("--scale", str(cam_scale)))
+
+ cam = frames_cam("new_camera")
new = {v: frames_of("--motion", motion, v, f"new_{v}") for v in views}
old = ({v: frames_of("--curves", rpath(args.legacy) / "curves.json", v, f"old_{v}") for v in views}
if args.legacy else None)
@@ -198,6 +275,39 @@ def main():
if old is None:
break # one grid already shows both views
+ if details:
+ smplx = rpath(spec.get("smplx") or (rpath(spec["landmarks"]).parent / "smplx.npz"))
+ kp2d = np.load(smplx)["kp2d"]
+ boxes = part_tracks(kp2d)
+ Z = 300 # one zoomed square
+ names = ("LEFT HAND", "RIGHT HAND", "LEFT FOOT", "RIGHT FOOT")
+
+ def detail(i):
+ j = min(kp2d.shape[0] - 1, int(round(i / fps * plate_fps)))
+ src = plate_at(i)
+ big = cv2.resize(src, None, fx=cam_scale, fy=cam_scale, interpolation=cv2.INTER_CUBIC).astype(np.float32)
+ rgba = cv2.imread(str(cam[min(i, len(cam) - 1)]), cv2.IMREAD_UNCHANGED).astype(np.float32)
+ if rgba.shape[:2] != big.shape[:2]:
+ rgba = cv2.resize(rgba, (big.shape[1], big.shape[0]))
+ a = rgba[..., 3:4] / 255.0 * 0.62
+ over = (big * (1 - a) + rgba[..., :3] * a).astype(np.uint8)
+ tiles = []
+ for k in range(4):
+ cx, cy, half = boxes[j, k]
+ pl = tag(zoom(src, cx, cy, half, Z), f"{names[k]} - video")
+ ov = tag(zoom(over, cx, cy, half, Z, cam_scale), f"{names[k]} - retarget over the video")
+ t = np.hstack([pl, ov])
+ t[:, Z - 1:Z + 1] = 24
+ tiles.append(t)
+ g = np.vstack([np.hstack([tiles[0], tiles[1]]), np.hstack([tiles[2], tiles[3]])])
+ g[Z - 2:Z + 2, :] = 24
+ g[:, 2 * Z - 2:2 * Z + 2] = 24
+ return g
+
+ out = clip / "review_details.mp4"
+ encode(out, (detail(i) for i in range(len(cam))), fps, crf=25)
+ print("wrote", out)
+
if not args.keep_frames:
shutil.rmtree(work, ignore_errors=True)
diff --git a/pipeline/retarget_smplx.py b/pipeline/retarget_smplx.py
index c2693ea..04d2be4 100644
--- a/pipeline/retarget_smplx.py
+++ b/pipeline/retarget_smplx.py
@@ -43,12 +43,15 @@ What cannot be copied, and is solved instead
removes the drift; frames with a planted foot put the lowest sole on
the floor; flight frames keep the source arc.
- foot contacts: a foot is planted when the network's contact detector
- says so, when its sole sits still on the floor, or when it does not
- move on screen. Planted segments lock flat, or pivot on the ball
- (heel raised) or briefly on the heel (toes up), with short ramps in
- and out; a real slide is left to slide.
- - hands: GVHMR estimates no fingers; they hold a relaxed curl, closing
- into the rig-validated fist inside the spec's `fists` windows.
+ says so, when its sole sits still on the floor, when it does not move
+ on screen, or when it carries the body alone (the other foot well
+ up). Planted segments lock flat, or pivot on the ball (heel raised) or
+ briefly on the heel (toes up), with short ramps in and out; a real
+ slide is left to slide, and no free foot goes under the floor.
+ - fingers: GVHMR estimates none. detect_hands.py reads each hand on a
+ zoomed crop of the plate; every finger joint bends by the measured
+ angle. Where the detector lost the hand, a relaxed curl, closing into
+ the rig-validated fist inside the spec's `fists` windows.
"""
from __future__ import annotations
@@ -218,6 +221,86 @@ def fist_quats() -> dict:
return out
+# MediaPipe hand landmark ids per finger: base (CMC / MCP) to tip
+FINGERS = {"Thumb": (1, 2, 3, 4), "Index": (5, 6, 7, 8), "Middle": (9, 10, 11, 12),
+ "Ring": (13, 14, 15, 16), "Pinky": (17, 18, 19, 20)}
+
+
+def _angle(a: np.ndarray, b: np.ndarray) -> np.ndarray:
+ c = np.sum(a * b, -1) / np.maximum(np.linalg.norm(a, axis=-1) * np.linalg.norm(b, axis=-1), 1e-9)
+ return np.arccos(np.clip(c, -1.0, 1.0))
+
+
+def hand_flexion(src: dict, rs: dict, min_score: float = 0.5):
+ """Finger joint flexion from the video, per hand (L, R): (T, 5, 3)
+ angles in radians at the destination clock — five fingers in FINGERS
+ order, three joints each (MCP, PIP, DIP; thumb CMC, MCP, IP) — and a
+ (T,) weight: how much to trust them.
+
+ Angles between consecutive bones of MediaPipe's METRIC landmarks,
+ which do not depend on the view. Confident frames only, a running
+ median over them (a hand misread for a frame is not a twitch), gaps
+ interpolated, and the weight falls to zero across gaps longer than a
+ few frames — there the spec's fists and the relaxed curl take over.
+ None without detect_hands.py output."""
+ if "hands" not in src:
+ return None
+ world = np.asarray(src["hands"]["world"], dtype=np.float64)
+ score = np.asarray(src["hands"]["score"], dtype=np.float64)
+ n = world.shape[0]
+ u = np.clip(rs["src_frame"] - 1.0, 0, n - 1)
+ out = {}
+ for k, side in enumerate(("L", "R")):
+ ok = score[:, k] >= min_score
+ idx = np.nonzero(ok)[0]
+ if len(idx) < 5:
+ continue
+ W = world[:, k]
+ ang = np.zeros((n, 5, 3))
+ for f, (a, b, c, d) in enumerate(FINGERS.values()):
+ base = W[:, a] - W[:, 0]
+ s1, s2, s3 = W[:, b] - W[:, a], W[:, c] - W[:, b], W[:, d] - W[:, c]
+ ang[:, f] = np.stack([_angle(base, s1), _angle(s1, s2), _angle(s2, s3)], 1)
+ sm = np.empty_like(ang)
+ good = ang[idx]
+ med = np.stack([np.median(good[max(0, i - 2):i + 3], axis=0) for i in range(len(idx))])
+ for f in range(5):
+ for j in range(3):
+ sm[:, f, j] = gaussian_smooth(np.interp(np.arange(n), idx, med[:, f, j]), 1.0)
+ i0 = np.floor(u).astype(int)
+ i1 = np.minimum(i0 + 1, n - 1)
+ w = (u - i0)[:, None, None]
+ weight = gaussian_smooth(ok.astype(float), 1.5)
+ out[side] = (sm[i0] * (1 - w) + sm[i1] * w, np.interp(u, np.arange(n), weight))
+ return out or None
+
+
+def finger_quats(flex: np.ndarray, fists: dict, side: str) -> dict:
+ """Local rotations of one hand's finger bones from its flexion angles
+ (T, 5, 3). Fingers bend about the axis of the rig-validated fist (the
+ bone's local +X on Mixamo skeletons), each joint capped a little past
+ the fist's own angle. The thumb's fist folds on a mix of axes: it
+ follows a single curl, its MCP and IP flexion over the fist's."""
+ pre = "Left" if side == "L" else "Right"
+ T = flex.shape[0]
+ ident = np.tile([1.0, 0.0, 0.0, 0.0], (T, 1))
+ out = {}
+ for f, name in enumerate(FINGERS):
+ fq = [np.asarray(fists[f"{pre}Hand{name}{j}"], dtype=np.float64) for j in (1, 2, 3)]
+ fq = [q / np.linalg.norm(q) for q in fq]
+ fist_ang = [2.0 * np.arccos(np.clip(q[0], -1.0, 1.0)) for q in fq]
+ if name == "Thumb":
+ curl = np.clip((flex[:, f, 1] + flex[:, f, 2]) / (fist_ang[1] + fist_ang[2]), 0.0, 1.0)
+ for j in range(3):
+ out[f"{pre}Hand{name}{j + 1}"] = SB.slerp(ident, np.tile(fq[j], (T, 1)), curl)
+ continue
+ for j in range(3):
+ axis = fq[j][1:] / max(np.linalg.norm(fq[j][1:]), 1e-9)
+ a = np.clip(flex[:, f, j], 0.0, 1.15 * fist_ang[j])
+ out[f"{pre}Hand{name}{j + 1}"] = np.concatenate([np.cos(a / 2)[:, None], np.sin(a / 2)[:, None] * axis], 1)
+ return out
+
+
def window_amount(frames_src: np.ndarray, rise, fall) -> np.ndarray:
"""0 -> 1 over `rise` (src frames), 1 -> 0 over `fall`, smooth edges."""
up = smootherstep((frames_src - rise[0]) / max(1e-6, rise[1] - rise[0]))
@@ -232,6 +315,11 @@ def load_source(path: Path) -> dict:
z = np.load(path, allow_pickle=False)
src = {k: z[k] for k in z.files}
src["foot"] = json.loads(str(src["foot"]))
+ hands = path.with_name("hands2d.npz") # detect_hands.py
+ if hands.exists():
+ h = np.load(hands)
+ if h["world"].shape[0] == src["body_pose"].shape[0]:
+ src["hands"] = {"world": h["world"], "score": h["score"]}
return src
@@ -342,40 +430,107 @@ def ramp_weights(segs, T: int, ramp: int, select=None):
return w, owner
+def median_track(p: np.ndarray, conf: np.ndarray, min_conf: float, r: int = 2) -> np.ndarray:
+ """Running median of a 2D keypoint track over its confident frames
+ (window 2r+1), NaN elsewhere: a keypoint that jumps for a frame or two
+ (MediaPipe swapping feet mid-turn, a detector glitch) does not become a
+ fast foot."""
+ n = len(p)
+ out = np.full((n, 2), np.nan)
+ good = conf >= min_conf
+ for t in np.nonzero(good)[0]:
+ a, b = max(0, t - r), min(n, t + r + 1)
+ g = good[a:b]
+ if g.sum() >= 2:
+ out[t] = np.median(p[a:b][g], axis=0)
+ return out
+
+
def still_in_image(src: dict, rs: dict, still_px: float, min_conf: float = 0.5):
"""(T, 2) bool per foot (L, R) at the destination clock: the foot does
not move ON SCREEN. The camera is static, so a foot still in the image
is still in the world — the most direct contact evidence there is, and
independent of GVHMR's contact flag and of the 3D fit (both of which
- can move a foot the video shows planted). Uses the ankle (ViTPose) and,
- when detect_feet.py ran, the heel and toe tip (MediaPipe): the median
- speed of the confident points, in pixels per second."""
+ can move a foot the video shows planted). The median speed, in pixels
+ per second, of ViTPose's ankle and — when detect_feet.py ran —
+ MediaPipe's heel and toe tip, on median-filtered tracks.
+
+ Tried and dropped: counting a foot as down when only its ball or heel
+ is still (a pivot). It merged a kung-fu stance with the next one across
+ a turn on the heel and cost leg accuracy, for no gain on the spin
+ plate, whose pivots single_support() already covers."""
fps = float(src["fps"])
kp = np.asarray(src["kp2d"], dtype=np.float64)
- pts = {"L": [kp[:, 15]], "R": [kp[:, 16]]}
- if "feet2d" in src:
- f2 = np.asarray(src["feet2d"], dtype=np.float64)
- pts["L"] += [f2[:, 0], f2[:, 1]]
- pts["R"] += [f2[:, 2], f2[:, 3]]
n = kp.shape[0]
- out = np.zeros((rs["n"], 2), dtype=bool)
u = np.clip(np.round(rs["src_frame"] - 1.0).astype(int), 0, n - 1)
- for k, side in enumerate(("L", "R")):
- speeds = []
- for p in pts[side]:
- v = np.zeros(n)
- v[1:] = np.linalg.norm(np.diff(p[:, :2], axis=0), axis=1) * fps
- ok = np.minimum(p[:, 2], np.r_[p[:1, 2], p[:-1, 2]]) >= min_conf
- speeds.append(np.where(ok, v, np.nan))
- sp = np.nanmedian(np.stack(speeds, 1), axis=1)
- sp = np.where(np.isnan(sp), np.inf, sp)
- sp = np.minimum(sp, 1e6)
- sp = gaussian_smooth(sp, 1.0)
- out[:, k] = sp[u] < still_px
+ out = np.zeros((rs["n"], 2), dtype=bool)
+
+ def speed(x):
+ v = np.full(n, np.nan)
+ v[1:] = np.linalg.norm(np.diff(x, axis=0), axis=1) * fps
+ return gaussian_smooth(np.where(np.isnan(v), 1e6, v), 1.0)
+
+ for k in range(2):
+ tracks = [kp[:, 15 + k]]
+ if "feet2d" in src:
+ f2 = np.asarray(src["feet2d"], dtype=np.float64)
+ tracks += [f2[:, 2 * k], f2[:, 2 * k + 1]]
+ speeds = [speed(median_track(t[:, :2], t[:, 2], min_conf)) for t in tracks]
+ out[:, k] = np.median(np.stack(speeds, 1), axis=1)[u] < still_px
return out
-def source_contacts(Ps, Rs, rj, foot, static, fps, opts, floor_known=False, still2d=None):
+def floor_envelope(h: np.ndarray, fps: float, half_s: float = 1.5) -> np.ndarray:
+ """The floor under a height track that drifts slowly: the lowest value
+ within +-`half_s` seconds (a jump or a kick lasts less than that)."""
+ w = max(1, int(round(half_s * fps)))
+ return np.array([h[max(0, t - w):t + w + 1].min() for t in range(len(h))])
+
+
+def single_support(local, transl, rj, foot, fps, opts, lift=None):
+ """The foot that carries the body alone, and the joints to pin for it.
+
+ One foot well above the other (a kick, a knee, a step) means the lower
+ one is standing, whatever the network's contact flag says and however
+ it slides in the world track: through the spin plate's kicks GVHMR
+ slides the support foot at up to 2 m/s while the video shows it fixed,
+ pivoting on the ball. Not when the lower foot is off the floor itself
+ (a jump): the floor is the lower envelope of the lower foot's height,
+ which follows the world track's slow vertical drift (`lift`, the
+ camera's height correction, removes most of it).
+
+ Returns (T, 4) pin weights in static_conf order (L ankle, L ball,
+ R ankle, R ball) — the ball alone when the heel is up (the foot turns
+ about it) — and (T, 2) bool per foot.
+ """
+ P, R = SB.fk_local(local, transl, rj)
+ if lift is not None:
+ P = P + np.asarray(lift, dtype=np.float64)[:, None, None] * np.array([0.0, 1.0, 0.0])
+ T = P.shape[0]
+ low, heel_h, ball_h = {}, {}, {}
+ for side, (_, _, ank, ball) in LEGS.items():
+ f = foot[side]
+
+ def att(j, pt):
+ return P[:, j] + np.einsum("tij,j->ti", R[:, j], np.asarray(pt, dtype=np.float64) - rj[j])
+ heel_h[side] = att(ank, [rj[ank, 0], f["sole_y"], rj[ank, 2]])[:, 1]
+ ball_h[side] = np.minimum(att(ank, [rj[ball, 0], f["sole_y"], rj[ball, 2]])[:, 1],
+ att(ball, [rj[ball, 0], f["sole_y"], f["toe_tip_z"]])[:, 1])
+ low[side] = np.minimum(heel_h[side], ball_h[side])
+ floor = floor_envelope(np.minimum(low["L"], low["R"]), fps)
+ swing, near = float(opts["contact_swing_height"]), float(opts["contact_support_height"])
+ weights = np.zeros((T, 4))
+ support = np.zeros((T, 2), dtype=bool)
+ for k, (side, other) in enumerate((("L", "R"), ("R", "L"))):
+ on = clean_mask((low[other] - low[side] > swing) & (low[side] - floor < near), 3, 2)
+ support[:, k] = on
+ heel_up = heel_h[side] - ball_h[side] > 0.02
+ weights[:, 2 * k] = (on & ~heel_up).astype(float)
+ weights[:, 2 * k + 1] = on.astype(float)
+ return weights, support
+
+
+def source_contacts(Ps, Rs, rj, foot, static, fps, opts, floor_known=False, still2d=None, support=None):
"""When is each foot planted? GVHMR's own contact detector, CHECKED
against the geometry.
@@ -394,6 +549,11 @@ def source_contacts(Ps, Rs, rj, foot, static, fps, opts, floor_known=False, stil
network says. On the spin plate the network's confidence stays under
0.4 for 200 frames of turning footwork with the feet on the floor.
+ A foot still on screen is planted when its sole is within
+ `contact_still_height` of the floor (the estimate can leave a planted
+ foot 6-9 cm up, the spin plate's landing), and a single-support foot
+ (`single_support`) is planted, full stop.
+
Returns per foot the heel and ball masks, plus the lowest source sole
per frame (the grounding reference).
"""
@@ -411,6 +571,10 @@ def source_contacts(Ps, Rs, rj, foot, static, fps, opts, floor_known=False, stil
"ball": attached(ank, [rj[ball, 0], f["sole_y"], rj[ball, 2]]),
"tip": attached(ball, [rj[ball, 0], f["sole_y"], f["toe_tip_z"]])}
low = np.min(np.stack([p[:, 1] for side in pts for p in pts[side].values()], 1), 1)
+ # heights above the floor for the image's evidence: the known floor, or
+ # before it is known the lower envelope of the lowest sole (the height
+ # track is camera-true, so it drifts little)
+ floor = np.zeros(len(low)) if floor_known else floor_envelope(low, fps)
def speed(x):
v = np.zeros(len(x))
@@ -437,7 +601,10 @@ def source_contacts(Ps, Rs, rj, foot, static, fps, opts, floor_known=False, stil
& (np.minimum(heel_h, ball_h) < hmax))
if still2d is not None:
# still on screen and low: planted, whatever the 3D track says
- still |= still2d[:, 0 if side == "L" else 1] & (np.minimum(heel_h, ball_h) < hmax)
+ sole = np.minimum(pts[side]["heel"][:, 1], np.minimum(pts[side]["ball"][:, 1], pts[side]["tip"][:, 1]))
+ still |= still2d[:, 0 if side == "L" else 1] & (sole - floor < float(opts["contact_still_height"]))
+ if support is not None:
+ still |= support[:, 0 if side == "L" else 1]
if floor_known:
g_h, g_v = float(opts["contact_geo_height"]), float(opts["contact_geo_speed"])
heel_c |= (pts[side]["heel"][:, 1] < g_h) & (v_heel < g_v)
@@ -475,6 +642,69 @@ def camera_drift(src, rj, rs, R_c2w, t_c2w, Ry, opts):
return np.einsum("ij,tj->ti", Ry, d)
+def foot_tilt_bias(local, rj, static, max_deg: float = 12.0, min_frames: int = 10):
+ """Per foot (L, R), the constant local rotation that lays GVHMR's foot
+ flat when it stands still: its median tilt, pitch and roll, over the
+ frames the network calls the whole foot static (ankle and ball > 0.8).
+
+ GVHMR's feet stand 0-10 degrees toes-up and rolled a few degrees on the
+ five plates, differently per foot but steadily through a take. A flat
+ lock lays the foot flat anyway; the bias then shows twice: a
+ 4-frame flap as each lock ramps in and out, and a toes-up swing foot.
+ Removing the take's own bias from every frame fixes both. Capped at
+ `max_deg`; identity with too few static frames."""
+ _, R = SB.fk_local(local, np.zeros((local.shape[0], 3)), rj)
+ out = {}
+ for side, (ank, (ca, cb)) in (("L", (7, STATIC["L"])), ("R", (8, STATIC["R"]))):
+ still = (static[:, ca] > 0.8) & (static[:, cb] > 0.8)
+ if still.sum() < min_frames:
+ out[side] = np.eye(3)
+ continue
+ up_local = np.einsum("tji,j->ti", R[still, ank], [0.0, 1.0, 0.0]) # world up, in the foot's frame
+ m = np.median(up_local, axis=0)
+ m /= np.linalg.norm(m)
+ C = SB.align_rotation(np.array([0.0, 1.0, 0.0]), m)
+ ang = np.degrees(np.arccos(np.clip(m[1], -1.0, 1.0)))
+ if ang > max_deg: # keep the direction, cap the size
+ C = SB.align_rotation(np.array([0.0, 1.0, 0.0]),
+ np.array([0.0, 1.0, 0.0]) * np.cos(np.radians(max_deg))
+ + (m - np.array([0.0, m[1], 0.0])) / max(np.linalg.norm(m - np.array([0.0, m[1], 0.0])), 1e-9)
+ * np.sin(np.radians(max_deg)))
+ out[side] = C
+ return out
+
+
+def image_anchor(src, side, frames_src, cam_pos, R_cam, K, ankle_y, min_conf=0.7, max_spread_px=12.0):
+ """Where the video puts a planted foot: the camera ray through the
+ detected ankle (ViTPose, median over the lock's frames), met by the
+ horizontal plane at the performer's ankle height above the floor, in
+ the rig's armature space. None when the ankle is not seen well or
+ moves on screen (not a planted foot to anchor).
+
+ Opt-in (`contact_image_anchor`): flat locks move across the line of
+ sight to where the plate shows the ankle, capped, and only as far as
+ the leg reaches without lowering the hips more than 1.5 cm. It puts
+ planted feet exactly on the performer's in the overlay, but a rig with
+ wider hips than the performer (the Y Bot) must then angle its legs
+ away from the video's: leg angles came out 0.3 deg better on two plates
+ and 0.2-0.6 deg worse on three."""
+ kp = np.asarray(src["kp2d"], dtype=np.float64)
+ n = kp.shape[0]
+ fr = np.unique(np.clip(np.round(np.asarray(frames_src) - 1.0).astype(int), 0, n - 1))
+ a = kp[fr, 15 if side == "L" else 16]
+ a = a[a[:, 2] >= min_conf]
+ if len(a) < 5:
+ return None
+ uv = np.median(a[:, :2], axis=0)
+ if np.median(np.linalg.norm(a[:, :2] - uv, axis=1)) > max_spread_px:
+ return None
+ ray = R_cam @ np.array([(uv[0] - K[0, 2]) / K[0, 0], (uv[1] - K[1, 2]) / K[1, 1], 1.0])
+ if ray[1] > -1e-6: # looking up: no floor down there
+ return None
+ t = (ankle_y - cam_pos[1]) / ray[1]
+ return cam_pos + t * ray if t > 0 else None
+
+
def pin_static_feet(local, transl, rj, static):
"""Keep the feet GVHMR calls static still in the world, by moving the
body instead (horizontal only; the floor solver owns height).
@@ -613,9 +843,20 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
# -- 1. source FK, heading + origin normalisation ------------------------
n_src = src["global_orient"].shape[0]
+ foot = src["foot"]
+ # GVHMR's steady foot tilt, removed from every frame (feet stand flat)
+ tilt = foot_tilt_bias(rs["local"], rj, rs["static"]) if opts.get("foot_tilt_fix", True) else {}
+ for side, C in tilt.items():
+ j = 7 if side == "L" else 8
+ rs["local"][:, j] = rs["local"][:, j] @ C
+ R_c2w, t_c2w = static_camera(src, rj, slice(0, max(2, min(n_src, int(opts["camera_fit_frames"])))))
+ use_cam = bool(opts.get("camera_root", True))
still2d = still_in_image(src, rs, float(opts["contact_still_px"]))
+ # the camera's height correction does not depend on horizontal pinning
+ lift = camera_drift(src, rj, rs, R_c2w, t_c2w, np.eye(3), opts)[:, 1] if use_cam else None
+ sup_w, support = single_support(rs["local"], rs["transl"], rj, foot, dst_fps, opts, lift)
if opts.get("pin_static_feet", True):
- pin_w = np.maximum(rs["static"][:, :4], np.repeat(still2d.astype(float), 2, axis=1))
+ pin_w = np.maximum(np.maximum(rs["static"][:, :4], np.repeat(still2d.astype(float), 2, axis=1)), sup_w)
rs["transl"] = pin_static_feet(rs["local"], rs["transl"], rj, pin_w)
Ps, Rs = SB.fk_local(rs["local"], rs["transl"], rj)
h0 = float(SB.heading(Rs[0, 0]))
@@ -623,20 +864,17 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
origin = np.array([Ps[0, 0, 0], 0.0, Ps[0, 0, 2]])
Ps = np.einsum("ij,tkj->tki", Ry, Ps - origin)
Rs = np.einsum("ij,tkjl->tkil", Ry, Rs)
- foot = src["foot"]
src_floor_rest = min(foot["L"]["sole_y"], foot["R"]["sole_y"])
static = rs["static"]
# -- 2. camera-consistent trajectory, contacts, floor ---------------------
- R_c2w, t_c2w = static_camera(src, rj, slice(0, max(2, min(n_src, int(opts["camera_fit_frames"])))))
- use_cam = bool(opts.get("camera_root", True))
d_cam = camera_drift(src, rj, rs, R_c2w, t_c2w, Ry, opts) if use_cam else np.zeros((T, 3))
# Height: the camera sees it directly (image row x depth), so GVHMR's
# vertical drift is removed continuously — it moves no foot sideways.
Ps[:, :, 1] += d_cam[:, 1:2]
# Floor: on every frame a foot is planted (network + geometry), the
# lowest sole goes on the floor; flight keeps the (camera-true) arc.
- contacts, low_src = source_contacts(Ps, Rs, rj, foot, static, dst_fps, opts, still2d=still2d)
+ contacts, low_src = source_contacts(Ps, Rs, rj, foot, static, dst_fps, opts, still2d=still2d, support=support)
grounded = contacts["L"]["any"] | contacts["R"]["any"]
if grounded.sum() >= 2:
idx = np.nonzero(grounded)[0]
@@ -646,7 +884,8 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
Ps[:, :, 1] += g[:, None]
# Second contact pass against the now-known floor: a sole on the floor
# that does not move is planted, whatever the network says.
- contacts, _ = source_contacts(Ps, Rs, rj, foot, static, dst_fps, opts, floor_known=True, still2d=still2d)
+ contacts, _ = source_contacts(Ps, Rs, rj, foot, static, dst_fps, opts, floor_known=True, still2d=still2d,
+ support=support)
contacts = bridge_contacts(contacts, Ps)
grounded = contacts["L"]["any"] | contacts["R"]["any"]
if use_cam:
@@ -677,6 +916,12 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
if spec.get("fists"):
fist_amt = window_amount(src_frames, spec["fists"]["rise_src"], spec["fists"]["fall_src"])
hand_amt = np.maximum(float(opts["hand_relaxed"]), fist_amt)
+ # fingers from the video where the hand detector saw them
+ flex = hand_flexion(src, rs) if opts.get("hand_detect", True) else None
+ seen = {}
+ for side_, (fl, wt) in (flex or {}).items():
+ for bone, q in finger_quats(fl, fists, side_).items():
+ seen[bone] = (q, wt)
P = np.empty((T, B, 3, 3))
bone_joint = {b: j for j, b in rig.joint_bone.items()}
for i in range(B):
@@ -689,6 +934,8 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
if short in fists:
fq = np.asarray(fists[short], dtype=np.float64)
qt = SB.slerp(np.tile([1.0, 0, 0, 0], (T, 1)), np.tile(fq / np.linalg.norm(fq), (T, 1)), hand_amt)
+ if short in seen:
+ qt = SB.slerp(qt, seen[short][0], seen[short][1])
local = SB.quat_to_matrix(qt)
P[:, i] = (P[:, p] if p >= 0 else np.eye(3)) @ rig.rest_rel[i] @ local
@@ -791,6 +1038,15 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
pull = np.full(T, -low.min())
root[:, 1] += pull
pos = rig.chain_positions(P, root)
+ # The plate's camera in the rig's (scaled, grounded) armature space:
+ # static, placed where frame 1 puts the body relative to it.
+ C = Ry @ (t_c2w - origin) + cam_corr[0]
+ C[1] += g[0] + d_cam[0, 1]
+ R_cam = Ry @ R_c2w # OpenCV camera axes -> normalised source world
+ cam_pos_arm = C * s
+ cam_pos_arm[1] += rig.floor + tgt_hips_h - s * src_pelvis_h + pull[0]
+ cam_pos_arm[[0, 2]] += rig.head[rig.root][[0, 2]]
+ K_src = np.asarray(src["K"], dtype=np.float64)
ramp = int(opts["contact_ramp"])
contact_report = {}
@@ -829,6 +1085,13 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
np.array([rj[ball_j, 0], f_src["sole_y"], rj[ball_j, 2]]) - rj[ank_j])}
max_drift = float(opts["contact_max_drift"])
slide_v = float(opts["contact_slide_speed"])
+ # the heading of the source foot laid flat (a foot rolled onto its
+ # edge keeps the direction it points in; its tilted axis does not)
+ R_lay = SB.align_rotation(np.einsum("tij,j->ti", Rs[:, ank_j], [0.0, 1.0, 0.0]),
+ np.tile([0.0, 1.0, 0.0], (T, 1))) @ Rs[:, ank_j]
+ lay_yaw = np.unwrap(SB.heading(R_lay))
+ yaw_s = gaussian_smooth(lay_yaw, 2.0)
+ max_turn = np.radians(float(opts["contact_max_turn_deg"]))
segs, flat, on_heel, parent = [], [], [], []
for k0, ((a_, b_), fl, oh) in enumerate(zip(segs0, flat0, heel0)):
ref = refs["ankle" if fl else "heel" if oh else "ball"][:, [0, 2]]
@@ -841,12 +1104,18 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
# follows the source there (smoothly), and only the still runs
# lock. A foot the IMAGE shows still counts as still even when
# the 3D track creeps.
- steady = (v < slide_v) | still2d[:, k]
+ steady = (v < slide_v) | still2d[:, k] | support[:, k]
for a2, b2 in runs(steady[a_:b_ + 1]):
a2, b2 = a2 + a_, b2 + a_
start, t = a2, a2
while t <= b2:
- if np.linalg.norm(ref[t] - ref[start]) > max_drift and not still2d[t, k]:
+ drifted = (np.linalg.norm(ref[t] - ref[start]) > max_drift
+ and not (still2d[t, k] or support[t, k]))
+ # A flat foot that turns (a stance pivoting on its heel
+ # or ball: the ankle barely moves) gets a new heading
+ # for each piece, instead of one heading for both stances.
+ turned = fl and abs(yaw_s[t] - yaw_s[start]) > max_turn
+ if drifted or turned:
if t - 1 - start >= 2:
segs.append((start, t - 1)); flat.append(fl); on_heel.append(oh); parent.append(k0)
start = t
@@ -864,9 +1133,6 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
ha, hb = rig.head[b_ank], rig.head[b_ball]
yaw_align = foot_yaw[side]
src_yaw = np.unwrap(SB.heading(Rs[:, ank_j]))
- R_lay = SB.align_rotation(np.einsum("tij,j->ti", Rs[:, ank_j], [0.0, 1.0, 0.0]),
- np.tile([0.0, 1.0, 0.0], (T, 1))) @ Rs[:, ank_j]
- lay_yaw = np.unwrap(SB.heading(R_lay))
seg_yaw = [float(np.median(lay_yaw[a_:b_ + 1])) for a_, b_ in segs]
flat_w, flat_owner = ramp_weights(segs, T, ramp, lambda k_: flat[k_])
toe_w, _ = ramp_weights(segs, T, ramp, lambda k_: not on_heel[k_])
@@ -884,8 +1150,11 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
# minus what the root already carries at that frame.
resid = [s * (np.asarray(deltas[side][parent[k_]])[None, :] - cam_corr[a_:b_ + 1])
for k_, (a_, b_) in enumerate(segs)]
- feet[side] = dict(b_ank=b_ank, b_ball=b_ball, b_hip=rig.joint_bone[hip_j],
- b_knee=rig.joint_bone[knee_j], segs=segs, flat=flat, on_heel=on_heel,
+ b_hip_, b_knee_ = rig.joint_bone[hip_j], rig.joint_bone[knee_j]
+ feet[side] = dict(b_ank=b_ank, b_ball=b_ball, b_hip=b_hip_, b_knee=b_knee_,
+ leg_len=float(np.linalg.norm(rig.head[b_knee_] - rig.head[b_hip_])
+ + np.linalg.norm(rig.head[b_ank] - rig.head[b_knee_])),
+ segs=segs, flat=flat, on_heel=on_heel,
ha=ha, hb=hb, w=ramp_weights(segs, T, ramp), resid=resid)
contact_report[side] = {"segments": len(segs), "flat_segments": int(sum(flat)),
"heel_pivots": int(sum(on_heel)),
@@ -902,10 +1171,48 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
ball_free = rig.attached(P, pos, f["b_ank"], [f["hb"][0], rig.floor, f["hb"][2]])
f["locks"] = []
heel_free = rig.attached(P, pos, f["b_ank"], [f["ha"][0], rig.floor, f["ha"][2]])
+ f["anchor_cm"] = []
for (a_, b_), is_flat, on_heel_, r_ in zip(f["segs"], f["flat"], f["on_heel"], f["resid"]):
ref = (ankle_free if is_flat else heel_free if on_heel_ else ball_free)[a_:b_ + 1]
lk = np.median(ref + r_, 0)
lk[1] = f["ha"][1] if is_flat else rig.floor
+ if is_flat and opts.get("contact_image_anchor", False):
+ d = image_anchor(src, side, src_frames[a_:b_ + 1], cam_pos_arm, R_cam, K_src,
+ rig.floor + s * src["foot"][side]["ankle_h"])
+ if d is not None:
+ shift = d[[0, 2]] - lk[[0, 2]]
+ # only across the line of sight: along it, the ray meets
+ # the floor at a grazing 12-19 degrees, and 1 cm of error
+ # in the ankle's height is 3-4 cm of depth
+ view = (d - cam_pos_arm)[[0, 2]]
+ view /= max(float(np.linalg.norm(view)), 1e-9)
+ shift = shift - np.dot(shift, view) * view
+ n = float(np.linalg.norm(shift))
+ cap = float(opts["contact_anchor_max"])
+ if n > cap:
+ shift *= cap / n
+ # ...and only as far as the leg reaches without lowering
+ # the hips more than 1.5 cm: the rear leg of a kung-fu bow
+ # stance is straight, and a foot set 5 cm wider there
+ # dropped the hips to their cap and bent both legs
+ hips = pos[a_:b_ + 1, f["b_hip"]]
+ L2 = (0.998 * f["leg_len"]) ** 2
+
+ def hips_drop(target):
+ v = target[None] - hips
+ h2 = v[:, 0] ** 2 + v[:, 2] ** 2
+ return float(np.max(np.maximum(-v[:, 1] - np.sqrt(np.maximum(L2 - h2, 0.0)), 0.0)))
+ base = hips_drop(lk)
+ allowed = min(base + float(opts["contact_anchor_drop"]),
+ max(base, float(opts["max_hips_drop"]) - 0.01))
+ for alpha in (1.0, 0.75, 0.5, 0.25, 0.0):
+ trial = lk.copy()
+ trial[[0, 2]] += alpha * shift
+ if hips_drop(trial) <= allowed:
+ break
+ shift = alpha * shift
+ lk[[0, 2]] += shift
+ f["anchor_cm"].append(round(float(np.linalg.norm(shift)) * 100, 1))
f["locks"].append(lk)
def ankle_targets(pos_):
@@ -949,6 +1256,15 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
root[:, 1] -= drop
pos = rig.chain_positions(P, root)
tg = ankle_targets(pos)
+ # A free foot the estimate carries under the floor (turning footwork
+ # on the spin plate, 4 cm) is lifted by its own leg, not by the body:
+ # raising the hips for it would float the other foot.
+ for side, f in feet.items():
+ ys = np.min(np.stack([rig.attached(P, pos, b, p)[:, 1] for b, p in tgt_probes[side]], 1), 1)
+ sink = np.maximum(rig.floor - ys - float(opts["sink_tolerance"]), 0.0) * (1.0 - f["w"][0])
+ if sink.any():
+ sink = gaussian_smooth(np.array([sink[max(0, t - 2):t + 3].max() for t in range(T)]), 1.0)
+ tg[side] = tg[side] + sink[:, None] * np.array([0.0, 1.0, 0.0])
for side in feet:
solve_leg(side, tg[side])
pos = rig.chain_positions(P, root)
@@ -1004,13 +1320,7 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
joints_world = to_world(pos_final)
# -- 10. the plate's camera, in the same (scaled, grounded) world --------
- # the camera stays where it was at frame 1; the body was moved onto it
- C = Ry @ (t_c2w - origin) + cam_corr[0]
- C[1] += g[0] + d_cam[0, 1]
- R_cam = Ry @ R_c2w # OpenCV camera axes -> normalised source world
- cam_pos_arm = C * s
- cam_pos_arm[1] += rig.floor + tgt_hips_h - s * src_pelvis_h + pull[0]
- cam_pos_arm[[0, 2]] += rig.head[rig.root][[0, 2]]
+ # (placed in section 7; the camera stays where it was at frame 1)
R_bl = rig.arm_rot @ R_cam @ np.diag([1.0, -1.0, -1.0]) # Blender camera looks down -Z, +Y up
cam_mw = np.eye(4)
cam_mw[:3, :3] = R_bl
@@ -1018,6 +1328,10 @@ def retarget(spec: dict, src: dict, rig: Rig, opts: dict) -> dict:
# -- 11. QA numbers ---------------------------------------------------------
qa = {"scale": round(s, 4), "frames": T, "ik_clamped_frames": ik_clamped,
+ "single_support_frames": {"L": int(support[:, 0].sum()), "R": int(support[:, 1].sum())},
+ "image_anchor_cm": {side: f.get("anchor_cm", []) for side, f in feet.items()},
+ "foot_tilt_fix_deg": {side: round(float(np.degrees(np.arccos(np.clip((np.trace(C) - 1) / 2, -1, 1)))), 1)
+ for side, C in tilt.items()},
"contact_reach_error_cm": reach_err, "hips_drop_cm": round(float(drop.max() * 100), 2),
"ground_drift_m": [round(float(g.min()), 3), round(float(g.max()), 3)],
"target_pull_m": [round(float(pull.min()), 3), round(float(pull.max()), 3)],
@@ -1091,7 +1405,10 @@ def main() -> None:
rig = Rig(json.loads(prof_path.read_text(encoding="utf-8")))
opts = {"contact_threshold": 0.5, "contact_max_speed": 0.3, "contact_max_height": 0.06,
"contact_geo_height": 0.025, "contact_geo_speed": 0.2, "contact_heel_height": 0.03,
- "contact_max_drift": 0.04, "contact_slide_speed": 0.12, "contact_still_px": 15.0, "max_hips_drop": 0.05,
+ "contact_max_drift": 0.04, "contact_slide_speed": 0.12, "contact_still_px": 15.0,
+ "contact_still_height": 0.12, "contact_swing_height": 0.15, "contact_support_height": 0.10,
+ "contact_max_turn_deg": 20.0, "sink_tolerance": 0.01, "contact_anchor_max": 0.12, "contact_anchor_drop": 0.015,
+ "max_hips_drop": 0.05,
"contact_heel_max_s": 0.5, "ground_sigma": 3.0, "contact_ramp": 4, "hand_relaxed": 0.3,
"camera_fit_frames": 12, "camera_root": True, "camera_smooth": 5.0}
opts.update(spec.get("smplx_retarget", {}))