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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import open3d as o3d
import numpy as np
import os
import cv2
from evaluation.constants import SCANNET_LABELS, SCANNET_IDS, SCANNET18_LABELS, SCANNET18_IDS, SCANNETPP84_IDS, SCANNETPP84_LABELS, SCANNET20_LABELS, SCANNET20_IDS, ARKIT_LABELS, ARKIT_IDS

class ScanNetDataset:

    def __init__(self, seq_name, root='data/scannet', use_templates=False) -> None:
        self.seq_name = seq_name
        self.use_templates = use_templates
        self.root = os.path.join(root, 'processed', seq_name)
        self.rgb_dir = f'{self.root}/color'
        self.depth_dir = f'{self.root}/depth'
        self.segmentation_dir = f'{self.root}/output/mask'
        self.object_dict_dir = f'{self.root}/output/object'
        self.point_cloud_path = f'{self.root}/{seq_name}_vh_clean_2.ply'
        self.mesh_path = self.point_cloud_path
        self.extrinsics_dir = f'{self.root}/pose'
        self.intrinsic_dir = f'{self.root}/intrinsic'
        self.label_features_dict = None

        self.depth_scale = 1000.0
        self.image_size = self.get_image_size()
        self.depth_size = self.get_depth_shape()


    def get_frame_list(self, stride):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))

        end = int(image_list[-1].split('.')[0]) + 1
        frame_id_list = [int(a.split('.')[0]) for a in image_list]
        return list(frame_id_list)

    def get_image_size(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))
        image_path = os.path.join(self.rgb_dir, image_list[0])
        image = cv2.imread(image_path)
        return image.shape[:2][::-1]

    def get_depth_shape(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))
        depth_path = os.path.join(self.depth_dir, f"{image_list[0].split('.')[0]}.png")
        depth = cv2.imread(depth_path, -1)
        return depth.shape[:2][::-1]

    def get_intrinsics(self, frame_id):
        intrinsic_path = f'{self.intrinsic_dir}/intrinsic_depth.txt'
        intrinsics = np.loadtxt(intrinsic_path)

        intrinisc_cam_parameters = o3d.camera.PinholeCameraIntrinsic()
        intrinisc_cam_parameters.set_intrinsics(self.image_size[0], self.image_size[1], intrinsics[0, 0], intrinsics[1, 1], intrinsics[0, 2], intrinsics[1, 2])
        return intrinisc_cam_parameters
    

    def get_extrinsic(self, frame_id):
        pose_path = os.path.join(self.extrinsics_dir, str(frame_id) + '.txt')
        pose = np.loadtxt(pose_path)
        return pose
    

    def get_depth(self, frame_id):
        depth_path = os.path.join(self.depth_dir, str(frame_id) + '.png')
        depth = cv2.imread(depth_path, -1)
        depth = depth / self.depth_scale
        depth = depth.astype(np.float32)
        return depth


    def get_rgb(self, frame_id, change_color=True):
        rgb_path = os.path.join(self.rgb_dir, str(frame_id) + '.jpg')
        rgb = cv2.imread(rgb_path)

        if change_color:
            rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)
        return rgb    


    def get_segmentation(self, frame_id, align_with_depth=False):
        segmentation_path = os.path.join(self.segmentation_dir, f'{frame_id}.png')
        if not os.path.exists(segmentation_path):
            assert False, f"Segmentation not found: {segmentation_path}"
        segmentation = cv2.imread(segmentation_path, cv2.IMREAD_UNCHANGED)
        if align_with_depth:
            segmentation = cv2.resize(segmentation, self.depth_size, interpolation=cv2.INTER_NEAREST)
        return segmentation


    def get_frame_path(self, frame_id):
        rgb_path = os.path.join(self.rgb_dir, str(frame_id) + '.jpg')
        segmentation_path = os.path.join(self.segmentation_dir, f'{frame_id}.png')
        return rgb_path, segmentation_path
    

    def get_label_features(self):
        if self.label_features_dict is None:
            if self.use_templates:
                label_features_dict = np.load(f'data/text_features/scannet_templates.npy', allow_pickle=True).item()
            else:
                label_features_dict = np.load(f'data/text_features/scannet.npy', allow_pickle=True).item()
            self.label_features_dict = label_features_dict
        return self.label_features_dict


    def get_scene_points(self):
        mesh = o3d.io.read_point_cloud(self.point_cloud_path)
        vertices = np.asarray(mesh.points)
        return vertices
    
    
    def get_label_id(self):
        self.class_id = SCANNET_IDS
        self.class_label = SCANNET_LABELS

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label
    

class ARKitDataset(ScanNetDataset):
    def __init__(self, seq_name, root='data/arkit_dust3r_posed'):
        super().__init__(seq_name, root)
        self.image_size = self.get_image_size()

    def get_image_size(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))
        image_path = os.path.join(self.rgb_dir, image_list[0])
        image = cv2.imread(image_path)
        return image.shape[:2][::-1]

    def get_frame_list(self, stride):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))

        end = int(image_list[-1].split('.')[0]) + 1
        frame_id_list = [a.split('.')[0] for a in image_list]
        return list(frame_id_list)
    
    def get_label_id(self):
        self.class_id = ARKIT_IDS
        self.class_label = ARKIT_LABELS

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label

    def get_label_features(self):
        label_features_dict = np.load(f'data/text_features/arkit.npy', allow_pickle=True).item()
        return label_features_dict

class ITWDataset(ARKitDataset):

    def get_image_size(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('_')[0]))
        image_path = os.path.join(self.rgb_dir, image_list[0])
        image = cv2.imread(image_path)
        return image.shape[:2][::-1]

    def get_depth_shape(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('_')[0]))
        depth_path = os.path.join(self.depth_dir, f"{image_list[0].split('.')[0]}.png")
        depth = cv2.imread(depth_path, -1)
        return depth.shape[:2][::-1]

    def get_frame_list(self, stride):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('_')[0]))

        frame_id_list = [a.split('.')[0] for a in image_list]
        return list(frame_id_list)
    
    def get_label_features(self):
        label_features_dict = np.load(f'{self.root}/text_features.npy', allow_pickle=True).item()
        return label_features_dict

    def get_label_id(self):
        text_features = self.get_label_features()

        self.class_label = list(text_features.keys())
        self.class_id = list(range(len(self.class_label)))

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label

class WildDataset(ARKitDataset):
    def __init__(self, seq_name, root):
        self.root = os.path.join(root, seq_name)
        self.rgb_dir = f'{self.root}/images'
        self.depth_dir = f'{self.root}/depth'
        self.segmentation_dir = f'{self.root}/output/mask'
        self.object_dict_dir = f'{self.root}/output/object'
        self.point_cloud_path = f'{self.root}/point_cloud.ply'
        self.mesh_path = self.point_cloud_path
        self.extrinsics_dir = f'{self.root}/pose'
        self.intrinsic_dir = f'{self.root}/intrinsic'
        self.label_features_dict = None

        self.depth_scale = 1000.0
        self.image_size = self.get_depth_shape()
        self.depth_size = self.get_depth_shape()

    def get_label_features(self):
        label_features_dict = np.load(f'{self.root}/text_features.npy', allow_pickle=True).item()
        return label_features_dict

    def get_segmentation(self, frame_id, align_with_depth=False):
        segmentation_path = os.path.join(self.segmentation_dir, f'{frame_id}.png')
        if not os.path.exists(segmentation_path):
            assert False, f"Segmentation not found: {segmentation_path}"
        segmentation = cv2.imread(segmentation_path, cv2.IMREAD_UNCHANGED)
        segmentation = cv2.resize(segmentation, self.depth_size, interpolation=cv2.INTER_NEAREST)
        return segmentation
    def get_label_id(self):
        text_features = self.get_label_features()

        self.class_label = list(text_features.keys())
        self.class_id = list(range(len(self.class_label)))

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label


class ScannetPP2Dataset(ScanNetDataset):
    def __init__(self, seq_name, root='data/scannetpp_dust3r_posed'):
        super().__init__(seq_name, root)
        self.image_size = self.get_image_size()
        self.depth_size = self.get_depth_shape()

        self.point_cloud_path = f'{self.root}/{seq_name}.ply'

    def get_image_size(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0].split('_')[1]))
        image_path = os.path.join(self.rgb_dir, image_list[0])
        image = cv2.imread(image_path)
        return image.shape[:2][::-1]

    def get_depth_shape(self):

        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0].split('_')[1]))
        depth_path = os.path.join(self.depth_dir, f"{image_list[0].split('.')[0]}.png")
        depth = cv2.imread(depth_path, -1)
        return depth.shape[:2][::-1]

    def get_frame_list(self, stride):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0].split('_')[1]))

        frame_id_list = [a.split('.')[0] for a in image_list]
        return list(frame_id_list)

    def get_segmentation(self, frame_id, align_with_depth=False):
        segmentation_path = os.path.join(self.segmentation_dir, f'{frame_id}.png')
        if not os.path.exists(segmentation_path):
            assert False, f"Segmentation not found: {segmentation_path}"
        segmentation = cv2.imread(segmentation_path, cv2.IMREAD_UNCHANGED)
        segmentation = cv2.resize(segmentation, self.depth_size, interpolation=cv2.INTER_NEAREST)
        return segmentation

    
    def get_label_id(self):
        self.class_id = SCANNETPP84_IDS
        self.class_label = SCANNETPP84_LABELS

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label
    
    def get_label_features(self):
        label_features_dict = np.load(f'data/text_features/scannetpp84.npy', allow_pickle=True).item()
        return label_features_dict
    
    def get_depth(self, frame_id):
        depth_path = os.path.join(self.depth_dir, str(frame_id) + '.png')
        depth = cv2.imread(depth_path, -1)
        depth = depth / self.depth_scale
        depth = depth.astype(np.float32)
        return depth

    
    def get_intrinsics(self, frame_id):
        intrinsic_path = f'{self.intrinsic_dir}/intrinsic_depth.txt'
        intrinsics = np.loadtxt(intrinsic_path)

        intrinisc_cam_parameters = o3d.camera.PinholeCameraIntrinsic()
        intrinisc_cam_parameters.set_intrinsics(self.image_size[0], self.image_size[1], intrinsics[0, 0], intrinsics[1, 1], intrinsics[0, 2], intrinsics[1, 2])
        return intrinisc_cam_parameters


class ScanNet18Dataset:

    def __init__(self, seq_name, root='data/scannet') -> None:
        self.seq_name = seq_name
        self.root = os.path.join(root, 'processed', seq_name)
        self.rgb_dir = f'{self.root}/color'
        self.depth_dir = f'{self.root}/depth'
        self.segmentation_dir = f'{self.root}/output/mask'
        self.object_dict_dir = f'{self.root}/output/object'
        self.point_cloud_path = f'{self.root}/{seq_name}.ply'
        self.mesh_path = self.point_cloud_path
        self.extrinsics_dir = f'{self.root}/pose'
        self.intrinsic_dir = f'{self.root}/intrinsic'

        self.depth_scale = 1000.0
        self.image_size = self.get_image_size()
        self.depth_size = self.get_depth_shape()


    def get_frame_list(self, stride):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))

        end = int(image_list[-1].split('.')[0]) + 1
        frame_id_list = [a.split('.')[0] for a in image_list]
        return list(frame_id_list)

    def get_image_size(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))
        image_path = os.path.join(self.rgb_dir, image_list[0])
        image = cv2.imread(image_path)
        return image.shape[:2][::-1]

    def get_depth_shape(self):
        image_list = os.listdir(self.rgb_dir)
        image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))
        depth_path = os.path.join(self.depth_dir, f"{image_list[0].split('.')[0]}.png")
        depth = cv2.imread(depth_path, -1)
        return depth.shape[:2][::-1]

    def get_intrinsics(self, frame_id):
        intrinsic_path = f'{self.intrinsic_dir}/intrinsic_depth.txt'
        intrinsics = np.loadtxt(intrinsic_path)

        intrinisc_cam_parameters = o3d.camera.PinholeCameraIntrinsic()
        intrinisc_cam_parameters.set_intrinsics(self.image_size[0], self.image_size[1], intrinsics[0, 0], intrinsics[1, 1], intrinsics[0, 2], intrinsics[1, 2])
        return intrinisc_cam_parameters
    

    def get_extrinsic(self, frame_id):
        pose_path = os.path.join(self.extrinsics_dir, str(frame_id) + '.txt')
        pose = np.loadtxt(pose_path)
        return pose
    

    def get_depth(self, frame_id):
        depth_path = os.path.join(self.depth_dir, str(frame_id) + '.png')
        depth = cv2.imread(depth_path, -1)
        depth = depth / self.depth_scale
        depth = depth.astype(np.float32)
        return depth


    def get_rgb(self, frame_id, change_color=True):
        rgb_path = os.path.join(self.rgb_dir, str(frame_id) + '.jpg')
        rgb = cv2.imread(rgb_path)

        if change_color:
            rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)
        return rgb    


    def get_segmentation(self, frame_id, align_with_depth=False):
        segmentation_path = os.path.join(self.segmentation_dir, f'{frame_id}.png')
        if not os.path.exists(segmentation_path):
            assert False, f"Segmentation not found: {segmentation_path}"
        segmentation = cv2.imread(segmentation_path, cv2.IMREAD_UNCHANGED)
        segmentation = cv2.resize(segmentation, self.depth_size, interpolation=cv2.INTER_NEAREST)
        return segmentation


    def get_frame_path(self, frame_id):
        rgb_path = os.path.join(self.rgb_dir, str(frame_id) + '.jpg')
        segmentation_path = os.path.join(self.segmentation_dir, f'{frame_id}.png')
        return rgb_path, segmentation_path
    

    def get_label_features(self):
        label_features_dict = np.load(f'data/text_features/scannet18.npy', allow_pickle=True).item()
        return label_features_dict


    def get_scene_points(self):
        mesh = o3d.io.read_point_cloud(self.point_cloud_path)
        vertices = np.asarray(mesh.points)
        return vertices
    
    
    def get_label_id(self):
        self.class_id = SCANNET18_IDS
        self.class_label = SCANNET18_LABELS

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label


class ScanNet20Dataset(ScanNet18Dataset):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.point_cloud_path = f'{self.root}/{self.seq_name}_vh_clean_2.ply'

    def get_label_features(self):
        label_features_dict = np.load(f'/home/jovyan/users/lemeshko/Indoor/MaskClustering/data/text_features/scannet20.npy', allow_pickle=True).item()
        return label_features_dict
    
    
    def get_label_id(self):
        self.class_id = SCANNET20_IDS
        self.class_label = SCANNET20_LABELS

        self.label2id = {}
        self.id2label = {}
        for label, id in zip(self.class_label, self.class_id):
            self.label2id[label] = id
            self.id2label[id] = label

        return self.label2id, self.id2label