如何使用请求模块和 Flask 通过 http post 请求发送 numpy 数组或 pytorch 张量
how to send an numpy array or a pytorch Tensor through http post request using requests module and Flask
我有一张图片,我想将它发送到服务器。我正在使用 requests
模块来执行简单的 post 请求,如下所示(信息是字典):
import requests
print(type(info["array_image"]))
print(type(info["visual_features"]))
response = requests.post("url", data=info)
输出:
<class 'numpy.ndarray'>
<class 'torch.Tensor'>
在服务器端,我试图至少将它们作为数组接收:
from flask import Flask, request
@app.route('/path', methods=['POST'])
def function_name():
visual_features = request.form['visual_features']
array_image = request.form['array_image']
print(type(array_image))
print(type(visual_features))
输出:
<class 'str'>
<class 'str'>
我想获取一个字节数组来构建图像,但我得到的是一个字符串...
如果我找不到方法,我将在 bas64 中对数组进行编码,然后在服务器中对其进行解码...
您可以尝试将数据转换为正确的 JSON 或使用 Python pickle module or if you have an image as you've mentioned you can send it as a file (multipart request) to the server as shown in the examples here.
感谢@praba230890 给我一个简单易懂的例子。
我仍然会在这里写下解决方案,因为提供的 link 不完全适合我的情况。
import pickle
import io
bytes_image = pickle.dumps(info["array_image"])
stream = io.BytesIO(bytes_image)
files = {"bytes_image": stream}
info["array_image"] = None
response = http.post("url", data=info, files=files)
在服务器端:
from flask import Flask, request
@app.route('/path', methods=['POST'])
def function_name():
image = request.files.get('bytes_image')
bytes_image = image.read()
如果您想从文件中获取图像,则:
requests.post("http://localhost:5000/predict",
files={"file": open('<PATH/TO/.jpg/FILE>/cat.jpg','rb')})
我目前使用的解决方案:
记住info["array_image"]
是一个numpy数组,info
是一个字典
import io
info["image_shape_width"] = info["array_image"].shape[0]
info["image_shape_height"] = info["array_image"].shape[1]
bytes_image = info["array_image"].tobytes()
stream = io.BytesIO(bytes_image)
files = {"bytes_image": stream}
info["array_image"] = None
response = http.post(self.ip + "path", data=info, files=files)
然后收到
from flask import Flask, request
import numpy as np
@app.route('/path', methods=['POST'])
def function_name():
bytes_image = request.files.get('bytes_image')
bytes_image = bytes_image.read()
array_image = np.frombuffer(bytes_image, dtype=dtype)
shape = (int(request.form['image_shape_width']), int(request.form['image_shape_height']), 3)
array_image = np.reshape(array_image, shape)
image = Image.fromarray(array_image)
我有一张图片,我想将它发送到服务器。我正在使用 requests
模块来执行简单的 post 请求,如下所示(信息是字典):
import requests
print(type(info["array_image"]))
print(type(info["visual_features"]))
response = requests.post("url", data=info)
输出:
<class 'numpy.ndarray'>
<class 'torch.Tensor'>
在服务器端,我试图至少将它们作为数组接收:
from flask import Flask, request
@app.route('/path', methods=['POST'])
def function_name():
visual_features = request.form['visual_features']
array_image = request.form['array_image']
print(type(array_image))
print(type(visual_features))
输出:
<class 'str'>
<class 'str'>
我想获取一个字节数组来构建图像,但我得到的是一个字符串... 如果我找不到方法,我将在 bas64 中对数组进行编码,然后在服务器中对其进行解码...
您可以尝试将数据转换为正确的 JSON 或使用 Python pickle module or if you have an image as you've mentioned you can send it as a file (multipart request) to the server as shown in the examples here.
感谢@praba230890 给我一个简单易懂的例子。
我仍然会在这里写下解决方案,因为提供的 link 不完全适合我的情况。
import pickle
import io
bytes_image = pickle.dumps(info["array_image"])
stream = io.BytesIO(bytes_image)
files = {"bytes_image": stream}
info["array_image"] = None
response = http.post("url", data=info, files=files)
在服务器端:
from flask import Flask, request
@app.route('/path', methods=['POST'])
def function_name():
image = request.files.get('bytes_image')
bytes_image = image.read()
如果您想从文件中获取图像,则:
requests.post("http://localhost:5000/predict",
files={"file": open('<PATH/TO/.jpg/FILE>/cat.jpg','rb')})
我目前使用的解决方案:
记住info["array_image"]
是一个numpy数组,info
是一个字典
import io
info["image_shape_width"] = info["array_image"].shape[0]
info["image_shape_height"] = info["array_image"].shape[1]
bytes_image = info["array_image"].tobytes()
stream = io.BytesIO(bytes_image)
files = {"bytes_image": stream}
info["array_image"] = None
response = http.post(self.ip + "path", data=info, files=files)
然后收到
from flask import Flask, request
import numpy as np
@app.route('/path', methods=['POST'])
def function_name():
bytes_image = request.files.get('bytes_image')
bytes_image = bytes_image.read()
array_image = np.frombuffer(bytes_image, dtype=dtype)
shape = (int(request.form['image_shape_width']), int(request.form['image_shape_height']), 3)
array_image = np.reshape(array_image, shape)
image = Image.fromarray(array_image)