Last active
October 6, 2019 05:50
-
-
Save deep1401/1255524df0898d871952e7cd29eb1795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment