出现错误 - 'PngImageFile' 对象在使用 Socketio 将视频帧从客户端传输到 Flask 服务器时没有属性 'shape'

Getting error - 'PngImageFile' object has no attribute 'shape' on transferring video frames from client to flask server using Socketio

我的应用是在客户端打开cam,获取frame,在后端对其进行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)

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

pimg = np.array(pimg)

为此,您必须像下面这样导入 numpy 库:

import numpy as np

试试这个。这对我的类似问题有所帮助。

img_arr = np.array(img.convert("RGB"))

问题出在图像的模式上。我不得不将它从 'P' 转换为 'RGB'。

print(img)
>> <PIL.PngImagePlugin.PngImageFile image mode=P size=500x281 at 0x7FE836909C10>