-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
128 lines (100 loc) · 3.72 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
import base64
import logging
import io
import os
from flask import Flask, render_template, request
from load import init_model
from PIL import Image
from util import decode_prob
logger = logging.getLogger("dog_breed_classifier")
logger.setLevel(logging.DEBUG)
app = Flask(__name__)
# Initialize
MODEL_DIR = os.path.abspath("./models")
RESNET_CONFIG = {'arch':
os.path.join(MODEL_DIR,
'model.Resnet50.json'),
'weights':
os.path.join(MODEL_DIR,
'weights.Resnet50.hdf5')}
INCEPTION_CONFIG = {'arch':
os.path.join(MODEL_DIR,
'model.inceptionv3.json'),
'weights':
os.path.join(MODEL_DIR,
'weights.inceptionv3.h5')}
MODELS = {'resnet': RESNET_CONFIG,
'inception': INCEPTION_CONFIG}
@app.route('/index')
@app.route('/')
def index():
return render_template('settings.html')
@app.route('/settings', methods=['GET', 'POST'])
def settings():
"""Select Model Architecture and Initialize
"""
global model, graph, preprocess
# grab model selected
model_name = request.form['model']
config = MODELS[model_name]
# init the model with pre-trained architecture and weights
model, graph = init_model(config['arch'], config['weights'])
# use the proper preprocessing method
if model_name == 'inception':
from util import preprocess_inception
preprocess = preprocess_inception
else:
from util import preprocess_resnet
preprocess = preprocess_resnet
return render_template('select_files.html', model_name=model_name)
@app.route('/predict', methods=['GET', 'POST'])
def predict():
"""File selection and display results
"""
if request.method == 'POST' and 'file[]' in request.files:
inputs = []
files = request.files.getlist('file[]')
for file_obj in files:
# Check if no files uploaded
if file_obj.filename == '':
if len(files) == 1:
return render_template('select_files.html')
continue
entry = {}
entry.update({'filename': file_obj.filename})
try:
img_bytes = io.BytesIO(file_obj.stream.getvalue())
entry.update({'data':
Image.open(
img_bytes
)})
except AttributeError:
img_bytes = io.BytesIO(file_obj.stream.read())
entry.update({'data':
Image.open(
img_bytes
)})
# keep image in base64 for later use
img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
entry.update({'img': img_b64})
inputs.append(entry)
outputs = []
with graph.as_default():
for input_ in inputs:
# convert to 4D tensor to feed into our model
x = preprocess(input_['data'])
# perform prediction
out = model.predict(x)
outputs.append(out)
# decode output prob
outputs = decode_prob(outputs)
results = []
for input_, probs in zip(inputs, outputs):
results.append({'filename': input_['filename'],
'image': input_['img'],
'predict_probs': probs})
return render_template('results.html', results=results)
# if no files uploaded
return render_template('select_files.html')
if __name__ == '__main__':
app.run(debug=True)