| |
| """ |
| Batch data collection for tactile manipulation tasks. |
| |
| Collects episodes with randomized object placements, saves per-episode HDF5 files, |
| and generates per-episode MP4 videos with tactile overlays. |
| |
| File structure: |
| tactile_data/ |
| precision_grasp/ |
| episode_00.hdf5 |
| episode_01.hdf5 |
| ... |
| peg_insertion/ |
| episode_00.hdf5 |
| ... |
| gentle_stack/ |
| episode_00.hdf5 |
| ... |
| videos/ |
| precision_grasp/ |
| precision_grasp_ep00.mp4 |
| ... |
| |
| Usage: |
| # Collect all tasks, 50 episodes each |
| python tactile_tasks/run_collection.py --n_episodes 50 |
| |
| # Collect single task |
| python tactile_tasks/run_collection.py --task peg_insertion --n_episodes 50 |
| |
| # With visualization window (slower) |
| python tactile_tasks/run_collection.py --task gentle_stack --n_episodes 10 --visualize |
| |
| # Custom output directory |
| python tactile_tasks/run_collection.py --save_dir ./my_data --n_episodes 20 |
| |
| # Skip video generation (faster) |
| python tactile_tasks/run_collection.py --n_episodes 50 --no_video |
| """ |
|
|
| import os |
| import sys |
| import argparse |
| import time |
|
|
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
|
|
| from tactile_tasks.collect_data import collect_task_data, TASK_CONFIGS |
| from tactile_tasks.visualize_data import generate_video |
|
|
|
|
| def generate_all_episode_videos(task_dir, n_episodes, video_dir, task_name): |
| """Generate videos for all episodes in a task directory.""" |
| os.makedirs(video_dir, exist_ok=True) |
| for ep_idx in range(n_episodes): |
| data_file = os.path.join(task_dir, f"episode_{ep_idx:02d}.hdf5") |
| video_path = os.path.join(video_dir, f"{task_name}_ep{ep_idx:02d}.mp4") |
| if os.path.exists(video_path): |
| continue |
| if not os.path.exists(data_file): |
| print(f" Warning: {data_file} not found, skipping video") |
| continue |
| try: |
| generate_video(data_file, output_path=video_path) |
| except Exception as e: |
| print(f" Warning: video for episode {ep_idx} failed: {e}") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Collect tactile manipulation data") |
| parser.add_argument("--task", type=str, default="all", |
| choices=list(TASK_CONFIGS.keys()) + ["all"], |
| help="Task to collect (default: all)") |
| parser.add_argument("--n_episodes", type=int, default=50, |
| help="Number of episodes per task (default: 50)") |
| parser.add_argument("--save_dir", type=str, default="./tactile_data", |
| help="Output directory (default: ./tactile_data)") |
| parser.add_argument("--visualize", action="store_true", |
| help="Show renderer window during collection") |
| parser.add_argument("--no_video", action="store_true", |
| help="Skip video generation") |
| args = parser.parse_args() |
|
|
| tasks = list(TASK_CONFIGS.keys()) if args.task == "all" else [args.task] |
| total_start = time.time() |
|
|
| for task in tasks: |
| print(f"\n{'='*60}") |
| print(f" Collecting: {task} | {args.n_episodes} episodes") |
| print(f"{'='*60}") |
|
|
| t0 = time.time() |
| task_dir = collect_task_data( |
| task, |
| n_episodes=args.n_episodes, |
| save_dir=args.save_dir, |
| visualize=args.visualize, |
| ) |
| elapsed = time.time() - t0 |
| print(f" Collection time: {elapsed:.0f}s ({elapsed/args.n_episodes:.1f}s/ep)") |
|
|
| if not args.no_video: |
| print(f"\n Generating videos...") |
| video_dir = os.path.join(args.save_dir, "videos", task) |
| generate_all_episode_videos(task_dir, args.n_episodes, video_dir, task) |
| print(f" Videos saved to: {video_dir}/") |
|
|
| total_elapsed = time.time() - total_start |
| print(f"\n{'='*60}") |
| print(f" Done! Total time: {total_elapsed:.0f}s") |
| print(f" Data saved to: {args.save_dir}/") |
| print(f"{'='*60}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|