Created
June 1, 2021 00:49
-
-
Save khalidmeister/02f89d2d24a341c72ea1f03bc1e547a1 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
import flask | |
import io | |
import string | |
import time | |
import os | |
import numpy as np | |
import tensorflow as tf | |
from PIL import Image | |
from flask import Flask, jsonify, request | |
model = tf.keras.models.load_model('resnet50_food_model') | |
def prepare_image(img): | |
img = Image.open(io.BytesIO(img)) | |
img = img.resize((224, 224)) | |
img = np.array(img) | |
img = np.expand_dims(img, 0) | |
return img | |
def predict_result(img): | |
return 1 if model.predict(img)[0][0] > 0.5 else 0 | |
app = Flask(__name__) | |
@app.route('/predict', methods=['POST']) | |
def infer_image(): | |
if 'file' not in request.files: | |
return "Please try again. The Image doesn't exist" | |
file = request.files.get('file') | |
if not file: | |
return | |
img_bytes = file.read() | |
img = prepare_image(img_bytes) | |
return jsonify(prediction=predict_result(img)) | |
@app.route('/', methods=['GET']) | |
def index(): | |
return 'Machine Learning Inference' | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment