-
Notifications
You must be signed in to change notification settings - Fork 20
/
api.py
46 lines (33 loc) · 1.11 KB
/
api.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
import flask
from flask import Flask, request, render_template
from sklearn.externals import joblib
import numpy as np
from scipy import misc
app = Flask(__name__)
@app.route("/")
@app.route("/index")
def index():
return flask.render_template('index.html')
@app.route('/predict', methods=['POST'])
def make_prediction():
if request.method=='POST':
# get uploaded image file if it exists
file = request.files['image']
if not file: return render_template('index.html', label="No file")
# read in file as raw pixels values
# (ignore extra alpha channel and reshape as its a single image)
img = misc.imread(file)
img = img[:,:,:3]
img = img.reshape(1, -1)
# make prediction on new image
prediction = model.predict(img)
# squeeze value from 1D array and convert to string for clean return
label = str(np.squeeze(prediction))
# switch for case where label=10 and number=0
if label=='10': label='0'
return render_template('index.html', label=label)
if __name__ == '__main__':
# load ml model
model = joblib.load('model.pkl')
# start api
app.run(host='0.0.0.0', port=8000, debug=True)