[Python] Read a base 64 encoded image from memory using OpenCv python library

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    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:

    cv2.imshow('image',img)


    The data URI looks something like this:

    data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7


    So, for clarity I've shown what the image looks like so the base64 string is not broken.


    <img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7">

    The official doc says, that imread accepts a file path as the argument. From this SO answer, if I do something like:

    import base64
    imgdata = base64.b64decode(imgstring) #I use imgdata as this variable itself in references below
    filename = 'some_image.jpg'
    with open(filename, 'wb') as f:
    f.write(imgdata)


    The above code snippet works and the image file gets generated properly. However I don't think so many File IO operations are feasible considering I'd be doing this for every frame of the stream. I want to be able to read the image into the memory directly creating the img object.

    I have tried two solutions that seem to be working for some people.


    1. Using PIL reference:

      pilImage = Image.open(StringIO(imgdata))
      npImage = np.array(pilImage)
      matImage = cv.fromarray(npImage)


      I get cv not defined as I have openCV3 installed which is available to me as cv2 module. I tried img = cv2.imdecode(npImage,0), this returns nothing.


    2. Getting the bytes from decoded string and converting it into an numpy array of sorts

      file_bytes = numpy.asarray(bytearray(imgdata), dtype=numpy.uint8)
      img = cv2.imdecode(file_bytes, 0) #Here as well I get returned nothing

    The documentation doesn't really mention what the imdecode function returns. However, from the errors that I encountered, I guess it is expecting a numpy array or a scalar as the first argument. How do I get a handle on that image in memory so that I can do cv2.imshow('image',img) and all kinds of cool stuff thereafter.

    I hope I was able to make myself clear.

    Continue reading...

Compartilhe esta Página