Help needed - handling images in python

Hello,
I have a python script successfully running

In the script I receive image data via mqtt. I capture the data and convert it using numpy

import numpy as np

# convert string of image data to uint8
nparr = np.fromstring(msg.payload, np.uint8)

This gives an array typically like this: [255 216 255 …, 127 255 217]

It allows me then to manipulate the image using functions in cv2 (OpenCV) so that I have an image in memory.

image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Now to make inference, I use the following code, first saving to file system, then reading again

# save the image to disk
cv2.imwrite(cnbr+'.jpg', image)
# load the image (into shared CPU/GPU memory)
img, width, height = jetson.utils.loadImageRGBA(cnbr+'.jpg')
detections = net.Detect(img, width, height, "box,labels,conf")

It works fine but I would really like to skip the saving to file and instead use the image directly but I think the format is wrong. Does anybody know how I could do it instead?

Very grateful to any help and suggestions,

Kind regards, Walter

I answer to myself; figured it out, was right in front of my eyes.

There is a great function available, jetson.utils.cudaFromNumpy. The key is that the image has to have 4 channels including the alpha channel

When I receive the image data via mqtt, it doesn’t have the alpha channel, so I have to convert it into 4 channels in a numpy array including the alpha channel

Now it works great!!!

convert string of image data to uint8 and make some processing

nparr = np.fromstring(msg.payload, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
h,w = image.shape[:2]

add alpha channel to RGB image

cuda_img = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
cuda_mem = jetson.utils.cudaFromNumpy(cuda_img)
detections = net.Detect(cuda_mem, w, h, “box,labels,conf”)