File size: 5,323 Bytes
c4f8e4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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()