-
Notifications
You must be signed in to change notification settings - Fork 214
/
provider.py
725 lines (690 loc) · 33.8 KB
/
provider.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
"""
Large-scale Point Cloud Semantic Segmentation with Superpoint Graphs
http://arxiv.org/abs/1711.09869
2017 Loic Landrieu, Martin Simonovsky
functions for writing and reading features and superpoint graph
"""
import os
import sys
import random
import glob
from plyfile import PlyData, PlyElement
import numpy as np
#from numpy import genfromtxt
import pandas as pd
import h5py
#import laspy
from sklearn.neighbors import NearestNeighbors
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(DIR_PATH, '..'))
from partition.ply_c import libply_c
import colorsys
from sklearn.decomposition import PCA
#------------------------------------------------------------------------------
def partition2ply(filename, xyz, components):
"""write a ply with random colors for each components"""
random_color = lambda: random.randint(0, 255)
color = np.zeros(xyz.shape)
for i_com in range(0, len(components)):
color[components[i_com], :] = [random_color(), random_color()
, random_color()]
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1')
, ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = xyz[:, i]
for i in range(0, 3):
vertex_all[prop[i+3][0]] = color[:, i]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def geof2ply(filename, xyz, geof):
"""write a ply with colors corresponding to geometric features"""
color = np.array(255 * geof[:, [0, 1, 3]], dtype='uint8')
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = xyz[:, i]
for i in range(0, 3):
vertex_all[prop[i+3][0]] = color[:, i]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def prediction2ply(filename, xyz, prediction, n_label, dataset):
"""write a ply with colors for each class"""
if len(prediction.shape) > 1 and prediction.shape[1] > 1:
prediction = np.argmax(prediction, axis = 1)
color = np.zeros(xyz.shape)
for i_label in range(0, n_label + 1):
color[np.where(prediction == i_label), :] = get_color_from_label(i_label, dataset)
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = xyz[:, i]
for i in range(0, 3):
vertex_all[prop[i+3][0]] = color[:, i]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def error2ply(filename, xyz, rgb, labels, prediction):
"""write a ply with green hue for correct classifcation and red for error"""
if len(prediction.shape) > 1 and prediction.shape[1] > 1:
prediction = np.argmax(prediction, axis = 1)
if len(labels.shape) > 1 and labels.shape[1] > 1:
labels = np.argmax(labels, axis = 1)
color_rgb = rgb/255
for i_ver in range(0, len(labels)):
color_hsv = list(colorsys.rgb_to_hsv(color_rgb[i_ver,0], color_rgb[i_ver,1], color_rgb[i_ver,2]))
if (labels[i_ver] == prediction[i_ver]) or (labels[i_ver]==0):
color_hsv[0] = 0.333333
else:
color_hsv[0] = 0
color_hsv[1] = min(1, color_hsv[1] + 0.3)
color_hsv[2] = min(1, color_hsv[2] + 0.1)
color_rgb[i_ver,:] = list(colorsys.hsv_to_rgb(color_hsv[0], color_hsv[1], color_hsv[2]))
color_rgb = np.array(color_rgb*255, dtype='u1')
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = xyz[:, i]
for i in range(0, 3):
vertex_all[prop[i+3][0]] = color_rgb[:, i]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def spg2ply(filename, spg_graph):
"""write a ply displaying the SPG by adding edges between its centroid"""
vertex_prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]
vertex_val = np.empty((spg_graph['sp_centroids']).shape[0], dtype=vertex_prop)
for i in range(0, 3):
vertex_val[vertex_prop[i][0]] = spg_graph['sp_centroids'][:, i]
edges_prop = [('vertex1', 'int32'), ('vertex2', 'int32')]
edges_val = np.empty((spg_graph['source']).shape[0], dtype=edges_prop)
edges_val[edges_prop[0][0]] = spg_graph['source'].flatten()
edges_val[edges_prop[1][0]] = spg_graph['target'].flatten()
ply = PlyData([PlyElement.describe(vertex_val, 'vertex'), PlyElement.describe(edges_val, 'edge')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def scalar2ply(filename, xyz, scalar):
"""write a ply with an unisgned integer scalar field"""
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('scalar', 'f4')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = xyz[:, i]
vertex_all[prop[3][0]] = scalar
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def get_color_from_label(object_label, dataset):
"""associate the color corresponding to the class"""
if dataset == 's3dis': #S3DIS
object_label = {
0: [0 , 0, 0], #unlabelled .->. black
1: [ 233, 229, 107], #'ceiling' .-> .yellow
2: [ 95, 156, 196], #'floor' .-> . blue
3: [ 179, 116, 81], #'wall' -> brown
4: [ 81, 163, 148], #'column' -> bluegreen
5: [ 241, 149, 131], #'beam' -> salmon
6: [ 77, 174, 84], #'window' -> bright green
7: [ 108, 135, 75], #'door' -> dark green
8: [ 79, 79, 76], #'table' -> dark grey
9: [ 41, 49, 101], #'chair' -> darkblue
10: [223, 52, 52], #'bookcase' -> red
11: [ 89, 47, 95], #'sofa' -> purple
12: [ 81, 109, 114], #'board' -> grey
13: [233, 233, 229], #'clutter' -> light grey
}.get(object_label, -1)
elif (dataset == 'sema3d'): #Semantic3D
object_label = {
0: [0 , 0, 0], #unlabelled .->. black
1: [ 200, 200, 200], #'man-made terrain' -> grey
2: [ 0, 70, 0], #'natural terrain' -> dark green
3: [ 0, 255, 0], #'high vegetation' -> bright green
4: [ 255, 255, 0], #'low vegetation' -> yellow
5: [ 255, 0, 0], #'building' -> red
6: [ 148, 0, 211], #'hard scape' -> violet
7: [ 0, 255, 255], #'artifact' -> cyan
8: [ 255, 8, 127], #'cars' -> pink
}.get(object_label, -1)
elif (dataset == 'vkitti'): #vkitti3D
object_label = {
0: [ 0, 0, 0], # None-> black
1: [ 200, 90, 0], # Terrain .->.brown
2: [ 0, 128, 50], # Tree -> dark green
3: [ 0, 220, 0], # Vegetation-> bright green
4: [ 255, 0, 0], # Building-> red
5: [ 100, 100, 100] , # Road-> dark gray
6: [ 200, 200, 200], # GuardRail-> bright gray
7: [ 255, 0, 255], # TrafficSign-> pink
8: [ 255, 255, 0], # TrafficLight-> yellow
9: [ 128, 0, 255], # Pole-> violet
10: [ 255, 200, 150], # Misc-> skin
11: [ 0, 128, 255], # Truck-> dark blue
12: [ 0, 200, 255], # Car-> bright blue
13: [ 255, 128, 0], # Van-> orange
}.get(object_label, -1)
elif (dataset == 'custom_dataset'): #Custom set
object_label = {
0: [0 , 0, 0], #unlabelled .->. black
1: [ 255, 0, 0], #'classe A' -> red
2: [ 0, 255, 0], #'classeB' -> green
}.get(object_label, -1)
else:
raise ValueError('Unknown dataset: %s' % (dataset))
if object_label == -1:
raise ValueError('Type not recognized: %s' % (object_label))
return object_label
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
def read_s3dis_format(raw_path, label_out=True):
#S3DIS specific
"""extract data from a room folder"""
#room_ver = genfromtxt(raw_path, delimiter=' ')
room_ver = pd.read_csv(raw_path, sep=' ', header=None).values
xyz = np.ascontiguousarray(room_ver[:, 0:3], dtype='float32')
try:
rgb = np.ascontiguousarray(room_ver[:, 3:6], dtype='uint8')
except ValueError:
rgb = np.zeros((room_ver.shape[0],3), dtype='uint8')
print('WARN - corrupted rgb data for file %s' % raw_path)
if not label_out:
return xyz, rgb
n_ver = len(room_ver)
del room_ver
nn = NearestNeighbors(1, algorithm='kd_tree').fit(xyz)
room_labels = np.zeros((n_ver,), dtype='uint8')
room_object_indices = np.zeros((n_ver,), dtype='uint32')
objects = glob.glob(os.path.dirname(raw_path) + "/Annotations/*.txt")
i_object = 1
for single_object in objects:
object_name = os.path.splitext(os.path.basename(single_object))[0]
print(" adding object " + str(i_object) + " : " + object_name)
object_class = object_name.split('_')[0]
object_label = object_name_to_label(object_class)
#obj_ver = genfromtxt(single_object, delimiter=' ')
obj_ver = pd.read_csv(single_object, sep=' ', header=None).values
distances, obj_ind = nn.kneighbors(obj_ver[:, 0:3])
room_labels[obj_ind] = object_label
room_object_indices[obj_ind] = i_object
i_object = i_object + 1
return xyz, rgb, room_labels, room_object_indices
#------------------------------------------------------------------------------
def read_vkitti_format(raw_path):
#S3DIS specific
"""extract data from a room folder"""
data = np.load(raw_path)
xyz = data[:, 0:3]
rgb = data[:, 3:6]
labels = data[:, -1]+1
labels[(labels==14).nonzero()] = 0
return xyz, rgb, labels
#------------------------------------------------------------------------------
def object_name_to_label(object_class):
"""convert from object name in S3DIS to an int"""
object_label = {
'ceiling': 1,
'floor': 2,
'wall': 3,
'column': 4,
'beam': 5,
'window': 6,
'door': 7,
'table': 8,
'chair': 9,
'bookcase': 10,
'sofa': 11,
'board': 12,
'clutter': 13,
'stairs': 0,
}.get(object_class, 0)
return object_label
#------------------------------------------------------------------------------
def read_semantic3d_format(data_file, n_class, file_label_path, voxel_width, ver_batch):
"""read the format of semantic3d.
ver_batch : if ver_batch>0 then load the file ver_batch lines at a time.
useful for huge files (> 5millions lines)
voxel_width: if voxel_width>0, voxelize data with a regular grid
n_class : the number of class; if 0 won't search for labels (test set)
implements batch-loading for huge files
and pruning"""
xyz = np.zeros((0, 3), dtype='float32')
rgb = np.zeros((0, 3), dtype='uint8')
labels = np.zeros((0, n_class+1), dtype='uint32')
#---the clouds can potentially be too big to parse directly---
#---they are cut in batches in the order they are stored---
def process_chunk(vertex_chunk, label_chunk, has_labels, xyz, rgb, labels):
xyz_full = np.ascontiguousarray(np.array(vertex_chunk.values[:, 0:3], dtype='float32'))
rgb_full = np.ascontiguousarray(np.array(vertex_chunk.values[:, 4:7], dtype='uint8'))
if has_labels:
labels_full = label_chunk.values.squeeze()
else:
labels_full = None
if voxel_width > 0:
if has_labels > 0:
xyz_sub, rgb_sub, labels_sub, objets_sub = libply_c.prune(xyz_full, voxel_width
, rgb_full, labels_full , np.zeros(1, dtype='uint8'), n_class, 0)
labels = np.vstack((labels, labels_sub))
del labels_full
else:
xyz_sub, rgb_sub, l, o = libply_c.prune(xyz_full, voxel_width
, rgb_full, np.zeros(1, dtype='uint8'), np.zeros(1, dtype='uint8'), 0,0)
xyz = np.vstack((xyz, xyz_sub))
rgb = np.vstack((rgb, rgb_sub))
else:
xyz = xyz_full
rgb = xyz_full
labels = labels_full
return xyz, rgb, labels
if n_class>0:
for (i_chunk, (vertex_chunk, label_chunk)) in \
enumerate(zip(pd.read_csv(data_file,chunksize=ver_batch, delimiter=' '), \
pd.read_csv(file_label_path, dtype="u1",chunksize=ver_batch, header=None))):
print("processing lines %d to %d" % (i_chunk * ver_batch, (i_chunk+1) * ver_batch))
xyz, rgb, labels = process_chunk(vertex_chunk, label_chunk, 1, xyz, rgb, labels)
else:
for (i_chunk, vertex_chunk) in enumerate(pd.read_csv(data_file, delimiter=' ',chunksize=ver_batch, header=None)):
print("processing lines %d to %d" % (i_chunk * ver_batch, (i_chunk+1) * ver_batch))
xyz, rgb, dump = process_chunk(vertex_chunk, None, 0, xyz, rgb, None)
print("Reading done")
if n_class>0:
return xyz, rgb, labels
else:
return xyz, rgb
#------------------------------------------------------------------------------
def read_semantic3d_format2(data_file, n_class, file_label_path, voxel_width, ver_batch):
"""read the format of semantic3d.
ver_batch : if ver_batch>0 then load the file ver_batch lines at a time.
useful for huge files (> 5millions lines)
voxel_width: if voxel_width>0, voxelize data with a regular grid
n_class : the number of class; if 0 won't search for labels (test set)
implements batch-loading for huge files
and pruning"""
xyz = np.zeros((0, 3), dtype='float32')
rgb = np.zeros((0, 3), dtype='uint8')
labels = np.zeros((0, n_class+1), dtype='uint32')
#---the clouds can potentially be too big to parse directly---
#---they are cut in batches in the order they are stored---
i_rows = 0
while True:
try:
head = None
if ver_batch>0:
print("Reading lines %d to %d" % (i_rows, i_rows + ver_batch))
vertices = np.genfromtxt(data_file
, delimiter=' ', max_rows=ver_batch
, skip_header=i_rows)
#if i_rows > 0:
# head = i_rows-1
#vertices = pd.read_csv(data_file
# , sep=' ', nrows=ver_batch
# , header=head).values
else:
#vertices = np.genfromtxt(data_file, delimiter=' ')
vertices = np.pd.read_csv(data_file, sep=' ', header=None).values
break
except (StopIteration, pd.errors.ParserError):
#end of file
break
if len(vertices)==0:
break
xyz_full = np.ascontiguousarray(np.array(vertices[:, 0:3], dtype='float32'))
rgb_full = np.ascontiguousarray(np.array(vertices[:, 4:7], dtype='uint8'))
del vertices
if n_class > 0:
#labels_full = pd.read_csv(file_label_path, dtype="u1"
# , nrows=ver_batch, header=head).values.squeeze()
labels_full = np.genfromtxt(file_label_path, dtype="u1", delimiter=' '
, max_rows=ver_batch, skip_header=i_rows)
if voxel_width > 0:
if n_class > 0:
xyz_sub, rgb_sub, labels_sub, objets_sub = libply_c.prune(xyz_full, voxel_width
, rgb_full, labels_full , np.zeros(1, dtype='uint8'), n_class, 0)
labels = np.vstack((labels, labels_sub))
else:
xyz_sub, rgb_sub, l, o = libply_c.prune(xyz_full, voxel_width
, rgb_full, np.zeros(1, dtype='uint8'), np.zeros(1, dtype='uint8'), 0,0)
del xyz_full, rgb_full
xyz = np.vstack((xyz, xyz_sub))
rgb = np.vstack((rgb, rgb_sub))
i_rows = i_rows + ver_batch
print("Reading done")
if n_class>0:
return xyz, rgb, labels
else:
return xyz, rgb
#------------------------------------------------------------------------------
def read_ply(filename):
"""convert from a ply file. include the label and the object number"""
#---read the ply file--------
plydata = PlyData.read(filename)
xyz = np.stack([plydata['vertex'][n] for n in['x', 'y', 'z']], axis=1)
try:
rgb = np.stack([plydata['vertex'][n]
for n in ['red', 'green', 'blue']]
, axis=1).astype(np.uint8)
except ValueError:
rgb = np.stack([plydata['vertex'][n]
for n in ['r', 'g', 'b']]
, axis=1).astype(np.float32)
if np.max(rgb) > 1:
rgb = rgb
try:
object_indices = plydata['vertex']['object_index']
labels = plydata['vertex']['label']
return xyz, rgb, labels, object_indices
except ValueError:
try:
labels = plydata['vertex']['label']
return xyz, rgb, labels
except ValueError:
return xyz, rgb
#------------------------------------------------------------------------------
def read_las(filename):
"""convert from a las file with no rgb"""
#---read the ply file--------
try:
inFile = laspy.file.File(filename, mode='r')
except NameError:
raise ValueError("laspy package not found. uncomment import in /partition/provider and make sure it is installed in your environment")
N_points = len(inFile)
x = np.reshape(inFile.x, (N_points,1))
y = np.reshape(inFile.y, (N_points,1))
z = np.reshape(inFile.z, (N_points,1))
xyz = np.hstack((x,y,z)).astype('f4')
return xyz
#------------------------------------------------------------------------------
import pypcd
from pypcd import pypcd
def read_pcd(filename):
"""convert from a pcd file with no rgb"""
#---read the pcd file--------
pcddata = pypcd.PointCloud.from_path(filename)
xyz = np.mat(np.stack([pcddata.pc_data[n] for n in ['x', 'y', 'z']]))
labels = np.mat(np.mat(pcddata.pc_data['label']))
return xyz.T ,labels.T
#------------------------------------------------------------------------------
def write_ply_obj(filename, xyz, rgb, labels, object_indices):
"""write into a ply file. include the label and the object number"""
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1')
, ('green', 'u1'), ('blue', 'u1'), ('label', 'u1')
, ('object_index', 'uint32')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i_prop in range(0, 3):
vertex_all[prop[i_prop][0]] = xyz[:, i_prop]
for i_prop in range(0, 3):
vertex_all[prop[i_prop+3][0]] = rgb[:, i_prop]
vertex_all[prop[6][0]] = labels
vertex_all[prop[7][0]] = object_indices
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def embedding2ply(filename, xyz, embeddings):
"""write a ply with colors corresponding to geometric features"""
if embeddings.shape[1]>3:
pca = PCA(n_components=3)
#pca.fit(np.eye(embeddings.shape[1]))
pca.fit(np.vstack((np.zeros((embeddings.shape[1],)),np.eye(embeddings.shape[1]))))
embeddings = pca.transform(embeddings)
#value = (embeddings-embeddings.mean(axis=0))/(2*embeddings.std())+0.5
#value = np.minimum(np.maximum(value,0),1)
#value = (embeddings)/(3 * embeddings.std())+0.5
value = np.minimum(np.maximum((embeddings+1)/2,0),1)
color = np.array(255 * value, dtype='uint8')
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = xyz[:, i]
for i in range(0, 3):
vertex_all[prop[i+3][0]] = color[:, i]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def edge_class2ply2(filename, edg_class, xyz, edg_source, edg_target):
"""write a ply with edge weight color coded into the midway point"""
n_edg = len(edg_target)
midpoint = (xyz[edg_source,]+xyz[edg_target,])/2
color = np.zeros((edg_source.shape[0],3), dtype = 'uint8')
color[edg_class==0,] = [0,0,0]
color[(edg_class==1).nonzero(),] = [255,0,0]
color[(edg_class==2).nonzero(),] = [125,255,0]
color[(edg_class==3).nonzero(),] = [0,125,255]
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(n_edg, dtype=prop)
for i in range(0, 3):
vertex_all[prop[i][0]] = np.hstack(midpoint[:, i])
for i in range(3, 6):
vertex_all[prop[i][0]] = color[:,i-3]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def write_ply_labels(filename, xyz, rgb, labels):
"""write into a ply file. include the label"""
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1')
, ('blue', 'u1'), ('label', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i_prop in range(0, 3):
vertex_all[prop[i_prop][0]] = xyz[:, i_prop]
for i_prop in range(0, 3):
vertex_all[prop[i_prop+3][0]] = rgb[:, i_prop]
vertex_all[prop[6][0]] = labels
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def write_ply(filename, xyz, rgb):
"""write into a ply file"""
prop = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
vertex_all = np.empty(len(xyz), dtype=prop)
for i_prop in range(0, 3):
vertex_all[prop[i_prop][0]] = xyz[:, i_prop]
for i_prop in range(0, 3):
vertex_all[prop[i_prop+3][0]] = rgb[:, i_prop]
ply = PlyData([PlyElement.describe(vertex_all, 'vertex')], text=True)
ply.write(filename)
#------------------------------------------------------------------------------
def write_features(file_name, geof, xyz, rgb, graph_nn, labels):
"""write the geometric features, labels and clouds in a h5 file"""
if os.path.isfile(file_name):
os.remove(file_name)
data_file = h5py.File(file_name, 'w')
data_file.create_dataset('geof', data=geof, dtype='float32')
data_file.create_dataset('source', data=graph_nn["source"], dtype='uint32')
data_file.create_dataset('target', data=graph_nn["target"], dtype='uint32')
data_file.create_dataset('distances', data=graph_nn["distances"], dtype='float32')
data_file.create_dataset('xyz', data=xyz, dtype='float32')
if len(rgb) > 0:
data_file.create_dataset('rgb', data=rgb, dtype='uint8')
if len(labels) > 0 and len(labels.shape)>1 and labels.shape[1]>1:
data_file.create_dataset('labels', data=labels, dtype='uint32')
else:
data_file.create_dataset('labels', data=labels, dtype='uint8')
data_file.close()
#------------------------------------------------------------------------------
def read_features(file_name):
"""read the geometric features, clouds and labels from a h5 file"""
data_file = h5py.File(file_name, 'r')
#fist get the number of vertices
n_ver = len(data_file["geof"][:, 0])
has_labels = len(data_file["labels"])
#the labels can be empty in the case of a test set
if has_labels:
labels = np.array(data_file["labels"])
else:
labels = []
#---fill the arrays---
geof = data_file["geof"][:]
xyz = data_file["xyz"][:]
rgb = data_file["rgb"][:]
source = data_file["source"][:]
target = data_file["target"][:]
#---set the graph---
graph_nn = dict([("is_nn", True)])
graph_nn["source"] = source
graph_nn["target"] = target
return geof, xyz, rgb, graph_nn, labels
#------------------------------------------------------------------------------
def write_spg(file_name, graph_sp, components, in_component):
"""save the partition and spg information"""
if os.path.isfile(file_name):
os.remove(file_name)
data_file = h5py.File(file_name, 'w')
grp = data_file.create_group('components')
n_com = len(components)
for i_com in range(0, n_com):
grp.create_dataset(str(i_com), data=components[i_com], dtype='uint32')
data_file.create_dataset('in_component'
, data=in_component, dtype='uint32')
data_file.create_dataset('sp_labels'
, data=graph_sp["sp_labels"], dtype='uint32')
data_file.create_dataset('sp_centroids'
, data=graph_sp["sp_centroids"], dtype='float32')
data_file.create_dataset('sp_length'
, data=graph_sp["sp_length"], dtype='float32')
data_file.create_dataset('sp_surface'
, data=graph_sp["sp_surface"], dtype='float32')
data_file.create_dataset('sp_volume'
, data=graph_sp["sp_volume"], dtype='float32')
data_file.create_dataset('sp_point_count'
, data=graph_sp["sp_point_count"], dtype='uint64')
data_file.create_dataset('source'
, data=graph_sp["source"], dtype='uint32')
data_file.create_dataset('target'
, data=graph_sp["target"], dtype='uint32')
data_file.create_dataset('se_delta_mean'
, data=graph_sp["se_delta_mean"], dtype='float32')
data_file.create_dataset('se_delta_std'
, data=graph_sp["se_delta_std"], dtype='float32')
data_file.create_dataset('se_delta_norm'
, data=graph_sp["se_delta_norm"], dtype='float32')
data_file.create_dataset('se_delta_centroid'
, data=graph_sp["se_delta_centroid"], dtype='float32')
data_file.create_dataset('se_length_ratio'
, data=graph_sp["se_length_ratio"], dtype='float32')
data_file.create_dataset('se_surface_ratio'
, data=graph_sp["se_surface_ratio"], dtype='float32')
data_file.create_dataset('se_volume_ratio'
, data=graph_sp["se_volume_ratio"], dtype='float32')
data_file.create_dataset('se_point_count_ratio'
, data=graph_sp["se_point_count_ratio"], dtype='float32')
#-----------------------------------------------------------------------------
def read_spg(file_name):
"""read the partition and spg information"""
data_file = h5py.File(file_name, 'r')
graph = dict([("is_nn", False)])
graph["source"] = np.array(data_file["source"], dtype='uint32')
graph["target"] = np.array(data_file["target"], dtype='uint32')
graph["sp_centroids"] = np.array(data_file["sp_centroids"], dtype='float32')
graph["sp_length"] = np.array(data_file["sp_length"], dtype='float32')
graph["sp_surface"] = np.array(data_file["sp_surface"], dtype='float32')
graph["sp_volume"] = np.array(data_file["sp_volume"], dtype='float32')
graph["sp_point_count"] = np.array(data_file["sp_point_count"], dtype='uint64')
graph["se_delta_mean"] = np.array(data_file["se_delta_mean"], dtype='float32')
graph["se_delta_std"] = np.array(data_file["se_delta_std"], dtype='float32')
graph["se_delta_norm"] = np.array(data_file["se_delta_norm"], dtype='float32')
graph["se_delta_centroid"] = np.array(data_file["se_delta_centroid"], dtype='float32')
graph["se_length_ratio"] = np.array(data_file["se_length_ratio"], dtype='float32')
graph["se_surface_ratio"] = np.array(data_file["se_surface_ratio"], dtype='float32')
graph["se_volume_ratio"] = np.array(data_file["se_volume_ratio"], dtype='float32')
graph["se_point_count_ratio"] = np.array(data_file["se_point_count_ratio"], dtype='float32')
in_component = np.array(data_file["in_component"], dtype='uint32')
n_com = len(graph["sp_length"])
graph["sp_labels"] = np.array(data_file["sp_labels"], dtype='uint32')
grp = data_file['components']
components = np.empty((n_com,), dtype=object)
for i_com in range(0, n_com):
components[i_com] = np.array(grp[str(i_com)], dtype='uint32').tolist()
return graph, components, in_component
#------------------------------------------------------------------------------
def reduced_labels2full(labels_red, components, n_ver):
"""distribute the labels of superpoints to their repsective points"""
labels_full = np.zeros((n_ver, ), dtype='uint8')
for i_com in range(0, len(components)):
labels_full[components[i_com]] = labels_red[i_com]
return labels_full
#------------------------------------------------------------------------------
def interpolate_labels_batch(data_file, xyz, labels, ver_batch):
"""interpolate the labels of the pruned cloud to the full cloud"""
if len(labels.shape) > 1 and labels.shape[1] > 1:
labels = np.argmax(labels, axis = 1)
i_rows = None
labels_f = np.zeros((0, ), dtype='uint8')
#---the clouds can potentially be too big to parse directly---
#---they are cut in batches in the order they are stored---
nn = NearestNeighbors(n_neighbors=1, algorithm='kd_tree').fit(xyz)
while True:
try:
if ver_batch>0:
if i_rows is None:
print("read lines %d to %d" % (0, ver_batch))
else:
print("read lines %d to %d" % (i_rows, i_rows + ver_batch))
#vertices = np.genfromtxt(data_file
# , delimiter=' ', max_rows=ver_batch
# , skip_header=i_rows)
vertices = pd.read_csv(data_file
, sep=' ', nrows=ver_batch
, header=None if i_rows==None else i_rows-1).values
else:
#vertices = np.genfromtxt(data_file
# , delimiter=' ')
vertices = pd.read_csv(data_file
, delimiter=' ', header=None).values
break
except (StopIteration, pd.errors.ParserError):
#end of file
break
if len(vertices)==0:
break
xyz_full = np.array(vertices[:, 0:3], dtype='float32')
del vertices
distances, neighbor = nn.kneighbors(xyz_full)
del distances
labels_f = np.hstack((labels_f, labels[neighbor].flatten()))
if i_rows is None:
i_rows = ver_batch
else:
i_rows = i_rows + ver_batch
return labels_f
#------------------------------------------------------------------------------
def interpolate_labels(xyz_up, xyz, labels, ver_batch):
"""interpolate the labels of the pruned cloud to the full cloud"""
if len(labels.shape) > 1 and labels.shape[1] > 1:
labels = np.argmax(labels, axis = 1)
nn = NearestNeighbors(n_neighbors=1, algorithm='kd_tree').fit(xyz)
distances, neighbor = nn.kneighbors(xyz_up)
return labels[neighbor].flatten()
#------------------------------------------------------------------------------
def perfect_prediction(components, labels):
"""assign each superpoint with the majority label"""
full_pred = np.zeros((labels.shape[0],),dtype='uint32')
for i_com in range(len(components)):
label_com = labels[components[i_com],1:].sum(0).argmax()
full_pred[components[i_com]]=label_com
return full_pred
#----------------------------------------------------
#SEAL utilities
def compute_gt_connected_components(n_ver, edg_source, edg_target, is_transition, cutoff):
components, in_component = libcp.connected_comp(n_ver,
edg_source.astype('uint32'),
edg_target.astype('uint32'),
is_transition.astype('uint8'), 40) #rough guess
return components, in_component
#----------------------
def write_gt_connected_components(file_name, components, in_component):
"""save the label-based connected components of the ground truth"""
if os.path.isfile(file_name):
os.remove(file_name)
data_file = h5py.File(file_name, 'w')
grp = data_file.create_group('components')
for i_com in range(len(components)):
grp.create_dataset(str(i_com), data=components[i_com], dtype='uint32')
data_file.create_dataset('in_component', data=in_component, dtype='uint32')
#-------------------------------------
def read_gt_connected_components(file_name):
"""read the label-based connected components of the ground truth"""
data_file = h5py.File(file_name, 'r')
in_component = np.array(data_file["in_component"], dtype='uint32')
n_com = np.amax(in_component)
components = np.empty((n_com,), dtype=object)
for i_com in range(0, n_com):
components[i_com] = np.array(grp[str(i_com)], dtype='uint32').tolist()
return components, in_component