Configure USB Camera in Docker Environment with Jetson

I’m providing a description on how to integrate a USB Camera with the Jetson products (AGX Orin in my instance) inside one of the NVIDIA Docker containers. I found no clear resources online on the topic and had to patch together multiple sources of information, even though it’s the most expected configuration for anyone running multiple projects.

My configuration:

  • AGX Orin Development Kit
  • Jetpack 5.0.2 (L4T 35.1)

Let’s begin!
First and foremost I would suggest to read this post on USB Camera from Jetson Hacks.

  1. Find one of the NVIDIA containers and pull it, e.g. I use the one on DockerHub for PyTorch dustynv/l4t-pytorch:r35.1.0-pth1.13-py3. Make sure that it has opencv-python built for nvidia!

  2. Open a new terminal and run export DISPLAY=:0 . Note that the :0 part could also be :1 (it is in my case). To find it for your case run in the terminal echo $DISPLAY

  3. In the same terminal, run xhost +

→ The above commands are not permanent, so if you close the terminal, re-run them!

  1. To stream the USB Camera in the Docker environment, using libraries such as opencv-python there are two interfaces GSTREAMER or V4L2. The flags to add the docker run command need to be adjusted.
    This is the command I would run and will explain the new flags:

    sudo docker run -it --rm --runtime nvidia --network host --device /dev/video0:/dev/video0 -e DISPLAY:$DISPLAY -v /your/origin/path:/your/destination/path -v /tmp/argus_socket:/tmp/argus_socket dustynv/l4t-pytorch:r35.1.0-pth1.13-py3

    The --device flag should be the path of your connected device. Use the command v4l2-ctl --list-devices to find yours. -e is to set the environmet variable. -v is to mount volumes, and you can use as many as you need. /tmp/argus_socket:/tmp/argus_socket this one is NECESSARY if you want to run with GSTREAMER backend.

  2. Run the docker container, and once you’re in run the same export DISPLAY=:0 with the correct :X.

  3. (Optional) Run python3 and check if opencv-python is installed correctly by typing import cv2 and then cv2.__version__. Then type exit() to leave the Python environment. If there’s no installation I don’t think pip install opencv-python will work.

  4. To quickly test your USB Camera, I would recommend to cd into the home directory and git clone

    Run cd USB-Camera then python3 to check the V4L2 backend, or python3 to check the GSTREAMER backend.

    → Note that depending on your camera specs, you might have to adjust some parameters in the script (e.g. frame height, width or fps).

    Last note, for the GSTREAMER backend, you might/will get the following warning:
    [ WARN:0] global /opt/opencv/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
    It should still work, and I haven’t fully figured out why.

Anyway, happy coding!

1 Like


Thanks for sharing the steps in detail.

Based on the warning message, it seems a bug between GStreamer and OpenCV:


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