Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found"
-
@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. -
@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 ?
-
@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...
-
@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
- It is just standard docker windows setup (It just uses the WSL2). It is the standard installation process.
- How can I check if the Xserver is running?
Thanks !
-
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.
-
@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...
-
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.)
-
@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":
UI of the host..
Yes that was my goal (docker with gui)! and above mentioned was my error when I try GUI on docker.! Anyway thanks!
-
@yashwant said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
that was my goal (docker with gui)!
Yes, I understand that. Please take into account that this issue is not related (or produced by) Qt itself so the solution might come from some other source(s), not only this forum
-
@jeremy_k said in Using PyQt5 with opencv-python (cv2) causes error "could not load Qt platform plugin xcb even though it was found":
worka
Hello guys, so the problem is that
import cv2
setsQT_*
env vars.
I came to this workaround, maybe it will help someone:import cv2 for k, v in os.environ.items(): if k.startswith("QT_") and "cv2" in v: del os.environ[k]
-
@Hikari-Tretaki, muito obrigado por sua ajuda. Também consegui resolver o meu problema com sua dica.
-
@Hikari-Tretaki Thank you so much, installing opencv-python-headless fixed the issue
-
@Hikari-Tretaki MAN, I LOVE YOU SO MUCH. THANKS!
-
I also have this problem, my environments are
1, OS: ubuntu20.04
2, conda: 4.13
3, PyQt5: 5.15.2
4, local Qt5: 5.15.6
5, opencv- python 4.5.xIt really takes several days to find a solution, @jeremy_k gave almost the solution. I use PyQt5 + OpenCV-python on conda virtual environment,
summarize the solutions :1, delete cv2/libqxcb.so, it works for pyqt5 which has its own version of Qt, but opencn-python doesn't work for other application which isn't relevant to PyQt5
2, if pyside2 is installed, pyside2 has its owen version of Qt, so add below code at the beginning of python program
``` import PySide2 dirname = os.path.dirname(PySide2.__file__) plugin_path = os.path.join(dirname, 'plugins', 'platforms') os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path ```
this solution works, at expense of intalling pyside2 which is 400M+
3, PyQt5 has its own version of Qt,similiar to above, add below code at the beginning of python program
``` import PyQt5 dirname = os.path.dirname(PyQt5.__file__) plugin_path = os.path.join(dirname, 'Qt5','plugins', 'platforms') os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
this soltuion is fine,