-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
343 lines (301 loc) · 10.3 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from flask import Flask, flash, redirect, render_template, request, session, abort
from flask_swagger_ui import get_swaggerui_blueprint
import flask_login
from werkzeug.utils import secure_filename
from back_end import *
import codecs
import cv2
from sqlalchemy.orm import sessionmaker
from models import *
from sqlalchemy.inspection import inspect
from utils import save_image
from werkzeug.security import generate_password_hash, \
check_password_hash
import os
import re
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from marshmallow import Schema, fields
from flask import Flask, abort, request, make_response, jsonify
import json
from api import api as api_blueprint
from api import *
import logging
logging.basicConfig(filename='logs.log', level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
# Swagger API Docs Auto generation
spec = APISpec(
title="ASAR API",
version="1.0.0",
openapi_version="3.0.2",
info=dict(
description="This is ASAR swagger api documentation",
version="1.0.0",
contact=dict(
email="[email protected]"
),
license=dict(
name="Apache 2.0",
url='http://www.apache.org/licenses/LICENSE-2.0.html'
)
),
servers=[
dict(
description="ASAR server",
url="http://127.0.0.1:5000"
)
],
plugins=[FlaskPlugin(), MarshmallowPlugin()],
)
''' This handles the setup of the web application. All client requests are
routed through this module to
'''
login_manager = flask_login.LoginManager()
engine = create_engine('sqlite:///asar.db', echo=True, connect_args={"check_same_thread": False})
Session = sessionmaker(bind=engine)
s = Session()
app = Flask(__name__)
app.secret_key = 'SUPER SCRET KEY FOR ASSAR PROJECT'
app.config['SESSION_TYPE'] = 'filesystem'
login_manager.init_app(app)
### swagger specific ###
SWAGGER_URL = '/api/docs'
API_URL = '/static/swagger.json'
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "ASAR project"
}
)
app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
app.register_blueprint(api_blueprint, url_prefix="/api")
#LoginManager
@login_manager.user_loader
def load_user(username):
'''Load a user from the database using a user_id.
Args:
username: The id of the user to be found.
Returns:
res: The user where user.user_id == user_id.
'''
res = s.query(User).filter(User.username == username).first()
s.close()
return res
@app.route('/')
def hub():
''' The home naviagion option.
If a user is not logged in, show Login screen.
else show the Home.
'''
if not session.get('logged_in'):
return render_template('index.html')
else:
return home()
# Login / Signup ------------------------------------------------------------
@app.route('/login', methods=['POST'])
def do_admin_login():
'''Log a user into the application.
if username and password match, log into the application.
else call hub().
'''
POST_USERNAME = str(request.form['username'])
POST_PASSWORD = str(request.form['password'])
result = s.query(User).filter(User.username.in_([POST_USERNAME])).first()
s.close()
if result:
if check_password_hash(result.password, POST_PASSWORD):
session['logged_in'] = True
session['user'] = POST_USERNAME
user = load_user(POST_USERNAME)
flask_login.login_user(user)
else:
return (render_template('index.html', password=False))
return hub()
@app.route('/logout')
@flask_login.login_required
def logout():
'''Log out of the application.
Logs a user out of the application and shows the login screen.
'''
flask_login.logout_user()
session['logged_in'] = False
session['user'] = ''
return hub()
@app.route('/guest')
def guest():
''' Log in as a guest.
Sets the current user to guest and loads the classify_anonymous page.
'''
session['logged_in'] = False
session['user'] = 'guest'
return render_template('classify_anonymous.html')
@app.route('/create_user', methods = ['POST'])
def create_user():
''' Create a new user.
Takes information from the forms and creates a new entry in the database
for the user, if the username does not already exist.
'''
POST_USERNAME = str(request.form['username'])
POST_PASSWORD = str(request.form['password'])
POST_PASSWORD_CONFIRM = str(request.form['confirm-password'])
res = s.query(User).filter(User.username.in_([POST_USERNAME])).first()
if res == None:
if POST_PASSWORD == POST_PASSWORD_CONFIRM:
user = User(POST_USERNAME,POST_PASSWORD)
s.add(user)
s.commit()
s.close()
session['logged_in'] = True
session['user'] = POST_USERNAME
user = load_user(POST_USERNAME)
if not os.path.exists('static/users/{}'.format(user.username)):
os.makedirs('static/users/{}'.format(user.username))
flask_login.login_user(user)
return render_template('home.html')
else:
return (render_template('index.html', username=False))
return 'ok'
# Hub page ------------------------------------------------------------------
@app.route('/home')
@flask_login.login_required
def home():
'''Retrieve a users predictions and load the Hub page.
'''
zipped = get_images()
return render_template('home.html', zipped=zipped)
def get_images():
'''Load the currently logged in users prediction history.
This is used in the web application to populate the Hub page.
Returns:
A list of predictions for the current user.
'''
user = load_user(session['user'])
image_ref = s.query(Prediction.id).filter(Prediction.user == user.id)
list_result = []
for ref in image_ref:
list_result.append(ref[0])
all_images = []
image_filename = s.query(Prediction.image).filter(Prediction.user == user.id)
for img in image_filename:
all_images.append(img[0])
zipped = [list(a) for a in zip(list_result, all_images)]
print(zipped)
zipped = reversed(zipped)
s.close()
return zipped
@app.route('/download_result/', methods=['GET','POST'])
def download_result():
'''Create a text file using the data from a prediction in the database.
Returns:
The prediction.
'''
prediction_id = request.get_data()
prediction_id = prediction_id.decode("utf-8")
result = s.query(Prediction.result).filter(Prediction.id == prediction_id)
for res in result:
print(res)
loc = 'static/users/{}/result.txt'.format(session['user'])
s.close()
return res[0]
@app.route('/delete_result/', methods=['POST'])
def delete_result():
''' Delete a prediciton from the database.
'''
prediction_id = request.get_data()
prediction_id = prediction_id.decode("utf-8")
s.query(Prediction).filter(Prediction.id == prediction_id).delete()
s.commit()
s.close()
return(home())
# Prediction Page ------------------------------------------------------------
@app.route('/classify')
def index():
'''Loads the classify page
'''
return render_template('classify.html')
# Decoding an image from base64 into raw representation
def convertImage(imgData1, path):
'''Decodes an image from base64.
Args:
imgData1: The image to be decoded
path: The location the decoded image is saved
'''
img_str = re.search(b'base64,(.*)', imgData1).group(1)
with open(path + '/temp.png', 'wb') as output:
output.write(codecs.decode(img_str, 'base64'))
# Predict the output
@app.route('/predict/', methods=['GET','POST'])
def predict():
''' Makes a prediciton from an image send from the application.
The method retrieves the image, converts it to a usable format, makes a
prediciton, then saves it if the user is not a guest.
Returns:
The result of the prediciton.
'''
path = 'static/users/{}'.format(session['user'])
if not os.path.exists(path):
os.makedirs(path)
img_data = request.get_data()
convertImage(img_data, path)
result = str(get_result(path + '/temp.png'))
if session['user'] != 'guest':
save_pred(result)
return result
def save_pred(result):
'''Saves the prediction to the users file.
Args:
result: the result of the prediction
'''
save_loc = save_thumbnail(session['user'], cv2.imread('static/users/{}/temp.png'.format(session['user'])))
os.remove('static/users/{}/temp.png'.format(session['user']))
create_prediction(save_loc, result)
def create_prediction(loc, result):
'''creates the prediction entry in the database.
Args:
loc: the location of the image
result: the prediciton result
'''
user = load_user(session['user'])
res = s.query(User.id).filter(User.username == user.username)
for r in res:
identity = r.id
pred = Prediction(identity, loc, result)
s.add(pred)
s.commit()
s.close()
@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
'''Upload a file for prediction
Returns:
Confirmation string
'''
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return 'file uploaded successfully'
@app.route('/save_result/', methods=['GET','POST'])
def save_result():
'''Save the image to the users folder.
'''
img_data = request.get_data()
user = session['user']
path = 'static/users/{}'.format(user)
save_image(user, img_data, path)
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run( host='0.0.0.0', port=5000)
# Since path inspects the view and its route,
# we need to be in a Flask request context
with app.test_request_context():
spec.path(view=api_user_login)
spec.path(view=api_user_register)
spec.path(view=api_user_logout)
spec.path(view=api_predict)
spec.path(view=api_delete_result)
spec.path(view=api_prediction)
spec.path(view=api_home)
pass
# We're good to go! Save this to a file for now.
#with open('static/swagger.json', 'w') as f:
# json.dump(spec.to_dict(), f)