How to get just the mask from jetson utils segnet and change colour in opencv

I want to just get the mask from jetson-inference segmentation and change the colour in opencv. I tried the following:

net = jetson.inference.segNet("fcn-resnet18-mhp-512x320")
camera = jetson.utils.gstCamera(2560,720,"/dev/video3")
display = jetson.utils.glDisplay()
net.SetOverlayAlpha(150)

while True:
    cuda_frame, width, height = camera.CaptureRGBA(zeroCopy=1)
    net.Process(cuda_frame, width, height)
    net.Mask(cuda_frame, width, height)
    jetson.utils.cudaDeviceSynchronize ()
    frame = jetson.utils.cudaToNumpy (cuda_frame, width, height, 4)
    lo=np.array([0,0,0,255])
    hi=np.array([255,255,255,255])
    mask=cv2.inRange(frame,lo,hi)
    frame[mask>0] = (255,255,255,255)
    cv2.cvtColor(frame.astype (np.uint8), cv2.COLOR_RGBA2RGB)
    cv2.imshow("frame", frame)
    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

But this turns the entire screen white with some blotches of colour

Hi,

Could you comment out the below API first?
This can check if the camera frame can be read successfully.

    # frame[mask>0] = (255,255,255,255)

Thanks.

Yes the camera frame can be read and outputs the colored mask successfully

Hi,

In your source, the cuda_frame is used for input frame and mask.
Please follows the below sample to declare a mask variable instead.

Thanks.

Is there a way to do this without declaring segmentation buffers?

for example could I just do net.Mask(mask, width, height) and store values as a np array in mask?

Hi,

Please try the following:

cuda_mask = jetson.utils.cudaAllocMapped(width=512, height=320, format=frame.format)

net.Mask(cuda_mask, filter_mode=opt.filter_mode)

mask = jetson.utils.cudaToNumpy(cuda_mask)

Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.