How to use external USB webcam from L4T-ML Docker container with OpenCV commands?

Dear Experts,

I know this type of question (or similar) has already been asked several times on this forum and please apologize if I ask it ones more, but I read/worked many hours on this problem and could still make zero progress:

I am working with a Jetson AGX Xavier device (Jetpack 4.6) and have just prepared the L4T-ML docker container (NVIDIA NGC) for working with GPU(Cuda) accelerated OpenCV for Computer Vision applications. The OpenCV works well in the container it can communicate with Cuda and I can run several basic image/video processing test in “OpenCV-GPU mode”, but there is a big problem:

OpenCV can’t sense my webcam from the L4T-ML container

If I use OpenCV installed by simple pip install from the SSD/SD card it can perfectly use the webcam but it can only run in cpu mode in this case. I use this code to test my webcam in CPU mode and it works fine:

import cv2
vid = cv2.VideoCapture(0)

while(True):

ret, frame = vid.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

vid.release()
cv2.destroyAllWindows()

I am not even sure that this code is the right test for GPU mode OpenCV from the L4T-ML container but I tried this.

I have tried many modifications that I read in other topics about similar camera poblems, but with zero success, I know the problem can be in many points.

Can you tell me how I could use my webcam from the L4T-ML container?

I really appreciate any opinion.

Hi,

Are you able to access the camera with the OpenCV code you shared?
If yes, you are able to access the camera within docker already.

The VideoCapture only has the CPU implementation.
A simple way to convert the frame data to GPU buffer is via upload.

For example:

gpu_frame = cv.cuda_GpuMat()
gpu_frame.upload(frame)

Here is a public tutorial for your reference:

Thanks.

This OpenCV Code opens the camera when I am outside the container ( from jupyter notebook which is installed directly on the SD Card of the Jetson). This OpenCV Code doesn’t open the camera when I use it inside the L4T-ML container.

I will check the tutorial and the code that you pasted a.s.a.p. !

Thank you very much!

I tried the tutorial, but this code doesn’t open the webcam either:

import cv2 as cv

vod = cv.VideoCapture(0)

ret, frame = vod.read()

scale = 0.5

gpu_frame = cv.cuda_GpuMat()

while ret:

gpu_frame.upload(frame)

resized = cv.cuda.resize(gpu_frame, (int(1280 * scale), int(720 * scale)))

luv = cv.cuda.cvtColor(resized, cv.COLOR_BGR2LUV)
hsv = cv.cuda.cvtColor(resized, cv.COLOR_BGR2HSV)
gray = cv.cuda.cvtColor(resized, cv.COLOR_BGR2GRAY)

# download new image(s) from GPU to CPU (cv2.cuda_GpuMat -> numpy.ndarray)
resized = resized.download()
luv = luv.download()
hsv = hsv.download()
gray = gray.download()

ret, frame = vod.read()

Any idea for correction of the code to open webcam?

Hi,

You will need to enable the webcam access for docker first.

For example, if your camera is mounted at /dev/video0.
Then launch docker with the following command:

sudo docker run --runtime nvidia -it --rm --network host \
    --device /dev/video0 \
    ...                 

Thanks.

Thank you I will try it a.s.a.p. !