Display images using Open CV when using docker container with xhost +

Hi,

Jetson AGX Orin
GStreamer: 1.16.3
Jetpack: 5.0.2
CUDA Version: 11.4
Operating System + Version: Ubuntu 20.04.4 LT
TensorRT Version: 8.4.1.5-1+cuda11.4
Python Version: 3.8.2-0

I’m trying to display image with container
Here’s the command I do:
docker run --gpus all -it --name test -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --ipc=host –v /home/aaeon/test:/workspace lpr

After enter container:
python /workspace/readVideo.py

And the error message:

No protocol specified
Traceback (most recent call last):
File “/workspace/readVideo.py”, line 9, in
cv2.namedWindow(“Demo”, cv2.WINDOW_NORMAL)
cv2.error: OpenCV(4.5.3) /git/opencv-4.5.3/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can’t initialize GTK backend in function ‘cvInitSystem’

I’ve found a similar topic for my question, but it’s not work before running container

I have to execute “xhost +” every time before executing the python script in container

Here’s the python script

import cv2
rtsp_url = ‘rtsp://77.110.228.219/axis-media/media.amp’
cap = cv2.VideoCapture(rtsp_url)
cv2.namedWindow(“Demo”, cv2.WINDOW_NORMAL)
cv2.resizeWindow(“Demo”, 960, 540)
if not cap.isOpened():
print(“can not open RTSP stream!”)
exit()
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow(‘Demo’, frame)
if cv2.waitKey(1) == ord(‘q’):
break
cap.release()
cv2.destroyAllWindows()

Is there any suggestion?
Thanks for help

Hi,

Could you try if :1 works in your environment?

$ export DISPLAY=:1

Thanks.

Hi,

it doesn’t work and even if using the command “xhost +”

Hi,

Is there any other suggestion?

In addition, what’s the reason cause this error?

I’m not sure whether is related to hardware design or just the software issue of opencv.

Hi,

Have you also exported the DISPLAY variable within the container?

Outside of the container

$ export DISPLAY=:0
$ xhost +

Inside of the container

$ export DISPLAY=:0

Thanks.

Hi

I have check the variable by echo and it showed “:0”

Also double check by using “xrandr” and “who”

Hi,

Could you try to launch the container with the below command?

$ sudo docker run -it --rm --net=host --runtime nvidia -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix -v /home/aaeon/test:/workspace lpr

Thanks.

Hi,

The result is as same as the beginning

Hi,

Sorry for the late update.
If you run the script outside of the container, could you display the image with OpenCV?

Thanks.

Hi,

Yes, only run the script in container failed

GUI backend for your opencv build seems to be GTK.
Is GTK installed in your container ?

If the opencv lib is the same out of docker and it works there, then try installing same GTK version into container if not yet done.
You may check what GUI backend and its version for an opencv build with function getBuildInformation().
For example, if using from python3:

python3 -c 'import cv2; print(cv2.getBuildInformation())'  |& tee | grep -A 8 GUI                

If the versions seems matching, it may be a GTK/X issue.
Can you run this simple GTK test from container or does it also fails to initialize ?

#!/usr/bin/env python

import gi

# You may adapt for your GTK version
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class GTK_Main:
    def __init__(self):
        window = Gtk.Window()
        window.set_title("Test GTK")
        window.set_default_size(100, 100)
        window.connect("destroy", Gtk.main_quit, "WM destroy")
        vbox = Gtk.VBox()
        window.add(vbox)
        hbox = Gtk.HBox()
        vbox.pack_start(hbox, False, False, 0)
        hbox.set_border_width(10)
        self.button = Gtk.Button(label="Quit")
        self.button.connect("clicked", self.exit)
        hbox.pack_start(self.button, False, False, 0)
        window.show_all()

    def exit(self, w):
        Gtk.main_quit()

GTK_Main()
Gtk.main()

If this also fails, you may post the error messages.

Or just checking X display from docker, does this work ?

xeyes

Hi ,

Here’s log

Is that mean I have installed GTK already? Or I miss some packages?

The log showed

No, it just means that your opencv build installed for python3 has been built for using GTK2 as GUI backend.

  1. The log showed


You may just require version 2.0:

gi.require_version('Gtk', '2.0')
  1. What about xeyes ? This would confirm X is working from container.

Hi,

Sorry for bothering, but I’ve solved this issue by remaking our rootfs and reinstalling all the components by SDK manager

It can execute normally by only typing the command “xhost +” once before entering the container

The route cause could be the version of components or we use the EA version of jp5.0.2 rootfs

Anyway, it is fine now

Thanks for your help

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