简体   繁体   中英

What is the best way (performance) to send an numpy array (an image actually) by requests.post to an API? (python)

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:

  • less size in data
  • less libraries
  • less computing time
  • what you want..

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:

  • less size in data
  • less libraries
  • less computing time

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.

less size in data

To decrease the payload we can try following things:

  • Reduce image dimensions, this can drastically reduce the image size, but you have to find the right threshold where your object detection accuracy is much degraded with decrase in the image size. Minimal effect on computing time
  • Secondly you can try reducing the colors(basically downsampling the RGB domain). while JPEG already does this stuff for you but it tries to preserve most of the color information. If treating (255, 255, 255) and (255, 255, 250) won't affect the accuracy of your object detection model then you can just use int(color/5)*5 to downsample the image color domain. Minimal effect on computing time
  • Last thing we can try is discard the colors entirely. and use gray-scale image instead of RGB colors. This also depends upon your object detection model, whether it is sensetive to color data or just requires grayscale information. Moderate 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM