NVIDIA / nvImageCodec

A nvImageCodec library of GPU- and CPU- accelerated codecs featuring a unified interface
https://docs.nvidia.com/cuda/nvimagecodec/index.html
Apache License 2.0
52 stars 4 forks source link

Can decode cv2 image? #11

Open anhtt20172948 opened 6 days ago

anhtt20172948 commented 6 days ago

I am trying to decode cv2 image. I read that the decode function receives "Numpy array with bytes to decode" as input. link When try this code below

decoder = nvimgcodec.Decoder()
img = cv2.imread(img_path)
nvimg = decoder.decode(img)

Got: ValueError: array has incorrect number of dimensions: 3; expected When I try

decoder = nvimgcodec.Decoder()
img = cv2.imread(img_path).astype(np.uint8).tobytes()
nvimg = decoder.decode(img)

Got: RuntimeError: nvImageCodec failure: '#4' Where did I go wrong? Thank you very much

smatysik-nv commented 6 days ago

As in sample you can either read data from file and send it decoder to decode

from nvidia import nvimgcodec
decoder = nvimgcodec.Decoder()
with open(resources_dir + "tabby_tiger_cat.jpg", 'rb') as in_file:
    data = in_file.read()
    nv_img_cat = decoder.decode(data)

or you can just pass image path to decoder read function

nv_img = decoder.read(resources_dir + "cat-1046544_640.jp2")

In your example you decoded image using cv2 imread function and passed already decoded image to nvImageCodec decoder.

Anyway, you do not need to use cv2 at all.