forked from NVlabs/MobilityGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
130 lines (102 loc) · 4.69 KB
/
reader.py
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
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import PIL.Image
import glob
import numpy as np
import os
from collections import OrderedDict
from occupancy_map import OccupancyMap
import json
from dataclasses import dataclass, asdict
@dataclass
class Config:
scenario_type: str
robot_type: str
scene_usd: str
def to_json(self):
return json.dumps(asdict(self), indent=2)
@staticmethod
def from_json(data: str):
return Config(**json.loads(data))
class Reader:
def __init__(self, recording_path: str):
self.recording_path = recording_path
state_dict_paths = glob.glob(os.path.join(
self.recording_path, "state", "common", "*.npy"
))
steps = [int(os.path.basename(path).split('.')[0]) for path in state_dict_paths]
self.steps = sorted(steps)
self.rgb_folders = glob.glob(os.path.join(self.recording_path, "state", "rgb", "*"))
self.segmentation_folders = glob.glob(os.path.join(self.recording_path, "state", "segmentation", "*"))
self.depth_folders = glob.glob(os.path.join(self.recording_path, "state", "depth", "*"))
self.rgb_names = [os.path.basename(folder) for folder in self.rgb_folders]
self.segmentation_names = [os.path.basename(folder) for folder in self.segmentation_folders]
self.depth_names = [os.path.basename(folder) for folder in self.depth_folders]
def read_config(self) -> Config:
with open(os.path.join(self.recording_path, "config.json"), 'r') as f:
config = Config.from_json(f.read())
return config
def read_occupancy_map(self):
return OccupancyMap.from_ros_yaml(os.path.join(self.recording_path, "occupancy_map", "map.yaml"))
def read_rgb(self, name: str, index: int):
step = self.steps[index]
image = PIL.Image.open(os.path.join(self.recording_path, "state", "rgb", name, f"{step:08d}.jpg"))
return np.asarray(image)
def read_state_dict_rgb(self, index: int):
rgb_dict = OrderedDict()
for name in self.rgb_names:
data = self.read_rgb(name, index)
rgb_dict[name] = data
return rgb_dict
def read_segmentation(self, name: str, index: int):
step = self.steps[index]
image = PIL.Image.open(os.path.join(self.recording_path, "state", "segmentation", name, f"{step:08d}.png"))
return np.asarray(image)
def read_state_dict_segmentation(self, index: int):
segmentation_dict = OrderedDict()
for name in self.segmentation_names:
data = self.read_segmentation(name, index)
segmentation_dict[name] = data
return segmentation_dict
def read_depth(self, name: str, index: int, eps=1e-6):
step = self.steps[index]
image = PIL.Image.open(os.path.join(self.recording_path, "state", "depth", name, f"{step:08d}.png")).convert("I;16")
depth = 65535 / (np.asarray(image).astype(np.float32) + eps) - 1.0
return depth
def read_state_dict_depth(self, index: int):
depth_dict = OrderedDict()
for name in self.depth_names:
data = self.read_depth(name, index)
depth_dict[name] = data
return depth_dict
def read_state_dict_common(self, index: int):
step = self.steps[index]
state_dict = np.load(os.path.join(self.recording_path, "state", "common", f"{step:08d}.npy"), allow_pickle=True).item()
return state_dict
def read_state_dict(self, index: int):
state_dict = self.read_state_dict_common(index)
rgb_dict = self.read_state_dict_rgb(index)
segmentation_dict = self.read_state_dict_segmentation(index)
depth_dict = self.read_state_dict_depth(index)
full_dict = OrderedDict()
full_dict.update(state_dict)
full_dict.update(rgb_dict)
full_dict.update(segmentation_dict)
full_dict.update(depth_dict)
return full_dict
def __len__(self) -> int:
return len(self.steps)
def __getitem__(self, index: int):
return self.read_state_dict(index)