diff --git a/src/biomechanical_hcl_smart_simulation_platform/beatsvr_bimanual_eval.py b/src/biomechanical_hcl_smart_simulation_platform/beatsvr_bimanual_eval.py deleted file mode 100644 index 89c0406c10..0000000000 --- a/src/biomechanical_hcl_smart_simulation_platform/beatsvr_bimanual_eval.py +++ /dev/null @@ -1,223 +0,0 @@ -import os -import numpy as np -from stable_baselines3 import PPO -import re -import argparse -import scipy.ndimage -from collections import defaultdict -import matplotlib.pyplot as pp -import cv2 - -from uitb.utils.logger import StateLogger, ActionLogger -from uitb.simulator import Simulator - -#from uitb.bm_models.effort_models import CumulativeFatigue3CCr, ConsumedEndurance - - -def natural_sort(l): - convert = lambda text: int(text) if text.isdigit() else text.lower() - alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] - return sorted(l, key=alphanum_key) - - -### DEPRECATED, use simulator.render() instead -# def grab_pip_image(simulator): -# # Grab an image from both 'for_testing' camera and 'oculomotor' camera, and display them 'picture-in-picture' -# -# # Grab images -# img, _ = simulator._GUI_camera.render() -# -# ocular_img = None -# for module in simulator.perception.perception_modules: -# if module.modality == "vision": -# # TODO would be better to have a class function that returns "human-viewable" rendering of the observation; -# # e.g. in case the vision model has two cameras, or returns a combination of rgb + depth images etc. -# ocular_img, _ = module._camera.render() -# -# if ocular_img is not None: -# -# # Resample -# resample_factor = 2 -# resample_height = ocular_img.shape[0]*resample_factor -# resample_width = ocular_img.shape[1]*resample_factor -# resampled_img = np.zeros((resample_height, resample_width, 3), dtype=np.uint8) -# for channel in range(3): -# resampled_img[:, :, channel] = scipy.ndimage.zoom(ocular_img[:, :, channel], resample_factor, order=0) -# -# # Embed ocular image into free image -# i = simulator._GUI_camera.height - resample_height -# j = simulator._GUI_camera.width - resample_width -# img[i:, j:] = resampled_img -# -# return img - - -if __name__ == "__main__": - - parser = argparse.ArgumentParser(description='Evaluate a policy.') - parser.add_argument('simulator_folder', type=str, - help='the simulation folder') - parser.add_argument('--action_sample_freq', type=float, default=20, - help='action sample frequency (how many times per second actions are sampled from policy, default: 20)') - parser.add_argument('--checkpoint', type=str, default=None, - help='filename of a specific checkpoint (default: None, latest checkpoint is used)') - parser.add_argument('--num_episodes', type=int, default=10, - help='how many episodes are evaluated (default: 10)') - parser.add_argument('--uncloned', dest="cloned", action='store_false', help='use source code instead of files from cloned simulator module') - parser.add_argument('--app_condition', type=str, default=None, - help="can be used to override the 'condition' argument passed to a Unity app") - parser.add_argument('--record', action='store_true', help='enable recording') - parser.add_argument('--out_file', type=str, default='evaluate.mp4', - help='output file for recording if recording is enabled (default: ./evaluate.mp4)') - parser.add_argument('--logging', action='store_true', help='enable logging') - parser.add_argument('--state_log_file', default='state_log', - help='output file for state log if logging is enabled (default: ./state_log)') - parser.add_argument('--action_log_file', default='action_log', - help='output file for action log if logging is enabled (default: ./action_log)') - args = parser.parse_args() - - # Define directories - checkpoint_dir = os.path.join(args.simulator_folder, 'checkpoints') - evaluate_dir = os.path.join(args.simulator_folder, 'evaluate') - - # Make sure output dir exists - os.makedirs(evaluate_dir, exist_ok=True) - - # Override run parameters - run_params = dict() - run_params["action_sample_freq"] = args.action_sample_freq - run_params["evaluate"] = True - - run_params["unity_record_gameplay"] = args.record #False - run_params["unity_logging"] = True - run_params["unity_output_folder"] = evaluate_dir - if args.app_condition is not None: - run_params["app_args"] = ['-condition', args.app_condition] - # run_params["unity_random_seed"] = 123 - - # Embed visual observations into main mp4 or store as separate mp4 files - render_mode_perception = "separate" if run_params["unity_record_gameplay"] else "embed" - - # Use deterministic actions? - deterministic = False - - # Initialise simulator - simulator = Simulator.get(args.simulator_folder, render_mode="rgb_array_list", render_mode_perception=render_mode_perception, run_parameters=run_params, use_cloned=args.cloned) - - # ## Change effort model #TODO: delete - # simulator.bm_model._effort_model = CumulativeFatigue3CCr(simulator.bm_model, dt=simulator._run_parameters["dt"]) - - print(f"run parameters are: {simulator.run_parameters}") - - # Load latest model if filename not given - _policy_loaded = False - if args.checkpoint is not None: - model_file = args.checkpoint - _policy_loaded = True - else: - try: - files = natural_sort(os.listdir(checkpoint_dir)) - model_file = files[-1] - _policy_loaded = True - except (FileNotFoundError, IndexError): - print("No checkpoint found. Will continue evaluation with randomly sampled controls.") - - if _policy_loaded: - # Load policy TODO should create a load method for uitb.rl.BaseRLModel - print(f'Loading model: {os.path.join(checkpoint_dir, model_file)}\n') - model = PPO.load(os.path.join(checkpoint_dir, model_file)) - - # Set callbacks to match the value used for this training point (if the simulator had any) - simulator.update_callbacks(model.num_timesteps) - - if args.logging: - # Initialise log - state_logger = StateLogger(args.num_episodes, keys=simulator.get_state().keys()) - - # Actions are logged separately to make things easier - action_logger = ActionLogger(args.num_episodes) - - # Visualise evaluations - # statistics = defaultdict(list) - for episode_idx in range(args.num_episodes): - - print(f"Run episode {episode_idx + 1}/{args.num_episodes}.") - - # Reset environment - obs, info = simulator.reset() - terminated = False - truncated = False - reward = 0 - - if args.logging: - state = simulator.get_state() - state_logger.log(episode_idx, state) - - # Loop until episode ends - while not terminated and not truncated: - # #print(f"Episode {episode_idx}: {simulator.get_episode_statistics_str()}") - # print(reward) - - if _policy_loaded: - # Get actions from policy - action, _internal_policy_state = model.predict(obs, deterministic=deterministic) - else: - # choose random action from action space - action = simulator.action_space.sample() - - # Take a step - obs, r, terminated, truncated, info = simulator.step(action) - reward += r - - if args.logging: - action_logger.log(episode_idx, - {"steps": state["steps"], "timestep": state["timestep"], "action": action.copy(), - "reward": r}) - state = simulator.get_state() - state.update(info) - state_logger.log(episode_idx, state) - - print(reward) - # print(f"Episode {episode_idx}: {simulator.get_episode_statistics_str()}") - - # episode_statistics = simulator.get_episode_statistics() - # for key in episode_statistics: - # statistics[key].append(episode_statistics[key]) - - # print(f'Averages over {args.num_episodes} episodes (std in parenthesis):', - # ', '.join(['{}: {:.2f} ({:.2f})'.format(k, np.mean(v), np.std(v)) for k, v in statistics.items()])) - - if args.logging: - # Output log - state_logger.save(os.path.join(evaluate_dir, args.state_log_file)) - action_logger.save(os.path.join(evaluate_dir, args.action_log_file)) - print(f'Log files have been saved files {os.path.join(evaluate_dir, args.state_log_file)}.pickle and ' - f'{os.path.join(evaluate_dir, args.action_log_file)}.pickle') - - if args.record: - simulator._GUI_camera.write_video_set_path(os.path.join(evaluate_dir, args.out_file)) - - # Write the video - # simulator._camera.write_video(imgs, os.path.join(evaluate_dir, args.out_file)) - _imgs = simulator.render() - for _img in _imgs: - simulator._GUI_camera.write_video_add_frame(_img) - - simulator._GUI_camera.write_video_close() - print(f'A recording has been saved to file {os.path.join(evaluate_dir, args.out_file)}') - - # Write additional videos for each perception module camera (only if simulator._render_mode_perception == "separate") - if simulator._render_mode_perception == "separate": - _perception_imgs = simulator.get_render_stack_perception() - for _module_name, _imgs in _perception_imgs.items(): - _out_file = os.path.splitext(args.out_file)[0] + f"_{_module_name.replace('/', '-')}" + os.path.splitext(args.out_file)[1] - - fourcc = cv2.VideoWriter_fourcc(*'mp4v') - out = cv2.VideoWriter(os.path.join(evaluate_dir, _out_file), fourcc, simulator._GUI_camera._fps, (_imgs[0].shape[1], _imgs[0].shape[0])) - # out.open(_out_file) - for img in _imgs: - out.write(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) - out.release() - print(f'A recording has been saved to file {os.path.join(evaluate_dir, _out_file)}') - - simulator.close() diff --git a/src/biomechanical_hcl_smart_simulation_platform/main_beatsvr_bimanual_envcamera_eval.py b/src/biomechanical_hcl_smart_simulation_platform/main_beatsvr_bimanual_envcamera_eval.py new file mode 100644 index 0000000000..0852330ff9 --- /dev/null +++ b/src/biomechanical_hcl_smart_simulation_platform/main_beatsvr_bimanual_envcamera_eval.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Single entrypoint for BeatSVR bimanual evaluation visualization. + +Outputs (same rollout, two views): +- evaluate/mainCamera.mp4 (GUI view) +- evaluate/envCamera.mp4 (env/perception view) +""" + +from __future__ import annotations +import argparse +from pathlib import Path +from typing import List, Optional, Any + +import numpy as np +import imageio.v2 as imageio + + +# ---- Import Simulator (works both inside UITB repo and inside built simulator folder) ---- +try: + from uitb import Simulator # type: ignore +except Exception: + from simulator import Simulator # type: ignore + + +# ----------------------------- +# Video utilities +# ----------------------------- +def _to_rgb_u8(frame: np.ndarray) -> np.ndarray: + """Ensure uint8 RGB (H,W,3).""" + if frame.dtype != np.uint8: + frame = np.clip(frame, 0, 255).astype(np.uint8) + if frame.ndim == 2: + frame = np.stack([frame] * 3, axis=-1) + if frame.shape[-1] == 4: + frame = frame[..., :3] + return frame + + +def write_mp4(frames: List[np.ndarray], out_path: Path, fps: int) -> None: + out_path.parent.mkdir(parents=True, exist_ok=True) + writer = imageio.get_writer(str(out_path), fps=fps) + try: + for f in frames: + writer.append_data(_to_rgb_u8(f)) + finally: + writer.close() + + +# ----------------------------- +# Frame stack helpers (GUI + perception) +# ----------------------------- +def pop_gui_frames(sim) -> List[np.ndarray]: + """Fetch & clear GUI render stack (mainCamera). Requires render_mode='rgb_array_list'.""" + try: + frames = sim.get_render_stack() or [] + sim.clear_render_stack() + return list(frames) + except Exception: + return [] + + +def pop_env_frames(sim) -> List[np.ndarray]: + """ + Fetch & clear perception render stack (envCamera) when render_mode_perception='separate'. + + Some implementations return a dict: {module_name: frames}. + We default to taking the first module's frames. + """ + try: + frames = sim.get_render_stack_perception() + sim.clear_render_stack_perception() + + if frames is None: + return [] + + if isinstance(frames, dict): + for _, v in frames.items(): + return list(v or []) + return [] + + return list(frames) + except Exception: + return [] + + +# ----------------------------- +# Core: evaluation + recording (same rollout, two views) +# ----------------------------- +def run_eval_record(args: argparse.Namespace) -> None: + sim_dir = Path(args.simulator_dir).resolve() + out_dir = Path(args.out_dir) + if not out_dir.is_absolute(): + out_dir = sim_dir / out_dir + out_dir.mkdir(parents=True, exist_ok=True) + + # Enable both stacks: + # GUI frames -> get_render_stack() + # Perception frames -> get_render_stack_perception() when "separate" + sim = Simulator.get( + str(sim_dir), + render_mode="rgb_array_list", + render_mode_perception="separate", + ) + + main_frames: List[np.ndarray] = [] + env_frames: List[np.ndarray] = [] + + for ep in range(args.episodes): + obs, info = sim.reset() + + # Grab initial frames (if any) + main_frames += pop_gui_frames(sim) + env_frames += pop_env_frames(sim) + + for _ in range(args.max_steps): + # Replace this with your trained policy action if available + action = sim.action_space.sample() + + obs, reward, terminated, truncated, info = sim.step(action) + + # Collect frames produced at this step + main_frames += pop_gui_frames(sim) + env_frames += pop_env_frames(sim) + + if terminated or truncated: + break + + # Write outputs + main_mp4 = out_dir / "mainCamera.mp4" + env_mp4 = out_dir / "envCamera.mp4" + + if main_frames: + write_mp4(main_frames, main_mp4, fps=args.fps) + print(f"[OK] mainCamera saved: {main_mp4}") + else: + print("[WARN] mainCamera frames empty. GUI render stack not populated?") + + if env_frames: + write_mp4(env_frames, env_mp4, fps=args.fps) + print(f"[OK] envCamera saved: {env_mp4}") + else: + print("[WARN] envCamera frames empty. Perception separate render not populated?") + + +# ----------------------------- +# CLI: single entrypoint +# ----------------------------- +def build_parser() -> argparse.ArgumentParser: + p = argparse.ArgumentParser( + prog="beatsvr_bimanual_eval", + description="Single-entry evaluation recorder: bimanual + envCamera + mainCamera." + ) + + p.add_argument( + "--simulator_dir", + default=".", + help="Built simulator folder (default: current dir)." + ) + p.add_argument("--episodes", type=int, default=1, help="Number of episodes to record.") + p.add_argument("--max_steps", type=int, default=2000, help="Max steps per episode.") + p.add_argument("--fps", type=int, default=20, help="Output video fps.") + p.add_argument( + "--out_dir", + default="evaluate", + help="Output dir (default: /evaluate)." + ) + return p + + +def main(argv: Optional[List[str]] = None) -> int: + args = build_parser().parse_args(argv) + run_eval_record(args) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/src/biomechanical_hcl_smart_simulation_platform/main_beatsvr_bimanual_eval.py b/src/biomechanical_hcl_smart_simulation_platform/main_beatsvr_bimanual_eval.py new file mode 100644 index 0000000000..baf9cbd629 --- /dev/null +++ b/src/biomechanical_hcl_smart_simulation_platform/main_beatsvr_bimanual_eval.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +""" +beatsvr_bimanual_eval - single entrypoint + +This script is the ONLY runnable entry for: +- training +- evaluation +- bimanual + envCamera/mainCamera evaluation visualization +""" + +from __future__ import annotations +import argparse +import sys + +from pipelines.train import run_train +from pipelines.eval import run_eval +from pipelines.record_multiview import run_record +from utils.export_gifs import run_export_gifs + + +def build_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + prog="beatsvr_bimanual_eval", + description="Single entrypoint for BeatSVR bimanual evaluation and visualization." + ) + sub = parser.add_subparsers(dest="cmd", required=True) + + # ---------------- train ---------------- + p_train = sub.add_parser("train", help="Train BeatSVR bimanual policy") + p_train.add_argument("--config", required=True, help="Path to training config") + p_train.add_argument("--seed", type=int, default=0) + p_train.add_argument("--total_steps", type=int, default=1_000_000) + p_train.set_defaults(func=run_train) + + # ---------------- eval ---------------- + p_eval = sub.add_parser("eval", help="Evaluate trained policy") + p_eval.add_argument("--simulator_dir", required=True) + p_eval.add_argument("--checkpoint", default="", help="Optional model checkpoint") + p_eval.add_argument("--episodes", type=int, default=10) + p_eval.set_defaults(func=run_eval) + + # ---------------- record ---------------- + p_rec = sub.add_parser( + "record", + help="Run bimanual evaluation and record envCamera + mainCamera" + ) + p_rec.add_argument("--simulator_dir", required=True) + p_rec.add_argument("--episodes", type=int, default=1) + p_rec.add_argument("--max_steps", type=int, default=2000) + p_rec.add_argument("--fps", type=int, default=20) + p_rec.add_argument("--out_dir", default="evaluate") + p_rec.add_argument("--checkpoint", default="", help="Optional model checkpoint") + p_rec.set_defaults(func=run_record) + + # ---------------- export gif ---------------- + p_gif = sub.add_parser("export_gif", help="Convert evaluation MP4 to GIF") + p_gif.add_argument("--inputs", nargs="+", required=True) + p_gif.add_argument("--out_dir", default=".") + p_gif.add_argument("--fps", type=int, default=15) + p_gif.add_argument("--width", type=int, default=640) + p_gif.set_defaults(func=run_export_gifs) + + return parser + + +def main(argv: list[str] | None = None) -> int: + parser = build_parser() + args = parser.parse_args(argv) + args.func(args) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/biomechanical_hcl_smart_simulation_platform/uitb_bimanual_camera_showcase_eval.py b/src/biomechanical_hcl_smart_simulation_platform/uitb_bimanual_camera_showcase_eval.py deleted file mode 100644 index 6807bb8cc4..0000000000 --- a/src/biomechanical_hcl_smart_simulation_platform/uitb_bimanual_camera_showcase_eval.py +++ /dev/null @@ -1,169 +0,0 @@ -""" -UITB Bimanual Evaluation Multi-View Recorder -Record evaluation rollouts with: -- mainCamera (GUI camera): simulator.get_render_stack() -- envCamera (env/perception camera): simulator.get_render_stack_perception() - -Outputs: - - mainCamera.mp4 - - envCamera.mp4 - (optional) mainCamera.gif / envCamera.gif - -Requirements: - pip install imageio pillow imageio-ffmpeg numpy - -Example: - python test/uitb_bimanual_eval_multiview.py \ - --simulator_dir ./ \ - --episodes 1 \ - --max_steps 2000 \ - --record_gif -""" - -from __future__ import annotations -import argparse -from pathlib import Path -import numpy as np -import imageio.v2 as imageio -from PIL import Image - -# 这里按你项目的实际导入路径调整: -# - 如果你是在 UITB 主仓库里:from uitb import Simulator -# - 如果你是在 simulator 独立包里:from simulator import Simulator -try: - from uitb import Simulator # type: ignore -except Exception: - from simulator import Simulator # type: ignore - - -def write_mp4(frames: list[np.ndarray], out_path: Path, fps: int = 20) -> None: - out_path.parent.mkdir(parents=True, exist_ok=True) - w = imageio.get_writer(str(out_path), fps=fps) - try: - for f in frames: - if f.dtype != np.uint8: - f = np.clip(f, 0, 255).astype(np.uint8) - if f.ndim == 2: - f = np.stack([f] * 3, axis=-1) - if f.shape[-1] == 4: - f = f[..., :3] - w.append_data(f) - finally: - w.close() - - -def write_gif(frames: list[np.ndarray], out_path: Path, fps: int = 15, width: int = 640) -> None: - out_path.parent.mkdir(parents=True, exist_ok=True) - w = imageio.get_writer(str(out_path), mode="I", fps=fps, loop=0) - try: - for f in frames: - f = f[..., :3] if f.shape[-1] == 4 else f - im = Image.fromarray(f.astype(np.uint8)) - ow, oh = im.size - if width and ow != width: - nh = int(oh * width / ow) - im = im.resize((width, nh), resample=Image.Resampling.LANCZOS) - w.append_data(np.array(im)) - finally: - w.close() - - -def pop_all_rendered_frames(sim) -> list[np.ndarray]: - """Take and clear GUI render stack (main camera).""" - try: - frames = sim.get_render_stack() - sim.clear_render_stack() - return list(frames) if frames is not None else [] - except Exception: - return [] - - -def pop_all_perception_frames(sim) -> list[np.ndarray]: - """Take and clear perception render stack (env camera when separate).""" - try: - frames = sim.get_render_stack_perception() - sim.clear_render_stack_perception() - # 有些实现会返回 dict(按 perception module 分开),这里兼容: - if isinstance(frames, dict): - # 常见:只有一个相机模块,就拿第一个 - for _, v in frames.items(): - return list(v) if v is not None else [] - return [] - return list(frames) if frames is not None else [] - except Exception: - return [] - - -def main(): - p = argparse.ArgumentParser() - p.add_argument("--simulator_dir", required=True, help="Built simulator folder (contains simulator.py/config).") - p.add_argument("--episodes", type=int, default=1) - p.add_argument("--max_steps", type=int, default=2000) - p.add_argument("--fps", type=int, default=20, help="MP4 fps") - p.add_argument("--out_dir", default="evaluate", help="Output folder (relative or absolute).") - p.add_argument("--record_gif", action="store_true", help="Also export GIFs.") - args = p.parse_args() - - sim_dir = Path(args.simulator_dir).resolve() - out_dir = Path(args.out_dir).resolve() if Path(args.out_dir).is_absolute() else (sim_dir / args.out_dir) - out_dir.mkdir(parents=True, exist_ok=True) - - # 关键:让环境把帧存到 render stack - # - render_mode="rgb_array_list": GUI / 主相机帧会进 _render_stack - # - render_mode_perception="separate": 感知相机帧会进 _render_stack_perception - sim = Simulator.get( - str(sim_dir), - render_mode="rgb_array_list", - render_mode_perception="separate", - ) - - main_frames: list[np.ndarray] = [] - env_frames: list[np.ndarray] = [] - - for ep in range(args.episodes): - obs, info = sim.reset() - - # reset 后先把可能的首帧取出来 - main_frames += pop_all_rendered_frames(sim) - env_frames += pop_all_perception_frames(sim) - - for _ in range(args.max_steps): - # 这里用随机动作;如果你要接 PPO/SB3,把 action 换成 model.predict(obs) - action = sim.action_space.sample() - - obs, reward, terminated, truncated, info = sim.step(action) - - # 每一步把两路相机帧取出来(可能每步 0~n 帧,取决于你的实现) - main_frames += pop_all_rendered_frames(sim) - env_frames += pop_all_perception_frames(sim) - - if terminated or truncated: - break - - # 输出命名(和你之前一致) - main_mp4 = out_dir / "mainCamera.mp4" - env_mp4 = out_dir / "envCamera.mp4" - - if main_frames: - write_mp4(main_frames, main_mp4, fps=args.fps) - print(f"[OK] mainCamera video: {main_mp4}") - else: - print("[WARN] mainCamera frames empty. (GUI render stack not populated)") - - if env_frames: - write_mp4(env_frames, env_mp4, fps=args.fps) - print(f"[OK] envCamera video: {env_mp4}") - else: - print("[WARN] envCamera frames empty. (perception separate render stack not populated)") - - # 可选:导出 GIF(便于 PR/报告) - if args.record_gif: - if main_frames: - write_gif(main_frames, out_dir / "beatsvr_bimanual_eval_gui_camera.gif", fps=15, width=640) - if env_frames: - write_gif(env_frames, out_dir / "beatsvr_bimanual_eval_env_camera.gif", fps=15, width=640) - print(f"[OK] gifs saved under: {out_dir}") - - -if __name__ == "__main__": - main() diff --git a/src/biomechanical_hcl_smart_simulation_platform/uitb_bimanual_eval_multiview.py b/src/biomechanical_hcl_smart_simulation_platform/uitb_bimanual_eval_multiview.py deleted file mode 100644 index ec9179f7bc..0000000000 --- a/src/biomechanical_hcl_smart_simulation_platform/uitb_bimanual_eval_multiview.py +++ /dev/null @@ -1,83 +0,0 @@ -# utils/export_gifs.py -""" -Convert evaluation MP4 recordings to GIFs. - -Default naming rules (for BeatSVR bimanual): -- envCamera.mp4 -> beatsvr_bimanual_eval_env_camera.gif -- mainCamera.mp4 -> beatsvr_bimanual_eval_gui_camera.gif -- other.mp4 -> other.gif - -Usage: - python utils/export_gifs.py --inputs envCamera.mp4 mainCamera.mp4 --out_dir . --fps 15 --width 640 -""" - -from __future__ import annotations -import argparse -from pathlib import Path - -import numpy as np -import imageio.v2 as imageio -from PIL import Image - - -def mp4_to_gif(mp4_path: Path, gif_path: Path, target_width: int = 640, fps: int = 15) -> None: - """Convert one MP4 to GIF with fps downsampling and width resize.""" - reader = imageio.get_reader(str(mp4_path)) - meta = reader.get_meta_data() - src_fps = float(meta.get("fps", 20.0)) - - # Downsample frames to target fps - step = max(1, int(round(src_fps / float(fps)))) - - gif_path.parent.mkdir(parents=True, exist_ok=True) - writer = imageio.get_writer(str(gif_path), mode="I", fps=fps, loop=0) - - try: - for i, frame in enumerate(reader): - if i % step != 0: - continue - - im = Image.fromarray(frame) - w, h = im.size - if target_width and w != target_width: - new_h = int(h * target_width / w) - im = im.resize((target_width, new_h), resample=Image.Resampling.LANCZOS) - - writer.append_data(np.array(im)) - finally: - writer.close() - reader.close() - - -def default_output_name(mp4_name: str) -> str: - """BeatSVR bimanual-friendly output naming.""" - name = mp4_name.lower() - if name == "envcamera.mp4": - return "beatsvr_bimanual_eval_env_camera.gif" - if name == "maincamera.mp4": - return "beatsvr_bimanual_eval_gui_camera.gif" - return Path(mp4_name).with_suffix(".gif").name - - -def main() -> None: - p = argparse.ArgumentParser() - p.add_argument("--inputs", nargs="+", required=True, help="Input mp4 files (paths).") - p.add_argument("--out_dir", default=".", help="Output folder.") - p.add_argument("--fps", type=int, default=15, help="GIF FPS.") - p.add_argument("--width", type=int, default=640, help="Output GIF width (keep aspect).") - args = p.parse_args() - - out_dir = Path(args.out_dir) - - for in_path_str in args.inputs: - in_path = Path(in_path_str) - if not in_path.exists(): - raise FileNotFoundError(f"Input not found: {in_path}") - - out_gif = out_dir / default_output_name(in_path.name) - mp4_to_gif(in_path, out_gif, target_width=args.width, fps=args.fps) - print(f"[OK] {in_path} -> {out_gif}") - - -if __name__ == "__main__": - main()