-
Notifications
You must be signed in to change notification settings - Fork 2
/
BopReader.py
277 lines (228 loc) · 9.57 KB
/
BopReader.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
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
from collections import defaultdict
import json
from pathlib import Path
import sys
from typing import Any, Dict, List, Tuple
import numpy as np
import yaml
from ambf6dpose.DataCollection.BOPSaver.BopSaver import (
get_folder_names,
SceneCameraKeys,
SceneGtKeys,
GroundTruthFiles,
DatasetConsts,
)
from ambf6dpose.DataCollection.DatasetSample import DatasetSample, RigidObjectsIds
from ambf6dpose.DataCollection.ReaderSaverUtils import AbstractReader, ImgDirs
from dataclasses import dataclass, field
import cv2
import imageio
from ambf6dpose.DataCollection.ReaderSaverUtils import is_rotation, trnorm
from natsort import natsorted
@dataclass
class BopDatasetReader(AbstractReader):
"""
See bop_toolkit for more info on the dataset structure
https://github.com/thodan/bop_toolkit/blob/master/docs/bop_datasets_format.md
Parameters
----------
dataset_split : str, optional
train, val, or test, by default 'train'
dataset_split_type : str, optional
split type, by default None
scene_id : List[int], optional
scene id, by default [] which means take all scene_ids
"""
dataset_split: str = "train"
dataset_split_type: str = None
scene_id_list: List[int] = field(default_factory=list) # If empty, read all scenes
def __post_init__(self):
if self.dataset_split_type is None:
self.dataset_split = ""
if len(self.dataset_split_type) == 0:
self.dataset_split_complete = self.dataset_split
else:
self.dataset_split_complete = (
self.dataset_split + "_" + self.dataset_split_type
)
self.root = self.root / self.dataset_split_complete
assert self.root.exists(), f"Path {self.root} does not exist"
self.__scene_gt = {}
self.__scene_camera = {}
self.__dict_paths: Dict[ImgDirs, Path] = {}
self.__idx2imgs: List[str, str] = [] # Store tuples of (scene_id, img_name)
self.__load_data()
self.__internal_idx: int = 0
self.__dataset_size: int = self.calculate_size()
def print_ds_info(self):
print(f"Dataset: {self.root.parent.name}")
print(f"Dataset split: {self.dataset_split_complete}")
print(f"Number of scenes: {len(self.scene_id_list)}")
print(f"Number of images: {len(self)}")
print("\n")
def __len__(self) -> int:
return self.__dataset_size
def calculate_size(self) -> int:
"""Make sure that data was loaded correctly"""
total_length = 0
for scene_id in self.scene_id_list:
total_length += len(
list(self.__dict_paths[scene_id][ImgDirs.RAW].glob("*.png"))
)
assert total_length == len(self.__idx2imgs), "Error while loading data"
return total_length
def __iter__(self):
self.__internal_idx = 0
return self
def __next__(self):
if self.__internal_idx < self.__dataset_size:
sample = self[self.__internal_idx]
self.__internal_idx += 1
return sample
else:
raise StopIteration
def get_scene_id(self) -> List[str]:
return self.scene_id_list
def __load_data(self):
folder_names = get_folder_names()
self.__dict_paths = defaultdict(dict)
if len(self.scene_id_list) == 0:
self.scene_id_list = [p.name for p in natsorted(self.root.glob("*"))]
else:
self.scene_id_list = [self.format_step(x) for x in self.scene_id_list]
for scene_id in self.scene_id_list:
for img_dir in ImgDirs:
self.__dict_paths[scene_id][img_dir] = (
self.root / scene_id / folder_names[img_dir]
)
# Create table of idx to scene_id and img_name
for scene_id in self.scene_id_list:
ids = [
(scene_id, p.name)
for p in natsorted(
self.__dict_paths[scene_id][ImgDirs.RAW].glob("*.png")
)
]
self.__idx2imgs += ids
self.__dict_paths = dict(self.__dict_paths)
# Load GT files
for scene_id in self.scene_id_list:
assert (self.root / scene_id).exists(), "Scene id {scene_id} does not exist"
with open(self.root / scene_id / GroundTruthFiles.SCENE_GT.value, "r") as f:
self.__scene_gt[scene_id] = self._load_json(
f, scene_id, GroundTruthFiles.SCENE_GT
)
with open(
self.root / scene_id / GroundTruthFiles.SCENE_CAMERA.value, "r"
) as f:
self.__scene_camera[scene_id] = self._load_json(
f, scene_id, GroundTruthFiles.SCENE_CAMERA
)
def _load_json(self, path: Path, scene_id: int, gt_file: GroundTruthFiles):
try:
data = json.load(path)
except json.decoder.JSONDecodeError as e:
print(e)
print(f"Error loading {scene_id} {gt_file.value} \n")
raise e
return data
def format_step(self, step: int) -> str:
return f"{step:{DatasetConsts.FMT_STR.value}}"
def __getitem__(self, key: int) -> DatasetSample:
if key > DatasetConsts.MAX_STEP.value or key < 0:
raise IndexError
else:
return self.construct_sample(key)
def get_metadata(self, key: int) -> Tuple[int, str]:
scene_id, img_name = self.__idx2imgs[key]
return scene_id, img_name
def construct_sample(self, key: int) -> DatasetSample:
step_str = self.format_step(key)
scene_id, img_name = self.__idx2imgs[key]
raw_path = str(self.__dict_paths[scene_id][ImgDirs.RAW] / f"{img_name}")
seg_path = str(self.__dict_paths[scene_id][ImgDirs.SEGMENTED] / f"{img_name}")
depth_path = str(self.__dict_paths[scene_id][ImgDirs.DEPTH] / f"{img_name}")
sample = DatasetSample(
raw_img=cv2.imread(raw_path),
segmented_img=cv2.imread(seg_path),
depth_img=self.load_depth(depth_path),
needle_pose=self.get_extrinsic_matrix(
RigidObjectsIds.needle_pose.value, scene_id, img_name
),
psm1_toolpitchlink_pose=self.get_extrinsic_matrix(
RigidObjectsIds.psm1_toolpitchlink_pose.value, scene_id, img_name
),
psm2_toolpitchlink_pose=self.get_extrinsic_matrix(
RigidObjectsIds.psm2_toolpitchlink_pose.value, scene_id, img_name
),
psm1_toolyawlink_pose=self.get_extrinsic_matrix(
RigidObjectsIds.psm1_toolyawlink_pose.value, scene_id, img_name
),
psm2_toolyawlink_pose=self.get_extrinsic_matrix(
RigidObjectsIds.psm2_toolyawlink_pose.value, scene_id, img_name
),
intrinsic_matrix=self.get_camera_intrinsics(scene_id, img_name),
)
return sample
def load_depth(self, path):
d: np.ndarray = imageio.imread(path)
return d.astype(np.float32)
def get_camera_intrinsics(self, scene_id: int, img_name: str) -> np.ndarray:
k = str(int(img_name[:-4]))
intrinsic = self.__scene_camera[scene_id][k][SceneCameraKeys.CAM_K.value]
intrinsic = np.array(intrinsic).reshape(3, 3)
return intrinsic
def get_extrinsic_matrix(self, obj_id: int, scene_id: int, img_name) -> np.ndarray:
k = str(int(img_name[:-4]))
for idx, obj in enumerate(self.__scene_gt[scene_id][k]):
if obj[SceneGtKeys.OBJ_ID.value] == obj_id:
break
rot = self.__scene_gt[scene_id][k][idx][SceneGtKeys.CAM_R_M2C.value]
rot = np.array(rot).reshape(3, 3)
t = self.__scene_gt[scene_id][k][idx][SceneGtKeys.CAM_T_M2C.value]
t = np.array(t)
obj_id = self.__scene_gt[scene_id][k][idx][SceneGtKeys.OBJ_ID.value]
rot = self.reorthogonalize(rot)
return self.create_rigid_transform_matrix(rot, t)
def create_rigid_transform_matrix(
self, rot_str: np.ndarray, t_str: np.ndarray
) -> np.ndarray:
rigid_transform = np.eye(4)
rigid_transform[:3, :3] = rot_str
rigid_transform[:3, 3] = t_str
return rigid_transform
def str_to_numpy(self, str_array: str, shape: Tuple[int, int]) -> np.ndarray:
loaded = np.fromstring(str_array, sep=",")
loaded = loaded.reshape(shape)
return loaded
def reorthogonalize(self, rot: np.ndarray) -> np.ndarray:
if is_rotation(rot):
return rot
else:
rot = trnorm(rot)
assert is_rotation(rot), "Reortogonalization failed..."
return trnorm(rot)
if __name__ == "__main__":
file_path = Path(__file__).resolve().parent
root_path2 = file_path / "../../../SampleData/BOP/needle_gripper_dataset_V0.0.2"
root_path2 = root_path2.resolve()
reader = BopDatasetReader(
root=Path(root_path2),
scene_id_list=[],
dataset_split="test",
dataset_split_type="",
)
## Calling another dataset
# root_path = "/home/juan1995/research_juan/accelnet_grant/6d_pose_dataset_collection/test_ds_bop"
# reader = BopReader(
# root=Path("."), scene_id_list=[0, 1], dataset_split="test", dataset_split_type="ds_bop"
# )
print(f"Dataset size: {len(reader)}")
for idx in range(0, len(reader)):
sample: DatasetSample = reader[idx]
scene_id, img_name = reader.get_metadata(idx)
sample.generate_gt_vis()
cv2.imshow("image", sample.gt_vis_img)
print(f"{scene_id}-{img_name}")
cv2.waitKey(0)
cv2.destroyAllWindows()