-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
241 lines (170 loc) · 7.2 KB
/
Copy pathapp.py
File metadata and controls
241 lines (170 loc) · 7.2 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import glob
import cv2
import numpy as np
from flask import Flask, render_template, request, redirect, session, url_for, g, flash
import os
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from werkzeug import secure_filename
import mxnet as mx
from mxnet import gluon
from PIL import Image
import ml
from OpenSSL import SSL
app = Flask(__name__)
print(SSL._CERTIFICATE_PATH_LOCATIONS)
app.config['SQLALCHEMY_DATABASE_URI'] = ""
app.config['UPLOAD_FOLDER'] = os.path.join('static', 'uploader')
app.config["IMAGE_UPLOADS"] = os.path.join('static', 'img_converted')
app.config["PRED_IMAGE"] = os.path.join('static', 'predicted_image')
app.config["fin_vid"] = os.path.join('static', 'final_video1')
db = SQLAlchemy(app)
app.secret_key = os.urandom(24)
class User(db.Model):
# sr,name,email,password
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(50), nullable=False)
number = db.Column(db.String(12), nullable=False)
password = db.Column(db.String(15), nullable=False)
date = db.Column(db.String(12), nullable=True)
@app.route('/dropsession')
def dropsession():
session.pop('user', None)
return render_template('index.html')
@app.before_request
def before_request():
g.user = None
if 'user' in session:
g.user = session['user']
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('index.html')
@app.route("/homepage")
def homePage():
return render_template('homePage.html')
@app.route("/login")
def login():
return render_template('login.html')
@app.route("/register")
def register():
return render_template('register.html')
@app.route("/add_user", methods=['POST'])
def add_user():
name = request.form.get('name')
email = request.form.get('email')
number = request.form.get('number')
password = request.form.get('password')
re_password = request.form.get('re_password')
entry = User(name=name, number=number, email=email, date=datetime.now(), password=password)
if password == re_password:
db.session.add(entry)
db.session.commit()
return redirect('/')
else:
msg = "Password mismatch"
return render_template('register.html', msg=msg)
@app.route("/login_validation", methods=['POST'])
def login_validation():
session.pop('user', None)
email = request.form.get('email')
password = request.form.get('password')
user_data = User.query.filter_by(email=email, password=password).first()
try:
if user_data.email is not None and user_data.password is not None:
session['user'] = user_data.name
session['email'] = user_data.email
return redirect('/user_dashboard')
except:
msg = "Incorrect username/password!"
return render_template('login.html', msg=msg)
@app.route("/user_dashboard")
def user_dashboard():
if g.user:
user = session['user']
email = session['email']
return render_template('user_dashboard.html', user=user, email=email)
else:
return redirect('/login')
@app.route('/uploader')
def upload_form():
if g.user:
user = session['user']
return render_template('multifiles.html',user = user)
@app.route("/uploader", methods=['POST'])
def uploader():
if g.user:
user = session['user']
if 'files[]' not in request.files:
#flash('No file part')
return render_template(request.url)
filelist = [f for f in os.listdir(app.config['UPLOAD_FOLDER']) if f.endswith(".tif")]
for f in filelist:
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], f))
filelist = [f for f in os.listdir(app.config["IMAGE_UPLOADS"]) if f.endswith(".jpeg")]
for f in filelist:
os.remove(os.path.join(app.config["IMAGE_UPLOADS"], f))
files = request.files.getlist('files[]')
for file in files:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
for infile in os.listdir(app.config['UPLOAD_FOLDER']):
if infile[-3:] == "tif" or infile[-3:] == "bmp":
outfile = infile[:-3] + "jpeg"
im = Image.open(os.path.join(app.config['UPLOAD_FOLDER'], infile))
#app.config['UPLOAD_FOLDER'] + infile)
out = im.convert("RGB")
save_path = os.path.join(app.config['IMAGE_UPLOADS'],outfile )
# app.config["IMAGE_UPLOADS"] + outfile
out.save(save_path, "JPEG", quality=90)
filelist = [os.path.join(app.config['IMAGE_UPLOADS'], f) for f in os.listdir(app.config["IMAGE_UPLOADS"]) if f.endswith(".jpeg")]
print(filelist)
return render_template('multifiles.html',user = user,data= filelist)
@app.route('/stores')
def store_locate():
if g.user:
user = session['user']
return render_template('index_map.html',user = user)
@app.route('/detect_anomalies')
def find_anomaly():
if g.user:
user = session['user']
print("1] Reading Data and Preprocessing. 2] Loading the Model. 3] Predicting ")
test_file = sorted(glob.glob(os.path.join('static', 'uploader/*')))
a = np.zeros((len(test_file), 2, 100, 100))
for idx, filename in enumerate(test_file):
im = Image.open(filename)
im = im.resize((100, 100))
a[idx, 0, :, :] = np.array(im, dtype=np.float32) / 255.0
dataset = gluon.data.ArrayDataset(mx.nd.array(a, dtype=np.float32))
dataloader = gluon.data.DataLoader(dataset, batch_size=1)
print("Done Reading and preprocessing")
model = ml.ConvolutionalAutoencoder()
model.load_parameters(os.path.join('static', 'autoencoder_ucsd.params'))
reg_scores_cae = ml.plot_regularity_score(model, dataloader)
print("Done loading params")
filelist = [f for f in os.listdir(app.config['PRED_IMAGE']) if f.endswith(".png")]
for f in filelist:
os.remove(os.path.join(app.config['PRED_IMAGE'], f))
ml.model_evaluation(model,dataloader)
print("Done Predicting and now showing predicted frames !!")
filelist = [os.path.join(app.config['PRED_IMAGE'], f) for f in os.listdir(app.config["PRED_IMAGE"]) if f.endswith(".png")]
return render_template('predicted_images.html',user = user,data= filelist)
@app.route('/convert_video')
def convert_video():
if g.user:
user = session['user']
if os.path.exists(os.path.join(app.config["fin_vid"] , 'project.mp4')):
os.remove(os.path.join(app.config["fin_vid"], 'project.mp4'))
img_array = []
for filename in sorted(glob.glob(os.path.join('static', 'predicted_image/*'))):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width, height)
img_array.append(img)
out = cv2.VideoWriter('static/final_video1/project.mp4', cv2.VideoWriter_fourcc(*'webm'), 15, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
return render_template('final_video.html', user=user)
app.run(port=8181, debug=True)