#!/usr/bin/env python3 """ Visualize mesh GT overlays for 50 episodes to identify alignment issues. Simply shows mesh vertices overlaid on video frames for visual inspection. """ import sys from pathlib import Path sys.path.append(str(Path(__file__).parent.parent)) import numpy as np import cv2 import mediapy as media from tqdm import tqdm # Load episodes data_path = Path('/mnt/kevin/data/droid_preprocessed/data') episode_files = sorted(list(data_path.glob("episode_*.npz"))) # Use first 50 episodes split_idx = int(0.9 * len(episode_files)) train_episodes = episode_files[:split_idx] samples_to_viz = train_episodes[:50] output_dir = Path('mesh_overlay_50_episodes') output_dir.mkdir(exist_ok=True) print(f"Visualizing {len(samples_to_viz)} episodes with mesh overlays") print(f"Saving individual videos to {output_dir}") for ep_idx, npz_file in enumerate(tqdm(samples_to_viz, desc="Processing")): data = np.load(npz_file) # Load data images_ext = data['images_exterior'] # [T, 180, 320, 3] images_wrist = data['images_wrist'] # [T, 180, 320, 3] mesh_ext = data['mesh_vertices_2d_exterior'] # [T, 7, 2] mesh_vis_ext = data['mesh_vertices_vis_exterior'] # [T, 7] mesh_wrist = data['mesh_vertices_2d_wrist_fixed'] # [T, 7, 2] mesh_vis_wrist = data['mesh_vertices_vis_wrist_fixed'] # [T, 7] gripper_states = data['actions'][:, 6] # Take first 32 frames num_frames = min(32, len(images_ext)) episode_frames = [] for t in range(num_frames): # Exterior view frame_ext = images_ext[t].copy() # Draw mesh vertices - color by index for i in range(7): if mesh_vis_ext[t, i]: pt = tuple(mesh_ext[t, i].astype(int)) if i == 0: # Palm base - white cv2.circle(frame_ext, pt, 4, (255, 255, 255), 2) elif i in [1, 3]: # Finger tips - cyan cv2.circle(frame_ext, pt, 5, (255, 255, 0), 2) elif i in [2, 4]: # Finger joints - yellow cv2.circle(frame_ext, pt, 3, (0, 255, 255), -1) else: # Palm interior - green cv2.circle(frame_ext, pt, 2, (0, 255, 0), -1) # Wrist view frame_wrist = images_wrist[t].copy() # Draw mesh vertices (hardcoded 2D - always visible, no check needed) for i in range(7): pt = tuple(mesh_wrist[t, i].astype(int)) # Bounds check (should always pass for hardcoded coordinates) if 0 <= pt[0] < 320 and 0 <= pt[1] < 180: if i == 0: # Palm base - white circle cv2.circle(frame_wrist, pt, 5, (255, 255, 255), 2) elif i in [1, 3]: # Finger tips - magenta circles (LARGER & THICKER) cv2.circle(frame_wrist, pt, 7, (255, 0, 255), 3) elif i in [2, 4]: # Finger joints - orange filled cv2.circle(frame_wrist, pt, 4, (0, 165, 255), -1) else: # Palm interior - green filled cv2.circle(frame_wrist, pt, 3, (0, 255, 0), -1) # Combine side by side combined = np.concatenate([frame_ext, frame_wrist], axis=1) # Add text overlay cv2.putText(combined, f"Episode {ep_idx:03d} | Frame {t}/{num_frames} | Gripper: {gripper_states[t]:.2f}", (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.putText(combined, "Exterior (3D proj)", (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1) cv2.putText(combined, "Wrist (Fixed 2D)", (330, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1) episode_frames.append(combined) # Save individual episode video output_path = output_dir / f'episode_{ep_idx:03d}_mesh_overlay.mp4' media.write_video(str(output_path), episode_frames, fps=10) data.close() print(f"\n✓ Saved {len(samples_to_viz)} individual videos to {output_dir}") print(f"\nEach video shows:") print(f" - Side-by-side: Exterior (left) | Wrist (right)") print(f" - Mesh vertices overlaid on actual gripper") print(f" - Gripper state value per frame") print(f"\nCheck if:") print(f" 1. Exterior: Cyan circles align with actual finger tips") print(f" 2. Wrist: Magenta circles align with actual gripper position") print(f" 3. Mesh follows gripper motion as gripper state changes")