-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathcompletion.py
197 lines (157 loc) · 6.34 KB
/
completion.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
import os
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--run', type=str, required=True)
parser.add_argument('--octree', type=str, required=False,
default='script/logs/completion/skip_connections_test')
args = parser.parse_args()
abs_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
root_folder = os.path.join(abs_path, 'script/dataset/ocnn_completion')
points2ply, ply2points, octree2pts = 'points2ply', 'ply2points', 'octree2points'
converter = os.path.join(abs_path, 'util/convert_tfrecords.py')
def download_point_clouds():
# download via wget
if not os.path.exists(root_folder):
os.makedirs(root_folder)
url = 'https://www.dropbox.com/s/z2x0mw4ai18f855/ocnn_completion.zip?dl=0'
cmd = 'wget %s -O %s.zip' % (url, root_folder)
print(cmd)
os.system(cmd)
# unzip
cmd = 'unzip %s.zip -d %s' % (root_folder, root_folder)
print(cmd)
os.system(cmd)
def _convert_ply_to_points(prefix='shape'):
ply_folder = os.path.join(root_folder, prefix + '.ply')
points_folder = os.path.join(root_folder, prefix + '.points')
folders = os.listdir(ply_folder)
for folder in folders:
curr_folder = os.path.join(ply_folder, folder)
# write filelist to disk
filenames = os.listdir(curr_folder)
filelist_name = os.path.join(curr_folder, 'filelist.txt')
with open(filelist_name, 'w') as fid:
for filename in filenames:
if filename.endswith('.ply'):
fid.write(os.path.join(curr_folder, filename) + '\n')
# run points2ply
output_path = os.path.join(points_folder, folder)
if not os.path.exists(output_path):
os.makedirs(output_path)
cmd = '%s --filenames %s --output_path %s --verbose 0' % \
(ply2points, filelist_name, output_path)
print(cmd)
os.system(cmd)
os.remove(filelist_name)
def convert_ply_to_points():
_convert_ply_to_points('shape')
_convert_ply_to_points('test.scans')
def _convert_points_to_ply(prefix='shape'):
ply_folder = os.path.join(root_folder, prefix + '.ply')
points_folder = os.path.join(root_folder, prefix + '.points')
folders = os.listdir(points_folder)
for folder in folders:
curr_folder = os.path.join(points_folder, folder)
# write filelist to disk
filenames = os.listdir(curr_folder)
filelist_name = os.path.join(curr_folder, 'filelist.txt')
with open(filelist_name, 'w') as fid:
for filename in filenames:
if filename.endswith('.points'):
fid.write(os.path.join(curr_folder, filename) + '\n')
# run points2ply
output_path = os.path.join(ply_folder, folder)
if not os.path.exists(output_path):
os.makedirs(output_path)
cmd = '%s --filenames %s --output_path %s --verbose 0' % \
(points2ply, filelist_name, output_path)
print(cmd)
os.system(cmd)
os.remove(filelist_name)
def convert_points_to_ply():
_convert_points_to_ply('shape')
_convert_points_to_ply('test.scans')
def generate_points_tfrecords():
# tfrecords for training
points_folder = os.path.join(root_folder, 'shape.points')
filelist = os.path.join(root_folder, 'filelist_train.txt')
records_name = os.path.join(root_folder, 'completion_train_points.tfrecords')
cmd = 'python %s --file_dir %s --list_file %s --records_name %s' % \
(converter, points_folder, filelist, records_name)
print(cmd)
os.system(cmd)
# tfrecords for testing
points_folder = os.path.join(root_folder, 'shape.points')
filelist = os.path.join(root_folder, 'filelist_test.txt')
records_name = os.path.join(root_folder, 'completion_test_points.tfrecords')
cmd = 'python %s --file_dir %s --list_file %s --records_name %s' % \
(converter, points_folder, filelist, records_name)
print(cmd)
os.system(cmd)
# tfrecords for 1200 testing scans
points_folder = os.path.join(root_folder, 'test.scans.points')
filelist = os.path.join(root_folder, 'filelist_test_scans.txt')
records_name = os.path.join(root_folder, 'completion_test_scans_points.tfrecords')
cmd = 'python %s --file_dir %s --list_file %s --records_name %s' % \
(converter, points_folder, filelist, records_name)
print(cmd)
os.system(cmd)
# tfrecords for original shape of 1200 testing scans
points_folder = os.path.join(root_folder, 'shape.points')
filelist = os.path.join(root_folder, 'filelist_test_scans.txt')
records_name = os.path.join(root_folder, 'completion_test_1200_points.tfrecords')
cmd = 'python %s --file_dir %s --list_file %s --records_name %s' % \
(converter, points_folder, filelist, records_name)
print(cmd)
os.system(cmd)
def generate_dataset():
download_point_clouds()
convert_ply_to_points()
generate_points_tfrecords()
def rename_output_octree():
filelist = os.path.join(root_folder, 'filelist_test_scans.txt')
filenames = []
with open(filelist, 'r') as fid:
for line in fid:
filename = line.split()[0]
filenames.append(filename[:-6] + 'octree')
idx = 0
folder_in = args.octree
octree_in = os.listdir(folder_in)
octree_in.sort()
folder_out = os.path.join(root_folder, 'output.octree')
for o in octree_in:
if o.endswith('output.octree'):
name_in = os.path.join(folder_in, o)
name_out = os.path.join(folder_out, filenames[idx])
os.renames(name_in, name_out)
idx += 1
assert (idx == 1200)
def _convert_octree_to_points(suffix='ply'):
octree_folder = os.path.join(root_folder, 'output.octree')
points_folder = os.path.join(root_folder, 'output.' + suffix)
folders = os.listdir(octree_folder)
for folder in folders:
curr_folder = os.path.join(octree_folder, folder)
# write filelist to disk
filenames = os.listdir(curr_folder)
filelist_name = os.path.join(curr_folder, 'filelist.txt')
with open(filelist_name, 'w') as fid:
for filename in filenames:
if filename.endswith('.octree'):
fid.write(os.path.join(curr_folder, filename) + '\n')
# run octree2points
output_path = os.path.join(points_folder, folder)
if not os.path.exists(output_path):
os.makedirs(output_path)
cmd = '%s --filenames %s --output_path %s --verbose 0 --suffix %s' % \
(octree2pts, filelist_name, output_path, suffix)
print(cmd)
os.system(cmd)
os.remove(filelist_name)
def convert_octree_to_points():
_convert_octree_to_points('points')
_convert_octree_to_points('ply')
if __name__ == '__main__':
eval('%s()' % args.run)