gpt4 book ai didi

python - cv2.imdecode()从base64中的图像(通过Websocket收到的mimetype image/jpeg)返回None

转载 作者:行者123 更新时间:2023-12-02 17:18:51 24 4
gpt4 key购买 nike

我使用websockets接收视频帧。图像以base64 mimetype image / jpeg编码。我正在尝试将图像转换为np.ndarray。当我阅读图像文件代码时,它工作正常。但是,当我尝试从套接字流中读取图像时,出现了问题。

        image_data = base64.b64decode(part.encoded_image)
np_array = np.frombuffer(image_data, np.uint8)
image = cv2.imdecode(np_array, cv2.IMREAD_UNCHANGED)
在docs中,如果图片不足或损坏,cv2.imdecode()返回None。我的图像是高清格式1280/720。数据没有损坏,因为我可以使用在线转换器成功解码。
这是接收到的数据的开始:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/

最佳答案

您必须跳过data:image/jpeg;base64,以获得仅base64数据,它将为您提供正确的图像数据,并且cv2将对其进行解码和显示。
在示例中,我为此使用split(',', 1)-它为我提供了data:image/jpeg;base64,变量中的header和为data中的图像数据。
我在代码中使用完整图像。

import base64
import numpy as np
import cv2

encoded_image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4QOIRXhpZgAASUkqAAgAAAAJAAABCQABAAAABwAAAAEBCQABAAAABwAAABIBCQABAAAAAQAAABoBCQABAAAASAAAABsBCQABAAAASAAAACgBCQABAAAAAgAAADIBAgAUAAAAegAAABMCCQABAAAAAQAAAGmHBAABAAAAjgAAANwAAAAyMDE5OjExOjA1IDAyOjE1OjE1AAYAAJAHAAQAAAAwMjIxAZEHAAQAAAABAgMAAKAHAAQAAAAwMTAwAaAJAAEAAAABAAAAAqAJAAEAAAAHAAAAA6AJAAEAAAAHAAAAAAAAAAYAAwEDAAEAAAAGAAAAGgEJAAEAAABIAAAAGwEJAAEAAABIAAAAKAEJAAEAAAACAAAAAQ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AAAAAAAAAAAAAAAb/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAGNB//EABUQAQEAAAAAAAAAAAAAAAAAAAQF/9oACAEBAAEFAkMLUL//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDAQE/AX//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/AX//xAAdEAACAgIDAQAAAAAAAAAAAAADBQQGAQIHFRcW/9oACAEBAAY/ApdJp+E4+dRp1y/SaqyNZfN74sHVfRNZVs24/RiFIEJHfjTH/rbj6PuG4y5bBbJ4aT//xAAWEAEBAQAAAAAAAAAAAAAAAAABABH/2gAIAQEAAT8hz/ORbKW1ur93/9oADAMBAAIAAwAAABAD/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAwEBPxB//8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAgEBPxB//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPxAgiWf92Otwc//Z'

header, data = encoded_image.split(',', 1)
#print('header:', header)
#print(' data:', data[:20])

image_data = base64.b64decode(data)
#print('result:', image_data[:20])

np_array = np.frombuffer(image_data, np.uint8)
#print(' array:', np_array[:2])

image = cv2.imdecode(np_array, cv2.IMREAD_UNCHANGED)
#print(' image:', image[:2])

cv2.imshow('image', image)
cv2.waitKey(0)

cv2.destroyAllWindows()
结果:
enter image description here
(尺寸为7x7的人脸图像)

关于python - cv2.imdecode()从base64中的图像(通过Websocket收到的mimetype image/jpeg)返回None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63197529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com