-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.py
92 lines (68 loc) · 2.8 KB
/
routes.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
from flask import Flask, render_template, request, redirect, url_for, flash
from core.face_net import CelebModel, CelebModelOperations
import os
import logging
from werkzeug.utils import secure_filename
from threading import Thread
import shutil
UPLOAD_FOLDER = 'static/images/uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
logger = logging.getLogger('fr.main')
fr_model = None
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/setup")
def setup():
message = None
if fr_model == None:
message = 'Setup in progress. Please wait for 5-10 minutes before trying anything.'
Thread(target=do_setup, args=()).start()
return redirect(url_for('index', message=message))
def do_setup():
global fr_model
if fr_model == None:
logger.info('start building the model')
celeb_model = CelebModel()
logger.info('model build finished')
fr_model = celeb_model.fr_model
@app.route("/")
@app.route("/<message>")
def index(message=None):
return render_template('upload.html', message=message)
@app.route("/face_recognition", methods=["POST"])
def face_recognizer():
if request.method == 'POST':
# check if the post request has the file part
if 'image_file' not in request.files:
return redirect(url_for('index'))
file = request.files['image_file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
return redirect(url_for('index'))
if file and allowed_file(file.filename):
try:
shutil.rmtree(os.path.abspath('static/images'))
except Exception as e:
logger.warning(e)
finally:
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=False)
global fr_model
if fr_model == None:
return redirect(url_for('setup'))
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
celeb_identity = CelebModelOperations.recognize_celebs(
file_path, fr_model
)
return render_template('result.html', image_path=file_path, celeb_identity=celeb_identity)
return redirect(url_for('index'))
if __name__ == '__main__':
# app.debug = True
app.secret_key = 'many random bytes'
port = int(os.environ.get('PORT', 5000)) #The port to be listening to — hence, the URL must be <hostname>:<port>/ inorder to send the request to this program
app.run(host='0.0.0.0', port=port) #Start listening