| |
| """ |
| Inspect preprocessed DROID data to verify structure and visualize tracks. |
| |
| Checks: |
| 1. Data structure (keys, shapes) |
| 2. Track counts (should be 1105 for both views after reprocessing) |
| 3. Wrist mesh vertices existence |
| 4. Visualize tracked points on sample frames |
| """ |
|
|
| import numpy as np |
| import cv2 |
| import sys |
| from pathlib import Path |
| import random |
|
|
| |
| sys.path.insert(0, str(Path(__file__).parent.parent)) |
|
|
|
|
| def inspect_npz_structure(npz_path): |
| """Print detailed structure of .npz file.""" |
| data = np.load(npz_path, allow_pickle=True) |
|
|
| print(f"\n{'='*80}") |
| print(f"File: {npz_path.name}") |
| print(f"{'='*80}") |
|
|
| |
| print("\nKeys in file:") |
| for key in sorted(data.keys()): |
| print(f" {key}") |
|
|
| |
| print("\nData shapes:") |
| for key in sorted(data.keys()): |
| value = data[key] |
| if hasattr(value, 'shape'): |
| print(f" {key:40s}: {value.shape}") |
| else: |
| print(f" {key:40s}: {type(value).__name__} = {value}") |
|
|
| |
| print("\nStructure checks:") |
|
|
| |
| if 'tracks_exterior' in data: |
| tracks_ext = data['tracks_exterior'] |
| print(f" ✓ Exterior tracks: {tracks_ext.shape} (expected: [T, 1105, 2])") |
| if tracks_ext.shape[1] == 1105: |
| print(f" ✓ Correct point count (1105)") |
| else: |
| print(f" ✗ WRONG point count! Expected 1105, got {tracks_ext.shape[1]}") |
| else: |
| print(f" ✗ Missing 'tracks_exterior'") |
|
|
| |
| if 'tracks_wrist' in data: |
| tracks_wrist = data['tracks_wrist'] |
| print(f" ✓ Wrist tracks: {tracks_wrist.shape} (expected: [T, 1105, 2])") |
| if tracks_wrist.shape[1] == 1105: |
| print(f" ✓ Correct point count (1105)") |
| else: |
| print(f" ✗ WRONG point count! Expected 1105, got {tracks_wrist.shape[1]}") |
| else: |
| print(f" ✗ Missing 'tracks_wrist'") |
|
|
| |
| if 'mesh_vertices_2d_wrist_fixed' in data: |
| mesh_wrist = data['mesh_vertices_2d_wrist_fixed'] |
| print(f" ✓ Wrist fixed mesh: {mesh_wrist.shape} (expected: [T, 7, 2])") |
| else: |
| print(f" ✗ Missing 'mesh_vertices_2d_wrist_fixed'") |
|
|
| |
| if 'mesh_vertices_2d_exterior' in data: |
| mesh_ext = data['mesh_vertices_2d_exterior'] |
| print(f" ✓ Exterior mesh: {mesh_ext.shape} (expected: [T, 7, 2])") |
| else: |
| print(f" ✗ Missing 'mesh_vertices_2d_exterior'") |
|
|
| return data |
|
|
|
|
| def visualize_tracks(data, npz_path, output_dir, num_frames=5): |
| """ |
| Visualize tracked points on sample frames. |
| |
| Shows: |
| - Exterior: mesh vertices (blue) + tracked points (green) |
| - Wrist: fixed mesh (cyan) + tracked mesh (magenta) + tracked points (green) |
| """ |
| images_ext = data['images_exterior'] |
| images_wrist = data['images_wrist'] |
| tracks_ext = data['tracks_exterior'] |
| tracks_wrist = data['tracks_wrist'] |
| tracks_vis_ext = data['tracks_vis_exterior'] |
| tracks_vis_wrist = data['tracks_vis_wrist'] |
|
|
| |
| mesh_2d_ext = data.get('mesh_vertices_2d_exterior', None) |
| mesh_vis_ext = data.get('mesh_vertices_vis_exterior', None) |
| mesh_2d_wrist_fixed = data.get('mesh_vertices_2d_wrist_fixed', None) |
|
|
| T = images_ext.shape[0] |
|
|
| |
| frame_indices = np.linspace(0, T-1, min(num_frames, T), dtype=int) |
|
|
| print(f"\nVisualizing {len(frame_indices)} frames...") |
|
|
| output_dir = Path(output_dir) |
| output_dir.mkdir(parents=True, exist_ok=True) |
|
|
| for idx, t in enumerate(frame_indices): |
| |
| viz_ext = images_ext[t].copy() |
|
|
| |
| if mesh_2d_ext is not None and mesh_vis_ext is not None: |
| for i in range(7): |
| if mesh_vis_ext[t, i]: |
| pt = tuple(mesh_2d_ext[t, i].astype(int)) |
| cv2.circle(viz_ext, pt, 5, (255, 0, 0), 2) |
| cv2.putText(viz_ext, str(i), (pt[0]+6, pt[1]-6), |
| cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1) |
|
|
| |
| for i in range(tracks_ext.shape[1]): |
| if tracks_vis_ext[t, i]: |
| pt = tuple(tracks_ext[t, i].astype(int)) |
| |
| if i < 7: |
| cv2.circle(viz_ext, pt, 3, (0, 0, 255), -1) |
| else: |
| cv2.circle(viz_ext, pt, 1, (0, 255, 0), -1) |
|
|
| cv2.putText(viz_ext, f"Ext: GT mesh (blue) | Tracked mesh (red) | Others (green) | {tracks_ext.shape[1]} pts", |
| (5, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) |
|
|
| |
| viz_wrist = images_wrist[t].copy() |
|
|
| |
| for i in range(min(7, tracks_wrist.shape[1])): |
| if tracks_vis_wrist[t, i]: |
| pt = tuple(tracks_wrist[t, i].astype(int)) |
| cv2.circle(viz_wrist, pt, 3, (255, 0, 255), -1) |
|
|
| |
| if mesh_2d_wrist_fixed is not None: |
| for i in range(7): |
| pt = tuple(mesh_2d_wrist_fixed[t, i].astype(int)) |
| cv2.circle(viz_wrist, pt, 4, (255, 255, 0), 2) |
| |
| if i in [1, 2, 3, 4]: |
| cv2.putText(viz_wrist, str(i), (pt[0]+5, pt[1]-5), |
| cv2.FONT_HERSHEY_SIMPLEX, 0.25, (255, 255, 0), 1) |
|
|
| |
| for i in range(7, tracks_wrist.shape[1]): |
| if tracks_vis_wrist[t, i]: |
| pt = tuple(tracks_wrist[t, i].astype(int)) |
| cv2.circle(viz_wrist, pt, 1, (0, 255, 0), -1) |
|
|
| cv2.putText(viz_wrist, f"Wrist: Tracked mesh (magenta) | Fixed mesh (cyan) | Others (green) | {tracks_wrist.shape[1]} pts", |
| (5, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1) |
|
|
| |
| combined = np.concatenate([viz_ext, viz_wrist], axis=1) |
| cv2.putText(combined, f"Episode {data['episode_idx']} | Frame {t}/{T}", |
| (combined.shape[1]//2 - 60, 15), |
| cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
|
|
| |
| output_path = output_dir / f"{npz_path.stem}_frame_{t:03d}.png" |
| cv2.imwrite(str(output_path), combined) |
| print(f" Saved: {output_path.name}") |
|
|
|
|
| def main(): |
| import argparse |
| parser = argparse.ArgumentParser(description="Inspect preprocessed DROID data") |
| parser.add_argument('--data-dir', type=str, default='/mnt/kevin/data/droid_processed_1000pts', |
| help='Directory with preprocessed .npz files') |
| parser.add_argument('--num-samples', type=int, default=5, |
| help='Number of episodes to sample (default: 5)') |
| parser.add_argument('--num-frames', type=int, default=5, |
| help='Number of frames to visualize per episode (default: 5)') |
| parser.add_argument('--output-dir', type=str, default='/tmp/droid_inspect', |
| help='Output directory for visualizations') |
| parser.add_argument('--specific-episodes', type=int, nargs='+', default=None, |
| help='Specific episode indices to inspect (e.g., 100 200 300)') |
| args = parser.parse_args() |
|
|
| data_dir = Path(args.data_dir) / 'data' |
| output_dir = Path(args.output_dir) |
| output_dir.mkdir(parents=True, exist_ok=True) |
|
|
| |
| npz_files = sorted(data_dir.glob('episode_*.npz')) |
| print(f"Found {len(npz_files)} preprocessed episodes in {data_dir}") |
|
|
| if len(npz_files) == 0: |
| print("No .npz files found!") |
| return |
|
|
| |
| if args.specific_episodes: |
| |
| sample_files = [] |
| for ep_idx in args.specific_episodes: |
| ep_file = data_dir / f"episode_{ep_idx:06d}.npz" |
| if ep_file.exists(): |
| sample_files.append(ep_file) |
| else: |
| print(f"Warning: Episode {ep_idx} not found") |
| else: |
| |
| sample_files = random.sample(npz_files, min(args.num_samples, len(npz_files))) |
|
|
| print(f"\nInspecting {len(sample_files)} episodes...\n") |
|
|
| |
| for npz_path in sample_files: |
| try: |
| |
| data = inspect_npz_structure(npz_path) |
|
|
| |
| visualize_tracks(data, npz_path, output_dir, num_frames=args.num_frames) |
|
|
| except Exception as e: |
| print(f"\nError processing {npz_path.name}: {e}") |
| import traceback |
| traceback.print_exc() |
| continue |
|
|
| print(f"\n{'='*80}") |
| print(f"Inspection complete!") |
| print(f"Visualizations saved to: {output_dir}") |
| print(f"{'='*80}") |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|