Camera doesn't open in docker

Hi! I have a question about camera in docker on Jetson Nano
I use the this simple docker container

# Container with pre-installed PyTorch 1.9.0 and python 3.6.9
FROM nvcr.io/nvidia/l4t-pytorch:r32.6.1-pth1.9-py3

WORKDIR /car

ADD . /car

RUN apt-get update

RUN apt-get install -y python3-opencv

RUN apt-get install -y v4l-utils

I run the container using docker run -it --runtime nvidia --volume /dev/video0:/dev/video0 --device /dev/video0 --volume /home/vovinsa/Documents/SelfDrivingCar/car/:/car/ c5 command
Then I run this script

from utils.camera import Camera

cam = Camera(framerate=1)

while True:
    frame = cam.capture()
    print(frame.shape)

P.S. The camera class

import cv2


class Camera:
    """

    Control CSI Camera Jetson Nano

    """
    def __init__(self,
                 sensor_id=0,
                 capture_width=1280,
                 capture_height=720,
                 display_width=1280,
                 display_height=720,
                 framerate=30,
                 flip_method=0, ):
        self.running = True
        self.gstreamer = self.gstreamer_pipeline(sensor_id=sensor_id,
                                                 capture_width=capture_width,
                                                 capture_height=capture_height,
                                                 display_width=display_width,
                                                 display_height=display_height,
                                                 framerate=framerate,
                                                 flip_method=flip_method)
        self.cap = cv2.VideoCapture(self.gstreamer, cv2.CAP_GSTREAMER)
        assert self.cap.isOpened(), "Unable to open camera"

    def capture(self):
        if self.running:
            ret, frame = self.cap.read()
            if ret is None:
                raise RuntimeError("Unable to get a frame")
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            return frame

    # TODO: implement the function
    def stop_capture(self):
        self.running = False
        self.cap.release()

    @staticmethod
    def gstreamer_pipeline(
            sensor_id,
            capture_width,
            capture_height,
            display_width,
            display_height,
            framerate,
            flip_method,
    ):
        return (
                "nvarguscamerasrc sensor-id=%d !"
                "video/x-raw(memory:NVMM), width=(int)%d, height=(int)%d, framerate=(fraction)%d/1 ! "
                "nvvidconv flip-method=%d ! "
                "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
                "videoconvert ! "
                "video/x-raw, format=(string)BGR ! appsink"
                % (
                    sensor_id,
                    capture_width,
                    capture_height,
                    framerate,
                    flip_method,
                    display_width,
                    display_height,
                )
        )

After running I have this

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    cam = Camera(framerate=1)
  File "/car/utils/camera.py", line 27, in __init__
    assert self.cap.isOpened(), "Unable to open camera"
AssertionError: Unable to open camera

The opencv version is 3.2

Hi @vovinsa, can you add this flag to your docker run command? -v /tmp/argus_socket:/tmp/argus_socket

The same error. Maybe it is due to 3.2 OpenCV version?

I have tried to change image to nvcr.io/nvidia/l4t-ml:r32.5.0-py3 and I have this error

root@c145a78a011b:/car# python3 main.py 
nvbuf_utils: Could not get EGL display connection
nvbuf_utils: ERROR getting proc addr of eglCreateImageKHR
nvbuf_utils: ERROR getting proc addr of eglDestroyImageKHR

(gst-plugin-scanner:45): GStreamer-WARNING **: 09:09:44.297: Failed to load plugin '/usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstnvvideosinks.so': /usr/lib/python3.6/dist-packages/cv2/../../../../lib/aarch64-linux-gnu/libcuda.so.1: file too short

(gst-plugin-scanner:45): GStreamer-WARNING **: 09:09:44.304: Failed to load plugin '/usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstnvvideocuda.so': /usr/lib/python3.6/dist-packages/cv2/../../../../lib/aarch64-linux-gnu/libcuda.so.1: file too short

(gst-plugin-scanner:45): GStreamer-WARNING **: 09:09:44.311: Failed to load plugin '/usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstnveglglessink.so': /usr/lib/python3.6/dist-packages/cv2/../../../../lib/aarch64-linux-gnu/libcuda.so.1: file too short

(gst-plugin-scanner:45): GStreamer-WARNING **: 09:09:44.330: Failed to load plugin '/usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstnvarguscamerasrc.so': /usr/lib/python3.6/dist-packages/cv2/../../../../lib/aarch64-linux-gnu/libcuda.so.1: file too short
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (711) open OpenCV | GStreamer warning: Error opening bin: no element "nvarguscamerasrc"
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    cam = Camera(framerate=1)
  File "/car/utils/camera.py", line 27, in __init__
    assert self.cap.isOpened(), "Unable to open camera"
AssertionError: Unable to open camera

It seems like GStreamer is unable to load the nvarguscamerasrc plugin inside container because of some container runtime issue with mounting the CUDA libraries from the host.

Do you have CUDA Toolkit installed on your device? Do you have these files on your device?

ls -ll /etc/nvidia-container-runtime/host-files-for-container.d/
total 32
-rw-r--r-- 1 root root   594 Aug 11  2020 cuda.csv
-rw-r--r-- 1 root root  5111 Jul  1  2020 cudnn.csv
-rw-r--r-- 1 root root 11726 Jan 15  2021 l4t.csv
-rw-r--r-- 1 root root  1642 Jun 23  2020 tensorrt.csv
-rw-r--r-- 1 root root   325 Aug 11  2020 visionworks.csv

If you continue having issues, you may want to reflash or reinstall the NVIDIA Container packages:

$ apt-cache search nvidia-container
libnvidia-container-tools - NVIDIA container runtime library (command-line tools)
libnvidia-container0 - NVIDIA container runtime library
nvidia-container-csv-cuda - Jetpack CUDA CSV file
nvidia-container-csv-cudnn - Jetpack CUDNN CSV file
nvidia-container-csv-tensorrt - Jetpack TensorRT CSV file
nvidia-container-csv-visionworks - Jetpack VisionWorks CSV file
nvidia-container-runtime - NVIDIA container runtime
nvidia-container-toolkit - NVIDIA container runtime hook
nvidia-docker2 - nvidia-docker CLI wrapper
nvidia-container - NVIDIA Container Meta Package

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