Download scripts/bvh_to_robot_dataset.py from Tinghong-Ye22/code-GMR: direct link, hf CLI and curl.
- Browser
- Download file 5.38 kB
-
https://huggingface.co/Tinghong-Ye22/code-GMR/resolve/main/scripts/bvh_to_robot_dataset.py
- Command line
-
hf download hf://Tinghong-Ye22/code-GMR/scripts/bvh_to_robot_dataset.py
-
curl -L -o bvh_to_robot_dataset.py https://huggingface.co/Tinghong-Ye22/code-GMR/resolve/main/scripts/bvh_to_robot_dataset.py
5.38 kB
| import argparse | |
| import pathlib | |
| import os | |
| import mujoco as mj | |
| import numpy as np | |
| from tqdm import tqdm | |
| import torch | |
| import pickle | |
| from general_motion_retargeting.utils.lafan1 import load_lafan1_file | |
| from general_motion_retargeting.kinematics_model import KinematicsModel | |
| from general_motion_retargeting import GeneralMotionRetargeting as GMR | |
| from rich import print | |
| if __name__ == "__main__": | |
| HERE = pathlib.Path(__file__).parent | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--src_folder", | |
| help="Folder containing BVH motion files to load.", | |
| required=True, | |
| type=str, | |
| ) | |
| parser.add_argument( | |
| "--tgt_folder", | |
| help="Folder to save the retargeted motion files.", | |
| default="../../motion_data/LAFAN1_g1_gmr" | |
| ) | |
| parser.add_argument( | |
| "--robot", | |
| default="unitree_g1", | |
| ) | |
| parser.add_argument( | |
| "--override", | |
| default=False, | |
| action="store_true", | |
| ) | |
| parser.add_argument( | |
| "--target_fps", | |
| default=30, | |
| type=int, | |
| ) | |
| args = parser.parse_args() | |
| src_folder = args.src_folder | |
| tgt_folder = args.tgt_folder | |
| # walk over all files in src_folder | |
| for dirpath, _, filenames in os.walk(src_folder): | |
| for filename in tqdm(sorted(filenames), desc="Retargeting files"): | |
| if not filename.endswith(".bvh"): | |
| continue | |
| # get the bvh file path | |
| bvh_file_path = os.path.join(dirpath, filename) | |
| # get the target file path | |
| tgt_file_path = bvh_file_path.replace(src_folder, tgt_folder).replace(".bvh", ".pkl") | |
| if os.path.exists(tgt_file_path) and not args.override: | |
| print(f"Skipping {bvh_file_path} because {tgt_file_path} exists") | |
| continue | |
| # Load LAFAN1 trajectory | |
| try: | |
| lafan1_data_frames, actual_human_height = load_lafan1_file(bvh_file_path) | |
| src_fps = 30 # LAFAN1 data is typically 30 FPS | |
| except Exception as e: | |
| print(f"Error loading {bvh_file_path}: {e}") | |
| continue | |
| # Initialize the retargeting system | |
| retarget = GMR( | |
| src_human="bvh", | |
| tgt_robot=args.robot, | |
| actual_human_height=actual_human_height, | |
| ) | |
| model = mj.MjModel.from_xml_path(retarget.xml_file) | |
| data = mj.MjData(model) | |
| # retarget to get all qpos | |
| qpos_list = [] | |
| for curr_frame in range(len(lafan1_data_frames)): | |
| smplx_data = lafan1_data_frames[curr_frame] | |
| # Retarget till convergence | |
| qpos = retarget.retarget(smplx_data) | |
| qpos_list.append(qpos.copy()) | |
| qpos_list = np.array(qpos_list) | |
| # Initialize the forward kinematics | |
| device = "cuda:0" | |
| kinematics_model = KinematicsModel(retarget.xml_file, device=device) | |
| root_pos = qpos_list[:, :3] | |
| root_rot = qpos_list[:, 3:7] | |
| root_rot[:, [0, 1, 2, 3]] = root_rot[:, [1, 2, 3, 0]] | |
| dof_pos = qpos_list[:, 7:] | |
| num_frames = root_pos.shape[0] | |
| # obtain local body pos | |
| identity_root_pos = torch.zeros((num_frames, 3), device=device) | |
| identity_root_rot = torch.zeros((num_frames, 4), device=device) | |
| identity_root_rot[:, -1] = 1.0 | |
| local_body_pos, _ = kinematics_model.forward_kinematics( | |
| identity_root_pos, | |
| identity_root_rot, | |
| torch.from_numpy(dof_pos).to(device=device, dtype=torch.float) | |
| ) | |
| body_names = kinematics_model.body_names | |
| HEIGHT_ADJUST = False | |
| PERFRAME_ADJUST = False | |
| if HEIGHT_ADJUST: | |
| body_pos, _ = kinematics_model.forward_kinematics( | |
| torch.from_numpy(root_pos).to(device=device, dtype=torch.float), | |
| torch.from_numpy(root_rot).to(device=device, dtype=torch.float), | |
| torch.from_numpy(dof_pos).to(device=device, dtype=torch.float) | |
| ) | |
| ground_offset = 0.00 | |
| if not PERFRAME_ADJUST: | |
| lowest_height = torch.min(body_pos[..., 2]).item() | |
| root_pos[:, 2] = root_pos[:, 2] - lowest_height + ground_offset | |
| else: | |
| for i in range(root_pos.shape[0]): | |
| lowest_body_part = torch.min(body_pos[i, :, 2]) | |
| root_pos[i, 2] = root_pos[i, 2] - lowest_body_part + ground_offset | |
| motion_data = { | |
| "root_pos": root_pos, | |
| "root_rot": root_rot, | |
| "dof_pos": dof_pos, | |
| "local_body_pos": local_body_pos.detach().cpu().numpy(), | |
| "fps": src_fps, | |
| "link_body_list": body_names, | |
| } | |
| os.makedirs(os.path.dirname(tgt_file_path), exist_ok=True) | |
| with open(tgt_file_path, "wb") as f: | |
| pickle.dump(motion_data, f) | |
| print("Done. saved to ", tgt_folder) | |