# Serve Model predictions using REST API # Predict and return JSON data @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 flask.request.method == "POST": if flask.request.files.get("image"): data["predictions"] = [] # Read input as image image = flask.request.files["image"].read() image = Image.open(io.BytesIO(image)) # Run inference and get prediction seg_mask, seg_product_category = get_model_output_json(image) # Add prediction results to JSON data r = {"label": seg_product_category} data["predictions"].append(r) # indicate that the request was a success data["success"] = True # return the data dictionary as a JSON response return flask.jsonify(data)