0

I'm building a flask image classification service and I'm trying to use cv2 to read the POSTed image.

Here's the postman post setup: enter image description here

On the server side, I have

@app.route("/inference", methods=["POST"])
def inference():

    img = request.files["img"]
    img = cv2.imread(str(img))[...,::-1] # 1
    img = cv2.imread(img)[...,::-1]      # 2
    ...

I wanted to use cv2 because I will needed to resize the image later. However, I'm getting errors:

for "# 1", I'm getting:

TypeError: bad argument type for built-in operation

for "# 2", I'm getting:

TypeError: 'NoneType' object is not subscriptable

Note that cv2.imread() is working fine on my local, and I suspect it's the file/class type of the request object. Any help is appreciated!

TYZ
  • 8,466
  • 5
  • 29
  • 60
  • 1
    The datastructure of `request.files` is a [FileStorage](https://werkzeug.palletsprojects.com/en/0.15.x/datastructures/#werkzeug.datastructures.FileStorage). maybe `request.files["img"].stream.read()` to get a bytestream – Patricio Jun 06 '19 at 01:40
  • @Patricio Thank for the hint! I think I found a solution here: https://stackoverflow.com/questions/47515243/reading-image-file-file-storage-object-using-cv2. I'm going to try to see if it works or not. – TYZ Jun 06 '19 at 01:42

0 Answers0