-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
73 lines (58 loc) · 2.12 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
import os
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from tensorflow.keras.models import Sequential, load_model
from werkzeug.utils import secure_filename
import numpy as np
from skimage.color import rgb2gray
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
IMAGE_SIZE = (48, 48)
UPLOAD_FOLDER = 'uploads'
modres = load_model('lenet_weights1.hdf5')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def predict(file):
img = load_img(file, target_size=IMAGE_SIZE)
img = img_to_array(img)/255.0
img = rgb2gray(img)
img = np.expand_dims(img, axis=0)
probs = modres.predict(img)
a = np.argmax(probs)
a_max = np.max(probs)
prob_temp = probs.flatten()
prob_temp[a] = 0.0
b = np.argmax(prob_temp)
b_max = np.max(prob_temp)
#probs[a] = -1
#b = np.argmax(probs)
#b_max = np.max(probs)
dicti = {0:'angry', 1:'irritated', 2:'fearful', 3:'happy', 4:'sad', 5:'surprised', 6:'neutral'}
#output = {dicti.get(a): a_max}
output = []
output.append(dicti.get(a))
output.append(a_max)
output.append(dicti.get(b))
output.append(b_max)
return output
app = Flask(__name__, template_folder='Templates')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def template_test():
return render_template('home.html', label='', imagesource='file://null')
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
output = predict(file_path)
return render_template("home.html", label=output, imagesource=file_path)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == "__main__":
app.run(threaded=False)