I wanna know how can I send a 3-dimension numpy array by requests's post method; how can compress this data, if is possible. The best way could be:
I've see this modes:
Send as a file:
requests.post(api_url, files={'image': open(img_path, 'rb')})
Send as encoded array:
# image_np is a numpy array
_, img_encoded = cv2.imencode('.jpg', image_np)
requests.post(api_url, data=img_encoded.tobytes())
Send as a buffer:
buf = io.BytesIO()
plt.imsave(buf, image_np, format='jpg')
image_data = buf.getvalue()
requests.post(api_url, data=image_data)
Send as string base64:
with open(img_path, 'rb') as fp:
img_encoded = str(b64encode(fp.read()))
r = requests.post(api_url, json={'image': img_encoded})
Firstly there is not going to be perfect way which minimizes all three of the following factors:
There would be some tradeoffs, I can explain the tradeoff with each of the approaches to give you a clear understanding of each method but it is heavily dependent upon the use-case to find the perfect sweet spot among the tradeoffs made.
I will discard the less libraries part here, as you are already compressing the image in JPG format which implicitly means that you are using libJPEG internally. And JPG does offers a pretty decent compression rate so we will go with the JPEG format only.
To decrease the payload we can try following things:
int(color/5)*5
to downsample the image color domain. Minimal effect on computing time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.