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:<
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)