Download scripts/deploy/offline_psi0_sonic_motion_tracking.py from Tinghong-Ye22/code-HFD: direct link, hf CLI and curl.
- Browser
- Download file 5.32 kB
-
https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/scripts/deploy/offline_psi0_sonic_motion_tracking.py
- Command line
-
hf download hf://Tinghong-Ye22/code-HFD/scripts/deploy/offline_psi0_sonic_motion_tracking.py
-
curl -L -o offline_psi0_sonic_motion_tracking.py https://huggingface.co/Tinghong-Ye22/code-HFD/resolve/main/scripts/deploy/offline_psi0_sonic_motion_tracking.py
5.32 kB
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import subprocess | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--run-dir", required=True) | |
| parser.add_argument("--ckpt-step", required=True) | |
| parser.add_argument("--device", default="cuda:0") | |
| parser.add_argument("--prompt", required=True) | |
| parser.add_argument("--motion-name", default="psi0_yth_v4_test") | |
| parser.add_argument("--num-output-frames", type=int, default=300) | |
| parser.add_argument("--num-inference-steps", type=int, default=8) | |
| parser.add_argument("--start-index", type=int, default=0) | |
| parser.add_argument("--sonic-repo", default="/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/GR00T-WholeBodyControl") | |
| parser.add_argument("--output-root", required=True) | |
| parser.add_argument("--run-sonic", default="1") | |
| return parser.parse_args() | |
| def run_cmd(cmd: list[str], cwd: Path) -> None: | |
| print("\n[run]") | |
| print(" ".join(cmd)) | |
| subprocess.run(cmd, cwd=str(cwd), check=True) | |
| def read_csv_array(path: Path) -> np.ndarray: | |
| if not path.exists(): | |
| raise FileNotFoundError(path) | |
| return pd.read_csv(path).to_numpy(dtype=np.float64) | |
| def validate_motion_dir(motion_dir: Path) -> None: | |
| joint_pos = read_csv_array(motion_dir / "joint_pos.csv") | |
| joint_vel = read_csv_array(motion_dir / "joint_vel.csv") | |
| body_pos = read_csv_array(motion_dir / "body_pos.csv") | |
| body_quat = read_csv_array(motion_dir / "body_quat.csv") | |
| body_lin_vel = read_csv_array(motion_dir / "body_lin_vel.csv") | |
| body_ang_vel = read_csv_array(motion_dir / "body_ang_vel.csv") | |
| frames = joint_pos.shape[0] | |
| checks = [ | |
| ("joint_pos", joint_pos, 29), | |
| ("joint_vel", joint_vel, 29), | |
| ("body_pos", body_pos, 3), | |
| ("body_quat", body_quat, 4), | |
| ("body_lin_vel", body_lin_vel, 3), | |
| ("body_ang_vel", body_ang_vel, 3), | |
| ] | |
| for name, array, width in checks: | |
| if array.shape[0] != frames or array.shape[1] != width: | |
| raise ValueError(f"{name} shape {array.shape} does not match expected ({frames}, {width})") | |
| quat_norm = np.linalg.norm(body_quat, axis=1) | |
| if not np.all(np.isfinite(joint_pos)): | |
| raise ValueError("joint_pos contains non-finite values") | |
| if not np.all(np.isfinite(body_quat)): | |
| raise ValueError("body_quat contains non-finite values") | |
| if np.max(np.abs(quat_norm - 1.0)) > 1e-2: | |
| raise ValueError("body_quat is not normalized enough") | |
| print(f"[check] SONIC reference motion OK: {motion_dir}") | |
| print(f"[check] frames={frames}, fps=50, duration={frames / 50.0:.2f}s") | |
| def main() -> None: | |
| args = parse_args() | |
| psi_repo = Path(__file__).resolve().parents[2] | |
| sonic_repo = Path(args.sonic_repo).resolve() | |
| run_tag = time.strftime("%y%m%d%H%M%S") | |
| output_root = Path(args.output_root).resolve() / run_tag | |
| motion_dir = output_root / "g1" / "csv_sonic" / "manual" / args.motion_name | |
| # <modify> 先复用仓库里已有的TextOp->36D->SONIC CSV导出逻辑,避免重新写一套动作解码。 | |
| infer_cmd = [ | |
| sys.executable, | |
| "scripts/infer/seed_text_to_motion.py", | |
| "--run-dir", | |
| args.run_dir, | |
| "--ckpt-step", | |
| args.ckpt_step, | |
| "--output-dir", | |
| str(output_root), | |
| "--prompt", | |
| args.prompt, | |
| "--prompt-motion-name", | |
| args.motion_name, | |
| "--num-output-frames", | |
| str(args.num_output_frames), | |
| "--num-inference-steps", | |
| str(args.num_inference_steps), | |
| "--start-index", | |
| str(args.start_index), | |
| "--device", | |
| args.device, | |
| ] | |
| run_cmd(infer_cmd, psi_repo) | |
| validate_motion_dir(motion_dir) | |
| meta = { | |
| "prompt": args.prompt, | |
| "run_dir": args.run_dir, | |
| "ckpt_step": args.ckpt_step, | |
| "motion_dir": str(motion_dir), | |
| "sonic_repo": str(sonic_repo), | |
| "target_fps": 50, | |
| } | |
| (output_root / "psi0_sonic_bridge_meta.json").write_text( | |
| json.dumps(meta, ensure_ascii=False, indent=2), | |
| encoding="utf-8", | |
| ) | |
| if args.run_sonic != "1": | |
| print("[done] Generated motion only. Set RUN_SONIC=\"1\" in sh to run tracking.") | |
| return | |
| sonic_python = sonic_repo / ".venv_sim" / "bin" / "python" | |
| if not sonic_python.exists(): | |
| raise FileNotFoundError(f"SONIC python not found: {sonic_python}") | |
| sonic_output = sonic_repo / "outputs" / "psi0_yth_v4_motion_tracking" / run_tag | |
| video_name = args.motion_name + ".mp4" | |
| # <modify> 这里调用GR00T-WholeBodyControl现有headless reference motion runner,实际进入SONIC motion tracking。 | |
| sonic_cmd = [ | |
| str(sonic_python), | |
| "gear_sonic/scripts/run_headless_reference_motion.py", | |
| "--motion-path", | |
| str(motion_dir), | |
| "--output-dir", | |
| str(sonic_output), | |
| "--video-name", | |
| video_name, | |
| "--video-width", | |
| "1440", | |
| "--video-height", | |
| "832", | |
| "--video-fps", | |
| "15", | |
| ] | |
| run_cmd(sonic_cmd, sonic_repo) | |
| print(f"[done] SONIC output: {sonic_output}") | |
| if __name__ == "__main__": | |
| main() | |