Skip to content

error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem' #18461

Closed

Description

@Namburger
System information (version)
  • OpenCV => 3.4
  • Operating System / Platform => aarch64 (debian based OS)
  • Compiler => g++ 8.3.0
Detailed description

I have opencv as an external dependency that I compile from source like this:

ExternalProject_Add(opencv
  GIT_REPOSITORY https://github.com/opencv/opencv
  GIT_TAG "3.4"
  SOURCE_DIR opencv
  BINARY_DIR opencv-build
  CMAKE_ARGS
    -DCMAKE_BUILD_TYPE=RELEASE 
    -DFORCE_VTK=ON
    -DWITH_TBB=ON
    -DWITH_V4L=ON
    -DWITH_QT=OFF
    -DWITH_GTK_2_x=ON
    -DWITH_OPENGL=ON
    -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/opencv_install
)
set(OPENCV_INCLUDE_DIR ${CMAKE_BINARY_DIR}/opencv_install/include/)
set(OPENCV_LIBRARY_DIR ${CMAKE_BINARY_DIR}/opencv_install/lib/)
set(OPENCV_LIBRARIES opencv_imgproc opencv_core opencv_highgui opencv_video opencv_videoio opencv_imgcodecs opencv_features2d)
if(EXISTS "${CMAKE_BINARY_DIR}/opencv_install/share/OpenCV/OpenCVConfig.cmake")
    include(${CMAKE_BINARY_DIR}/opencv_install/share/OpenCV/OpenCVConfig.cmake)
    add_custom_target(rerun)
else()
    add_custom_target(rerun ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} DEPENDS opencv)
endif()

Everything compiles just fine until I run the program, an error like this appears:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.12-pre) /media/sd/nam-detect/edgetpu-detection-camera/build/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Aborted

libgtk2 is definitely installed:

$ sudo apt search libgtk2.0-dev
Sorting... Done
Full Text Search... Done
libgtk2.0-dev/eagle,now 2.24.32-3 arm64 [installed]
  development files for the GTK+ library
Steps to reproduce

This code, which used to works without issue: https://github.com/Namburger/edgetpu-detection-camera
We are only seeing this behavior on one of the system o_0

Any suggestion would be highly appreciated!

Activity

  1. BoShaoLi commented on Oct 29, 2020

    @BoShaoLi

    I have the same question. Did you solve the problem?

  2. Namburger commented on Oct 29, 2020

    @Namburger
    Author

    @15927495612 nope, no words from the team, unfortunately :/

  3. BoShaoLi commented on Oct 29, 2020

    @BoShaoLi

    嘤嘤嘤~

  4. IstiakFahim28 commented on Nov 1, 2020

    @IstiakFahim28

    Facing same error. Please solve this issue....

  5. alalek commented on Nov 1, 2020

    @alalek
    Member

    Check Internet about gtk_init_check() call failure reasons.


    We are only seeing this behavior on one of the system o_0

    We don't help with remote investigation of problems which we are not able to reproduce on our side (because it is very time consuming and usually configuration problem without any following fix into OpenCV), but we can take a look on the clear reason of the problem and suggest some fix/workaround.

  6. cassidylinhares commented on Nov 6, 2020

    @cassidylinhares

    I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window.
    gtk_init_check error solved
    hope that helps

  7. zjdx1998 commented on Nov 28, 2020

    @zjdx1998

    Anything update? I encounter this error when I try the cpp-examples in the WSL ubuntu. Hope you could fix it soon.

  8. n-gineer commented on Dec 23, 2020

    @n-gineer

    To help others, I found that my .bashrc needed to contain the following:
    export DISPLAY=:0.0
    previously, it said
    export DISPLAY=localhost:0.0

  9. Wwupup commented on Jan 12, 2021

    @Wwupup
    Contributor

    I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window.
    gtk_init_check error solved
    hope that helps

    I solved the problem through this method. There is no GUI on the server side, so I need to comment out imshow.

  10. hcgfighting commented on Apr 8, 2021

    @hcgfighting

    To help others, I found that my .bashrc needed to contain the following:
    export DISPLAY=:0.0
    previously, it said
    export DISPLAY=localhost:0.0

    this method is effective

  11. w4w4n78 commented on Apr 13, 2021

    @w4w4n78

    I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window.
    gtk_init_check error solved
    hope that helps

    I tried this on Ubuntu 20.04 but still no luck

  12. TVI-BIZ commented on Apr 20, 2021

    @TVI-BIZ

    So I encounter same problem.
    "Can't initialize GTK backend in function 'cvInitSystem'". In my case I use Docker container and that error appears when I run OpenVino demo from docker. The problem is X11 server for the images or GUI server.You need install and run that server in Docker container. Running have to be when you start Docker container. In my case

    docker run --privileged -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -u 0 --device /dev/dri:/dev/dri --device-cgroup-rule='c 189:* rmw' -v /dev/bus/usb:/dev/bus/usb --rm openvino/ubuntu18_dev:update_1904

    may be that command have some redundant stuff but works for me.My container base on official Intel container - https://hub.docker.com/r/openvino/ubuntu18_dev

  13. chrjen commented on Jun 8, 2021

    @chrjen

    I also had the same issue when running inside of a docker container. To fix the issue and gain access to the X server and the display I found this tutorial very helpful.

    http://wiki.ros.org/docker/Tutorials/GUI

  14. erdemozc commented on Jun 25, 2021

    @erdemozc

    hey guys. I am getting this error on navio2. does anyone know the solution?

  15. reunanen commented on Sep 30, 2021

    @reunanen
    Contributor

    For what it's worth, I'm using xvfb-run -a <my program>.

  16. 4 remaining items

  17. scipioni commented on Jul 7, 2022

    @scipioni

    this mapping -v /tmp/.X11-unix:/tmp/.X11-unix solved for me

  18. phelix001 commented on Oct 4, 2022

    @phelix001

    If you're running headless, you will probably get this error.
    Can't initialize GTK backend in function 'cvInitSystem'

    Solution :
    ssh -Y user@wherever
    I'm not sure how I will overcome this issue when running opencv as a service. Perhaps just get rid of imshow.

  19. liuzihaode commented on Oct 17, 2022

    @liuzihaode

    thank u, your answer solved my problem

  20. bitorangej commented on Feb 8, 2023

    @bitorangej

    it works!

  21. kartikpodugu commented on Apr 11, 2023

    @kartikpodugu

    I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window. gtk_init_check error solved hope that helps

    I had to comment imshow() and also corresponding namedWindow() created for imshow(). Then my issue is resolved.
    I faced same problem with OpenCV 4.7

  22. phelix001 commented on Apr 11, 2023

    @phelix001

    The keyword here is headless install. If headless install and getting the cv2.imshow() error, then use the -Y flag on ssh

  23. sunbeam-217 commented on May 16, 2023

    @sunbeam-217

    If you use SSH to connect you divice or your progress is a server ,and nowhere to display any image.So please remove the code related windows or display.for example imshow()

  24. anvinder commented on May 25, 2023

    @anvinder

    I still have the problem. I am not doing ssh or using vnc. It's a rpi4 and the cv2.imshow function works fine if i run the script myself but when i do it via crontab or rc.local during boot then i see the error "can't initialize gtk backend in function 'cvinitsystem'"

  25. Nandu3219 commented on Aug 10, 2023

    @Nandu3219

    solve me ,for raspberrpi 4 terminal window

  26. arielkantorovich commented on Nov 14, 2023

    @arielkantorovich

    Anything update? I encounter this error when I try the cpp-examples in the WSL ubuntu. Hope you could fix it soon.

    You succeed to slove this problem to WSL?

  27. ramanrewati commented on Jun 7, 2024

    @ramanrewati

    It happens to me while running yolov4 using darknet inferencing on videos. Does it have a fix yet?

  28. rbgreenway commented on Aug 5, 2024

    @rbgreenway

    My fix was to run the command:
    xhost +
    from a terminal outside the docker container

  29. Nickiris commented on Aug 31, 2024

    @Nickiris
  30. shfunc commented on Nov 19, 2024

    @shfunc

    this helps me:

    from host machine:

    ~/work_dir$ vim ~/.ssh/config 
    1 Host 192.168.55.1                                                                                            
      2   HostName 192.168.55.1
      3   User user
      4   ForwardX11 yes
      5   ForwardX11Trusted yes
    

    from ssh machine:

    user@ubuntu:~$ cat /etc/ssh/sshd_config | grep X11
    X11Forwarding yes
    #X11DisplayOffset 10
    X11UseLocalhost yes
    #	X11Forwarding no
    
  31. alaingautherot commented on Feb 12, 2025

    @alaingautherot

    If running docker, you may need xhost +local:docker like so:

    xhost +local:docker \
    	&& docker run -it \
           --env 'DISPLAY=$(DISPLAY)' \
           -v /tmp/.X11-unix:/tmp/.X11-unix \
            ....
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions