-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathfields.py
496 lines (405 loc) · 16.5 KB
/
fields.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import os
import glob
import random
from PIL import Image
import numpy as np
from im2mesh.data.core import Field
import imageio
class IndexField(Field):
''' Basic index field.'''
def load(self, model_path, idx, category, **kwargs):
''' Loads the index field.
Args:
model_path (str): path to model
idx (int): ID of data point
category (int): index of category
'''
return idx
def check_complete(self, files):
''' Check if field is complete.
Args:
files: files
'''
return True
class CategoryField(Field):
''' Basic category field.'''
def load(self, model_path, idx, category, **kwargs):
''' Loads the category field.
Args:
model_path (str): path to model
idx (int): ID of data point
category (int): index of category
'''
return category
def check_complete(self, files):
''' Check if field is complete.
Args:
files: files
'''
return True
class SparsePointCloud(Field):
''' Image Field.
It is the field used for loading images.
Args:
folder_name (str): image folder name; this is used for calculating
how many images the object has.
file_name (str): file name of points file
n_views (int): number of views that should be used; if < 1, all views
in the folder are used
ignore_image_idx (list): list of IDs which should be ignored (only
used for the multi-view reconstruction experiments)
'''
def __init__(self, folder_name='image', file_name='pcl.npz',
random_view=True, ignore_image_idx=[]):
self.file_name = file_name
self.image_folder = folder_name
self.random_view = random_view
self.ignore_image_idx = ignore_image_idx
def load(self, model_path, idx, category, input_idx_img=None):
''' Loads the field.
Args:
model_path (str): path to model
idx (int): model id
category (int): category id
input_idx_img (int): image id which should be used (this
overwrites any other id). This is used when the fields are
cached.
'''
# We use the image folder to detect how many images the object has
files = os.listdir(os.path.join(model_path, self.image_folder))
img_list = [i for i in range(
len(files)) if i not in self.ignore_image_idx]
if input_idx_img is not None:
idx_img = input_idx_img
elif self.random_view:
idx_img = np.random.randint(0, len(img_list)-1)
else:
idx_img = 0
file_path = os.path.join(model_path, self.file_name)
npz_file = np.load(file_path)
p = npz_file['points']
is_in_visual_hull = npz_file['is_in_visual_hull']
c = npz_file['colors']
v = npz_file['visibility_%04d' % img_list[idx_img]]
p = p[v][is_in_visual_hull[v]]
c = c[v][is_in_visual_hull[v]]
# load cam
camera_dict = np.load(os.path.join(
os.path.join(model_path, 'cameras.npz')
))
Rt = camera_dict['world_mat_%d' % img_list[idx_img]].astype(np.float32)
K = camera_dict['camera_mat_%d' % img_list[idx_img]].astype(np.float32)
S = camera_dict.get('scale_mat_%d' %
img_list[idx_img]).astype(np.float32)
data = {}
# Project points into view space for depth value
phom = np.concatenate([
p, np.ones((p.shape[0], 1))
], axis=-1).transpose(1, 0)
p_proj = K @ Rt @ phom
d = p_proj[-2]
p_proj = p_proj[:2] / p_proj[-2].reshape(1, -1)
p_proj = p_proj.transpose(1, 0)
# Apply inverted scale matrix to transform 3D points to unit cube
p_world = np.linalg.inv(S) @ phom
p_world = p_world[:3].transpose(1, 0)
data['p_world'] = p_world.astype(np.float32)
data['p_img'] = p_proj.astype(np.float32)
data['d'] = d.astype(np.float32)
data['colors'] = c.astype(np.float32)
data['world_mat'] = Rt.astype(np.float32)
data['scale_mat'] = S.astype(np.float32)
data['camera_mat'] = K.astype(np.float32)
return data
class ImagesField(Field):
''' Image Field.
It is the field used for loading images.
Args:
folder_name (str): image folder name
mask_folder_name (str): mask folder name
depth_folder_name (str): depth folder name
visual_hull_depth_folder (str): visual hull depth folder name
transform (transform): transformations applied to images
extension (str): image extension
mask_extension (str): mask extension
depth_extension (str): depth extension
with_camera (bool): whether camera data should be provided
with_mask (bool): whether object masks should be provided
with_depth (bool): whether depth maps should be provided
random_view (bool): whether a random view should be used
all_images (bool): whether all images should be returned (instead of
one); only used for rendering
n_views (int): number of views that should be used; if < 1, all views
in the folder are used
depth_from_visual_hull (bool): whether the visual hull depth map
should be provided
ignore_image_idx (list): list of IDs which should be ignored (only
used for the multi-view reconstruction experiments)
'''
def __init__(self, folder_name, mask_folder_name='mask',
depth_folder_name='depth',
visual_hull_depth_folder='visual_hull_depth',
transform=None, extension='jpg', mask_extension='png',
depth_extension='exr', with_camera=False, with_mask=True,
with_depth=False, random_view=True,
all_images=False, n_views=0,
depth_from_visual_hull=False,
ignore_image_idx=[], **kwargs):
self.folder_name = folder_name
self.mask_folder_name = mask_folder_name
self.depth_folder_name = depth_folder_name
self.visual_hull_depth_folder = visual_hull_depth_folder
self.transform = transform
self.extension = extension
self.mask_extension = mask_extension
self.depth_extension = depth_extension
self.random_view = random_view
self.n_views = n_views
self.with_camera = with_camera
self.with_mask = with_mask
self.with_depth = with_depth
self.all_images = all_images
self.depth_from_visual_hull = depth_from_visual_hull
self.ignore_image_idx = ignore_image_idx
def load(self, model_path, idx, category, input_idx_img=None):
''' Loads the field.
Args:
model_path (str): path to model
idx (int): model id
category (int): category id
input_idx_img (int): image id which should be used (this
overwrites any other id). This is used when the fields are
cached.
'''
if self.all_images:
n_files = self.get_number_files(model_path)
data = {}
for input_idx_img in range(n_files):
datai = self.load_field(model_path, idx, category,
input_idx_img)
data['img%d' % input_idx_img] = datai
data['n_images'] = n_files
return data
else:
return self.load_field(model_path, idx, category, input_idx_img)
def get_number_files(self, model_path, ignore_filtering=False):
''' Returns how many views are present for the model.
Args:
model_path (str): path to model
ignore_filtering (bool): whether the image filtering should be
ignored
'''
folder = os.path.join(model_path, self.folder_name)
files = glob.glob(os.path.join(folder, '*.%s' % self.extension))
files.sort()
if not ignore_filtering and len(self.ignore_image_idx) > 0:
files = [files[idx] for idx in range(
len(files)) if idx not in self.ignore_image_idx]
if not ignore_filtering and self.n_views > 0:
files = files[:self.n_views]
return len(files)
def return_idx_filename(self, model_path, folder_name, extension, idx):
''' Loads the "idx" filename from the folder.
Args:
model_path (str): path to model
folder_name (str): name of the folder
extension (str): string of the extension
idx (int): ID of data point
'''
folder = os.path.join(model_path, folder_name)
files = glob.glob(os.path.join(folder, '*.%s' % extension))
files.sort()
if len(self.ignore_image_idx) > 0:
files = [files[idx] for idx in range(
len(files)) if idx not in self.ignore_image_idx]
if self.n_views > 0:
files = files[:self.n_views]
return files[idx]
def load_image(self, model_path, idx, data={}):
''' Loads an image.
Args:
model_path (str): path to model
idx (int): ID of data point
data (dict): data dictionary
'''
filename = self.return_idx_filename(model_path, self.folder_name,
self.extension, idx)
image = Image.open(filename).convert("RGB")
if self.transform is not None:
image = self.transform(image)
data[None] = image
def load_camera(self, model_path, idx, data={}):
''' Loads an image.
Args:
model_path (str): path to model
idx (int): ID of data point
data (dict): data dictionary
'''
camera_file = os.path.join(model_path, 'cameras.npz')
camera_dict = np.load(camera_file)
if len(self.ignore_image_idx) > 0:
n_files = self.get_number_files(model_path, ignore_filtering=True)
idx_list = [i for i in range(
n_files) if i not in self.ignore_image_idx]
idx_list.sort()
idx = idx_list[idx]
camera_file = os.path.join(model_path, 'cameras.npz')
camera_dict = np.load(camera_file)
Rt = camera_dict['world_mat_%d' % idx].astype(np.float32)
K = camera_dict['camera_mat_%d' % idx].astype(np.float32)
S = camera_dict.get(
'scale_mat_%d' % idx, np.eye(4)).astype(np.float32)
data['world_mat'] = Rt
data['camera_mat'] = K
data['scale_mat'] = S
def load_mask(self, model_path, idx, data={}):
''' Loads an object mask.
Args:
model_path (str): path to model
idx (int): ID of data point
data (dict): data dictionary
'''
filename = self.return_idx_filename(
model_path, self.mask_folder_name, self.mask_extension, idx)
mask = np.array(Image.open(filename)).astype(np.bool)
mask = mask.reshape(mask.shape[0], mask.shape[1], -1)[:, :, 0]
data['mask'] = mask.astype(np.float32)
def load_depth(self, model_path, idx, data={}):
''' Loads a depth map.
Args:
model_path (str): path to model
idx (int): ID of data point
data (dict): data dictionary
'''
filename = self.return_idx_filename(
model_path, self.depth_folder_name, self.depth_extension, idx)
depth = np.array(imageio.imread(filename)).astype(np.float32)
depth = depth.reshape(depth.shape[0], depth.shape[1], -1)[:, :, 0]
data['depth'] = depth
def load_visual_hull_depth(self, model_path, idx, data={}):
''' Loads a visual hull depth map.
Args:
model_path (str): path to model
idx (int): ID of data point
data (dict): data dictionary
'''
filename = self.return_idx_filename(
model_path, self.visual_hull_depth_folder, self.depth_extension,
idx)
depth = np.array(imageio.imread(filename)).astype(np.float32)
depth = depth.reshape(
depth.shape[0], depth.shape[1], -1)[:, :, 0]
data['depth'] = depth
def load_field(self, model_path, idx, category, input_idx_img=None):
''' Loads the data point.
Args:
model_path (str): path to model
idx (int): ID of data point
category (int): index of category
input_idx_img (int): image id which should be used (this
overwrites any other id). This is used when the fields are
cached.
'''
n_files = self.get_number_files(model_path)
if input_idx_img is not None:
idx_img = input_idx_img
elif self.random_view:
idx_img = random.randint(0, n_files - 1)
else:
idx_img = 0
# Load the data
data = {}
self.load_image(model_path, idx_img, data)
if self.with_camera:
self.load_camera(model_path, idx_img, data)
if self.with_mask:
self.load_mask(model_path, idx_img, data)
if self.with_depth:
self.load_depth(model_path, idx_img, data)
if self.depth_from_visual_hull:
self.load_visual_hull_depth(model_path, idx_img, data)
return data
def check_complete(self, files):
''' Check if field is complete.
Args:
files: files
'''
complete = (self.folder_name in files)
# TODO: check camera
return complete
class CameraField(Field):
''' Image Field.
It is the field used for loading the camera dictionary.
Args:
n_views (int): number of views
as_float (bool): whether to return the matrices as float
(instead of double)
'''
def __init__(self, n_views, as_float=True):
self.n_views = n_views
self.as_float = as_float
# def load(self, model_path, **kwargs):
def load(self, model_path, idx, category, input_idx_img=None):
''' Loads the camera field.
Args:
model_path (str): path to model
idx (int): ID of data point
category (int): index of category
input_idx_img (int): image id which should be used (this
overwrites any other id). This is used when the fields are
cached.
'''
camera_file = os.path.join(model_path, 'cameras.npz')
cam = np.load(camera_file)
data = {}
dtype = np.float32 if self.as_float else np.float64
for i in range(self.n_views):
data['camera_mat_%d' % i] = cam.get(
'camera_mat_%d' % i).astype(dtype)
data['world_mat_%d' % i] = cam.get(
'world_mat_%d' % i).astype(dtype)
data['scale_mat_%d' % i] = cam.get(
'scale_mat_%d' % i, np.eye(4)).astype(dtype)
return data
class PointCloudField(Field):
''' Point cloud field.
It provides the field used for point cloud data. These are the points
randomly sampled on the mesh.
Args:
file_name (str): file name
transform (list): list of transformations applied to data points
with_transforms (bool): whether scaling and rotation dat should be
provided
'''
def __init__(self, file_name, transform=None, with_transforms=False):
self.file_name = file_name
self.transform = transform
self.with_transforms = with_transforms
def load(self, model_path, idx, category):
''' Loads the data point.
Args:
model_path (str): path to model
idx (int): ID of data point
category (int): index of category
'''
file_path = os.path.join(model_path, self.file_name)
pointcloud_dict = np.load(file_path)
points = pointcloud_dict['points'].astype(np.float32)
normals = pointcloud_dict['normals'].astype(np.float32)
data = {
None: points,
'normals': normals,
}
if self.with_transforms:
data['loc'] = pointcloud_dict['loc'].astype(np.float32)
data['scale'] = pointcloud_dict['scale'].astype(np.float32)
if self.transform is not None:
data = self.transform(data)
return data
def check_complete(self, files):
''' Check if field is complete.
Args:
files: files
'''
complete = (self.file_name in files)
return complete