| """ |
| Verify that wrist camera is using: |
| 1. Refined extrinsics (not measured) |
| 2. Correctly scaled intrinsics (640x360 base resolution) |
| """ |
|
|
| import sys |
| from pathlib import Path |
| sys.path.append(str(Path(__file__).parent.parent)) |
|
|
| import numpy as np |
| import tensorflow as tf |
| tf.config.set_visible_devices([], 'GPU') |
| import tensorflow_datasets as tfds |
| import datetime |
| import re |
|
|
| from utils.load_camera_calibration import CameraCalibrationLoader |
| from utils.franka_mesh_projection import FrankaMeshProjector |
|
|
|
|
| def find_closest_calibration(episode, uuid_list): |
| try: |
| recording_path = episode['episode_metadata']['recording_folderpath'].numpy().decode('utf-8') |
| match = re.search(r'/([A-Z]+)/success/(\d{4}-\d{2}-\d{2})/\w+_\w+_+\d+_(\d{2}):(\d{2}):(\d{2})_\d{4}/', recording_path) |
| if not match: |
| return None |
| lab, date, hour, minute, second = match.groups() |
| episode_time = datetime.datetime.strptime(f"{date} {hour}:{minute}:{second}", "%Y-%m-%d %H:%M:%S") |
| matching_calibs = [uuid for uuid in uuid_list if uuid.startswith(f"{lab}+") and f"+{date}-" in uuid] |
| if len(matching_calibs) == 0: |
| return None |
| best_uuid = None |
| min_time_diff = float('inf') |
| for calib_uuid in matching_calibs: |
| parts = calib_uuid.split('+') |
| if len(parts) >= 3: |
| time_str = parts[2].replace('_cameras', '') |
| match_time = re.search(r'(\d{2})h-(\d{2})m-(\d{2})s', time_str) |
| if match_time: |
| calib_hour = int(match_time.group(1)) |
| calib_min = int(match_time.group(2)) |
| calib_sec = int(match_time.group(3)) |
| calib_time = datetime.datetime.strptime( |
| f"{date} {calib_hour}:{calib_min}:{calib_sec}", |
| "%Y-%m-%d %H:%M:%S" |
| ) |
| time_diff = abs((episode_time - calib_time).total_seconds()) |
| if time_diff < min_time_diff: |
| min_time_diff = time_diff |
| best_uuid = calib_uuid |
| return best_uuid |
| except: |
| return None |
|
|
|
|
| def main(): |
| print("=" * 80) |
| print("Verifying Wrist Camera Parameters") |
| print("=" * 80) |
|
|
| calib_dir = '/root/workspace/code/wmrl/Dual-Dynamics-Models/DROID-main/vision/u/wenlongh/datasets/droid_v4/cameras' |
| calib_loader = CameraCalibrationLoader(calib_dir) |
| projector = FrankaMeshProjector(use_gui=False) |
|
|
| calib_path = Path(calib_dir) |
| uuid_list = [f.stem.replace('_cameras', '') for f in sorted(calib_path.glob("*_cameras.json"))] |
|
|
| droid_path = '/mnt/kevin/data/droid/droid/1.0.0' |
| builder = tfds.builder_from_directory(droid_path) |
| dataset = builder.as_dataset(split='train') |
|
|
| |
| for episode_idx, episode in enumerate(dataset): |
| uuid = find_closest_calibration(episode, uuid_list) |
| if uuid is None or not calib_loader.has_refined_extrinsics(uuid): |
| continue |
|
|
| print(f"\nTesting episode {episode_idx}, UUID: {uuid}") |
|
|
| |
| calib = calib_loader.load_calibration(uuid) |
| serials = [k for k in calib.keys() if k not in ['uuid', 'scene_path', 'optimization_summary', 'error_info']] |
|
|
| |
| wrist_serial = serials[1] if len(serials) > 1 else None |
| if wrist_serial is None: |
| continue |
|
|
| cam_data = calib[wrist_serial] |
|
|
| print("\n" + "=" * 80) |
| print("Direct Calibration Data (from JSON):") |
| print("=" * 80) |
|
|
| K_measured = np.array(cam_data['measured_intrinsics']) |
| E_measured = np.array(cam_data['measured_extrinsics']) |
| E_refined = np.array(cam_data['refined_extrinsics']) |
|
|
| print(f"\nWrist Camera Serial: {wrist_serial}") |
| print(f"\nMeasured Intrinsics K:") |
| print(K_measured) |
| print(f" fx={K_measured[0,0]:.2f}, fy={K_measured[1,1]:.2f}") |
| print(f" cx={K_measured[0,2]:.2f}, cy={K_measured[1,2]:.2f}") |
|
|
| print(f"\nMeasured Extrinsics E:") |
| print(E_measured) |
|
|
| print(f"\nRefined Extrinsics E:") |
| print(E_refined) |
|
|
| print("\n" + "=" * 80) |
| print("CameraCalibrationLoader.get_dual_view_params():") |
| print("=" * 80) |
|
|
| |
| dual_params = calib_loader.get_dual_view_params(uuid, param_type='refined', require_refined=True) |
| K_wrist, E_wrist = dual_params['wrist'] |
|
|
| print(f"\nRequested: param_type='refined'") |
| print(f"Received Intrinsics K:") |
| print(K_wrist) |
| print(f" fx={K_wrist[0,0]:.2f}, fy={K_wrist[1,1]:.2f}") |
| print(f" cx={K_wrist[0,2]:.2f}, cy={K_wrist[1,2]:.2f}") |
|
|
| print(f"\nReceived Extrinsics E:") |
| print(E_wrist) |
|
|
| print(f"\n✓ Using MEASURED intrinsics: {np.allclose(K_wrist, K_measured)}") |
| print(f"✓ Using REFINED extrinsics: {np.allclose(E_wrist, E_refined)}") |
| print(f"✗ Using MEASURED extrinsics: {np.allclose(E_wrist, E_measured)}") |
|
|
| print("\n" + "=" * 80) |
| print("Intrinsics Resolution Check:") |
| print("=" * 80) |
|
|
| |
| |
| cx = K_wrist[0, 2] |
| cy = K_wrist[1, 2] |
|
|
| print(f"\nIntrinsics principal point: cx={cx:.1f}, cy={cy:.1f}") |
| print(f"Expected for 640x360: cx≈320, cy≈180") |
| print(f"Expected for 640x480: cx≈320, cy≈240") |
|
|
| if abs(cy - 180) < abs(cy - 240): |
| print(f"✓ Intrinsics appear to be for 640x360 (cy={cy:.1f} closer to 180)") |
| else: |
| print(f"⚠ Intrinsics appear to be for 640x480 (cy={cy:.1f} closer to 240)") |
|
|
| print("\n" + "=" * 80) |
| print("Projection Scaling Check:") |
| print("=" * 80) |
|
|
| |
| step = next(iter(episode['steps'])) |
| img_wrist = step['observation']['wrist_image_left'].numpy() |
| img_h, img_w = img_wrist.shape[:2] |
|
|
| print(f"\nActual wrist image resolution: {img_w}x{img_h}") |
| print(f"Intrinsics base resolution (from cy): 640x{int(cy*2)}") |
|
|
| print(f"\nProjection scaling applied by FrankaMeshProjector:") |
| print(f" original_w, original_h = 640, 360 (HARDCODED in line 500)") |
| print(f" scale_x = {img_w}/640 = {img_w/640:.4f}") |
| print(f" scale_y = {img_h}/360 = {img_h/360:.4f}") |
|
|
| if img_h == 180: |
| print(f"\n✓ Scaling is CORRECT:") |
| print(f" Image is 180 high, intrinsics for 360, scale = 180/360 = 0.5") |
| else: |
| print(f"\n⚠ Check scaling:") |
| print(f" Image is {img_h} high, intrinsics for {int(cy*2)}, scale = {img_h}/{int(cy*2)}") |
|
|
| break |
|
|
| print("\n" + "=" * 80) |
| print("Summary:") |
| print("=" * 80) |
| print("1. Wrist camera uses MEASURED intrinsics (always)") |
| print("2. Wrist camera uses REFINED extrinsics (when param_type='refined')") |
| print("3. Projection scaling: 640x360 -> 320x180 (scale 0.5 on both axes)") |
| print("=" * 80) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|