简体   繁体   English

通过 requests.post 将 numpy 数组(实际上是图像)发送到 API 的最佳方式(性能)是什么? (Python)

[英]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;我想知道如何通过请求的 post 方法发送一个 3 维 numpy 数组; 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:作为字符串 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.我将在这里丢弃较少的库部分,因为您已经以 JPG 格式压缩图像,这隐含地意味着您在内部使用 libJPEG。 And JPG does offers a pretty decent compression rate so we will go with the JPEG format only.并且 JPG 确实提供了相当不错的压缩率,因此我们将仅使用 JPEG 格式的 go。

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.减小图像尺寸,这可以显着减小图像尺寸,但是您必须找到正确的阈值,在该阈值处,您的 object 检测精度会随着图像尺寸的减小而大大降低。 Minimal effect on computing time对计算时间的影响最小
  • Secondly you can try reducing the colors(basically downsampling the RGB domain).其次,您可以尝试减少颜色(基本上是对 RGB 域进行下采样)。 while JPEG already does this stuff for you but it tries to preserve most of the color information.虽然 JPEG 已经为您完成了这些工作,但它试图保留大部分颜色信息。 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.如果处理 (255, 255, 255) 和 (255, 255, 250) 不会影响 object 检测 model 的准确性,那么您可以只使用int(color/5)*5对图像颜色域进行下采样。 Minimal effect on computing time对计算时间的影响最小
  • Last thing we can try is discard the colors entirely.我们可以尝试的最后一件事是完全丢弃 colors。 and use gray-scale image instead of RGB colors.并使用灰度图像代替 RGB colors。 This also depends upon your object detection model, whether it is sensetive to color data or just requires grayscale information.这也取决于您的 object 检测 model,是对颜色数据敏感还是只需要灰度信息。 Moderate effect on computing time对计算时间的中等影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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