-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_converter.py
executable file
·153 lines (103 loc) · 4.25 KB
/
format_converter.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
"""
Version: 1.5
Summary: compute the cross section plane based on 3d model
Author: suxing liu
Author-email: [email protected]
USAGE
python3 format_converter.py -p /home/suxingliu/model-scan/model-data/ -m surface.ply
argument:
("-p", "--path", required=True, help="path to *.ply model file")
("-m", "--model", required=True, help="file name")
("-i", "--interval", required=True, type = int, help="intervals along sweeping plane")
("-d", "--direction", required=True, type = int, help="direction of sweeping plane, X=0, Y=1, Z=2")
("-r", "--reverse", required=True, type = int, help="Reverse model top_down, 1 for Ture, 0 for False")
"""
#!/usr/bin/env python
# import the necessary packages
from plyfile import PlyData, PlyElement
import numpy as np
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
from operator import itemgetter
import argparse
import os
import sys
import open3d as o3d
def format_converter(current_path, model_name):
model_file = current_path + model_name
print("Converting file format for 3D point cloud model {}...\n".format(model_name))
# load the model file
try:
with open(model_file, 'rb') as f:
plydata = PlyData.read(f)
num_vertex = plydata.elements[0].count
print("Ply data structure: \n")
print(plydata)
print("Number of 3D points in current model: {0} \n".format(num_vertex))
except:
print("Model file not exist!")
sys.exit(0)
#Parse the ply format file and Extract the data
Data_array_ori = np.zeros((num_vertex, 3))
Data_array_ori[:,0] = plydata['vertex'].data['x']
Data_array_ori[:,1] = plydata['vertex'].data['y']
Data_array_ori[:,2] = plydata['vertex'].data['z']
#sort point cloud data based on Z values
Data_array = np.asarray(sorted(Data_array_ori, key = itemgetter(2), reverse = False))
'''
#accquire data range
min_x = Data_array[:, 0].min()
max_x = Data_array[:, 0].max()
min_y = Data_array[:, 1].min()
max_y = Data_array[:, 1].max()
min_z = Data_array[:, 2].min()
max_z = Data_array[:, 2].max()
range_data_x = max_x - min_x
range_data_y = max_y - min_y
range_data_z = max_z - min_z
print (range_data_x, range_data_y, range_data_z)
print(min_x,max_x)
print(min_y,max_y)
print(min_z,max_z)
'''
#Normalize data
#min_max_scaler = preprocessing.MinMaxScaler(feature_range = (0,1000000))
min_max_scaler = preprocessing.MinMaxScaler(feature_range = (0,10000))
point_normalized = min_max_scaler.fit_transform(Data_array)
#point_normalized_scale = [i * 1 for i in point_normalized]
# Pass xyz to Open3D.o3d.geometry.PointCloud
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(point_normalized)
#Save modelfilea as ascii format
filename = current_path + 'converted.ply'
o3d.io.write_point_cloud(filename, pcd, write_ascii = True)
if os.path.exists(filename):
print("Converted 3d model was saved at {0}".format(filename))
return True
else:
return False
print("Model file converter failed !")
sys.exit(0)
if __name__ == '__main__':
# construct the argument and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required = True, help = "path to *.ply model file")
ap.add_argument("-m", "--model", required = True, help = "model file name")
args = vars(ap.parse_args())
# setting path to model file
current_path = args["path"]
filename = args["model"]
file_path = current_path + filename
print ("results_folder: " + current_path)
# load the model file
try:
with open(file_path, 'rb') as f:
plydata = PlyData.read(f)
num_vertex = plydata.elements[0].count
print("Ply data structure: \n")
print(plydata)
print("Number of 3D points in current model: {0} \n".format(num_vertex))
format_converter(current_path, filename)
except:
print("Model file was not exist!")
sys.exit(0)