-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (52 loc) · 1.93 KB
/
app.py
File metadata and controls
62 lines (52 loc) · 1.93 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
import os
from flask import Flask, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
app = Flask(__name__)
from skimage.transform import resize
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.compat.v1.keras.models import load_model
from tensorflow.compat.v1.keras.backend import set_session
import numpy as np
print("Loading model")
global sess
sess = tf.compat.v1.Session()
set_session(sess)
global model
global graph
graph = tf.compat.v1.get_default_graph()
@app.route('/', methods=['GET', 'POST'])
def main_page():
if request.method == 'POST':
file = request.files['file']
filename = secure_filename(file.filename)
file.save(os.path.join('uploads', filename))
return redirect(url_for('prediction', filename=filename))
return render_template('index.html')
@app.route('/prediction/<filename>')
def prediction(filename):
# Step 1
my_image = plt.imread(os.path.join('uploads', filename))
# Step 2
my_image_re = resize(my_image, (32, 32, 3))
# Step 3
with graph.as_default():
set_session(sess)
model = load_model('my_cifar10_model.h5')
probabilities = model.predict(np.array([my_image_re, ]))[0, :]
print(probabilities)
# Step 4
number_to_class = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
index = np.argsort(probabilities)
predictions = {
"class1": number_to_class[index[9]],
"class2": number_to_class[index[8]],
"class3": number_to_class[index[7]],
"prob1": probabilities[index[9]],
"prob2": probabilities[index[8]],
"prob3": probabilities[index[7]],
}
# Step 5
return render_template('predict.html', predictions=predictions)
if __name__ == "__main__":
app.run(debug=True)