-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbinvox_converter.py
More file actions
84 lines (66 loc) · 2.85 KB
/
binvox_converter.py
File metadata and controls
84 lines (66 loc) · 2.85 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
import argparse
import os
import subprocess
import sys
if 'linux' in sys.platform:
# CMD = ("xvfb-run -s '-screen 0 640x480x24'"
# "./binvox -cb -pb -e -c -d {dim} {path}")
CMD = "./binvox -cb -pb -e -c -d {dim} {path}"
elif 'darwin' in sys.platform:
CMD = './binvox -cb -e -c -d {dim} {path}'
else:
raise SystemError('System is not of type darwin or linux, what kind of system are you using?')
def get_rid_of_misplaced_text(path):
with open(path, 'r') as f:
text = f.read()
if text[:3] == 'OFF':
new_text = 'OFF\n' + text[3:]
with open(path, 'w') as f:
f.write(new_text)
def convert_off_to_binvox(path, dim=30):
"Converts *.off file to *.binvox"
if ".off" not in path:
print("{} is not an '*.off' file... skipping".format(path))
return
get_rid_of_misplaced_text(path)
print('CONVERTING {}'.format(path))
process = subprocess.Popen(CMD.format(dim=dim, path=path).split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print("Output from Binvox: \n{}".format(output))
raise Exception("Binvox didn't do something right\nBinvox Error:\n{}".format(error))
def list_file_paths(path):
ignore_files = ['.DS_Store']
for root, _, files in os.walk(path):
for file_path in files:
# this is throwing a funky error but the files are still showing up will come back to this when it's not crunchtime
# /Users/ryan/repos/deep_learning_mabs/ModelNet10/sofa/train/sofa_0364.off
# wc: wc: open: No such file or directory
# wc: -l: open: No such file or directory
if file_path not in ignore_files:
abs_path = os.path.abspath(os.path.join(root, file_path))
print(abs_path)
yield abs_path
def _remove_all(path):
paths = list_file_paths(path)
for file_path in paths:
if ".binvox" in file_path:
print("removing...{}".format(file_path))
os.remove(file_path)
def main():
parser = argparse.ArgumentParser(description='Process *.off files into *.binvox')
parser.add_argument(dest="root_path",
help="give the root_path for the *.off files you want to convert to *.binvox")
parser.add_argument('--dimensions', default=30)
parser.add_argument('--remove-all-dupes', dest='remove_all', action='store_true')
args = parser.parse_args()
# assert 'ModelNet' in args.root_path, ("ONLY RUN THIS IN THE ModelNet folder!!!")
# Essentially to overwrite what's already there
if args.remove_all:
_remove_all(args.root_path)
# actually do the conversion from *.off to *.binvox
paths_generator = list_file_paths(args.root_path)
for path in paths_generator:
convert_off_to_binvox(path, dim=args.dimensions)
if __name__ == '__main__':
main()