|
| 1 | +# Author: Bingxin Ke |
| 2 | +# Last modified: 2024-02-19 |
| 3 | + |
| 4 | +import argparse |
| 5 | +import os |
| 6 | + |
| 7 | +import cv2 |
| 8 | +import h5py |
| 9 | +import numpy as np |
| 10 | +import pandas as pd |
| 11 | +from hypersim_util import dist_2_depth, tone_map |
| 12 | +from tqdm import tqdm |
| 13 | + |
| 14 | +IMG_WIDTH = 1024 |
| 15 | +IMG_HEIGHT = 768 |
| 16 | +FOCAL_LENGTH = 886.81 |
| 17 | + |
| 18 | +if "__main__" == __name__: |
| 19 | + parser = argparse.ArgumentParser() |
| 20 | + parser.add_argument( |
| 21 | + "--split_csv", |
| 22 | + type=str, |
| 23 | + default="data/Hypersim/metadata_images_split_scene_v1.csv", |
| 24 | + ) |
| 25 | + parser.add_argument("--dataset_dir", type=str, default="data/Hypersim/raw_data") |
| 26 | + parser.add_argument("--output_dir", type=str, default="data/Hypersim/processed") |
| 27 | + |
| 28 | + args = parser.parse_args() |
| 29 | + |
| 30 | + split_csv = args.split_csv |
| 31 | + dataset_dir = args.dataset_dir |
| 32 | + output_dir = args.output_dir |
| 33 | + |
| 34 | + # %% |
| 35 | + raw_meta_df = pd.read_csv(split_csv) |
| 36 | + meta_df = raw_meta_df[raw_meta_df.included_in_public_release].copy() |
| 37 | + |
| 38 | + # %% |
| 39 | + for split in ["train", "val", "test"]: |
| 40 | + split_output_dir = os.path.join(output_dir, split) |
| 41 | + os.makedirs(split_output_dir) |
| 42 | + |
| 43 | + split_meta_df = meta_df[meta_df.split_partition_name == split].copy() |
| 44 | + split_meta_df["rgb_path"] = None |
| 45 | + split_meta_df["rgb_mean"] = np.nan |
| 46 | + split_meta_df["rgb_std"] = np.nan |
| 47 | + split_meta_df["rgb_min"] = np.nan |
| 48 | + split_meta_df["rgb_max"] = np.nan |
| 49 | + split_meta_df["depth_path"] = None |
| 50 | + split_meta_df["depth_mean"] = np.nan |
| 51 | + split_meta_df["depth_std"] = np.nan |
| 52 | + split_meta_df["depth_min"] = np.nan |
| 53 | + split_meta_df["depth_max"] = np.nan |
| 54 | + split_meta_df["invalid_ratio"] = np.nan |
| 55 | + |
| 56 | + for i, row in tqdm(split_meta_df.iterrows(), total=len(split_meta_df)): |
| 57 | + # Load data |
| 58 | + rgb_path = os.path.join( |
| 59 | + row.scene_name, |
| 60 | + "images", |
| 61 | + f"scene_{row.camera_name}_final_hdf5", |
| 62 | + f"frame.{row.frame_id:04d}.color.hdf5", |
| 63 | + ) |
| 64 | + dist_path = os.path.join( |
| 65 | + row.scene_name, |
| 66 | + "images", |
| 67 | + f"scene_{row.camera_name}_geometry_hdf5", |
| 68 | + f"frame.{row.frame_id:04d}.depth_meters.hdf5", |
| 69 | + ) |
| 70 | + render_entity_id_path = os.path.join( |
| 71 | + row.scene_name, |
| 72 | + "images", |
| 73 | + f"scene_{row.camera_name}_geometry_hdf5", |
| 74 | + f"frame.{row.frame_id:04d}.render_entity_id.hdf5", |
| 75 | + ) |
| 76 | + assert os.path.exists(os.path.join(dataset_dir, rgb_path)) |
| 77 | + assert os.path.exists(os.path.join(dataset_dir, dist_path)) |
| 78 | + |
| 79 | + with h5py.File(os.path.join(dataset_dir, rgb_path), "r") as f: |
| 80 | + rgb = np.array(f["dataset"]).astype(float) |
| 81 | + with h5py.File(os.path.join(dataset_dir, dist_path), "r") as f: |
| 82 | + dist_from_center = np.array(f["dataset"]).astype(float) |
| 83 | + with h5py.File(os.path.join(dataset_dir, render_entity_id_path), "r") as f: |
| 84 | + render_entity_id = np.array(f["dataset"]).astype(int) |
| 85 | + |
| 86 | + # Tone map |
| 87 | + rgb_color_tm = tone_map(rgb, render_entity_id) |
| 88 | + rgb_int = (rgb_color_tm * 255).astype(np.uint8) # [H, W, RGB] |
| 89 | + |
| 90 | + # Distance -> depth |
| 91 | + plane_depth = dist_2_depth( |
| 92 | + IMG_WIDTH, IMG_HEIGHT, FOCAL_LENGTH, dist_from_center |
| 93 | + ) |
| 94 | + valid_mask = render_entity_id != -1 |
| 95 | + |
| 96 | + # Record invalid ratio |
| 97 | + invalid_ratio = (np.prod(valid_mask.shape) - valid_mask.sum()) / np.prod( |
| 98 | + valid_mask.shape |
| 99 | + ) |
| 100 | + plane_depth[~valid_mask] = 0 |
| 101 | + |
| 102 | + # Save as png |
| 103 | + scene_path = row.scene_name |
| 104 | + if not os.path.exists(os.path.join(split_output_dir, row.scene_name)): |
| 105 | + os.makedirs(os.path.join(split_output_dir, row.scene_name)) |
| 106 | + |
| 107 | + rgb_name = f"rgb_{row.camera_name}_fr{row.frame_id:04d}.png" |
| 108 | + rgb_path = os.path.join(scene_path, rgb_name) |
| 109 | + cv2.imwrite( |
| 110 | + os.path.join(split_output_dir, rgb_path), |
| 111 | + cv2.cvtColor(rgb_int, cv2.COLOR_RGB2BGR), |
| 112 | + ) |
| 113 | + |
| 114 | + plane_depth *= 1000.0 |
| 115 | + plane_depth = plane_depth.astype(np.uint16) |
| 116 | + depth_name = f"depth_plane_{row.camera_name}_fr{row.frame_id:04d}.png" |
| 117 | + depth_path = os.path.join(scene_path, depth_name) |
| 118 | + cv2.imwrite(os.path.join(split_output_dir, depth_path), plane_depth) |
| 119 | + |
| 120 | + # Meta data |
| 121 | + split_meta_df.at[i, "rgb_path"] = rgb_path |
| 122 | + split_meta_df.at[i, "rgb_mean"] = np.mean(rgb_int) |
| 123 | + split_meta_df.at[i, "rgb_std"] = np.std(rgb_int) |
| 124 | + split_meta_df.at[i, "rgb_min"] = np.min(rgb_int) |
| 125 | + split_meta_df.at[i, "rgb_max"] = np.max(rgb_int) |
| 126 | + |
| 127 | + split_meta_df.at[i, "depth_path"] = depth_path |
| 128 | + restored_depth = plane_depth / 1000.0 |
| 129 | + split_meta_df.at[i, "depth_mean"] = np.mean(restored_depth) |
| 130 | + split_meta_df.at[i, "depth_std"] = np.std(restored_depth) |
| 131 | + split_meta_df.at[i, "depth_min"] = np.min(restored_depth) |
| 132 | + split_meta_df.at[i, "depth_max"] = np.max(restored_depth) |
| 133 | + |
| 134 | + split_meta_df.at[i, "invalid_ratio"] = invalid_ratio |
| 135 | + |
| 136 | + with open( |
| 137 | + os.path.join(split_output_dir, f"filename_list_{split}.txt"), "w+" |
| 138 | + ) as f: |
| 139 | + lines = split_meta_df.apply( |
| 140 | + lambda r: f"{r['rgb_path']} {r['depth_path']}", axis=1 |
| 141 | + ).tolist() |
| 142 | + f.writelines("\n".join(lines)) |
| 143 | + |
| 144 | + with open( |
| 145 | + os.path.join(split_output_dir, f"filename_meta_{split}.csv"), "w+" |
| 146 | + ) as f: |
| 147 | + split_meta_df.to_csv(f, header=True) |
| 148 | + |
| 149 | + print("Preprocess finished") |
0 commit comments