Monday, November 1, 2021

[SOLVED] Passing image data to Flask server

Issue

I want to send an image by curl to flask server, i am trying this curl command

curl -F "[email protected]" http://localhost:8000

but it did not work

On the server side I handle the image by this code

@app.route('/home', methods=['POST'])
def home():
    data =request.files['file']
    img = cv2.imread(data)
    fact_resp= model.predict(img)
    return jsonify(fact_resp)

fact_resp is an integer and i am trying to read the image using cv2

Is there anyway to do this?


Solution

You should use the right url for your curl command, which is http://localhost:8000/home, if in fact your app is running on localhost, port 8000.

When it comes to your cv2 code, if you have an issue, please open a separate question with different tags to get the proper help!

Edit:

Tested minimal example curling.py

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/home', methods=['POST'])
def home():
    data = request.files['file']
    return jsonify({"status":"ok"})

app.run(port=8000)

Start with python curling.py

In separate terminal window:

curl -F "[email protected]" http://localhost:8000/home

Output:

{
  "status": "ok"
}


Answered By - Pax Vobiscum