forked from facebookresearch/map-anything
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviz_data.py
More file actions
402 lines (370 loc) · 13.4 KB
/
viz_data.py
File metadata and controls
402 lines (370 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the Apache License, Version 2.0
# found in the LICENSE file in the root directory of this source tree.
"""
Script to visualize WAI format data
"""
import argparse
import os
import cv2
import numpy as np
import rerun as rr
from tqdm import tqdm
from mapanything.utils.cropping import (
rescale_image_and_other_optional_info,
resize_with_nearest_interpolation_to_match_aspect_ratio,
)
from mapanything.utils.misc import seed_everything
from mapanything.utils.viz import log_posed_rgbd_data_to_rerun, script_add_rerun_args
from mapanything.utils.wai.core import load_data, load_frame
def viz_wai_rgbd_data(
args,
depth_key="depth",
local_frame=False,
viz_string="WAI_Viz",
load_skymask=False,
confidence_key=None,
confidence_thres=0,
):
"""
Visualize all the images in the scene directory by logging it to rerun
"""
# Setup Rerun if needed
if args.viz:
rr.script_setup(args, viz_string)
rr.set_time("stable_time", sequence=0)
rr.log("gt", rr.ViewCoordinates.RDF, static=True)
# Load the scene meta data
scene_root = os.path.join(args.root_dir, args.scene)
scene_meta = load_data(os.path.join(scene_root, "scene_meta.json"), "scene_meta")
scene_frame_names = list(scene_meta["frame_names"].keys())
# Loop over images and log to rerun
for frame_idx, frame in enumerate(tqdm(scene_frame_names)):
# Load the frame data
if load_skymask:
modalities = ["image", depth_key, "skymask"]
else:
modalities = ["image", depth_key]
if confidence_key is not None:
modalities.append(confidence_key)
frame_data = load_frame(
os.path.join(args.root_dir, args.scene),
frame,
modalities=modalities,
scene_meta=scene_meta,
)
# Convert necessary data to numpy
rgb_image = frame_data["image"].permute(1, 2, 0).numpy()
rgb_image = (rgb_image * 255).astype(np.uint8)
depth_data = frame_data[depth_key].numpy()
intrinsics = frame_data["intrinsics"].numpy()
# If depth is predicted, resize it to match the aspect ratio of the image
# Then, resize the image and update intrinsics to match the resized predicted depth
if "pred" in depth_key:
# Get the dimensions of the original image
img_h, img_w = rgb_image.shape[:2]
# Resize depth to match image aspect ratio while ensuring that depth resolution doesn't increase
depth_data, target_depth_h, target_depth_w = (
resize_with_nearest_interpolation_to_match_aspect_ratio(
input_data=depth_data, img_h=img_h, img_w=img_w
)
)
# Now resize the image and update intrinsics to match the resized depth
rgb_image, _, intrinsics, _ = rescale_image_and_other_optional_info(
image=rgb_image,
output_resolution=(target_depth_w, target_depth_h),
depthmap=None,
camera_intrinsics=intrinsics,
)
rgb_image = np.array(rgb_image)
# Mask depth if sky mask is loaded
if load_skymask:
mask_data = frame_data["skymask"].numpy().astype(int)
mask_data = cv2.resize(
mask_data,
(depth_data.shape[1], depth_data.shape[0]),
interpolation=cv2.INTER_NEAREST,
)
depth_data = np.where(mask_data, 0, depth_data)
if confidence_key is not None:
confidence_map = frame_data[confidence_key].numpy().astype(np.float32)
confidence_mask = (confidence_map > confidence_thres).astype(int)
confidence_mask = cv2.resize(
confidence_mask,
(depth_data.shape[1], depth_data.shape[0]),
interpolation=cv2.INTER_NEAREST,
)
depth_data = np.where(confidence_mask, depth_data, 0)
# Resize data to smaller resolution for visualization
# Set output resolution to longest side of image to be 224 (preserving aspect ratio)
target_longest_side = 224
if rgb_image.shape[0] > rgb_image.shape[1]:
# Height is longer, so set height to target and scale width
output_resolution = (
int(rgb_image.shape[1] * target_longest_side / rgb_image.shape[0]),
target_longest_side,
)
else:
# Width is longer or equal, so set width to target and scale height
output_resolution = (
target_longest_side,
int(rgb_image.shape[0] * target_longest_side / rgb_image.shape[1]),
)
rgb_image, depth_data, intrinsics, _ = rescale_image_and_other_optional_info(
image=rgb_image,
output_resolution=output_resolution,
depthmap=depth_data,
camera_intrinsics=intrinsics,
)
rgb_image = np.array(rgb_image)
# Init pose
if local_frame:
pose = np.eye(4)
base_name = "gt/image"
else:
pose = frame_data["extrinsics"].numpy()
base_name = f"gt/image_{frame_idx}"
# Log data to rerun
if args.viz:
rr.set_time("stable_time", sequence=frame_idx)
log_posed_rgbd_data_to_rerun(
rgb_image, depth_data, pose, intrinsics, base_name
)
def get_dataset_config(dataset_type):
"""
Get the configuration for a specific dataset type
Args:
dataset_type: The type of dataset to configure
Returns:
dict: Configuration for the dataset including root_dir, scene, and viz parameters
"""
configs = {
"scannetpp": {
"root_dir": "/ai4rl/fsx/xrtech/data/scannetppv2",
"scene": "0a5c013435",
"depth_key": "rendered_depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"blendedmvs": {
"root_dir": "/ai4rl/fsx/xrtech/data/blendedmvs",
"scene": "584b9a747072670e72bfc49d",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"eth3d": {
"root_dir": "/ai4rl/fsx/xrtech/data/eth3d",
"scene": "delivery_area",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"megadepth": {
"root_dir": "/ai4rl/fsx/xrtech/data/megadepth",
"scene": "0000_1",
# "scene": "0086_0", # Disjoint reconstructions with different scale
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"spring": {
"root_dir": "/ai4rl/fsx/xrtech/data/spring",
"scene": "0004",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": True,
"confidence_key": None,
"confidence_thres": 0.0,
},
"mpsd": {
"root_dir": "/ai4rl/fsx/xrtech/data/mpsd",
"scene": "geoeven_4_2019-03-19T11_33_14.019516",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"tav2": {
"root_dir": "/ai4rl/fsx/xrtech/data/tav2_wb",
"scene": "PolarSciFi",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"ase": {
"root_dir": "/ai4rl/fsx/xrtech/data/ase",
"scene": "10000",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"dl3dv": {
"root_dir": "/ai4rl/fsx/xrtech/data/dl3dv",
"scene": "9K_963080e5ee7ca52ee8fabd294ad9e12220ed5064686ec9786a17aed23da8850f",
"depth_key": "pred_depth/mvsanywhere",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": "depth_confidence/mvsanywhere",
"confidence_thres": 0.25,
},
"unrealstereo4k": {
"root_dir": "/ai4rl/fsx/xrtech/data/unrealstereo4k",
"scene": "00000",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"mvs_synth": {
"root_dir": "/ai4rl/fsx/xrtech/data/mvs_synth",
"scene": "0000",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"paralleldomain4d": {
"root_dir": "/ai4rl/fsx/xrtech/data/paralleldomain4d",
"scene": "scene_000000",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"sailvos3d": {
"root_dir": "/ai4rl/fsx/xrtech/data/sailvos3d",
"scene": "fam_6_mcs_5",
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
"dynamicreplica": {
"root_dir": "/ai4rl/fsx/xrtech/data/dynamicreplica",
"scene": "26dd2c-3_obj_source",
# "scene": "009850-3_obj_source", # Part of the floor depth is wrong
"depth_key": "depth",
"local_frame": False,
"viz_string": "WAI_Viz",
"load_skymask": False,
"confidence_key": None,
"confidence_thres": 0.0,
},
}
assert dataset_type in configs, (
f"Dataset {dataset_type} not found, available: {list(configs.keys())}"
)
return configs.get(dataset_type)
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--root_dir",
type=str,
help="Path to the root directory",
default="/ai4rl/fsx/xrtech/data/eth3d",
)
parser.add_argument(
"--scene", type=str, help="Scene to visualize", default="courtyard"
)
parser.add_argument("--viz", action="store_true")
parser.add_argument(
"--dataset",
type=str,
choices=[
"scannetpp",
"blendedmvs",
"eth3d",
"megadepth",
"spring",
"mpsd",
"ase",
"tav2",
"dl3dv",
"unrealstereo4k",
"mvs_synth",
"paralleldomain4d",
"sailvos3d",
"dynamicreplica",
],
default="eth3d",
help="Dataset type to visualize",
)
parser.add_argument(
"--depth_key", type=str, help="Key for depth data in the frame", default=None
)
parser.add_argument(
"--load_skymask", action="store_true", help="Whether to load and apply sky mask"
)
parser.add_argument(
"--local_frame",
action="store_true",
help="Whether to use local frame for visualization",
)
return parser
if __name__ == "__main__":
# Parser for Rerun
parser = get_parser()
script_add_rerun_args(
parser
) # Options: --headless, --connect, --serve, --addr, --save, --stdout
args = parser.parse_args()
# Set the seed
seed_everything(0)
# Get dataset configuration
config = get_dataset_config(args.dataset)
# Override config with command line arguments if provided
if args.root_dir != parser.get_default("root_dir"):
config["root_dir"] = args.root_dir
if args.scene != parser.get_default("scene"):
config["scene"] = args.scene
if args.depth_key is not None:
config["depth_key"] = args.depth_key
if args.load_skymask:
config["load_skymask"] = True
if args.local_frame:
config["local_frame"] = True
# Update args with config values
args.root_dir = config["root_dir"]
args.scene = config["scene"]
# Run visualization with the configured parameters
viz_wai_rgbd_data(
args,
depth_key=config["depth_key"],
local_frame=config["local_frame"],
viz_string=config["viz_string"],
load_skymask=config["load_skymask"],
confidence_key=config["confidence_key"],
confidence_thres=config["confidence_thres"],
)