-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict_app.py
58 lines (48 loc) · 1.63 KB
/
predict_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
import base64
import numpy as np
import io
from PIL import Image
import keras
from keras import backend as K
from keras.models import Sequential, load_model
from keras.preprocessing.image import img_to_array
from flask import request
from flask import jsonify
from flask import Flask
import tensorflow as tf
app = Flask(__name__)
def get_model():
global model
model = load_model("VGG16_cats_and_dogs.h5")
model._make_predict_function()
print("*.Model loaded!")
def preprocess_image(image,target_size):
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize(target_size)
image = img_to_array(image)
image = np.expand_dims(image,axis=0)
return image
print("* Loading Keras model...")
get_model()
global graph
graph = tf.get_default_graph()
@app.route("/predict", methods=['POST'])
def predict():
with graph.as_default():
message = request.get_json(force=True)
encoded = message['image']
decoded = base64.b64decode(encoded)
image = Image.open(io.BytesIO(decoded))
processed_image = preprocess_image(image, target_size=(224,224))
prediction = model.predict(processed_image).tolist()
response = { 'prediction': {'dog':prediction[0][0],'cat':prediction[0][1]}}
return jsonify(response)
# def predict():
# message = request.get_json(force=True)
#
# image = Image.open(message['image'])
# processed_image = preprocess_image(image, target_size=(224,224))
# prediction = model.predict(processed_image).tolist()
# response = { 'prediction': {'dog':prediction[0][0],'cat':prediction[0][1]}}
# return jsonify(response)