-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
165 lines (127 loc) · 5 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
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
from PIL import Image
from flask import Flask, request, jsonify
from flask_cors import CORS
from keras.models import load_model
from keras.preprocessing.image import img_to_array, load_img
from keras.applications.resnet50 import ResNet50, decode_predictions
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.backend import set_learning_phase
import tensorflow as tf
import numpy as np
import warnings
import pickle
import time
import cv2
import os
application = Flask(__name__)
CORS(application)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
set_learning_phase(0)
global models
models = {
'mnist' : load_model('/home/ubuntu/flask-keras-api-server/models/mnist_model.hdf5'),
'breed_classifier' : load_model('/home/ubuntu/flask-keras-api-server/models/dog-classifier.weights.best.inception3.hdf5'),
'dog_classifier' : ResNet50(weights='imagenet'),
'face_classifier' : cv2.CascadeClassifier('/home/ubuntu/flask-keras-api-server/models/haarcascade_frontalface_alt.xml'),
}
global graph
graph = tf.get_default_graph()
def is_human(img):
model = models['face_classifier']
faces = model.detectMultiScale(img)
return len(faces) > 0
def is_dog(img):
model = models['dog_classifier']
img_4d = np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2]))
pred_label = np.argmax(model.predict(img_4d))
return ((pred_label >= 151) & (pred_label <= 268))
def get_dog_breed(img):
# Reshaping hack to (1, 224, 244, 3)
img = np.array([img])
#img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2])
img = InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(img))
model = models['breed_classifier']
pred = model.predict(img)
index = np.argmax(pred)
with open('/home/ubuntu/flask-keras-api-server/dog_names.pkl', 'rb') as pf:
dog_names = pickle.load(pf)
return dog_names[index]
@application.route('/')
@application.route('/test', methods=["GET","POST"])
def test():
output = {'values' : "Test Succeeded!"}
if request.method == "POST":
return test_string
else:
if 'num' in request.args.keys():
return request.args['num']
else:
response = jsonify(output)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
#return jsonify(output)
@application.route('/mnist', methods=["GET","POST"])
def mnist():
model = models['mnist']
output = {
'image_file' : None,
'image_shape' : None,
'prediction' : None,
'error' : None,
}
with graph.as_default():
if request.method == "POST":
data = request.files.get("image")
output['image_file'] = str(data.filename)
img = Image.open(data)
img_arr = np.array(img, dtype='float32')
res = cv2.resize(img_arr, dsize=(28,28), interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
img_arr_sh = gray.reshape(1, 28, 28, 1)
prediction = model.predict(img_arr_sh)
result = np.argmax(prediction)
output['image_shape'] = str(img_arr.shape)
output['prediction'] = str(result)
else:
output['error'] = "Please POST an image."
response = jsonify(output)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
#return jsonify(output)
@application.route('/dog-classifier', methods=['GET', 'POST', 'PUT'])
def dog_classify():
output = {
'image_file' : None,
'image_shape': None,
'is_human' : False,
'is_dog' : False,
'dog_breed' : None,
'error' : None,
}
with graph.as_default():
test = {'test': 'does this return'}
if request.method == 'POST':
# Preprocess image
data = request.files.get('image')
#REMOVE
#return jsonify({'test': data.filename})
output['image_file'] = str(data.filename)
# Grayscale image
img_gray = load_img(data, color_mode='grayscale', target_size=(224, 224))
# Color image
img_color = load_img(data, target_size=(224, 224))
arr_gray = img_to_array(img_gray, dtype='uint8')
arr_color = img_to_array(img_color)
human = is_human(arr_gray)
dog = is_dog(arr_color)
breed = get_dog_breed(arr_color)
output['image_shape'] = str(arr_color.shape)
output['is_human'] = str(human)
output['is_dog'] = str(dog)
output['dog_breed'] = str(breed)
else:
output['error'] = "POST dog image."
return jsonify(output)
if __name__ == "__main__":
application.run(host='0.0.0.0')