Read a base64 encoded image from memory using OpenCv python library

前端 未结 3 1338
梦谈多话
梦谈多话 2020-12-05 07:38

I\'m working on an app that to do some facial recognition from a webcam stream. I get base64 encoded data uri\'s of the canvas and want to use it to do something like this:<

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 08:31

    This worked for me, and doesn't require PIL/pillow or any other dependencies (except cv2):

    import cv2
    import numpy as np
    
    def data_uri_to_cv2_img(uri):
        encoded_data = uri.split(',')[1]
        nparr = np.fromstring(encoded_data.decode('base64'), np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        return img
    
    data_uri = "data:image/jpeg;base64,/9j/4AAQ..."
    img = data_uri_to_cv2_img(data_uri)
    cv2.imshow(img)
    

提交回复
热议问题