-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
29 lines (23 loc) · 847 Bytes
/
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
from flask import Flask, request, jsonify
from fastai.basic_train import load_learner
from fastai.vision import open_image
from flask_cors import CORS,cross_origin
app = Flask(__name__)
CORS(app, support_credentials=True)
# load the learner
learn = load_learner(path='./models', file='trained_model.pkl')
classes = learn.data.classes
def predict_single(img_file):
'function to take image and return prediction'
prediction = learn.predict(open_image(img_file))
probs_list = prediction[2].numpy()
return {
'category': classes[prediction[1].item()],
'probs': {c: round(float(probs_list[i]), 5) for (i, c) in enumerate(classes)}
}
# route for prediction
@app.route('/predict', methods=['POST'])
def predict():
return jsonify(predict_single(request.files['image']))
if __name__ == '__main__':
app.run()