Flask request.data.decode 很慢

问题描述 投票:0回答:1

我正在创建一个简单的小型服务器,它使用 Flask 和 python 拍摄通过 POST 请求发送的照片,对其进行解码并保存。

一切工作正常,但有时真的很慢,经过一些调试我注意到唯一需要很多时间的部分是

request.data.decode('utf-8')
,它需要5到30秒,我可以做些什么来让它更快吗?或更好?这是代码

@app.route('/', methods=['GET', 'POST'])
def SaveImmage():
    if request.method =='POST':
        global counter
        counter+=1

        data_json = json.loads(request.data.decode('utf-8'))
        image = data_json['image']
        
        image_dec = base64.b64decode(image)
        data_np = np.frombuffer(image_dec, dtype = 'uint8')
        decimg = cv2.imdecode(data_np, 1)
        filename = r"C:\Users\... \image.png"
        cv2.imwrite(filename, decimg)

        return Response(...., status = 200)

python flask server request decode
1个回答
0
投票

我使用

request.get_json
而不是
json.loads(request.data.decode('utf-8'))
更改了我的代码,但无论如何看起来很慢,至少现在我知道request.data没有问题。 有更好、更有效的方法来使用 Flask 保存发布请求中的图像吗?

© www.soinside.com 2019 - 2024. All rights reserved.