Reputation: 41
i'm getting the following error message, when trying to call a pyplot.show.
> QObject::moveToThread: Current thread (0x55cb11264ad0) is not the object's thread (0x55cb1226c700).
> Cannot move to target thread (0x55cb11264ad0)
> qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/usr/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.
> Aborted (core dumped)
Looking for answers I though it was the same problem as https://www.reddit.com/r/archlinux/comments/d6h3kl/python_qt_cant_import_matplotlibpyplot/ but i just reinstalled a lot of packages(And almost broke the entire system) and Im getting the same error.
And also, matplotlib.pyplot.show() and opencv.imshow() works fine when they are alone, but when i am doing some procesing with opencv and i want to show in pyplot.show() is where the error comes out.
I don't know how to debug Qt as the same as u/tim-hilt done in his post so this is all information that i can give for the moment. I figured out how to do it.
Thanks in advance for any response that i can get.
Upvotes: 4
Views: 1554
Reputation: 185
This issue happens due to a conflict between GUI backend dependencies of opencv-python and matplotlib. If you are not using cv2.imshow(), then you can simply remove opencv-python and install rather opencv-python-headless which installs the package without any GUI dependency and this removes the conflict with matplotlib. That solved my problem. Just run the following on the terminal:
pip uninstall opencv-python
pip uninstall matplotlib
pip uninstall PyQt5
pip install matplotlib
pip install PyQt5
pip install opencv-python-headless
If you are using a conda environment, I'd suggest to scrap it. Create instead a new virtualenv environment and you can put it at the same location as the conda created ones maybe. Conda created environments create havoc with GUI backends. And yes, use an older version of Python while creating the new environment. For me Python 3.8 worked while the latest, i.e., Python 3.11 didn't.
This issue is also described here.
Upvotes: 1
Reputation: 19
You can import matplotlib and make it use tkagg:
import numpy as np
import matplotlib
matplotlib.use("tkagg")
import matplotlib.pyplot as plt
import cv2
Worked at least at my case.
Upvotes: 1