Download scripts/csv_to_mt.py from honglei-code/bones-seed-g1: direct link, hf CLI and curl.
- Browser
- Download file 4.12 kB
-
https://huggingface.co/datasets/honglei-code/bones-seed-g1/resolve/main/scripts/csv_to_mt.py
- Command line
-
hf download hf://datasets/honglei-code/bones-seed-g1/scripts/csv_to_mt.py
-
curl -L -o csv_to_mt.py https://huggingface.co/datasets/honglei-code/bones-seed-g1/resolve/main/scripts/csv_to_mt.py
4.12 kB
| #!/usr/bin/env python3 | |
| """Convert one BONES-SEED Unitree G1 CSV file to MT NPZ format.""" | |
| from __future__ import annotations | |
| import argparse | |
| from pathlib import Path | |
| import mujoco | |
| import numpy as np | |
| from scipy.spatial.transform import Rotation as R | |
| from converter_common import ( | |
| MT_BODY_NAMES, | |
| MT_JOINT_NAMES, | |
| ensure_output_parent, | |
| interpolate_linear, | |
| interpolate_quaternions, | |
| load_csv_motion, | |
| load_g1_model, | |
| output_frame_count, | |
| require_body_id, | |
| validate_fps, | |
| ) | |
| MIMIC_BODY_SOURCES = { | |
| "head_mimic": "torso_link", | |
| "left_hand_mimic": "left_wrist_roll_link", | |
| "right_hand_mimic": "right_wrist_roll_link", | |
| } | |
| def convert_csv_to_mt( | |
| csv_path: str | Path, | |
| output_path: str | Path, | |
| g1_xml: str | Path, | |
| source_fps: int = 120, | |
| target_fps: int = 50, | |
| verbose: bool = True, | |
| ) -> Path: | |
| """Convert a single CSV motion to MT format and return the output path.""" | |
| validate_fps(source_fps, target_fps) | |
| root_pos, root_rot_xyzw, dof_pos = load_csv_motion(csv_path) | |
| model = load_g1_model(str(Path(g1_xml).expanduser().resolve())) | |
| model_data = mujoco.MjData(model) | |
| physical_body_names = [ | |
| name for name in MT_BODY_NAMES if name not in MIMIC_BODY_SOURCES | |
| ] | |
| body_ids = { | |
| name: require_body_id(model, name) for name in physical_body_names | |
| } | |
| body_pos_w = np.zeros( | |
| (len(root_pos), len(MT_BODY_NAMES), 3), dtype=np.float32 | |
| ) | |
| for frame in range(len(root_pos)): | |
| model_data.qpos[:3] = root_pos[frame] | |
| model_data.qpos[3:7] = root_rot_xyzw[frame, [3, 0, 1, 2]] | |
| model_data.qpos[7:] = dof_pos[frame] | |
| mujoco.mj_forward(model, model_data) | |
| for body_name, body_id in body_ids.items(): | |
| output_index = MT_BODY_NAMES.index(body_name) | |
| body_pos_w[frame, output_index] = model_data.xpos[body_id] | |
| for mimic_name, source_name in MIMIC_BODY_SOURCES.items(): | |
| body_pos_w[:, MT_BODY_NAMES.index(mimic_name)] = body_pos_w[ | |
| :, MT_BODY_NAMES.index(source_name) | |
| ] | |
| root_rotation_inverse = R.from_quat(root_rot_xyzw).inv().as_matrix() | |
| local_body_pos = np.einsum( | |
| "tij,tbj->tbi", | |
| root_rotation_inverse, | |
| body_pos_w - root_pos[:, None, :], | |
| ).astype(np.float32) | |
| target_frames = output_frame_count( | |
| len(root_pos), source_fps, target_fps | |
| ) | |
| root_pos = interpolate_linear(root_pos, target_frames) | |
| root_rot_xyzw = interpolate_quaternions( | |
| root_rot_xyzw, target_frames | |
| ) | |
| dof_pos = interpolate_linear(dof_pos, target_frames) | |
| local_body_pos = interpolate_linear(local_body_pos, target_frames) | |
| output_path = ensure_output_parent(output_path) | |
| np.savez( | |
| output_path, | |
| root_pos=root_pos.astype(np.float32), | |
| root_rot=root_rot_xyzw.astype(np.float32), | |
| dof_pos=dof_pos.astype(np.float32), | |
| local_body_pos=local_body_pos.astype(np.float32), | |
| joint_names=np.asarray(MT_JOINT_NAMES), | |
| body_names=np.asarray(MT_BODY_NAMES), | |
| fps=np.asarray([target_fps], dtype=np.int64), | |
| ) | |
| if verbose: | |
| print( | |
| f"Saved MT: {output_path} " | |
| f"({len(root_pos)} frames @ {target_fps} fps)" | |
| ) | |
| return output_path | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser( | |
| description="Convert one BONES-SEED G1 CSV motion to MT NPZ" | |
| ) | |
| parser.add_argument("--input", required=True, help="Input G1 CSV file") | |
| parser.add_argument("--output", required=True, help="Output MT NPZ file") | |
| parser.add_argument( | |
| "--g1-xml", required=True, help="Compatible Unitree G1 MJCF XML" | |
| ) | |
| parser.add_argument("--source-fps", type=int, default=120) | |
| parser.add_argument("--target-fps", type=int, default=50) | |
| return parser.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| convert_csv_to_mt( | |
| csv_path=args.input, | |
| output_path=args.output, | |
| g1_xml=args.g1_xml, | |
| source_fps=args.source_fps, | |
| target_fps=args.target_fps, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |