|
|
from flask import Flask, request, jsonify, send_file |
|
|
from fastai.vision.all import * |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
model_path = './model.pkl' |
|
|
learn = load_learner(model_path) |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return send_file('index.html') |
|
|
|
|
|
@app.route('/predict', methods=['POST']) |
|
|
def predict(): |
|
|
|
|
|
if 'image' not in request.files: |
|
|
return jsonify({'error': 'No image file found'}), 400 |
|
|
|
|
|
image_file = request.files['image'] |
|
|
|
|
|
|
|
|
image_path = 'uploaded_image.jpg' |
|
|
image_file.save(image_path) |
|
|
|
|
|
|
|
|
img = PILImage.create(image_path) |
|
|
|
|
|
|
|
|
pred_class, pred_idx, probs = learn.predict(img) |
|
|
|
|
|
|
|
|
response = { |
|
|
'predicted_class': pred_class, |
|
|
'predicted_probabilities': probs.tolist() |
|
|
} |
|
|
|
|
|
|
|
|
os.remove(image_path) |
|
|
|
|
|
return jsonify(response) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run() |