OpenCV’s cv2.VideoCapture(0) does not work from NGC container

Hi

I’m using JetPack 5.0.2 (L4T R35.1.0).
I chose 「l4t-pytorch:r35.1.0-pth1.12-py3」 which fits this jetpack.

I ran the below command to start the container.

$ sudo docker run -it --runtime nvidia --network myappnet --device=/dev/video0:/dev/video0:rwm -e DISPLAY=$DISPLAY -v /tmp/X11-unix:/tmp/X11-unix:rw -v /home/usr/Desktop/work/test:/test nvcr.io/nvidia/l4t-pytorch:r35.1.0-pth1.12-py3 bash

I have verified the following:
・USB camera device is video0

I ran a very simple openCV python code.

import cv2

# Video
frameWidth = 640
frameHeight = 480

#Video Source
cap = cv2.VideoCapture(0)

while True:
    ret, img = cap.read()
    img = cv2.resize(img, (frameWidth, frameHeight))
    cv2.imshow('Video', img)
    print('ret=', ret)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

But I get an error! !

root@693584be5dfb:/test# python3 cap.py
[ 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
Unable to init server: Could not connect: Connection refused
Traceback (most recent call last):
  File "cap.py", line 13, in <module>
    cv2.imshow('Video', img)
cv2.error: OpenCV(4.5.0) /opt/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

I’ve been facing this issue for 2 days now and it’s breaking my heart.
Help me please

Hi,

Please check below comment for information:

Thanks.

@AastaLLL

Thank you for your advice.

I tried, but it didn’t work.
I will attach the log at that time.
What else do you have to do?

Hi

It worked when I changed DISPLAY = :0 to DISPLAY = :1.
I will continue to try camera photography.
If there is no problem, I will close this case.

export DISPLAY=:1

Hi

OpenCV’s cv2.VideoCapture(0) does not work from NGC container.
The return value of cap.read() of the capturing API will be False.

please someone help me

I have done the following
before running the container…

$ export DISPLAY=:1
$ xhost +

in a container…

$ export DISPLAY=:1

Hi,

So currently, the display works but the access of the camera fails.

To read the camera within the container, please enable access when launching the container.
For example, if the camera is mounted at /dev/video0:

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

Thanks.

@AastaLLL

Hi

Thank you my friend.
I’m sure the USB camera is mounted on dev/video0. And I’m sharing the command when I docker run in my first post.
I believe the docker run command is fine.

$ sudo docker run -it --runtime nvidia --network myappnet --device=/dev/video0:/dev/video0:rwm -e DISPLAY=$DISPLAY -v /tmp/X11-unix:/tmp/X11-unix:rw -v /home/usr/Desktop/work/test:/test nvcr.io/nvidia/l4t-pytorch:r35.1.0-pth1.12-py3 bash

After investigating various things, I suspect that GStreamer is occupied and OpenCV cannot be used.

Do you have information about this?
I haven’t tried it yet, but there was also an example of inputting parameters for cv2.VideoCapture in the code below.

GST_STR = 'nvarguscamerasrc \
    ! video/x-raw(memory:NVMM), width=1920, height=1080, format=(string)NV12, framerate=(fraction)30/1 \
    ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx \
    ! videoconvert \
    ! appsink'
WINDOW_NAME = 'Camera Test'
 
cap = cv2.VideoCapture(GST_STR, cv2.CAP_GSTREAMER)

Hi

See the link below for the solution.

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