-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
290 lines (237 loc) · 9.44 KB
/
main.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
# -*- coding: utf-8 -*-
"""
PyRecognizer loader
"""
import base64
import os
import signal
import sys
import flask_monitoringdashboard as dashboard
from flask import Flask, flash, jsonify, render_template, request, send_from_directory, session
from flask_login import LoginManager, UserMixin, login_required, login_user, current_user
from werkzeug.exceptions import abort
from werkzeug.utils import redirect, secure_filename
from api.Api import predict_image, train_network, tune_network
from datastructure.Administrator import Administrator
from datastructure.Classifier import Classifier
from datastructure.Response import Response
from utils.util import init_main_data, random_string, secure_request, find_duplicates
# ===== LOAD CONFIGURATION FILE =====
CONFIG_FILE = "conf/conf.json"
CFG, log, TMP_UPLOAD_PREDICTION, TMP_UPLOAD_TRAINING, TMP_UPLOAD, TMP_UNKNOWN, DETECTION_MODEL, JITTER, \
ENCODING_MODELS, enable_dashboard = init_main_data(CONFIG_FILE)
SSL_ENABLED = CFG["network"]["SSL"]["enabled"]
# Disable CSRF protection for if you need to use as REST server instead of use the GUI
ENABLE_CSRF = CFG["network"]["csrf_protection"]
# $(base64 /dev/urandom | head -n 1 | md5sum | awk '{print $1}')
SECRET_KEY = str(base64.b64encode(bytes(os.urandom(24)))).encode()
login_manager = LoginManager()
# ===== FLASK CONFIGURATION =====
app = Flask(__name__, template_folder=CFG["network"]["templates"])
app.secret_key = SECRET_KEY
# Used by flask when a upload is made
app.config['UPLOAD_FOLDER'] = TMP_UPLOAD
PUB_KEY = CFG["network"]["SSL"]["cert.pub"]
PRIV_KEY = CFG["network"]["SSL"]["cert.priv"]
if not os.path.isfile(PUB_KEY) or not os.path.isfile(PRIV_KEY):
log.error(
"Unable to find certs file, be sure that the following certs exists, disabling SSL")
log.warning("Public key: {}".format(PUB_KEY))
log.warning("Private key: {}".format(PRIV_KEY))
SSL_ENABLED = False
# =====FLASK DASHBOARD CONFIGURATION =====
if enable_dashboard:
dashboard.config.init_from(file=CFG["dashboard"]["config_file"])
dashboard.bind(app, SECRET_KEY)
# flask-login
login_manager.init_app(app)
login_manager.login_view = "login"
# ===== CLASSIFIER CONFIGURATION =====
log.debug("Init classifier ...")
clf = Classifier()
clf.model_path = CFG["classifier"]["model_path"]
clf.load_classifier_from_file(CFG["classifier"]["timestamp"])
@app.route('/', methods=['GET'])
def home():
"""
Show the html template for upload the image
"""
return render_template("upload.html")
@app.route('/', methods=["POST"])
def predict():
"""
Load the image using the HTML page and predict who is
:return:
"""
# check if the post request has the file part
if 'file' not in request.files or request.files['file'].filename == '':
# flash('No file choose :/', category="error")
log.warning("predict_api | No file choose!")
response = Response(status="KO", error="NO_FILE_IN_REQUEST")
response.description = "You have sent a request without the photo to predict :/"
return jsonify(response=response.__dict__)
# return redirect(request.url) # Return to HTML page [GET]
file = request.files['file']
threshold = request.form.get('threshold')
log.debug("Received file [{}] and threshold [{}]".format(file, threshold))
if threshold is None or len(threshold) == 0:
log.warning("Threshold not provided")
response = Response(status="KO", error="THRESHOLD_NOT_PROVIDED")
response.description = "You have sent a request without the `threshold` parameter :/"
return jsonify(response=response.__dict__)
try:
threshold = int(threshold)
except ValueError:
log.error("Unable to convert threshold")
response = Response(status="KO", error="UNABLE_CAST_INT")
response.description = "Threshold is not an integer!"
return jsonify(response=response.__dict__)
if not 0 <= threshold <= 100:
log.error("Threshold wrong value")
response = Response(status="KO", error="THRESHOLD_ERROR_VALUE")
response.description = "Threshold have to be greater than 0 and lesser than 100!"
return jsonify(response=response.__dict__)
threshold /= 100
log.debug("Received file {} and threshold {}".format(file, threshold))
filename = secure_filename(file.filename)
img_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(img_path)
return jsonify(
response=predict_image(img_path, clf, TMP_UPLOAD_PREDICTION, TMP_UNKNOWN, DETECTION_MODEL, JITTER,
ENCODING_MODELS, threshold))
@app.route('/train', methods=['GET'])
@login_required
def train():
"""
Show the html template for training the neural network
"""
return render_template("train.html")
@app.route('/train', methods=['POST'])
@login_required
def train_http():
"""
:return:
"""
# check if the post request has the file part
if 'file' not in request.files or request.files['file'].filename == '':
flash('No file choose :/', category="error")
return redirect(request.url) # Return to HTML page [GET]
file = request.files['file']
file.save(os.path.join(TMP_UPLOAD_TRAINING, file.filename))
return jsonify(train_network(TMP_UPLOAD_TRAINING, file, clf, DETECTION_MODEL, JITTER, ENCODING_MODELS))
@app.route('/tune', methods=['GET'])
@login_required
def tune():
"""
Show the html template for training the neural network
"""
return render_template("train.html")
@app.route('/tune', methods=['POST'])
@login_required
def tune_http():
"""
:return:
"""
# check if the post request has the file part
if 'file' not in request.files or request.files['file'].filename == '':
flash('No file choose :/', category="error")
return redirect(request.url) # Return to HTML page [GET]
file = request.files['file']
file.save(os.path.join(TMP_UPLOAD_TRAINING, file.filename))
return jsonify(tune_network(TMP_UPLOAD_TRAINING, file, clf, DETECTION_MODEL, JITTER, ENCODING_MODELS))
@app.route('/uploads/<filename>')
def uploaded_file(filename):
"""
Expose images only to the one that know the image name in a secure method
:param filename:
:return:
"""
if os.path.exists(os.path.join(TMP_UPLOAD_PREDICTION, filename)):
return send_from_directory(TMP_UPLOAD_PREDICTION, filename)
return "PHOTOS_NOT_FOUND"
class User(UserMixin):
pass
@login_manager.user_loader
def user_loader(email):
user = Administrator("administrator", email, "dummypassword")
user.init_redis_connection()
user_exists = user.verify_user_exist()
user.redis_client.close()
if not user_exists:
return None
user.id = email
return user
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template("login.html")
email = request.form['email']
password = request.form['password']
log.debug("Password in input -> {}".format(password))
# name (administrator) is not managed, only mail and psw will be used for login validation
admin = Administrator("administrator", email, password)
if not admin.init_redis_connection():
log.error("Unable to connect to redis-db!")
response = Response(status="KO", error="UNABLE_CONNECT_REDIS_DB")
response.description = "Seems that the DB is not reachable!"
return jsonify(response=response.__dict__)
authenticated = admin.verify_login(password)
admin.redis_client.close()
if not authenticated:
log.error("Password is not valid!")
response = Response(status="KO", error="PASSWORD_NOT_VALID")
response.description = "The password inserted is not valid!"
return jsonify(response=response.__dict__)
user = User()
user.id = email
login_user(user)
log.debug("Logged in!")
return redirect('/train')
@app.route('/protected')
@login_required
def protected():
return 'Logged in as: ' + current_user.id
@app.before_request
def csrf_protect():
"""
Validate csrf token against the one in session
:return:
"""
if ENABLE_CSRF:
if "dashboard" not in str(request.url_rule):
if request.method == "POST":
token = session.pop('_csrf_token', None)
if not token or token != request.form.get('_csrf_token'):
abort(403)
@app.after_request
def secure_headers(response):
"""
Apply security headers to the response call
:return:
"""
return secure_request(response, SSL_ENABLED)
def generate_csrf_token():
"""
Generate a random string and set the data into session
:return:
"""
if '_csrf_token' not in session:
session['_csrf_token'] = random_string()
return session['_csrf_token']
def signal_handler(signal, frame):
find_duplicates(TMP_UPLOAD)
find_duplicates(TMP_UPLOAD_PREDICTION)
find_duplicates(TMP_UNKNOWN)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
app.jinja_env.globals['csrf_token'] = generate_csrf_token
app.jinja_env.autoescape = True
if __name__ == '__main__':
if SSL_ENABLED:
log.debug("main | RUNNING OVER SSL")
app.run(host=CFG["network"]["host"], port=CFG["network"]["port"], threaded=False, debug=False,
use_reloader=False, ssl_context=(
PUB_KEY, PRIV_KEY))
else:
log.debug("main | HTTPS DISABLED | RUNNING OVER HTTP")
app.run(host=CFG["network"]["host"], port=CFG["network"]["port"], threaded=False, debug=False)