-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.create_partial_jsons.py
More file actions
327 lines (252 loc) · 9.93 KB
/
3.create_partial_jsons.py
File metadata and controls
327 lines (252 loc) · 9.93 KB
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
import numpy as np
import scipy
from pathlib import Path
from plyfile import PlyData
import open3d as o3d
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import copy
# import mpi4py
from tqdm import tqdm
import json
import scipy.spatial.distance as dist
import sys
import ray
ITEMS_TO_SKIP = [1, 2, 22, 31] # FLOOR, WALL, CEILING, PERSON
def get_transf_and_bb(ref_pcd):
ref_pcd = copy.deepcopy(ref_pcd)
obj_flat = copy.deepcopy(ref_pcd)
obj_flat[:, 2] = 0
pca = PCA(3)
pca.fit(obj_flat)
components = pca.components_
transf = np.array(
[components[0], [-components[0, 1], components[0, 0], 0], [0, 0, 1]]
)
mean = pca.mean_
rotated_ref_pcd = (ref_pcd) @ transf.T
width = rotated_ref_pcd[:, 0].max() - rotated_ref_pcd[:, 0].min()
length = rotated_ref_pcd[:, 1].max() - rotated_ref_pcd[:, 1].min()
height = rotated_ref_pcd[:, 2].max() - rotated_ref_pcd[:, 2].min()
return mean, transf, (width, length, height)
def transform_pcd(mean, transf, pcd):
pcd = copy.deepcopy(pcd)
pcd = pcd - mean
pcd = np.matmul(pcd, transf.T)
return pcd, transf
def transform_pcd_old(ref_pcd, pcd):
ref_pcd = copy.deepcopy(ref_pcd)
pcd = copy.deepcopy(pcd)
obj_flat = ref_pcd
obj_flat[:, 2] = 0
pca = PCA(3)
pca.fit(obj_flat)
components = pca.components_
transf = np.array(
[components[0], [-components[0, 1], components[0, 0], 0], [0, 0, 1]]
)
mean = pca.mean_
pcd = pcd - mean
pcd = np.matmul(pcd, transf.T)
rotated_ref_pcd = (ref_pcd) @ transf.T
width = rotated_ref_pcd[:, 0].max() - rotated_ref_pcd[:, 0].min()
length = rotated_ref_pcd[:, 1].max() - rotated_ref_pcd[:, 1].min()
height = rotated_ref_pcd[:, 2].max() - rotated_ref_pcd[:, 2].min()
# ll = np.array([rotated_ref_pcd[:, 0].min(), rotated_ref_pcd[:, 1].min()])
# new=[ll,ll+[width,0],ll+[width,length],ll+[0,length],ll]
# new=np.array(new)
return pcd, transf, (width, length, height)
def test_eval_scene(scene_ply_name):
ply = PlyData.read(scene_ply_name)
vertex = ply["vertex"]
xyz = np.array([vertex["x"], vertex["y"], vertex["z"]]).T
instance = np.array(vertex["instance"])
label = np.array(vertex["label"])
label_set = set(np.unique(label)) - {0}
for lab in label_set:
label_ind = label == lab
if lab < 4:
continue
ind_set = np.unique(instance[label_ind])
for inst in ind_set:
instance_ind = instance == inst
obj = xyz[label_ind & instance_ind]
obj_flat = obj.copy()
obj_flat[:, 2] = 0
pca = PCA(3)
pca.fit(obj_flat)
components = pca.components_
axis0 = components[0] # + pca.mean_
axis0 = np.array([axis0 * -1 + pca.mean_, axis0 * 1 + pca.mean_])
axis1 = components[1] # + pca.mean_
axis1 = np.array([axis1 * -1 + pca.mean_, axis1 * 1 + pca.mean_])
axis2 = components[2] # + pca.mean_
axis2 = np.array([axis2 * -1 + pca.mean_, axis2 * 1 + pca.mean_])
print(lab)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.scatter(obj[:, 0], obj[:, 1], obj[:, 2])
ax.plot(axis0[:, 0], axis0[:, 1], axis0[:, 2], "r")
ax.plot(axis1[:, 0], axis1[:, 1], axis1[:, 2], "g")
ax.plot(axis2[:, 0], axis2[:, 1], axis2[:, 2], "y")
transformer_obj = transform_pcd(obj, obj)
ax.scatter(
transformer_obj[:, 0],
transformer_obj[:, 1],
transformer_obj[:, 2],
color="orange",
)
plt.show()
pass
pass
@ray.remote
def calc_scene_distances(list_): # (partial_scene, complete_scene):
# print("start")
partial_scene = list_[0]
complete_scene = list_[1]
out_dir = Path("partial_jsons")
out_dir.mkdir(exist_ok=True)
# print("ready")
scene_pcd = PlyData.read(str(partial_scene))
vert = scene_pcd["vertex"]
xyz = np.stack([vert["x"], vert["y"], vert["z"]], 1)
label = np.array(vert["label"])
instance = np.array(vert["instance"])
j = {}
j["center"] = []
j["labels"] = []
j["distances"] = []
j["relative_pos"] = []
j["masked"] = []
j["bb_shape"] = []
seen_instances = []
############## objects in partial
for obj_i in range(1, 38):
if obj_i in ITEMS_TO_SKIP:
continue
ind_obj_i = label == obj_i
instances_i = sorted(np.unique(instance[ind_obj_i]))
for inst_i in instances_i:
pcd_i = xyz[(instance == inst_i) & ind_obj_i]
if pcd_i.shape[0] < 50:
continue
mean, transf, bb = get_transf_and_bb(pcd_i)
seen_instances.append(inst_i)
kdtree = scipy.spatial.cKDTree(pcd_i)
j["labels"].append(obj_i)
j["center"].append(((pcd_i.max(0) + pcd_i.min(0)) / 2).tolist())
j["masked"].append(0)
j["bb_shape"].append([bb[0], bb[1], bb[2]])
tmp_dist = []
tmp_rel = []
for obj_j in range(1, 38):
if obj_j in ITEMS_TO_SKIP:
continue
ind_obj_j = label == obj_j
instances_j = sorted(np.unique(instance[ind_obj_j]))
for inst_j in instances_j:
pcd_j = xyz[(instance == inst_j) & ind_obj_j]
if pcd_j.shape[0] < 50:
continue
cur_min = np.inf
# for i in range(0, pcd_j.shape[0], 1300):
# cur_min = min(
# dist.cdist(pcd_i, pcd_j[i : i + 300]).min(), cur_min
# )
cur_min = kdtree.query(pcd_j)[0].min()
# pcd_j_new, _ = transform_pcd_old(pcd_i, pcd_j)
pcd_j_new, _ = transform_pcd(mean, transf, pcd_j)
rel = ((pcd_j_new.max(0) + pcd_j_new.min(0)) / 2).tolist()
tmp_dist.append(cur_min)
tmp_rel.append(rel)
j["distances"].append(tmp_dist)
j["relative_pos"].append(tmp_rel)
########### complete scene for missing items
complete_pcd = PlyData.read(str(complete_scene))
complete_vert = complete_pcd["vertex"]
complete_xyz = np.stack(
[complete_vert["x"], complete_vert["y"], complete_vert["z"]], 1
)
complete_label = np.array(complete_vert["label"])
complete_instance = np.array(complete_vert["instance"])
instances_j_seen = []
j["masked_center"] = []
j["masked_label"] = []
j["distance_to_masked"] = []
j["relative_pos_to_masked"] = []
for obj_i in range(1, 38):
if obj_i in ITEMS_TO_SKIP:
continue
ind_obj_i = label == obj_i
instances_i = sorted(np.unique(instance[ind_obj_i]))
for inst_i in instances_i:
pcd_i = xyz[(instance == inst_i) & ind_obj_i]
if pcd_i.shape[0] < 50:
continue
mean, transf, bb = get_transf_and_bb(pcd_i)
kdtree = scipy.spatial.cKDTree(pcd_i)
tmp_dist = []
tmp_rel = []
for obj_j in range(1, 38):
if obj_j in ITEMS_TO_SKIP:
continue
ind_obj_j = complete_label == obj_j
instances_j = sorted(np.unique(complete_instance[ind_obj_j]))
for inst_j in instances_j:
if inst_j in seen_instances:
continue
pcd_j = complete_xyz[(complete_instance == inst_j) & ind_obj_j]
if inst_j not in instances_j_seen:
j["masked_center"].append(
((pcd_j.max(0) + pcd_j.min(0)) / 2).tolist()
)
j["masked_label"].append(obj_j)
instances_j_seen.append(inst_j)
cur_min = np.inf
# for i in range(0, pcd_j.shape[0], 1300):
# cur_min = min(
# dist.cdist(pcd_i, pcd_j[i : i + 300]).min(), cur_min
# )
cur_min = kdtree.query(pcd_j)[0].min()
# pcd_j_new, _ = transform_pcd_old(pcd_i, pcd_j)
pcd_j_new, _ = transform_pcd(mean, transf, pcd_j)
rel = ((pcd_j_new.max(0) + pcd_j_new.min(0)) / 2).tolist()
tmp_dist.append(cur_min)
tmp_rel.append(rel)
j["distance_to_masked"].append(tmp_dist)
j["relative_pos_to_masked"].append(tmp_rel)
if len(j['masked'])==0 or len(j['labels'])<3:
return
with (out_dir / f"{complete_scene.stem}_{partial_scene.stem}.json").open("w") as f:
json.dump(j, f)
return j
def main():
scene_ply_name = sorted(list(Path("annotated_ply").glob("*.ply")))
ROOT_PARTIAL_DIR = Path("partial_pcds")
SCENE_DIR = sorted([x for x in ROOT_PARTIAL_DIR.iterdir() if x.is_dir()])
ROOT_COMPLETE_DIR = Path("annotated_ply")
jobs = []
arg_list = []
for scene in SCENE_DIR:
partial_scenes = sorted(list(scene.glob("*.ply")))
scene_name = scene.stem
complete = ROOT_COMPLETE_DIR / f"{scene_name}.ply"
for partial in partial_scenes:
jobs.append(calc_scene_distances.remote((partial, complete)))
pass
pass
with tqdm(total=len(jobs)) as pbar:
unfinished = jobs
num_ret = min(len(unfinished), 3)
while unfinished:
num_ret = min(len(unfinished), 3)
## Returns the first ObjectRef that is ready.
# print(len(unfinished))
# print(num_ret)
finished, unfinished = ray.wait(unfinished, num_returns=num_ret)
result = ray.get(finished)
pbar.update(num_ret)
sys.stdout.flush()
if __name__ == "__main__":
ray.init()
main()