
# Turning off the warnings
import os, sys, json, time
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"

import tensorflow as tf
from tensorflow import keras

import PIL.Image as Image
import numpy as np
import io

from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications import imagenet_utils
from tensorflow.keras.preprocessing.image import img_to_array

from flask import Flask, request, jsonify, make_response
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)
model = None

import flask
# Loading the model
def load_model():
    global model
    model = ResNet50(weights="imagenet")

def prepare_image(image, target):
    # if the image is not RGB then convert it to RGB
    if image.mode != "RGB":
        image = image.convert("RGB")
    # resize the image to desired shape
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = imagenet_utils.preprocess_input(image)
    return image

@app.route("/predict", methods=["POST"])
def predict():
    data = {"success": False}
    if request.method == "POST":
        if request.files.get("image"):
            # read the image in PIL format
            image = request.files.get("image").read()
            image = Image.open(io.BytesIO(image))

            # preprocess the image
            image = prepare_image(image, target=(224, 224))
            preds = model.predict(image)
            results = imagenet_utils.decode_predictions(preds)
            data["predictions"] = []
            for (imageID, label, prob) in results[0]:
                r = {"label": label, "probability": float(prob)}
                data["predictions"].append(r)

            data["success"] = True
    return jsonify(data)


@app.route('/', methods=["GET", "POST"])
def hello():
    return "Hello world"

if __name__ == '__main__':
    print("loading the model please await....")
    load_model()
    app.run(host="localhost", port=3001)


numpy.expand_dims tensorflow.keras.preprocessing.image.img_to_array tensorflow.keras.applications.imagenet_utils.decode_predictions tensorflow.keras.applications.ResNet50 tensorflow.keras.applications.imagenet_utils.preprocess_input
[(20, 'flask.Flask', 'Flask', (['__name__'], {}), False, 'from flask import Flask, request, jsonify, make_response\n'), (21, 'flask_cors.CORS', 'CORS', (['app'], {}), False, 'from flask_cors import CORS, cross_origin\n'), (28, 'tensorflow.keras.applications.ResNet50', 'ResNet50', ([], {'weights': '"""imagenet"""'}), False, 'from tensorflow.keras.applications import ResNet50\n'), (36, 'tensorflow.keras.preprocessing.image.img_to_array', 'img_to_array', (['image'], {}), False, 'from tensorflow.keras.preprocessing.image import img_to_array\n'), (37, 'numpy.expand_dims', 'np.expand_dims', (['image'], {'axis': '(0)'}), True, 'import numpy as np\n'), (38, 'tensorflow.keras.applications.imagenet_utils.preprocess_input', 'imagenet_utils.preprocess_input', (['image'], {}), False, 'from tensorflow.keras.applications import imagenet_utils\n'), (60, 'flask.jsonify', 'jsonify', (['data'], {}), False, 'from flask import Flask, request, jsonify, make_response\n'), (45, 'flask.request.files.get', 'request.files.get', (['"""image"""'], {}), False, 'from flask import Flask, request, jsonify, make_response\n'), (53, 'tensorflow.keras.applications.imagenet_utils.decode_predictions', 'imagenet_utils.decode_predictions', (['preds'], {}), False, 'from tensorflow.keras.applications import imagenet_utils\n'), (48, 'io.BytesIO', 'io.BytesIO', (['image'], {}), False, 'import io\n'), (47, 'flask.request.files.get', 'request.files.get', (['"""image"""'], {}), False, 'from flask import Flask, request, jsonify, make_response\n')]