cv2.imdecode() returns None from image in base64, mimetype image/jpeg received via Websockets

天大地大妈咪最大 提交于 2021-01-29 17:50:25

问题


I use websockets to receive video frames. Image is encoded in base64 mimetype image/jpeg. I'm trying to convert image to np.ndarray. When I read image file code works correct. But when I'm trying to read image from socket stream, issue is occured.

        image_data = base64.b64decode(part.encoded_image)
        np_array = np.frombuffer(image_data, np.uint8)
        image = cv2.imdecode(np_array, cv2.IMREAD_UNCHANGED)

In docs cv2.imdecode() returns None in case when image is short or corrupted. My image is in HD format, 1280/720. Data isn't corrupted as I could successfully decode it using online converter. This is the beginning of received data:

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


回答1:


You have to skip data:image/jpeg;base64, to get only base64 data which will give you correct image data and cv2 will decode and display it.

In example I use split(',', 1) for this - it gives me data:image/jpeg;base64, in variable header and image data in data.

I use full image in code.

import base64
import numpy as np
import cv2

encoded_image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4QOIRXhpZgAASUkqAAgAAAAJAAABCQABAAAABwAAAAEBCQABAAAABwAAABIBCQABAAAAAQAAABoBCQABAAAASAAAABsBCQABAAAASAAAACgBCQABAAAAAgAAADIBAgAUAAAAegAAABMCCQABAAAAAQAAAGmHBAABAAAAjgAAANwAAAAyMDE5OjExOjA1IDAyOjE1OjE1AAYAAJAHAAQAAAAwMjIxAZEHAAQAAAABAgMAAKAHAAQAAAAwMTAwAaAJAAEAAAABAAAAAqAJAAEAAAAHAAAAA6AJAAEAAAAHAAAAAAAAAAYAAwEDAAEAAAAGAAAAGgEJAAEAAABIAAAAGwEJAAEAAABIAAAAKAEJAAEAAAACAAAAAQIEAAEAAAAqAQAAAgIEAAEAAABVAgAAAAAAAP/Y/+AAEEpGSUYAAQEAAAEAAQAA/9sAQwABAQEBAQEBAQEBA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AAAAAAAAAAQF/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()

Result:

(face image with size 7x7)



来源:https://stackoverflow.com/questions/63197529/cv2-imdecode-returns-none-from-image-in-base64-mimetype-image-jpeg-received-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!