-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (95 loc) · 4.32 KB
/
app.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
from flask import Flask, request, make_response, jsonify, redirect, url_for, send_file, render_template
import os
import sys
import werkzeug
from datetime import datetime
import subprocess
import shutil
import configparser
import shlex
from modules import hololens
from modules import rgb2grayscale
from modules import ply2obj
app = Flask(__name__)
# read config file and create HoloLens instance
config = configparser.ConfigParser()
config.read('config.ini')
holo_conf = config['HoloLens']
upload = config['Upload']
unity = config['Unity']
# create instance
hololens2 = hololens.Hololens(
holo_conf['username'], holo_conf['password'], holo_conf['ipaddress'])
cmd2 = "python main.py --test --data_dir ../../../Media/Uploads/Chair/ --train_dir ../Checkpoint/Chair/ --test_dir ../../../Media/Output --sketch_views FS"
cmd3 = "bash ReconstructMesh.sh"
# AssetBundle Build command
buildAssetBundle = unity['UnityPath'] + " -batchmode -quit -logFile ./build.log -projectPath " + unity['projectPath'] + " -executeMethod " + unity['methodName']
app.config['UPLOAD_FOLDER'] = './Media/Uploads/Chair/sketch/p1/'
app.config['OUTPUT_FOLDER'] = "./Media/Output"
app.config['PLY_FOLDER'] = "./Media/Ply/p1/mesh.ply"
@app.route('/', methods=['GET', 'POST'])
def upload_files():
if request.method == 'POST':
if 'file' not in request.files:
sys.stderr.write("file not in request.files")
return make_response(jsonify({'result':'upload files is required'}))
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
if os.path.exists("Media/Ply"):
shutil.rmtree("Media/Ply")
object_type = request.form["obj-type"]
print(object_type)
upload_files = request.files.getlist('file')
for file in upload_files:
if file.filename == "":
#TODO: return message (ex) Please choose images
return redirect(request.url)
file_name = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))
# rgb2grayscale
app.logger.info("convert rgb2grayscale")
rgb2grayscale.convert(app.config['UPLOAD_FOLDER'])
# calc depth and normal map
os.chdir("Network/code/MonsterNet")
subprocess.call(cmd2.split())
# create point cloud and poisson surface reconstruction
os.chdir("../../../Fusion/output/ReconstructMesh/Win32/Release")
subprocess.call(cmd3.split(), shell=True)
# ply2obj
os.chdir("../../../../../")
ply2obj.convert(app.config['PLY_FOLDER'])
# copy obj file to Unity AssetBundleResources folder
try:
shutil.copy2("Media\Ply\p1\mesh.obj", unity['projectPath'] + unity['AssetBundleResources'])
except FileNotFoundError as e:
app.logger.error("Failed to copy obj file to AssetBundleResouces: %s", e)
except Exception as e:
app.logger.error("Failed to copy obj file to AssetBundleResouces: %s", e)
# build AssetBundle
try:
app.logger.info("Build AssetBundle...")
subprocess.call(shlex.split(buildAssetBundle))
app.logger.info("Finish: Build AssetBundle")
except FileNotFoundError as e:
app.logger.error("Failed to build AssetBundle: %s", e)
except Exception as e:
app.logger.error("Failed to build AssetBundle: %s", e)
# AssetBundle send hololens
try:
app.logger.info("Upload HoloLens...")
hololens2.upload(upload['directory'], upload['filename'])
app.logger.info("Finished: Upload HoloLens")
except FileNotFoundError as e:
app.logger.error("Failed to send AssetBundle to HoloLens: %s", e)
except Exception as e:
app.logger.error("Failed to send AssetBundle to HoloLens: %s", e)
# Delete Upload image directory and normal,depth map
if os.path.exists(app.config['UPLOAD_FOLDER']):
shutil.rmtree(app.config['UPLOAD_FOLDER'])
if os.path.exists(app.config['OUTPUT_FOLDER']):
shutil.rmtree(app.config['OUTPUT_FOLDER'])
# return mesh file
return send_file(filename_or_fp="Media/Ply/p1/mesh.obj", as_attachment=True)
return render_template("index.html")
if __name__ == "__main__":
print(app.url_map)
app.run(host='0.0.0.0', debug=True)