Skip to content

Instantly share code, notes, and snippets.

@The-Anton
Forked from khalidmeister/tds-flask-api-full.py
Created November 21, 2022 20:07
Show Gist options
  • Save The-Anton/0965dc3b2c1e18fd7226424e853096d7 to your computer and use it in GitHub Desktop.
Save The-Anton/0965dc3b2c1e18fd7226424e853096d7 to your computer and use it in GitHub Desktop.
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