-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
173 lines (115 loc) · 4.95 KB
/
app.py
File metadata and controls
173 lines (115 loc) · 4.95 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
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
#python library imports
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
import os
import torch
import json
import random
import threading
import time
#local module imports
import python_modules.eval as eval
import python_modules.preprocess.utils as preprocess_utils
import python_modules.postprocess.reconstruction as reconstruction
#show the status text top right
global statusText
statusText = "view mode"
app = Flask(__name__ , static_url_path = '/static' )
#prevent file caching
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
#relative paths
cwd = os.getcwd()
output_path = cwd + '/static/img/output'
load_path = cwd + '/static/models/load'
input_path = cwd + '/static/models/inputs'
output_path = cwd + '/static/models/outputs'
mesh_path = cwd + '/static/models/meshes'
#base route
@app.route('/')
def index():
return render_template('index.html')
#route to hold the latest status
@app.route('/progress/<int:thread_id>')
def progress(thread_id):
global statusText
return str(statusText)
#handles uploading files via https forms into python
@app.route('/upload', methods = ['GET', 'POST'])
def upload_file():
global statusText
if request.method == 'POST':
f = request.files['file']
#upload file from local and save to folder, downsample if too dense for model
if(f.filename[-4:].lower() == ".ply"):
fileToCopy = secure_filename(f.filename)
f.save(os.path.join(load_path, fileToCopy))
print(load_path + "/" + fileToCopy)
statusText = "subsampling cloud.."
pcd = preprocess_utils.down_sample(load_path, fileToCopy, statusText)
statusText = "removing outliers and normalizing..."
preprocess_utils.standardise(pcd, fileToCopy, input_path)
statusText = "view mode"
else:
print("invalid file format")
return redirect(url_for('modelViewer'))
@app.route('/modelViewer' , methods=["GET", "POST"])
def modelViewer():
global statusText
inputFiles = os.listdir(input_path)
outputFiles = os.listdir(output_path)
meshFiles = os.listdir(mesh_path)
if request.method == "POST":
# removes files from app folder
if(request.form.get("fileNameRemove")):
fileToRemove = request.form.get("fileNameRemove")
print(input_path + "/" + fileToRemove)
statusText = f"removing file : {fileToRemove}.."
os.remove(input_path + "/" + fileToRemove)
try:
os.remove(output_path + "/" + fileToRemove[:-4] + "_labels.ply")
except:
pass
try:
os.remove(mesh_path + "/" + fileToRemove[:-4] + ".obj")
except:
pass
try:
os.remove(mesh_path + "/" + fileToRemove[:-4] + ".mtl")
except:
pass
statusText = "view mode"
#runs model
elif(request.form.get("fileNameInput")):
fileName = request.form.get("fileNameInput")
torch.cuda.empty_cache()
print(input_path + "/" + fileName)
statusText = "calculating stats.."
#get stats for cleaned pointcloud
_, _, _, _, _, density = preprocess_utils.get_stats(input_path, fileName)
print(f"density : {density}")
statusText = "evaluating model.."
#load filtered pointcloud, apply the model and save to new ply file
eval.evaluate(input_path, fileName, density)
#creates mesh
elif (request.form.get("fileNameOutput")):
statusText = "reconstructing mesh.."
fileName = request.form.get("fileNameOutput")
filterList = []
#get the label filters
if (request.form.get("filters")):
filterList = request.form.get("filters").split(",")
print(filterList)
reconstruction.save_to_mesh(input_path, output_path, mesh_path, fileName, filters = filterList)
statusText = "view mode"
#downloads mesh
elif (request.form.get("fileNameDownload")):
fileName = request.form.get("fileNameDownload")[:-4] + ".obj"
print(f"downloading {fileName}")
#open3d seems to always create an MTL file, can't figure out how to remove it. But the model should load fine without the MTL.
return send_from_directory(directory=mesh_path, filename=fileName , as_attachment=True)
#https://en.wikipedia.org/wiki/Post/Redirect/Get
return redirect(url_for('modelViewer'))
return render_template('modelViewer.html', inputFiles = inputFiles , outputFiles = outputFiles, meshFiles = meshFiles)
if __name__ == '__main__':
# app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000))) //use this version of line if running inside docker
app.run()