Retarget human motion from video to an Adobe Mixamo-rigged 3D character.
Python
Fix the body staying turned away after the person turns back to the camera
MaxLiu443b703be193
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex db5aa8a..6cc54e8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.## [Unreleased]+### Fixed++- A subject who turned away and back could stay facing away in the recovered 3D motion for the rest of the clip. The 2D left/right stabilizer decided "real turn or label flip" at the single transition frame (it required four consecutive side-view frames after the first turn) and never revisited the decision, so one mislabelled frame or one fast spin locked the wrong facing in. `motion2mixamorig/stabilize_kp2d.py` now cuts the clip into segments at side-view frames and takes the majority of RTMPose's labels inside each segment; only runs of the opposite sign shorter than ~0.33 s count as flips. No facial keypoints are used (hats and masks do not matter) and each person track is handled on its own. On a dance clip with a fast half-second turn, wrong-facing frames went from 320 of 642 to 3. Re-running on an existing cache re-stabilizes from the `vitpose_raw.pt` backup, not from the previous rule's output.+## [0.1.7] - 2026-10-17Tested on macOS (Apple Silicon) only; Windows and Linux are untested.diff --git a/motion2mixamorig/extract.py b/motion2mixamorig/extract.pyindex 841d9f2..dd2e564 100644--- a/motion2mixamorig/extract.py+++ b/motion2mixamorig/extract.py@@ -239,10 +239,13 @@ def _stabilize_pose_cache(pose_cache: Path) -> bool:from .stabilize_kp2d import stabilize_coco17current = torch.load(pose_cache, weights_only=False)- fixed, swaps = stabilize_coco17(current)+ # Always stabilize the estimator's own output: a cache written by an older+ # rule must not be stabilized a second time on top of its earlier swaps.+ raw_backup = pose_cache.with_name(pose_cache.stem + "_raw" + pose_cache.suffix)+ source = torch.load(raw_backup, weights_only=False) if raw_backup.exists() else current+ fixed, swaps = stabilize_coco17(source)if np.allclose(fixed, _to_numpy(current).astype(np.float32)):return False- raw_backup = pose_cache.with_name(pose_cache.stem + "_raw" + pose_cache.suffix)if not raw_backup.exists():shutil.copy2(pose_cache, raw_backup)torch.save(torch.from_numpy(fixed), pose_cache)@@ -385,9 +388,10 @@ def extract_motion(result = recover()- # RTMPose labels left/right per frame and can swap them when the subject- # turns away, which spins the recovered 3D heading. Stabilize the 2D cache- # and redo the 3D lift once if anything was corrected.+ # RTMPose labels left/right per frame and mislabels isolated frames, which+ # spins the recovered 3D heading for those frames. Stabilize the 2D cache+ # (segment majority, see stabilize_kp2d.py) and redo the 3D lift once if+ # anything was corrected.pose_cache = output_dir / "preprocess" / "vitpose.pt"if pose_cache.exists() and _stabilize_pose_cache(pose_cache):motion_cache = output_dir / "hmr4d_results.pt"diff --git a/motion2mixamorig/stabilize_kp2d.py b/motion2mixamorig/stabilize_kp2d.pyindex 19199ae..5f9f2da 100644--- a/motion2mixamorig/stabilize_kp2d.py+++ b/motion2mixamorig/stabilize_kp2d.py@@ -1,9 +1,33 @@-"""Keep COCO-17 left/right identity consistent across time.--RTMPose/ViTPose label left/right independently per frame. When the subject-turns to face away, those labels often swap. GVHMR then reads a jumping 2D-skeleton and spins the 3D heading. This assigns each left/right pair to the-previous frame's positions so anatomical identity is tracked, not re-guessed.+"""Keep COCO-17 left/right identity consistent across time (segment majority).++RTMPose / ViTPose decide left and right from appearance, one frame at a time,+with no memory. Whether the subject faces the camera or away is implied by+those labels: the left shoulder drawn on the image's right means the subject+faces the camera. Per frame the estimator is right most of the time but it+mislabels isolated frames, and one mislabelled frame makes GVHMR spin the 3D+body around for that frame.++The *facing sign* of a frame is ``sign(x_left - x_right)`` of the shoulders+(hips when the shoulders are collapsed). Two facts drive the rule:++* a real 180-degree turn passes through a side view, where the facing sign+ changes while the shoulder width is near zero ("narrow" frames);+* a label flip is isolated: a few frames with the opposite sign, surrounded by+ frames that agree with each other, at full shoulder width.++So the clip is cut into *wide segments* at the narrow frames. Inside a wide+segment the facing sign cannot genuinely change, so runs of the minority sign+shorter than ``MIN_TURN_FRAMES`` are flips and get swapped back; a longer run+is treated as a real turn that happened too fast to show a side view and is+left alone. Narrow frames between two segments that agree are forced to that+sign (a flip can happen in a near-side view as well); narrow frames between+two segments that disagree are a turn in progress and keep the estimator's+labels. Nothing here looks at the face, so hats and masks do not matter, and+every decision rests on the whole segment rather than on one transition+frame, so one bad frame cannot lock the rest of the clip into the wrong state.++Each person's track is stabilized on its own, so people in the same clip may+face different ways."""from __future__ import annotations@@ -16,6 +40,17 @@ import numpy as np# Full COCO-17 left/right permutation (nose stays).COCO17_SWAP = np.array([0, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16, 15], dtype=np.int64)+# A frame is "narrow" (side view) below this fraction of the subject's usual+# shoulder width, clamped to a pixel range so tiny or huge subjects behave.+NARROW_SCALE = 0.45+NARROW_MIN_PX = 40.0+NARROW_MAX_PX = 80.0+# Inside a wide segment, a run of the opposite facing sign at least this long+# (frames at 30 fps, ~0.33 s) is a real turn with no side view, not a flip.+MIN_TURN_FRAMES = 10+# Narrow frames narrower than this are too ambiguous to touch.+NARROW_FORCE_MIN_PX = 12.0+def _as_numpy(x) -> np.ndarray:if hasattr(x, "detach"):@@ -31,52 +66,109 @@ def _lr_x(frame: np.ndarray) -> float:return float(frame[11, 0] - frame[12, 0])-def stabilize_coco17(kp2d: np.ndarray) -> tuple[np.ndarray, np.ndarray]:- """Return (stabilized (F,17,3), per-frame swap flags (F,)).-- A real 180-degree turn drives left-right x through a narrow side view. A- label flip jumps the sign while the width stays large. Those jumps are- un-swapped so GVHMR sees one anatomical identity.-- "Narrow" is relative to how wide this subject usually reads: on a mid-spin- frame the estimator can report the shoulders 50-60 px apart instead of ~0- (one such borderline frame per turn is common), and a fixed 40 px cut then- misreads the whole genuine turn as a label flip and un-swaps it away. The- width is invariant under label swaps, so the scale can be measured on the- raw input up front.+def facing_sign(frame: np.ndarray) -> float:+ """+1 when the labels say the subject faces the camera, -1 away, 0 undecided."""+ return float(np.sign(_lr_x(frame)))+++def _runs(mask: np.ndarray) -> list[tuple[int, int]]:+ """Maximal runs of True as inclusive (start, end) pairs."""+ out: list[tuple[int, int]] = []+ i, n = 0, len(mask)+ while i < n:+ if not mask[i]:+ i += 1+ continue+ j = i+ while j < n and mask[j]:+ j += 1+ out.append((i, j - 1))+ i = j+ return out+++def _sign_runs(signs: np.ndarray) -> list[tuple[int, int, float]]:+ """Runs of equal sign as (start, end, sign)."""+ out: list[tuple[int, int, float]] = []+ i, n = 0, len(signs)+ while i < n:+ j = i+ while j < n and signs[j] == signs[i]:+ j += 1+ out.append((i, j - 1, float(signs[i])))+ i = j+ return out+++def _denoise_segment(signs: np.ndarray, min_turn: int) -> np.ndarray:+ """Flip every run shorter than `min_turn` into its neighbours, shortest first.++ Works on a copy of the wide segment's signs (all non-zero). Stops when every+ remaining run is at least `min_turn` long or the segment is one run."""+ out = signs.copy()+ while True:+ runs = _sign_runs(out)+ if len(runs) < 2:+ return out+ short = [r for r in runs if (r[1] - r[0] + 1) < min_turn]+ if not short:+ return out+ a, b, s = min(short, key=lambda r: (r[1] - r[0], r[0]))+ out[a : b + 1] = -s+++def stabilize_coco17(+ kp2d: np.ndarray, *, min_turn_frames: int = MIN_TURN_FRAMES+) -> tuple[np.ndarray, np.ndarray]:+ """Return (stabilized (F,17,3), per-frame swap flags (F,))."""src = np.asarray(_as_numpy(kp2d), dtype=np.float64)if src.ndim != 3 or src.shape[1] < 17:raise ValueError(f"expected (F, 17, 3) COCO-17, got {src.shape}")+ n = len(src)+ widths = np.array([_lr_x(f) for f in src])+ raw_sign = np.sign(widths)+ narrow_px = float(np.clip(NARROW_SCALE * np.median(np.abs(widths)), NARROW_MIN_PX, NARROW_MAX_PX))+ wide = (np.abs(widths) >= narrow_px) & (raw_sign != 0)++ target = raw_sign.copy()+ # 1) wide segments: the facing sign is constant inside, short minority runs are flips+ for a, b in _runs(wide):+ target[a : b + 1] = _denoise_segment(raw_sign[a : b + 1], min_turn_frames)+ # 2) narrow frames: forced to the sign both neighbouring segments agree on+ for a, b in _runs(~wide):+ before = target[a - 1] if a > 0 else None+ after = target[b + 1] if b + 1 < n else None+ if before is None and after is None:+ continue+ if before is not None and after is not None and before != after:+ continue # a turn in progress: keep the estimator's labels+ force = before if before is not None else after+ for i in range(a, b + 1):+ if abs(widths[i]) >= NARROW_FORCE_MIN_PX and raw_sign[i] != 0 and raw_sign[i] != force:+ target[i] = force++ swapped = (target != raw_sign) & (raw_sign != 0)out = src.copy()- n = len(out)- narrow = float(np.clip(0.45 * np.median([abs(_lr_x(f)) for f in src]), 40.0, 80.0))- swapped = np.zeros(n, dtype=np.int32)- last_sign = np.sign(_lr_x(out[0])) or 1.0- last_turn = -(10**9)+ for i in np.flatnonzero(swapped):+ out[i] = src[i][COCO17_SWAP]+ # 3) hips must agree with the shoulders within a framefor i in range(n):- raw = src[i]- width = abs(_lr_x(raw))- sign = np.sign(_lr_x(raw)) or last_sign- if width >= 40.0 and sign != last_sign:- start = max(0, last_turn + 1, i - 18)- widths = [abs(_lr_x(f)) for f in out[start:i]]- run = best = 0- for w in widths:- run = run + 1 if w < narrow else 0- best = max(best, run)- if best >= (1 if last_turn < 0 else 4):- last_sign = sign- last_turn = i- else:- raw = raw[COCO17_SWAP]- swapped[i] = 1- out[i] = rawsh = out[i, 5, 0] - out[i, 6, 0]hip = out[i, 11, 0] - out[i, 12, 0]if abs(sh) >= 40.0 and abs(hip) >= 40.0 and np.sign(sh) != np.sign(hip):out[i, [11, 12, 13, 14, 15, 16]] = out[i, [12, 11, 14, 13, 16, 15]]- return out.astype(np.float32), swapped+ return out.astype(np.float32), swapped.astype(np.int32)+++def facing_segments(kp2d: np.ndarray) -> list[tuple[int, int, str]]:+ """Human-readable (start, end, 'front'|'back'|'side') runs of a stabilized sequence."""+ src = np.asarray(_as_numpy(kp2d), dtype=np.float64)+ widths = np.array([_lr_x(f) for f in src])+ narrow_px = float(np.clip(NARROW_SCALE * np.median(np.abs(widths)), NARROW_MIN_PX, NARROW_MAX_PX))+ labels = np.where(np.abs(widths) < narrow_px, 0.0, np.sign(widths))+ names = {1.0: "front", -1.0: "back", 0.0: "side"}+ return [(a, b, names[s]) for a, b, s in _sign_runs(labels)]def stabilize_cache(path: Path) -> np.ndarray: