| """ |
| Visualize GT depth - both reverse and non-reverse versions |
| Must match exactly with visualize_depth.py sample selection |
| """ |
| import os |
| import sys |
| import numpy as np |
| from PIL import Image |
| import matplotlib.pyplot as plt |
| import matplotlib |
| matplotlib.use('Agg') |
| from tqdm import tqdm |
|
|
| |
| KITTI_BASE = '/home/ywan0794/datasets/eval/moge_style_eval/KITTI' |
| DDAD_BASE = '/home/ywan0794/datasets/eval/moge_style_eval/DDAD/val' |
| OUTPUT_DIR = '/home/ywan0794/MoGe/vis_output' |
|
|
|
|
| def colorize_depth(depth, cmap='Spectral', reverse=False, mask_invalid=False): |
| depth = depth.copy() |
| if mask_invalid: |
| invalid_mask = depth <= 0 |
| valid_depth = depth[~invalid_mask] |
| if len(valid_depth) > 0: |
| vmin = np.percentile(valid_depth, 2) |
| vmax = np.percentile(valid_depth, 98) |
| else: |
| vmin, vmax = 0, 1 |
| else: |
| vmin = np.percentile(depth, 2) |
| vmax = np.percentile(depth, 98) |
|
|
| depth = (depth - vmin) / (vmax - vmin + 1e-8) |
| depth = np.clip(depth, 0, 1) |
|
|
| if reverse: |
| depth = 1 - depth |
|
|
| cm = plt.get_cmap(cmap) |
| colored = cm(depth)[:, :, :3] |
| colored = (colored * 255).astype(np.uint8) |
|
|
| if mask_invalid: |
| colored[invalid_mask] = 0 |
|
|
| return colored |
|
|
|
|
| def load_gt_depth(depth_path): |
| depth = np.array(Image.open(depth_path)) |
| if depth.dtype == np.uint16: |
| depth = depth.astype(np.float32) / 256.0 |
| elif depth.dtype == np.uint8: |
| depth = depth.astype(np.float32) |
| return depth |
|
|
|
|
| def main(): |
| print("=" * 50, flush=True) |
| print("GT Depth Visualization (both versions)", flush=True) |
| print("=" * 50, flush=True) |
|
|
| |
| for dataset in ['KITTI', 'DDAD']: |
| os.makedirs(os.path.join(OUTPUT_DIR, dataset, 'gt'), exist_ok=True) |
| os.makedirs(os.path.join(OUTPUT_DIR, dataset, 'gt_reverse'), exist_ok=True) |
|
|
| |
| |
| |
| print("\nCollecting KITTI samples...", flush=True) |
| kitti_samples = [] |
| for drive in os.listdir(KITTI_BASE): |
| drive_path = os.path.join(KITTI_BASE, drive, 'image_02') |
| if os.path.isdir(drive_path): |
| for frame in sorted(os.listdir(drive_path)): |
| sample_dir = os.path.join(drive_path, frame) |
| img_path = os.path.join(sample_dir, 'image.jpg') |
| gt_path = os.path.join(sample_dir, 'depth.png') |
| |
| if os.path.exists(img_path) and os.path.exists(gt_path): |
| kitti_samples.append({ |
| 'image': img_path, |
| 'gt': gt_path, |
| 'name': f"{drive}_{frame}" |
| }) |
|
|
| |
| np.random.seed(42) |
| num_samples = 10 |
| kitti_selected = np.random.choice(len(kitti_samples), min(num_samples, len(kitti_samples)), replace=False) |
|
|
| print(f"Processing {len(kitti_selected)} KITTI GT samples...", flush=True) |
| for idx, i in tqdm(enumerate(kitti_selected), total=len(kitti_selected), desc="KITTI GT", file=sys.stdout): |
| sample = kitti_samples[i] |
| gt_depth = load_gt_depth(sample['gt']) |
|
|
| |
| gt_colored = colorize_depth(gt_depth, reverse=False, mask_invalid=True) |
| Image.fromarray(gt_colored).save(os.path.join(OUTPUT_DIR, 'KITTI', 'gt', f"{idx:03d}.png")) |
|
|
| |
| gt_colored_rev = colorize_depth(gt_depth, reverse=True, mask_invalid=True) |
| Image.fromarray(gt_colored_rev).save(os.path.join(OUTPUT_DIR, 'KITTI', 'gt_reverse', f"{idx:03d}.png")) |
|
|
| |
| |
| |
| print("\nCollecting DDAD samples...", flush=True) |
| ddad_samples = [] |
| for scene in sorted(os.listdir(DDAD_BASE)): |
| scene_path = os.path.join(DDAD_BASE, scene) |
| if os.path.isdir(scene_path): |
| for cam in sorted(os.listdir(scene_path)): |
| sample_dir = os.path.join(scene_path, cam) |
| img_path = os.path.join(sample_dir, 'image.jpg') |
| gt_path = os.path.join(sample_dir, 'depth.png') |
| |
| if os.path.exists(img_path) and os.path.exists(gt_path): |
| ddad_samples.append({ |
| 'image': img_path, |
| 'gt': gt_path, |
| 'name': f"{scene}_{cam}" |
| }) |
|
|
| |
| |
| |
| ddad_selected = np.random.choice(len(ddad_samples), min(num_samples, len(ddad_samples)), replace=False) |
|
|
| print(f"Processing {len(ddad_selected)} DDAD GT samples...", flush=True) |
| for idx, i in tqdm(enumerate(ddad_selected), total=len(ddad_selected), desc="DDAD GT", file=sys.stdout): |
| sample = ddad_samples[i] |
| gt_depth = load_gt_depth(sample['gt']) |
|
|
| |
| gt_colored = colorize_depth(gt_depth, reverse=False, mask_invalid=True) |
| Image.fromarray(gt_colored).save(os.path.join(OUTPUT_DIR, 'DDAD', 'gt', f"{idx:03d}.png")) |
|
|
| |
| gt_colored_rev = colorize_depth(gt_depth, reverse=True, mask_invalid=True) |
| Image.fromarray(gt_colored_rev).save(os.path.join(OUTPUT_DIR, 'DDAD', 'gt_reverse', f"{idx:03d}.png")) |
|
|
| print("\n" + "=" * 50, flush=True) |
| print("Done!", flush=True) |
| print("Output:", flush=True) |
| print(f" {OUTPUT_DIR}/KITTI/gt/ (non-reverse)", flush=True) |
| print(f" {OUTPUT_DIR}/KITTI/gt_reverse/ (reverse)", flush=True) |
| print(f" {OUTPUT_DIR}/DDAD/gt/ (non-reverse)", flush=True) |
| print(f" {OUTPUT_DIR}/DDAD/gt_reverse/ (reverse)", flush=True) |
| print("=" * 50, flush=True) |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|