【问题标题】:how to send an numpy array or a pytorch Tensor through http post request using requests module and Flask如何使用 requests 模块和 Flask 通过 http post 请求发送 numpy 数组或 pytorch 张量
【发布时间】:2022-01-07 11:34:15
【问题描述】:

我有一张图片,我想将它发送到服务端。我正在使用requests 模块执行简单的发布请求,如下所示(信息是字典):

    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 中对数组进行编码,然后在服务器中对其进行解码......

【问题讨论】:

    标签: python numpy flask python-requests pytorch


    【解决方案1】:

    您可以尝试将数据转换为正确的 JSON 或使用 Python pickle module,或者如果您有您提到的图像,您可以将其作为文件(多部分请求)发送到服务器,如示例中所示@ 987654322@.

    【讨论】:

    • 这是一半...它工作,但我将图像保存在一个文件中,然后我将文件传递给发布请求...这不好,因为我必须保存存储中的每个图像,我想传递一个文件而不存储它
    【解决方案2】:

    感谢@praba230890 给了我一个简单易学的例子。

    我仍然会在这里写下解决方案,因为提供的链接不完全适合我的情况。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 2014-01-14
      • 2023-03-19
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      相关资源
      最近更新 更多