Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found"
QtWS25 Last Chance

Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found"

Scheduled Pinned Locked Moved Solved Qt for Python
30 Posts 19 Posters 68.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lvlanson
    wrote on last edited by
    #1

    Hey there,

    I am working on a big project, creating a GUI for working with convolutional neural networks. I need to use cv2 for image processing. For some reason I cannot import cv2 without my application creating an error.

    I am using pycharm as an IDE which is handling my virtual environment via pip. The librarys I use are the following:

    • PyQt5
    • pyqtchart
    • pyyaml
    • Pillow
    • tensorflow
    • distro
    • numpy
    • sklearn
    • pandas
    • opencv-python

    When I use cv2 I get the following error-message:

    qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "$HOME/Documents/gui_nn/venv/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
    This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

    Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.

    JonBJ Pablo J. RoginaP eyllanescE 3 Replies Last reply
    0
    • L lvlanson

      I am running an ubuntu based linux system (pop_os 20.04). I am not connecting remotely to a device. I am using Nvidia xserver driver from my workstation.

      Weird thing is, as long as I don't import or use cv2, everything works flawlessly. As soon as I import cv2, I get this error.
      This is what the export QT_DEBUG_PLUGINS=1 printed on the command line:
      https://pastebin.com/uxm7LZpc

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #5

      $ grep libqxcb.so output
      QFactoryLoader::QFactoryLoader() looking at "/home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"
      Found metadata in lib /home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so, metadata=
      QFactoryLoader::QFactoryLoader() looking at "/home/lvlanson/.local/lib/python3.8/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so"
      Found metadata in lib /home/lvlanson/.local/lib/python3.8/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so, metadata=
      loaded library "/home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"

      The QPA plugin loading is finding 2 copies of the XCB QPA plugin, and choosing to load the version packaged with cv2. The version of Qt the plugin comes from appears to be incompatible with the version of Qt attempting to load it. I'm guess that Qt version is from PyQt.

      This might be solvable by changing the order of imports, object instantiation, or both. Changing the plugin loading path might be possible, although python modules sometimes hardcode their own idea of where things should come from. Deleting the cv2 version of the plugin might work. If those options are unworkable or unreasonable, you may be stuck rebuilding PyQt and/or OpenCV.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      6
      • L lvlanson

        Hey there,

        I am working on a big project, creating a GUI for working with convolutional neural networks. I need to use cv2 for image processing. For some reason I cannot import cv2 without my application creating an error.

        I am using pycharm as an IDE which is handling my virtual environment via pip. The librarys I use are the following:

        • PyQt5
        • pyqtchart
        • pyyaml
        • Pillow
        • tensorflow
        • distro
        • numpy
        • sklearn
        • pandas
        • opencv-python

        When I use cv2 I get the following error-message:

        qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "$HOME/Documents/gui_nn/venv/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
        This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

        Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @lvlanson

        export QT_DEBUG_PLUGINS=1
        

        and then run your app for debug diagnostics for this error.

        1 Reply Last reply
        1
        • L lvlanson

          Hey there,

          I am working on a big project, creating a GUI for working with convolutional neural networks. I need to use cv2 for image processing. For some reason I cannot import cv2 without my application creating an error.

          I am using pycharm as an IDE which is handling my virtual environment via pip. The librarys I use are the following:

          • PyQt5
          • pyqtchart
          • pyyaml
          • Pillow
          • tensorflow
          • distro
          • numpy
          • sklearn
          • pandas
          • opencv-python

          When I use cv2 I get the following error-message:

          qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "$HOME/Documents/gui_nn/venv/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
          This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

          Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #3

          @lvlanson said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

          qt.qpa.plugin: Could not load the Qt platform plugin "xcb"

          Although you didn't state your platform/OS, are you running an X server, or if connecting remotely to your device, are you properly forwarding X?

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • L Offline
            L Offline
            lvlanson
            wrote on last edited by
            #4

            I am running an ubuntu based linux system (pop_os 20.04). I am not connecting remotely to a device. I am using Nvidia xserver driver from my workstation.

            Weird thing is, as long as I don't import or use cv2, everything works flawlessly. As soon as I import cv2, I get this error.
            This is what the export QT_DEBUG_PLUGINS=1 printed on the command line:
            https://pastebin.com/uxm7LZpc

            jeremy_kJ 1 Reply Last reply
            0
            • L lvlanson

              I am running an ubuntu based linux system (pop_os 20.04). I am not connecting remotely to a device. I am using Nvidia xserver driver from my workstation.

              Weird thing is, as long as I don't import or use cv2, everything works flawlessly. As soon as I import cv2, I get this error.
              This is what the export QT_DEBUG_PLUGINS=1 printed on the command line:
              https://pastebin.com/uxm7LZpc

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #5

              $ grep libqxcb.so output
              QFactoryLoader::QFactoryLoader() looking at "/home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"
              Found metadata in lib /home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so, metadata=
              QFactoryLoader::QFactoryLoader() looking at "/home/lvlanson/.local/lib/python3.8/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so"
              Found metadata in lib /home/lvlanson/.local/lib/python3.8/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so, metadata=
              loaded library "/home/lvlanson/.local/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"

              The QPA plugin loading is finding 2 copies of the XCB QPA plugin, and choosing to load the version packaged with cv2. The version of Qt the plugin comes from appears to be incompatible with the version of Qt attempting to load it. I'm guess that Qt version is from PyQt.

              This might be solvable by changing the order of imports, object instantiation, or both. Changing the plugin loading path might be possible, although python modules sometimes hardcode their own idea of where things should come from. Deleting the cv2 version of the plugin might work. If those options are unworkable or unreasonable, you may be stuck rebuilding PyQt and/or OpenCV.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              6
              • L Offline
                L Offline
                lvlanson
                wrote on last edited by
                #6

                Deleting it worked. This is a real bummer this problem.

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  Hikari Tretaki
                  wrote on last edited by
                  #7

                  @lvlanson I had the same problem. You can install headless opencv without GUI. Then it works.

                  pip install opencv-python-headless

                  argosopentechA R V V 4 Replies Last reply
                  5
                  • H Hikari Tretaki

                    @lvlanson I had the same problem. You can install headless opencv without GUI. Then it works.

                    pip install opencv-python-headless

                    argosopentechA Offline
                    argosopentechA Offline
                    argosopentech
                    wrote on last edited by
                    #8

                    @Hikari-Tretaki thanks for the tip! I had the same problem and switching from opencv-python to opencv-python-headless fixed it for me.

                    1 Reply Last reply
                    1
                    • H Hikari Tretaki

                      @lvlanson I had the same problem. You can install headless opencv without GUI. Then it works.

                      pip install opencv-python-headless

                      R Offline
                      R Offline
                      Rainerino
                      wrote on last edited by
                      #9

                      @Hikari-Tretaki Thank you very much! Your solution work for me too!

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        michwil99
                        wrote on last edited by
                        #10

                        @Hikari-Tretaki thank you very much!!

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          yashwant
                          wrote on last edited by
                          #11

                          Hi, The error still persists even when I use "pip install opencv-python-headless"

                          Error message:
                          qt.qpa.xcb: could not connect to display
                          qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
                          This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

                          Available platform plugins are: eglfs, minimal, minimalegl, offscreen, vnc, webgl, xcb.

                          /opt/conda/envs/yashenv2/.tmp8gb_phfb: line 3: 17 Aborted python TAD_GUI_Contoller.py
                          ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', 'TAD_GUI_Contoller.py']' command failed. (See above for error)

                          Other info: When I used opencv-python (Error was similar like above except it had - Could not load the Qt platform plugin "xcb" in ".../python3.7/site-packages/cv2/qt/plugins" even though it was found.). I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container. (The python codes work fine in windows)

                          Am I missing some Ubuntu libraries? I tried installing libxcb's and many other libraries. It just does not work. Or is it related to PyQt5?

                          jsulmJ Pablo J. RoginaP 2 Replies Last reply
                          0
                          • Y yashwant

                            Hi, The error still persists even when I use "pip install opencv-python-headless"

                            Error message:
                            qt.qpa.xcb: could not connect to display
                            qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
                            This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

                            Available platform plugins are: eglfs, minimal, minimalegl, offscreen, vnc, webgl, xcb.

                            /opt/conda/envs/yashenv2/.tmp8gb_phfb: line 3: 17 Aborted python TAD_GUI_Contoller.py
                            ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', 'TAD_GUI_Contoller.py']' command failed. (See above for error)

                            Other info: When I used opencv-python (Error was similar like above except it had - Could not load the Qt platform plugin "xcb" in ".../python3.7/site-packages/cv2/qt/plugins" even though it was found.). I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container. (The python codes work fine in windows)

                            Am I missing some Ubuntu libraries? I tried installing libxcb's and many other libraries. It just does not work. Or is it related to PyQt5?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                            Am I missing some Ubuntu libraries?

                            Yes.
                            Set QT_DEBUG_PLUGINS before starting the app and check output.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • Y Offline
                              Y Offline
                              yashwant
                              wrote on last edited by
                              #13

                              @jsulm said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                              Set QT_DEBUG_PLUGINS

                              Can you tell me how can I set "export QT_DEBUG_PLUGINS =1" in docker container? Should it be written inside Dockerfile? or should I run the container something like docker run -it Image -v export QT_DEBUG_PLUGINS =1 ? or should I be using docker exec ?

                              jsulmJ 1 Reply Last reply
                              0
                              • Y yashwant

                                @jsulm said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                                Set QT_DEBUG_PLUGINS

                                Can you tell me how can I set "export QT_DEBUG_PLUGINS =1" in docker container? Should it be written inside Dockerfile? or should I run the container something like docker run -it Image -v export QT_DEBUG_PLUGINS =1 ? or should I be using docker exec ?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #14

                                @yashwant That I don't know, sorry.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • Y Offline
                                  Y Offline
                                  yashwant
                                  wrote on last edited by
                                  #15

                                  ok :( any thanks!

                                  1 Reply Last reply
                                  0
                                  • Y yashwant

                                    Hi, The error still persists even when I use "pip install opencv-python-headless"

                                    Error message:
                                    qt.qpa.xcb: could not connect to display
                                    qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
                                    This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

                                    Available platform plugins are: eglfs, minimal, minimalegl, offscreen, vnc, webgl, xcb.

                                    /opt/conda/envs/yashenv2/.tmp8gb_phfb: line 3: 17 Aborted python TAD_GUI_Contoller.py
                                    ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', 'TAD_GUI_Contoller.py']' command failed. (See above for error)

                                    Other info: When I used opencv-python (Error was similar like above except it had - Could not load the Qt platform plugin "xcb" in ".../python3.7/site-packages/cv2/qt/plugins" even though it was found.). I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container. (The python codes work fine in windows)

                                    Am I missing some Ubuntu libraries? I tried installing libxcb's and many other libraries. It just does not work. Or is it related to PyQt5?

                                    Pablo J. RoginaP Offline
                                    Pablo J. RoginaP Offline
                                    Pablo J. Rogina
                                    wrote on last edited by Pablo J. Rogina
                                    #16

                                    @yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                                    qt.qpa.xcb: could not connect to display

                                    Are you sure you have an X server running and available?

                                    I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container.

                                    Please be aware that you have a (very) unusual environment.

                                    Given that Docker is involved you may need to check how to make the container output work properly under X...

                                    Upvote the answer(s) that helped you solve the issue
                                    Use "Topic Tools" button to mark your post as Solved
                                    Add screenshots via postimage.org
                                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                    Y 1 Reply Last reply
                                    0
                                    • Pablo J. RoginaP Pablo J. Rogina

                                      @yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                                      qt.qpa.xcb: could not connect to display

                                      Are you sure you have an X server running and available?

                                      I am using docker (with Ubuntu 18 + WSL2). The problem persist only in docker container.

                                      Please be aware that you have a (very) unusual environment.

                                      Given that Docker is involved you may need to check how to make the container output work properly under X...

                                      Y Offline
                                      Y Offline
                                      yashwant
                                      wrote on last edited by
                                      #17

                                      @Pablo-J-Rogina said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                                      rly under

                                      1. It is just standard docker windows setup (It just uses the WSL2). It is the standard installation process.
                                      2. How can I check if the Xserver is running?

                                      Thanks !

                                      1 Reply Last reply
                                      0
                                      • Y Offline
                                        Y Offline
                                        yashwant
                                        wrote on last edited by
                                        #18

                                        I am just trying to open a python GUI(which opens fine in windows 10) using docker setup. I did install docker for windows using the standard procedure. And I can load the python codes (without .ui - python files without GUI) into the container without any problem. The problem arises only when I try to load python files with GUI.

                                        Pablo J. RoginaP 1 Reply Last reply
                                        0
                                        • Y yashwant

                                          I am just trying to open a python GUI(which opens fine in windows 10) using docker setup. I did install docker for windows using the standard procedure. And I can load the python codes (without .ui - python files without GUI) into the container without any problem. The problem arises only when I try to load python files with GUI.

                                          Pablo J. RoginaP Offline
                                          Pablo J. RoginaP Offline
                                          Pablo J. Rogina
                                          wrote on last edited by
                                          #19

                                          @yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":

                                          The problem arises only when I try to load python files with GUI.

                                          That's the point. You may need to check how to make a Docker container use the GUI of the host...

                                          Upvote the answer(s) that helped you solve the issue
                                          Use "Topic Tools" button to mark your post as Solved
                                          Add screenshots via postimage.org
                                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                          Y 1 Reply Last reply
                                          0
                                          • A Offline
                                            A Offline
                                            alphaomega
                                            wrote on last edited by alphaomega
                                            #20

                                            Replacing opencv-python with opencv-python-headless is one option. However, for some reasons if you want to keep the library, then you could add following lines after import cv2:

                                            import os, sys
                                            ci_build_and_not_headless = False
                                            try:
                                                from cv2.version import ci_build, headless
                                                ci_and_not_headless = ci_build and not headless
                                            except:
                                                pass
                                            if sys.platform.startswith("linux") and ci_and_not_headless:
                                                os.environ.pop("QT_QPA_PLATFORM_PLUGIN_PATH")
                                            if sys.platform.startswith("linux") and ci_and_not_headless:
                                                os.environ.pop("QT_QPA_FONTDIR")
                                            

                                            This is based on the cv2's init file . What it does is essentially removing environment variables that are added by the init file (This are only done at runtime and does not have permanent effect upon your system.)

                                            1 Reply Last reply
                                            1

                                            • Login

                                            • Login or register to search.
                                            • First post
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved