Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. # import the necessary packages
  2. from keras.applications import ResNet50
  3. from keras.preprocessing.image import img_to_array
  4. from keras.applications import imagenet_utils
  5. from PIL import Image
  6. import numpy as np
  7. import flask
  8. import io
  9.  
  10. # initialize our Flask application and the Keras model
  11. app = flask.Flask(__name__)
  12. model = None
  13.  
  14. def load_model():
  15.     # load the pre-trained Keras model (here we are using a model
  16.     # pre-trained on ImageNet and provided by Keras, but you can
  17.     # substitute in your own networks just as easily)
  18.     global model
  19.     model = ResNet50(weights="imagenet")
  20.  
  21. def prepare_image(image, target):
  22.     # if the image mode is not RGB, convert it
  23.     if image.mode != "RGB":
  24.         image = image.convert("RGB")
  25.  
  26.     # resize the input image and preprocess it
  27.     image = image.resize(target)
  28.     image = img_to_array(image)
  29.     image = np.expand_dims(image, axis=0)
  30.     image = imagenet_utils.preprocess_input(image)
  31.  
  32.     # return the processed image
  33.     return image
  34.    
  35. @app.route("/predict", methods=["POST"])
  36. def predict():
  37.     # initialize the data dictionary that will be returned from the
  38.     # view
  39.     data = {"success": False}
  40.  
  41.     # ensure an image was properly uploaded to our endpoint
  42.     if flask.request.method == "POST":
  43.         if flask.request.files.get("image"):
  44.             # read the image in PIL format
  45.             image = flask.request.files["image"].read()
  46.             image = Image.open(io.BytesIO(image))
  47.  
  48.             # preprocess the image and prepare it for classification
  49.             image = prepare_image(image, target=(224, 224))
  50.  
  51.             # classify the input image and then initialize the list
  52.             # of predictions to return to the client
  53.             preds = model.predict(image)
  54.             results = imagenet_utils.decode_predictions(preds)
  55.             data["predictions"] = []
  56.  
  57.             # loop over the results and add them to the list of
  58.             # returned predictions
  59.             for (imagenetID, label, prob) in results[0]:
  60.                 r = {"label": label, "probability": float(prob)}
  61.                 data["predictions"].append(r)
  62.  
  63.             # indicate that the request was a success
  64.             data["success"] = True
  65.  
  66.     # return the data dictionary as a JSON response
  67.     return flask.jsonify(data)
  68.  
  69. # if this is the main thread of execution first load the model and
  70. # then start the server
  71. if __name__ == "__main__":
  72.     print(("* Loading Keras model and Flask starting server..."
  73.         "please wait until server has fully started"))
  74.     load_model()
  75.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement