-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (47 loc) · 1.67 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
# Flask
from flask import Flask , request, render_template, jsonify
from gevent.pywsgi import WSGIServer
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import cv2
# Some utilites
import numpy as np
from util import base64_to_pil
# Declare a flask app
app = Flask(__name__)
print('Model loaded. Check http://127.0.0.1:5003/')
#add your model_path
model_path = '/home/chirag/ML_Model/mobile.h5'
# Load your own trained model
model = load_model(model_path)
model._make_predict_function() # Necessary
print('Model loaded. Start serving...')
def model_predict(img, model):
img = img.resize((224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
return preds
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
# Get the image from post request
img = base64_to_pil(request.json)
# Make prediction
preds = model_predict(img, model)
# Process your result for human
pred_proba = "{:.3f}".format(np.amax(preds))
pred_class = decode_predictions(preds, top=1)
result = str(pred_class[0][0][1])
# result = result.replace('_', ' ').capitalize()
# Serialize the result, you can add additional fields
return jsonify(result=result, probability=pred_proba)
return None
if __name__ == '__main__':
app.run(port=5003, threaded=False)