-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_point_cloud.py
63 lines (54 loc) · 1.94 KB
/
create_point_cloud.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
import os
import numpy as np
import argparse
import trimesh
from sklearn.preprocessing import MinMaxScaler
parser = argparse.ArgumentParser(description='Sample Points from Meshes and Create Point Cloud Dataset')
parser.add_argument('--path', type=str, help='Path to the dataset', required = True)
parser.add_argument('--N', type=int, help='Number of points to sample from each mesh', default=4096)
args = parser.parse_args()
map = {}
train_points, train_labels = [], []
test_points, test_labels = [], []
DIR_PATH = args.path
for idx, class_name in enumerate(os.listdir(DIR_PATH)):
# loading the pointcloud files
map[idx] = class_name
train_class_path = os.path.join(DIR_PATH, class_name, "train")
test_class_path = os.path.join(DIR_PATH, class_name, "test")
print(f"Creating training set for class {class_name}")
for file_name in os.listdir(train_class_path):
train_file_name = os.path.join(train_class_path, file_name)
# loading the training mesh
mesh = trimesh.load(train_file_name)
# uniformly sample the training mesh
points = mesh.sample(args.N).astype('float32')
# scale the points to (-1,1)
scaler = MinMaxScaler((-1, 1))
points = scaler.fit_transform(points)
train_points.append(points)
train_labels.append(idx)
print(f"Creating testing set for class {class_name}")
for file_name in os.listdir(test_class_path):
test_file_name = os.path.join(test_class_path, file_name)
# loading the testing mesh
mesh = trimesh.load(test_file_name)
# uniformly sample the testing mesh
points = mesh.sample(args.N).astype('float32')
# scale the points to (-1,1)
scaler = MinMaxScaler((-1, 1))
points = scaler.fit_transform(points)
test_points.append(points)
test_labels.append(idx)
train_dict = {
"points": np.array(train_points),
"labels": np.array(train_labels),
"map": map
}
test_dict = {
"points": np.array(test_points),
"labels": np.array(test_labels),
"map": map
}
np.save("train.npy", train_dict)
np.save("test.npy", test_dict)