Datasets:
Create raw/scannet/extract-frames.py
Browse files
data/raw/scannet/extract-frames.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
|
3 |
+
# This source code is licensed under the MIT license found in the
|
4 |
+
# LICENSE file in the root directory of this source tree.
|
5 |
+
|
6 |
+
import argparse
|
7 |
+
import json
|
8 |
+
from pathlib import Path
|
9 |
+
from typing import Dict
|
10 |
+
|
11 |
+
import tqdm
|
12 |
+
from SensorData import SensorData
|
13 |
+
|
14 |
+
|
15 |
+
def parse_args() -> argparse.Namespace:
|
16 |
+
parser = argparse.ArgumentParser()
|
17 |
+
parser.add_argument(
|
18 |
+
"--dataset",
|
19 |
+
type=Path,
|
20 |
+
default="data/open-eqa-v0.json",
|
21 |
+
help="path to open-eqa dataset (default: data/open-eqa-v0.json)",
|
22 |
+
)
|
23 |
+
parser.add_argument(
|
24 |
+
"--scannet-root",
|
25 |
+
type=Path,
|
26 |
+
default="data/raw/scannet",
|
27 |
+
help="path to scannet data (default: data/raw/scannet)",
|
28 |
+
)
|
29 |
+
parser.add_argument(
|
30 |
+
"--output-directory",
|
31 |
+
type=Path,
|
32 |
+
default="data/frames",
|
33 |
+
help="path to output folder (default: data/frames)",
|
34 |
+
)
|
35 |
+
parser.add_argument(
|
36 |
+
"--rgb-only",
|
37 |
+
action="store_true",
|
38 |
+
help="only extract rgb frames (default: false)",
|
39 |
+
)
|
40 |
+
parser.add_argument(
|
41 |
+
"--max-num-frames",
|
42 |
+
type=int,
|
43 |
+
default=600,
|
44 |
+
help="maximum frames to extract from a scene (default: 600)",
|
45 |
+
)
|
46 |
+
args = parser.parse_args()
|
47 |
+
args.output_directory.mkdir(parents=True, exist_ok=True)
|
48 |
+
return args
|
49 |
+
|
50 |
+
|
51 |
+
def get_folder_to_scene(args: argparse.Namespace) -> Dict[str, str]:
|
52 |
+
# get unique scannet folders
|
53 |
+
scene_folders = set()
|
54 |
+
dataset = json.load(args.dataset.open())
|
55 |
+
for item in dataset:
|
56 |
+
if "scannet" not in item["episode_history"]:
|
57 |
+
continue
|
58 |
+
scene_folders.add(item["episode_history"])
|
59 |
+
|
60 |
+
# map folders to scene name
|
61 |
+
scenes = {}
|
62 |
+
for folder in sorted(scene_folders):
|
63 |
+
scene_name = folder.split("/")[-1].split("-")[-1]
|
64 |
+
scenes[folder] = scene_name
|
65 |
+
|
66 |
+
return scenes
|
67 |
+
|
68 |
+
|
69 |
+
def get_scene_path(args: argparse.Namespace, scene: str) -> Path:
|
70 |
+
for folder in ["scans", "scans_test"]:
|
71 |
+
scene_path = args.scannet_root / folder / scene / (scene + ".sens")
|
72 |
+
if scene_path.exists():
|
73 |
+
return scene_path
|
74 |
+
raise ValueError("scene ({}) not found in {}".format(scene, args.scannet_root))
|
75 |
+
|
76 |
+
|
77 |
+
def extract_frames(
|
78 |
+
scene_path: str, output_folder: Path, args: argparse.Namespace
|
79 |
+
) -> None:
|
80 |
+
output_folder.mkdir(exist_ok=True, parents=True)
|
81 |
+
output_folder = str(output_folder)
|
82 |
+
|
83 |
+
print("Extracting frames to: {}".format(output_folder))
|
84 |
+
sd = SensorData(scene_path)
|
85 |
+
if not args.rgb_only:
|
86 |
+
sd.export_intrinsics(output_folder)
|
87 |
+
sd.export_poses(output_folder, num_frames=args.max_num_frames)
|
88 |
+
sd.export_depth_images(output_folder, num_frames=args.max_num_frames)
|
89 |
+
sd.export_color_images(output_folder, num_frames=args.max_num_frames)
|
90 |
+
print("Extracting frames to: {} done!".format(output_folder))
|
91 |
+
|
92 |
+
|
93 |
+
def main(args):
|
94 |
+
folder_to_scene = get_folder_to_scene(args)
|
95 |
+
for folder, scene in tqdm.tqdm(folder_to_scene.items()):
|
96 |
+
output_folder = args.output_directory / folder
|
97 |
+
scene_path = get_scene_path(args, scene)
|
98 |
+
extract_frames(scene_path=scene_path, output_folder=output_folder, args=args)
|
99 |
+
|
100 |
+
|
101 |
+
if __name__ == "__main__":
|
102 |
+
main(parse_args())
|