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.
compare_reference.py: frame-by-frame refinement against the source plate
Sacha50c51d41e3f9
diff --git a/action_specs/spin.json b/action_specs/spin.jsonindex 5e93687..3c21aa4 100644--- a/action_specs/spin.json+++ b/action_specs/spin.json@@ -122,17 +122,16 @@220,241],- "arm_pose": [+ "arm_follow": [{"src": [- 138,- 206+ 140,+ 202],- "ramp_src": 6,+ "ramp_src": 4,"side": "both",- "drop_m": 0.145,- "widen_m": 0.085,- "comment": "owner pass-8: hands sat ~15 cm above the reference (head-relative) and slightly too close from the kick landing to the end. Rigid rotation about each shoulder, solved per frame to hit the drop/widen targets in metres - preserves elbow bend, wrist alignment and hand separation."+ "anchor": "shoulders",+ "comment": "owner pass-9: guard and punch arms follow the performer's own wrist geometry, shoulder-anchored (anatomically faithful). The residual few centimetres of hand-vs-face height are this character's proportions - forcing them out drives the hands into the torso."}]}\ No newline at end of filediff --git a/docs/PIPELINE.md b/docs/PIPELINE.mdindex 52cf30a..f2179b2 100644--- a/docs/PIPELINE.md+++ b/docs/PIPELINE.md@@ -138,6 +138,18 @@ Windowed correctors for systematic estimator bias inside a phase (allramped; they leave the rest of the clip untouched):```jsonc+// Follow the performer's own arm geometry for a beat: the wrist is+// placed at their wrist offset (scaled to this character) and the chain+// re-solved with exact bone lengths. This is the strongest corrector —+// it copies real human geometry, so it cannot self-intersect — and it+// needs no hand-tuned numbers. Anchor "shoulders" (default) keeps the+// anatomy honest; "head" matches hand-to-face height instead, which can+// drive hands into the torso when proportions differ. Use it for any+// beat being judged frame by frame.+"arm_follow": [+ { "src": [140, 202], "ramp_src": 4, "side": "both", "anchor": "shoulders" }+],+// Rotate a whole arm rigidly about its shoulder socket. Targets are in// METRES and the angle is solved per frame, because a fixed angle over-// or under-shoots as the arm swings. Rigid rotation preserves elbow@@ -241,7 +253,29 @@ To produce the review videos (`preview.mp4` + the side-by-sidetools\GVHMR\.venv\Scripts\python.exe pipeline\render_preview.py action_specs\<motion>.json --showcase```-## 6. QA+## 6. Compare against the reference (refinement)++```+tools\GVHMR\.venv\Scripts\python.exe pipeline\compare_reference.py --spec action_specs\<motion>.json+... --from-src 130 --to-src 210 limit to a beat+... --detail per-frame table+```++QA proves a clip is not broken; this proves it looks like the video. It+measures both on the quantities an eye reads — hand height relative to+the face, distance between the hands, hand distance from the chest,+limbs inside the torso, gaze yaw and elevation — and reports the+**source-frame windows** where they diverge, ready to become spec+entries. Run it after every pass; it catches over-corrections (a fix+that pushes a hand into the body) before a human has to.++Two measurement traps it handles, worth knowing if you extend it:+compare against a FACE proxy (mid-skull), not the head joint, which+sits at the skull base and bakes in a false offset; and report the+character's own head/shoulder proportion separately, so proportion is+never mistaken for pose error.++## 7. QA```tools\GVHMR\.venv\Scripts\python.exe pipeline\qa_clip.py --spec action_specs\<motion>.json@@ -262,7 +296,7 @@ stance widening) — check the video before "fixing" it. **Numbers canpass while the motion is wrong: always look at the stills and play theclip. The human eye is the last word.**-## 7. Iteration protocol (how 16 passes became 1–2)+## 8. Iteration protocol (how 16 passes became 1–2)- Beat sheet before any spec. A wrong limb in the spec is a wasted pass.- Human notes come as frame ranges + a region ("frames 18–50, punchingdiff --git a/pipeline/compare_reference.py b/pipeline/compare_reference.pynew file mode 100644index 0000000..48dab49--- /dev/null+++ b/pipeline/compare_reference.py@@ -0,0 +1,230 @@+"""Frame-by-frame comparison of a retargeted clip against its source+plate — the refinement stage after the first pass.++Usage:+ tools\\GVHMR\\.venv\\Scripts\\python.exe pipeline\\compare_reference.py --spec action_specs\\<motion>.json+ ... --from-src 130 --to-src 210 limit to a beat+ ... --detail per-frame table instead of windows++Why this exists: QA (`qa_clip.py`) proves a clip is not broken — no+explosions, no skate, no pops. It cannot tell you the clip does not+*look* like the video. This does, by measuring both on the quantities+an eye actually reads and reporting where they diverge:++ hand height vs the head "hands at face level instead of chest"+ hand separation "fists collide" / "fists too far apart"+ hand distance from chest "arm not tucked in"+ limb inside the torso "his arm glitches behind his back"+ gaze yaw / elevation "looking at the sky / off to the side"++The reference is scaled to the character by arm length, and every+quantity is chosen to be proportion-robust — except hand-vs-head, which+is reported alongside the character's own head/shoulder proportion+offset so pose error and proportion error are not confused.++Findings are grouped into contiguous source-frame windows, ready to be+turned into `arm_pose` / `reach` / `arm_overrides` spec entries.+"""++from __future__ import annotations++import argparse+import json+from pathlib import Path++import numpy as np++REPO = Path(__file__).resolve().parents[1]++# Flag a window when the mean |delta| over it exceeds these (metres, degrees).+TOL = {+ "hand_height": 0.06,+ "separation": 0.06,+ "chest_distance": 0.07,+ "torso_clearance": 0.005,+ "gaze_yaw": 20.0,+ "gaze_elev": 15.0,+}+MIN_RUN = 3 # source frames; shorter excursions are noise+TORSO_R = 0.16 # metres from the spine axis; closer than this is inside the body+++def rpath(p) -> Path:+ p = Path(p)+ return p if p.is_absolute() else (REPO / p)+++def unit(v):+ n = np.linalg.norm(v)+ return v / n if n > 1e-9 else np.zeros(3)+++def main() -> None:+ ap = argparse.ArgumentParser()+ ap.add_argument("--spec", required=True)+ ap.add_argument("--from-src", type=int, default=None)+ ap.add_argument("--to-src", type=int, default=None)+ ap.add_argument("--detail", action="store_true")+ ap.add_argument("--step", type=int, default=1)+ args = ap.parse_args()++ spec = json.loads(rpath(args.spec).read_text(encoding="utf-8"))+ lm = json.loads(rpath(spec["landmarks"]).read_text(encoding="utf-8"))["frames"]+ curves = json.loads((rpath(spec["clip_dir"]) / "curves.json").read_text(encoding="utf-8"))["frames"]+ profile = json.loads((REPO / "rig_profile.json").read_text(encoding="utf-8"))+ src_fps = float(spec.get("src_fps", 24))+ dst_fps = float(spec.get("dst_fps", 30))++ L = profile["lengths"]+ char_arm = L["l_arm"] + L["l_fore"]++ def ref(i, n):+ p = lm[i]["world"][n]+ return np.array([p["x"], p["y"], p["z"]])++ ref_arm = float(np.mean([+ np.linalg.norm(ref(i, "left_shoulder") - ref(i, "left_elbow"))+ + np.linalg.norm(ref(i, "left_elbow") - ref(i, "left_wrist"))+ for i in range(0, len(lm), 5)]))+ S = char_arm / max(ref_arm, 1e-6)++ def bone(f, b):+ return np.array(curves[f - 1]["bones"][b]["world_location"])++ # Character vs performer proportion (head above shoulders), so a+ # hand-height delta can be split into pose and proportion.+ ref_head = float(np.mean([+ -(ref(i, "nose")[1] - 0.5 * (ref(i, "left_shoulder")[1] + ref(i, "right_shoulder")[1]))+ for i in range(min(60, len(lm)))])) * S+ def face_z(f):+ """Height of the character's FACE. The Head bone sits at the base+ of the skull; the performer's reference point is the nose, so+ comparing against the joint bakes in a systematic offset."""+ head = bone(f, "mixamorig:Head")[2]+ try:+ return 0.5 * (head + bone(f, "mixamorig:HeadTop_End")[2])+ except KeyError:+ return head++ app_head = float(np.mean([+ face_z(f) - 0.5 * (bone(f, "mixamorig:LeftArm")[2] + bone(f, "mixamorig:RightArm")[2])+ for f in range(1, min(40, len(curves)) + 1)]))+ proportion = app_head - ref_head++ def metrics_ref(i):+ ls, rs = ref(i, "left_shoulder"), ref(i, "right_shoulder")+ lh, rh = ref(i, "left_hip"), ref(i, "right_hip")+ shm = 0.5 * (ls + rs)+ up = unit(shm - 0.5 * (lh + rh)) # y is DOWN in this frame+ left = unit((ls - rs) - up * np.dot(ls - rs, up))+ fwd = unit(np.cross(up, left))+ out = {"sep": np.linalg.norm(ref(i, "left_wrist") - ref(i, "right_wrist")) * S}+ for side, tag in (("left", "l"), ("right", "r")):+ v = ref(i, f"{side}_wrist") - shm+ e = ref(i, f"{side}_elbow") - shm+ out[f"h_{tag}"] = -(ref(i, f"{side}_wrist")[1] - ref(i, "nose")[1]) * S+ out[f"d_{tag}"] = float(np.linalg.norm(np.array([np.dot(v, left), np.dot(v, fwd)]))) * S+ out[f"b_{tag}"] = float(np.dot(v, fwd)) * S # + = in front of the chest+ g = np.array(lm[i].get("gaze", [0, 0, 0]), dtype=float)+ out["gyaw"] = float(np.degrees(np.arctan2(g[0], -g[2]))) if np.any(g) else 0.0+ out["gelev"] = float(np.degrees(np.arcsin(np.clip(-g[1], -1, 1)))) if np.any(g) else 0.0+ return out++ def metrics_app(f):+ la, ra = bone(f, "mixamorig:LeftArm"), bone(f, "mixamorig:RightArm")+ shm = 0.5 * (la + ra)+ fwd = np.array(curves[f - 1].get("body_forward", [0, -1, 0]), dtype=float)+ fwd[2] = 0.0+ fwd = unit(fwd)+ left = np.array([-fwd[1], fwd[0], 0.0])+ head_z = face_z(f)+ out = {"sep": float(np.linalg.norm(bone(f, "mixamorig:LeftHand") - bone(f, "mixamorig:RightHand")))}+ for side, tag in (("Left", "l"), ("Right", "r")):+ wrist = bone(f, f"mixamorig:{side}Hand")+ elbow = bone(f, f"mixamorig:{side}ForeArm")+ v = wrist - shm+ out[f"h_{tag}"] = float(wrist[2] - head_z)+ out[f"d_{tag}"] = float(np.linalg.norm(np.array([np.dot(v, left), np.dot(v, fwd)])))+ out[f"b_{tag}"] = float(np.dot(v, fwd))+ # Self-intersection: how far the wrist/elbow sit from the spine+ # axis. This is absolute — a limb inside the torso is a defect+ # whatever the reference does — and it is what actually catches+ # "the arm glitches behind his back".+ hips, neck = bone(f, "mixamorig:Hips"), bone(f, "mixamorig:Neck")+ axis = unit(neck - hips)+ for tag2, pt in ((f"clear_{tag}", wrist), (f"clear_e{tag}", elbow)):+ d = pt - hips+ out[tag2] = float(np.linalg.norm(d - axis * np.dot(d, axis)))+ face = np.array(curves[f - 1].get("face_dir", [0, -1, 0]), dtype=float)+ out["gyaw"] = float(np.degrees(np.arctan2(face[0], -face[1])))+ out["gelev"] = float(np.degrees(np.arcsin(np.clip(face[2], -1, 1))))+ return out++ s0 = args.from_src or 1+ s1 = args.to_src or len(lm)+ rows = []+ for sf in range(s0, min(s1, len(lm)) + 1, args.step):+ df = int(round((sf - 1) * dst_fps / src_fps + 1))+ if df < 1 or df > len(curves):+ continue+ R, A = metrics_ref(sf - 1), metrics_app(df)+ rows.append({"src": sf, "dest": df, "R": R, "A": A})++ print(f"reference scaled by {S:.3f} (arm {ref_arm:.3f} -> {char_arm:.3f} m)")+ print(f"character head sits {proportion:+.3f} m relative to the performer's, shoulder-referenced "+ f"— subtract this from hand-height deltas before calling them pose error\n")++ if args.detail:+ print(" src/dest | hand height L/R (ref->app) | separation | behind-chest L/R | gaze yaw")+ for r in rows:+ R, A = r["R"], r["A"]+ print(f" {r['src']:3d}/{r['dest']:3d} | {R['h_l']:+.2f}->{A['h_l']:+.2f} {R['h_r']:+.2f}->{A['h_r']:+.2f}"+ f" | {R['sep']:.2f}->{A['sep']:.2f} | {R['b_l']:+.2f}->{A['b_l']:+.2f} {R['b_r']:+.2f}->{A['b_r']:+.2f}"+ f" | {R['gyaw']:+6.1f}->{A['gyaw']:+6.1f}")+ print()++ # --- findings: contiguous windows where a metric is out of tolerance ---+ checks = [+ # Target: the hand sits as far below the character's head as the+ # performer's does below theirs — that is what the eye compares.+ # (The `proportion` figure printed above explains WHY matching+ # shoulder-relative geometry is not enough; it is not added here.)+ ("hands too high vs the head", "hand_height", lambda R, A: (R["h_l"] - A["h_l"] + R["h_r"] - A["h_r"]) / 2),+ ("hand separation", "separation", lambda R, A: R["sep"] - A["sep"]),+ ("left hand distance from chest", "chest_distance", lambda R, A: R["d_l"] - A["d_l"]),+ ("right hand distance from chest", "chest_distance", lambda R, A: R["d_r"] - A["d_r"]),+ ("LEFT hand inside the torso", "torso_clearance", lambda R, A: min(0.0, A["clear_l"] - TORSO_R)),+ ("RIGHT hand inside the torso", "torso_clearance", lambda R, A: min(0.0, A["clear_r"] - TORSO_R)),+ ("LEFT elbow inside the torso", "torso_clearance", lambda R, A: min(0.0, A["clear_el"] - TORSO_R)),+ ("RIGHT elbow inside the torso", "torso_clearance", lambda R, A: min(0.0, A["clear_er"] - TORSO_R)),+ ("gaze yaw", "gaze_yaw", lambda R, A: (R["gyaw"] - A["gyaw"] + 180) % 360 - 180),+ ("gaze elevation", "gaze_elev", lambda R, A: R["gelev"] - A["gelev"]),+ ]++ print("FINDINGS (windows where the retarget diverges from the reference)")+ any_found = False+ for label, tol_key, fn in checks:+ tol = TOL[tol_key]+ vals = np.array([fn(r["R"], r["A"]) for r in rows])+ bad = np.abs(vals) > tol+ i = 0+ while i < len(bad):+ if not bad[i]:+ i += 1+ continue+ j = i+ while j + 1 < len(bad) and bad[j + 1]:+ j += 1+ if (rows[j]["src"] - rows[i]["src"]) >= MIN_RUN:+ seg = vals[i:j + 1]+ k = int(np.argmax(np.abs(seg)))+ any_found = True+ print(f" src {rows[i]['src']:3d}-{rows[j]['src']:3d} (dest {rows[i]['dest']:3d}-{rows[j]['dest']:3d})"+ f" {label}: mean {seg.mean():+.3f}, worst {seg[k]:+.3f} @ src {rows[i + k]['src']}")+ i = j + 1+ if not any_found:+ print(" none — every tracked quantity is within tolerance of the reference")+++if __name__ == "__main__":+ main()diff --git a/pipeline/lift_to_mixamo.py b/pipeline/lift_to_mixamo.pyindex 1f01571..e1d4fd2 100644--- a/pipeline/lift_to_mixamo.py+++ b/pipeline/lift_to_mixamo.py@@ -361,6 +361,14 @@ def main():g = np.stack([np.array([v[0], v[2], -v[1]]) for v in g]) # mp dir -> mixamo dirgaze_mix = np.stack([unit(v) for v in g])+ # Scale between the performer and this character (arm length), used+ # by `arm_follow` to place reference-derived targets.+ _ref_arm = float(np.mean([+ np.linalg.norm(world["left_shoulder"][k] - world["left_elbow"][k])+ + np.linalg.norm(world["left_elbow"][k] - world["left_wrist"][k])+ for k in range(0, len(times), 5)]))+ ref_scale = (LEN["l_arm"] + LEN["l_fore"]) / max(_ref_arm, 1e-6)+def src2dest(sf):return (sf - 1) * dst_fps / fps + 1@@ -411,6 +419,52 @@ def main():rec[f"{s}_elbow"], rec[f"{s}_wrist"] = e, wrec[f"{s}_hand"] = w + unit(w - e) * LEN[f"{s}_hand"]+ # Windowed reference following (spec "arm_follow"): place the+ # wrist where the PERFORMER has it — their wrist offset from the+ # shoulder mid, scaled to this character — and re-solve the chain+ # with exact bone lengths. The estimator preserves directions but+ # proportion differences accumulate into the pose; for a beat the+ # owner is judging frame by frame, following the reference+ # geometry beats any hand-tuned rotation.+ for af in spec.get("arm_follow", []):+ amt = window_amount(+ f,+ [af["src"][0], af["src"][0] + af.get("ramp_src", 5)],+ [af["src"][1] - af.get("ramp_src", 5), af["src"][1]],+ src2dest,+ )+ if amt <= 1e-4:+ continue+ amt *= float(af.get("amount", 1.0))+ drop = float(af.get("drop_m", 0.0)) # extra lowering, e.g. for a head that sits lower+ # Anchor: "head" reproduces what the eye compares (hand height+ # relative to the face) and is exact even when the character's+ # head sits differently on its shoulders; "shoulders" keeps the+ # anatomical shoulder-relative geometry instead.+ if af.get("anchor", "head") == "head":+ ref_sh = mp["nose"][i]+ char_sh = np.asarray(rec["head"], float)+ else:+ ref_sh = 0.5 * (mp["left_shoulder"][i] + mp["right_shoulder"][i])+ char_sh = 0.5 * (np.asarray(rec["l_arm"], float) + np.asarray(rec["r_arm"], float))+ sides = ("l", "r") if af.get("side", "both") == "both" else (af["side"][0],)+ for s in sides:+ name = "left_wrist" if s == "l" else "right_wrist"+ target = char_sh + (mp[name][i] - ref_sh) * ref_scale+ target = target - np.asarray(rec["basis_z"], float) * drop+ sock = np.asarray(rec[f"{s}_arm"], float)+ l1, l2 = LEN[f"{s}_arm"], LEN[f"{s}_fore"]+ elbow, wrist = two_bone(sock, target, l1, l2, np.asarray(rec[f"{s}_elbow"], float))+ hand = wrist + unit(wrist - elbow) * LEN[f"{s}_hand"]+ rec[f"{s}_elbow"] = lerp(np.asarray(rec[f"{s}_elbow"], float), elbow, amt)+ rec[f"{s}_wrist"] = lerp(np.asarray(rec[f"{s}_wrist"], float), wrist, amt)+ rec[f"{s}_hand"] = lerp(np.asarray(rec[f"{s}_hand"], float), hand, amt)+ # keep exact bone lengths after the blend+ e = sock + unit(rec[f"{s}_elbow"] - sock) * l1+ w_ = e + unit(rec[f"{s}_wrist"] - e) * l2+ rec[f"{s}_elbow"], rec[f"{s}_wrist"] = e, w_+ rec[f"{s}_hand"] = w_ + unit(w_ - e) * LEN[f"{s}_hand"]+# Windowed arm-chain rotation (spec "arm_pose"): rotate a whole# arm rigidly about its shoulder socket. `pitch_deg` lowers (+)# or raises (-) the hand along an arc; `yaw_deg` swings it toward@@ -469,7 +523,9 @@ def main():a_pitch += solve(lat, -abs(drop), lambda v: float(np.dot(v, up)))a_yaw = yaw * signif abs(widen) > 1e-4:- a_yaw += solve(up, abs(widen) * sign, lambda v: float(np.dot(v, lat)))+ # widen_m is the change in the DISTANCE BETWEEN the hands;+ # each arm therefore moves half of it.+ a_yaw += solve(up, abs(widen) * 0.5 * sign, lambda v: float(np.dot(v, lat)))for k in (f"{s}_elbow", f"{s}_wrist", f"{s}_hand"):v = np.asarray(rec[k], float) - sockif abs(a_pitch) > 1e-5: