File size: 5,095 Bytes
9fc9873 | 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 | from __future__ import annotations
import argparse
import json
import subprocess
from pathlib import Path
import numpy as np
import pandas as pd
from tqdm import tqdm
from psi.data.seed.motion_repr import build_seed_textop_action
def load_motion_array(motion_dir: Path) -> np.ndarray:
joint = pd.read_csv(motion_dir / "joint_pos.csv").to_numpy(dtype=np.float32)
body_pos = pd.read_csv(motion_dir / "body_pos.csv").to_numpy(dtype=np.float32)
body_quat = pd.read_csv(motion_dir / "body_quat.csv").to_numpy(dtype=np.float32)
length = min(joint.shape[0], body_pos.shape[0], body_quat.shape[0])
return np.concatenate([joint[:length], body_pos[:length], body_quat[:length]], axis=1).astype(np.float32)
def count_csv_rows(path: Path) -> int:
with path.open("r", encoding="utf-8", errors="ignore") as f:
return max(0, sum(1 for _ in f) - 1)
def has_audio_stream(video_path: Path) -> bool:
# <modify_audio_probe_stats> 统计文件与训练集保持一致:没有音频流的视频对应motion不参与统计。
cmd = [
"ffprobe",
"-v",
"error",
"-select_streams",
"a:0",
"-show_entries",
"stream=index",
"-of",
"csv=p=0",
str(video_path),
]
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
return proc.returncode == 0 and bool(proc.stdout.strip())
def stats_from_arrays(array: np.ndarray) -> dict[str, list[float]]:
return {
"mean": np.mean(array, axis=0).tolist(),
"std": np.std(array, axis=0).tolist(),
"min": np.min(array, axis=0).tolist(),
"max": np.max(array, axis=0).tolist(),
"q01": np.quantile(array, 0.01, axis=0).tolist(),
"q99": np.quantile(array, 0.99, axis=0).tolist(),
}
def main() -> None:
parser = argparse.ArgumentParser(description="Calculate 70D TextOp stats for dance audio->motion data.")
parser.add_argument("--motion-root", type=str, required=True)
parser.add_argument("--video-root", type=str, default="")
parser.add_argument("--output-path", type=str, required=True)
parser.add_argument("--action-chunk-size", type=int, default=30)
parser.add_argument("--num-past-frames", type=int, default=0)
args = parser.parse_args()
motion_root = Path(args.motion_root)
video_root = Path(args.video_root) if args.video_root else None
motion_dirs = sorted([p for p in motion_root.iterdir() if p.is_dir()])
if not motion_dirs:
raise RuntimeError(f"No motion dirs found in {motion_root}")
actions = []
states = []
valid_motion_dirs = []
skipped_no_audio = 0
for motion_dir in tqdm(motion_dirs, desc="Collecting dance TextOp stats"):
# <modify_audio_probe_stats> 如果提供video-root,则只统计确实有音频输入的motion。
if video_root is not None:
video_path = video_root / f"{motion_dir.name}.mp4"
if not video_path.exists():
raise FileNotFoundError(f"Matched video file does not exist: {video_path}")
if not has_audio_stream(video_path):
skipped_no_audio += 1
continue
joint_path = motion_dir / "joint_pos.csv"
if not joint_path.exists() or count_csv_rows(joint_path) <= 1:
continue
motion = load_motion_array(motion_dir)
if motion.shape[0] <= 1:
continue
actions.append(build_seed_textop_action(motion[1:], motion[:1]))
states.append(motion)
valid_motion_dirs.append(motion_dir)
if not actions:
raise RuntimeError(f"No valid motion rows found in {motion_root}")
action_array = np.concatenate(actions, axis=0).astype(np.float32)
state_array = np.concatenate(states, axis=0).astype(np.float32)
stats = {
"action": stats_from_arrays(action_array),
"states": stats_from_arrays(state_array),
"meta": {
"dataset": "psi0_dance_gvhmr_audio",
"representation": "joint_abs29_joint_delta29_root_delta_local3_root_rot6d_delta6_root_height1_foot_contact2",
"stat_mode": "frame_level_motion_root_audio_filtered" if video_root is not None else "frame_level_motion_root",
"motion_root": str(motion_root),
"video_root": str(video_root) if video_root is not None else "",
"skipped_no_audio": int(skipped_no_audio),
"action_dim": 70,
"state_dim": 36,
"action_chunk_size": int(args.action_chunk_size),
"num_past_frames": int(args.num_past_frames),
"num_motions": int(len(valid_motion_dirs)),
"num_action_rows": int(action_array.shape[0]),
"num_state_rows": int(state_array.shape[0]),
},
}
output_path = Path(args.output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(json.dumps(stats, indent=2), encoding="utf-8")
print(f"Wrote {output_path}")
print(json.dumps(stats["meta"], indent=2))
if __name__ == "__main__":
main()
|