Skip to content

Instantly share code, notes, and snippets.

@yavuzKomecoglu
Created May 10, 2018 21:25
Show Gist options
  • Save yavuzKomecoglu/cc7022ebbe3324b573359f7c6dec2e62 to your computer and use it in GitHub Desktop.
Save yavuzKomecoglu/cc7022ebbe3324b573359f7c6dec2e62 to your computer and use it in GitHub Desktop.
@app.route("/predict", methods=["POST"])
def predict():
# initialize the data dictionary that will be returned from the
# view
data = {"success": False}
# ensure an image was properly uploaded to our endpoint
if request.method == "POST" and request.files['image']:
imagefile = request.files["image"].read()
image = Image.open(io.BytesIO(imagefile))
# preprocess the image and prepare it for classification
image = prepare_image(image, target=(224, 224))
# classify the input image and then initialize the list
# of predictions to return to the client
preds = model.predict(image)
results = imagenet_utils.decode_predictions(preds)
data["predictions"] = []
# loop over the results and add them to the list of
# returned predictions
for (imagenetID, label, prob) in results[0]:
r = {"label": label, "probability": float(prob)}
data["predictions"].append(r)
# indicate that the request was a success
data["success"] = True
print(data)
# return the data dictionary as a JSON response
return jsonify(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment