Passing image data to Flask server
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, requestapp = 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 "file=@image.jpg" http://localhost:8000/home
Output:
{ "status": "ok"}
Typical problem: Client sends a .png
image to server and server returns (a numpy array) as result.
For those trying to doing it without post and within Python, this may help:
server-side
import cv2import numpy as npimport requestsfrom flask import Flask,jsonify,request,jsonimport jsonpickle@app.route('/test',methods=['POST'])def predict(): r = request # convert string of image data to uint8 nparr = np.fromstring(r.data, np.uint8) # decode image img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # perform some inference and return numpy array response = np.arange(200).reshape(100,2) #dummy response response_pickled = jsonpickle.encode(response) return response_pickledif __name__ == '__main__': app.run(host='0.0.0.0',port=5000, debug=True)
client-side
import requestsimport jsonimport cv2import jsonpickleaddr = 'http://localhost:5000'test_url = addr + '/test'# prepare headers for http requestcontent_type = 'image/png'headers = {'content-type': content_type}# read image to send to serverimg = cv2.imread('m.png')# encode image as png_, img_encoded = cv2.imencode('.png', img)response = requests.post(test_url, data=img_encoded.tostring(), headers=headers)# check response/resultresult = jsonpickle.decode(response.text)print(np.array(result).shape) #[200,2]