得到错误-'PngImageFile'对象在使用Socketio从客户端向烧瓶服务器传输视频帧时没有属性'shape'

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

我的应用程序是在客户端打开cam,获取框架,在后端对其执行ML处理,然后将其扔回客户端。

这部分代码(粗体)抛出错误-PngImageFile对象没有属性'shape'。

此代码行有问题-frame = imutils.resize(pimg, width = 700)

我想某些处理格式不正确。请指导

@socketio.on('image')
def image(data_image):
    sbuf = io.StringIO()
    sbuf.write(data_image)

    # decode and convert into image
    b = io.BytesIO(base64.b64decode(data_image))
    pimg = Image.open(b)

    # Process the image frame
    frame = imutils.resize(**pimg,** width=700)
    frame = cv2.flip(frame, 1)
    imgencode = cv2.imencode('.jpg', frame)[1]

    # base64 encode
    stringData = base64.b64encode(imgencode).decode('utf-8')
    b64_src = 'data:image/jpg;base64,'
    stringData = b64_src + stringData

    # emit the frame back
    emit('response_back', stringData)
opencv python-imaging-library flask-socketio
1个回答
0
投票

问题是pimgPIL图像格式。虽然imutils.resize功能要求图像为Numpy数组格式。因此,在pimg = Image.open(b)行之后,您需要将PIL图片转换为Numpy数组,如下所示:

pimg = np.array(pimg)

为此,您必须导入如下的numpy库:

import numpy as np
© www.soinside.com 2019 - 2024. All rights reserved.