Skip to content

Instantly share code, notes, and snippets.

@deepak-karkala
Created December 21, 2020 06:35
Show Gist options
  • Select an option

  • Save deepak-karkala/d51c41cad25ea0363708378c5595b791 to your computer and use it in GitHub Desktop.

Select an option

Save deepak-karkala/d51c41cad25ea0363708378c5595b791 to your computer and use it in GitHub Desktop.

Revisions

  1. deepak-karkala created this gist Dec 21, 2020.
    29 changes: 29 additions & 0 deletions rest_api_service.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # 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)