0

I am trying to send a compressed numpy array (compressed with zlib) to the flask server with post request, but the compressed bytes object is getting changed in the server end. How to properly send the bytes object with requests post request so that I can decompress on the server end?

server.py

from flask import Flask
from flask_restful import Resource, Api, reqparse
import json
import numpy as np
import base64


# compression
import zlib

app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('imgb64')

class Predict(Resource):
    def post(self):
        data = parser.parse_args()
        if data['imgb64'] == "":
            return {
                    'data':'',
                    'message':'No file found',
                    'status':'error'
                    }

        img = data['imgb64']

        print('rec')

        # decompress
        print(type(img))
        print(img)

        dec = zlib.decompress(img) # this gives me error



        if img:
            pass

            return json.dumps({
                    'data': 'done', 
                    'message':'darknet processed',
                    'status':'success'
                    })
        return {
                'data':'',
                'message':'Something when wrong',
                'status':'error'
                }


api.add_resource(Predict,'/predict')

if __name__ == '__main__':
    app.run(debug=True, host = '0.0.0.0', port = 5000, threaded=True)

client.py

import numpy as np 
import base64
import zlib
import requests

frame = np.random.randint(0,255,(5,5,3)) # dummy rgb image
# compress

data = zlib.compress(frame)
print('b64 encoded')
print(data)
print(len(data))
print(type(data))

r = requests.post("http://127.0.0.1:5000/predict", data={'imgb64' : data}) # sending compressed numpy array

This gives me the following error:

TypeError: a bytes-like object is required, not 'str'

So, I tried to convert the string to bytes object:

dec = zlib.decompress(img.encode()) # this gives me error

But, this one also gives me an error:

zlib.error: Error -3 while decompressing data: incorrect header check

I tried with other encodings, they also failed.

One thing I noticed is, when I print the compressed bytes in the client end, it reads:

b'x\x9c-\xcf?J\x82q\x00\x06\xe0\x0ftrVP\\C\xf4\x06\x9268\t\xcdR\x8ej.F\xa0\xe0\xd0\xa6\xa3\xe0V\x07\x10\x1cL\xc8\xd1\x03\xd4\xe4\t\x0c\x12\x84\xb6D\x0c#\xbc\x80O\xf0\x1b\x9e\xf5\xfdS\x89\xa2h\xcf\x9a\x03\xef\xc4\xf8cF\x92\r\xbf4i\x11g\xc83\x0f\x8c\xb9\xa2@\x8e\x1bn\xd91g\xc0\x91%\xd7\xdc\xf3M\x83<i:L\xa8\xf1\x19\xfa\xffw\xfd\xf0\xc5\x94:O\x9cH\x85\xcc6#\x1e\xc3\xf6\x05\xe5\xa0\xc7\x96\x04]J\\\x90\xa1\x1f~Ty\xe1\x8d\x15w|P\xe4\x95K\xb2!\xe3\x0cw)%I'

But on the server end, the received string is completely different:

�4ig�3���@�n�1g��%���M�<i:L����w��Ŕ:O�H��6#���ǖ]J\��~Ty�w|P�K�!�w)%I

I also tried to send the bytes as string, by

r = requests.post("http://127.0.0.1:5000/predict", data={'imgb64' : str(data)})

But, I can't decompress the data on the server end.

1 Answer 1

1

It seems, I can't send the zlib compressed bytes directly, so I used base64 to encode the data into ascii string.

So, in summary this worked for me, numpy array/any non-string data -> zlib compression -> base64 encode -> post request -> flask -> base64 decode -> zlib decompress

client.py

import numpy as np 
import base64
import zlib
import requests

frame = np.random.randint(0,255,(5,5,3)) # dummy rgb image

# compress

data = zlib.compress(frame)

data = base64.b64encode(data)


data_send = data

data2 = base64.b64decode(data)

data2 = zlib.decompress(data2)


fdata = np.frombuffer(data2, dtype=np.uint8)

print(fdata)


r = requests.post("http://127.0.0.1:5000/predict", data={'imgb64' : data_send})

server.py

from flask import Flask
from flask_restful import Resource, Api, reqparse
import json
import numpy as np
import base64


# compression
import zlib
import codecs


app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('imgb64', help = 'type error')

class Predict(Resource):
    def post(self):
        data = parser.parse_args()
        #print(data)
        if data['imgb64'] == "":
            return {
                    'data':'',
                    'message':'No file found',
                    'status':'error'
                    }

        #img = open(data['imgb64'], 'r').read() # doesn't work
        img = data['imgb64']


        data2 = img.encode()
        data2 = base64.b64decode(data2)

        data2 = zlib.decompress(data2)

        fdata = np.frombuffer(data2, dtype=np.uint8)

        print(fdata)

        if img:

            return json.dumps({
                    'data': 'done', 
                    'message':'darknet processed',
                    'status':'success'
                    })
        return {
                'data':'',
                'message':'Something when wrong',
                'status':'error'
                }


api.add_resource(Predict,'/predict')

if __name__ == '__main__':
    app.run(debug=True, host = '0.0.0.0', port = 5000, threaded=True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.