-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_photos_and_predict.py
executable file
·180 lines (138 loc) · 5.52 KB
/
upload_photos_and_predict.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Combined App using uploading and prediction @author: bilal """
# import the necessary packages
from flask import Flask, render_template, request
from flask_uploads import UploadSet, configure_uploads, IMAGES
import pandas as pd
from bokeh.palettes import Reds5
from bokeh.plotting import figure
from bokeh.embed import components
from keras.applications import ResNet50
from keras.preprocessing.image import img_to_array
from keras.applications import imagenet_utils
from PIL import Image
import numpy as np
import os
IMAGE_PATH = os.path.join("/var/www/html/Dog-breed-Identifier","static/img/")
#model = None
global model
model = ResNet50(weights="imagenet")
app = Flask(__name__)
photos = UploadSet('photos', IMAGES)
app.config['UPLOADED_PHOTOS_DEST'] = IMAGE_PATH
configure_uploads(app, photos)
@app.route('/', methods=['GET'])
def home(error = ""):
photo_name = 'dog_picture.jpg'
predictions = make_predictions_api(photo_name)
if predictions is not None:
top_class = get_top_class(predictions)
script,div = make_predictions_visual(predictions)
else:
predictions = []
top_class = None
script,div = None, None
return render_template('upload_form.html',
photo_name = photo_name,
predictions = predictions,
prediction_result = "prediction.png",
top_class = top_class,
script = script,
div = div,
error = error)
@app.route('/predict', methods=['POST'])
def predict():
try:
"""Make predict and returns the result with the visual embedded"""
if request.method == 'POST' and 'photo' in request.files:
filename = photos.save(request.files['photo'])
predictions = make_predictions_api(filename)
if predictions is not None:
top_class = get_top_class(predictions)
script,div = make_predictions_visual(predictions)
else:
predictions = []
top_class = None
script,div = None, None
return render_template('upload_form.html',
photo_name = filename,
predictions = predictions,
prediction_result = "prediction.png",
top_class = top_class,
script = script,
div = div)
else:
return home("Please upload image of your dog")
except Exception as error:
print(error)
return home("Please upload jpeg image of your dog")
def make_predictions_visual(predictions):
"""returns the html elements of the visual generated"""
df = pd.DataFrame(predictions)
df.sort_values(by="probability",
ascending = False,
inplace = True)
labels = df['label'].values
sizes = df['probability'].values
p = figure(x_range = labels, plot_height = 350,
title = "Confidence over dog breeds",
tools = "", toolbar_location = None)
p.vbar(x=labels, top=sizes, width=0.9, color=Reds5)
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 1
p.xaxis.axis_label = 'Potential Breeds'
p.yaxis.axis_label = 'Probability'
script, div = components(p)
return script,div
def get_top_class(predictions):
"""Return top class, i.e. with the highest probability"""
df = pd.DataFrame(predictions)
df.sort_values(by="probability",
ascending = False,
inplace = True)
return df['label'].iloc[0]
def load_model():
"""load the pre-trained Keras model (here we are using a model"""
global model
model = ResNet50(weights="imagenet")
def prepare_image(image, target):
"""Preparing image for inputting into Neural network"""
# if the image mode is not RGB, convert it
if image.mode != "RGB":
image = image.convert("RGB")
# resize the input image and preprocess it
image = image.resize(target)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = imagenet_utils.preprocess_input(image)
# return the processed image
return image
def make_predictions_api(filename):
"""Make predictions on the uploaded image"""
data = None
try:
## loading uploaded image
image_path = IMAGE_PATH + filename
image = Image.open(open(image_path,'rb'))
# preprocess the image and prepare it for classification
image = prepare_image(image, target=(224, 224))
# classify the input image and then initialize the list
# of predictions to return to the client
preds = model.predict(image)
results = imagenet_utils.decode_predictions(preds)
data = []
# loop over the results and add them to the list of
# returned predictions
for (imagenetID, label, prob) in results[0]:
r = {"label": label, "probability": float(prob)}
data.append(r)
return data['predictions']
except Exception as error:
print(error)
return data
#print('Loading pretrained model')
#load_model()
#print('Model Loaded and application is running')
#app.run(port=80, debug=True)