-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
181 lines (151 loc) · 8.33 KB
/
extract.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
import open3d as o3d
import numpy as np
from o3d_tools.io import read_csv, read_points, read_objects, read_masks, read_object_bb, get_global_mask
class PointCloudProject:
def __init__(self, project):
self.project = project
# Read Point cloud file
self.pcd = read_points(project)
# Read objects bounding boxes as dict of pandas df's
self.objects_df = read_objects(project)
# Convert object bounding boxes into Open3d objects (for drawing purposes)
self.objects = read_object_bb(self.objects_df)
# Read object mask as dict
self.masks = read_masks(project)
# Convert to global mask (for drawing purposes)
self.global_mask = get_global_mask(self.masks)
# Read CSV file
self.read_csv = read_csv(project)
def add_mask(self):
# Read mask
mask_indices = self.global_mask
# Create a boolean mask
mask = np.zeros(np.array(self.pcd.points).shape[0], dtype=bool)
mask[mask_indices] = True
# Get only mask subset of point cloud
masked_points = np.array(self.pcd.points)[mask, :]
masked_colors = np.array(self.pcd.colors)[mask, :]
masked_colors = np.array([[0, 1, 0] for row in masked_colors])
masked_pcd = o3d.geometry.PointCloud()
masked_pcd.points = o3d.utility.Vector3dVector(masked_points)
masked_pcd.colors = o3d.utility.Vector3dVector(masked_colors)
return masked_pcd
def add_invmask(self):
# Read mask
mask_indices = self.global_mask
# Create a boolean mask
mask = np.zeros(np.array(self.pcd.points).shape[0], dtype=bool)
mask[mask_indices] = True
# Get only mask subset of point cloud
inv_masked_points = np.array(self.pcd.points)[~mask, :]
inv_masked_colors = np.array(self.pcd.colors)[~mask, :]
inv_masked_pcd = o3d.geometry.PointCloud()
inv_masked_pcd.points = o3d.utility.Vector3dVector(inv_masked_points)
inv_masked_pcd.colors = o3d.utility.Vector3dVector(inv_masked_colors)
return inv_masked_pcd
def resample_points(self, pcd, num_points):
points = np.asarray(pcd.points)
colors = np.asarray(pcd.colors)
num_rows = len(points)
if num_rows > num_points:
# If there are more points, randomly select a subset
indices = np.random.choice(num_rows, num_points, replace=False)
resampled_points = points[indices]
resampled_colors = colors[indices]
elif num_rows < num_points:
# If there are fewer points, randomly sample points to fill
diff = num_points - num_rows
sampled_indices = np.random.choice(num_rows, diff, replace=True)
sampled_points = points[sampled_indices]
sampled_colors = colors[sampled_indices]
resampled_points = np.vstack((points, sampled_points))
resampled_colors = np.vstack((colors, sampled_colors))
else:
# If exactly `num_points`, keep the array as is
resampled_points = points
resampled_colors = colors
# Create a new point cloud object with the resampled points and colors
resampled_pcd = o3d.geometry.PointCloud()
resampled_pcd.points = o3d.utility.Vector3dVector(resampled_points)
resampled_pcd.colors = o3d.utility.Vector3dVector(resampled_colors)
return resampled_pcd
def resample_points_and_points_labels(self, pcd, points_labels, num_points):
points = np.asarray(pcd.points)
colors = np.asarray(pcd.colors)
points_labels = np.asarray(points_labels)
num_rows = len(points)
if num_rows > num_points:
# If there are more points, randomly select a subset
indices = np.random.choice(num_rows, num_points, replace=False)
resampled_points = points[indices]
resampled_colors = colors[indices]
resampled_points_labels = points_labels[indices]
elif num_rows < num_points:
# If there are fewer points, randomly sample points to fill
diff = num_points - num_rows
sampled_indices = np.random.choice(num_rows, diff, replace=True)
sampled_points = points[sampled_indices]
sampled_colors = colors[sampled_indices]
sampled_points_labels = points_labels[sampled_indices]
resampled_points = np.vstack((points, sampled_points))
resampled_colors = np.vstack((colors, sampled_colors))
resampled_points_labels = np.hstack((points_labels, sampled_points_labels))
else:
# If exactly `num_points`, keep the array as is
resampled_points = points
resampled_colors = colors
resampled_points_labels = points_labels
# Create a new point cloud object with the resampled points and colors
resampled_pcd = o3d.geometry.PointCloud()
resampled_pcd.points = o3d.utility.Vector3dVector(resampled_points)
resampled_pcd.colors = o3d.utility.Vector3dVector(resampled_colors)
return resampled_pcd, resampled_points_labels
def extract_box(self, resample=False, num_points=1024):
boxed_pcd = []
for label in self.objects.keys():
for bb in self.objects[label]:
cropped_pcd = self.pcd.crop(bb)
if resample:
cropped_pcd = self.resample_points(cropped_pcd, num_points)
boxed_pcd.append(cropped_pcd)
return boxed_pcd
def extract_box_and_label(self, resample=False, num_points=1024):
labels = []
boxed_pcd = []
for label in self.objects.keys():
for bb in self.objects[label]:
cropped_pcd = self.pcd.crop(bb)
if resample:
cropped_pcd = self.resample_points(cropped_pcd, num_points)
labels.append(label)
boxed_pcd.append(cropped_pcd)
return boxed_pcd, labels
def extract_box_and_points_labels(self, resample=False, num_points=1024):
points_labels = []
boxed_pcd = []
for label in self.objects.keys():
for bb in self.objects[label]:
cropped_mask_pcd = self.add_mask().crop(bb)
cropped_inv_mask_pcd = self.add_invmask().crop(bb)
# mask_labels = np.vstack((np.ones(np.array(cropped_mask_pcd.points).shape[0]), np.zeros(np.array(cropped_mask_pcd.points).shape[0]))).T
# inv_mask_labels = np.vstack((np.zeros(np.array(cropped_inv_mask_pcd.points).shape[0]), np.ones(np.array(cropped_inv_mask_pcd.points).shape[0]))).T
mask_labels = np.ones(np.array(cropped_mask_pcd.points).shape[0])
inv_mask_labels = np.zeros(np.array(cropped_inv_mask_pcd.points).shape[0])
mask_points = np.array(cropped_mask_pcd.points)
inv_mask_points = np.array(cropped_inv_mask_pcd.points)
mask_colors = np.array(cropped_mask_pcd.colors)
inv_mask_colors = np.array(cropped_inv_mask_pcd.colors)
cropped_pcd_labels = np.concatenate((mask_labels, inv_mask_labels))
cropped_pcd = o3d.geometry.PointCloud()
cropped_pcd.points = o3d.utility.Vector3dVector(np.concatenate((mask_points, inv_mask_points)))
cropped_pcd.colors = o3d.utility.Vector3dVector(np.concatenate((mask_colors, inv_mask_colors)))
# permutation = np.random.permutation(len(cropped_pcd.points))
# # Shuffle both arrays using the permutation
# cropped_pcd.points = o3d.utility.Vector3dVector(np.array(cropped_pcd.points)[permutation])
# cropped_pcd.colors = o3d.utility.Vector3dVector(np.array(cropped_pcd.colors)[permutation])
# cropped_pcd_labels = cropped_pcd_labels[permutation]
if resample:
cropped_pcd, cropped_pcd_labels = self.resample_points_and_points_labels(cropped_pcd, cropped_pcd_labels, num_points)
points_labels.append(cropped_pcd_labels)
boxed_pcd.append(cropped_pcd)
return boxed_pcd, points_labels