Open hitbuyi opened 2 years ago
OpenCV uses QT as imshow backend on some platforms. QT is linked to C++ code and distributed with OpenCV. There is no simple solution to share the same QT object between PyQT and OpenCV. I cannot recommend you something scalable. In case if you manage full setup, you can try to rebuild OpenCV with other backend like GTK on Linux.
Ran into this issue using tensorflow, mediapipe, and opencv (in notebooks). Very very annoying. Can't use new versions of opencv, and monitor detections at the same time. Even if I am not utilizing matplotlib. Well, I can, but my notebooks get spammed with the QObject::moveToThread:
notices.
@bstivers @asmorkalov, Here are some methods,you can try it
error as follow
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in ".../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
the clues indicate you should install PyQt5 lib version corresponding with opencv-python lib, the source of opencv-python code line of 12: 4.x/patches/patchQtPlugins ,as pic show, i think the blow will work
pip install opencv-python==4.xx
pip install PyQt5==5.15.0
sure, my pyqt + cv2 project work well, enjoy
besides, i find opencv-python source have other code snap, ARG QT_VERSION=5.15.0
pip install opencv-python==4.5.1.48
pip install PyQt5==5.15.0
Uninstall opencv-python and PyQt5 before installing,
pip lis| grep cv , pip list | grep PyQt5
, then pip uninstall xxx_the_list_of_cv_and_PyQt5 If you report another error,you can try add this code in you scriptimport os envpath = '/home/udi/anaconda3/envs/qt_env/lib/python3.9/site-packages/cv2/qt/plugins/platforms' os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = envpath
I found that commenting out the code of lines 15~18 in cv2/config-3.py can avoid the error when running use cv2 and pyqt5 together. But this will make error when only use opencv. Hope this helps.
Comment this will avoid the error below when cv2 and pyqt5 together:
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/ly/apps/py38_qt/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.
But, this method will make another error when only ues cv2:
qt.qpa.plugin: Could not find the Qt platform plugin "xcb" in "" This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
$ uname -a
> Linux Ubuntu 5.15.0-91-generic #101~20.04.1-Ubuntu SMP Thu Nov 16 14:22:28 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ python --version
> Python 3.8.10
$ pip list
> Package Version
------------------- ----------
click 8.1.7
contourpy 1.1.1
cycler 0.12.1
fonttools 4.47.2
importlib-resources 6.1.1
kiwisolver 1.4.5
matplotlib 3.7.4
numpy 1.24.4
opencv-python 4.9.0.80
packaging 23.2
pillow 10.2.0
pip 23.3.2
pkg_resources 0.0.0
pyparsing 3.1.1
PyQt5 5.15.9
pyqt5-plugins 5.15.9.2.3
PyQt5-Qt5 5.15.2
PyQt5-sip 12.13.0
pyqt5-tools 5.15.9.3.3
python-dateutil 2.8.2
python-dotenv 1.0.0
qt5-applications 5.15.2.2.3
qt5-tools 5.15.2.1.3
setuptools 44.0.0
sip 6.8.1
six 1.16.0
tomli 2.0.1
zipp 3.17.0
Pure opencv2, cv2.py
import cv2
cap = cv2.VideoCapture(0)
if cap.isOpened()!=True:
cap.open()
while True:
ret, frame = cap.read()
if ret==False or cv2.waitKey(1)==ord('q'):
break
cv2.imshow('frame', frame)
cap.realease()
cv2.destroyAllWindows()
opencv2 with pyqt5, qt-cv2.py
:
import sys
import cv2
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class win(QMainWindow):
def __init__(self, parent=None):
super().__init__()
self.setGeometry(250, 80, 800, 600)
self.setWindowTitle('camera')
self.cap = cv2.VideoCapture(0)
self._timer = QTimer(self)
self._timer.timeout.connect(self.refresh)
self._timer.start(27)
self.videoFrame = QLabel('VideoCapture')
self.setCentralWidget(self.videoFrame)
def refresh(self):
try:
ret, img = self.cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width = img.shape[:2]
img = QPixmap.fromImage(QImage(img, width, height, QImage.Format_RGB888))
self.videoFrame.setPixmap(img)
except TypeError:
print('No Frame')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = win()
win.show()
sys.exit(app.exec_())
Uncomment the code and run the pure opencv2 code cv2.py
Comment the code in cv2/config-3.py and run the opencv2 with pyqt5 code qt-cv2.py
:
I used pycharm to connect to a remote server and wanted to visualize it locally
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/usr/local/lib/python3.8/dist-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.
I ran into this issue. I'm commenting here because unlike the others, this one is open.
I'm actually fine with the incompatibility, I think the instructions on how to fix it are clear.
What I really would like to see, is a detection of what's going on and a proper, pythonic error when I'm doing stuff with python.
What I'm seeing right now is no python error trace, no line number, etc. I'm just getting:
QObject::moveToThread: Current thread (0x25b0b60) is not the object's thread (0x3c70250).
Cannot move to target thread (0x25b0b60)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/max/.local/lib/python3.12/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.
Meaning, I had to manually search for the line and function that causes this.
Many people found the Opencv-python of version > 4.4.x conflicts with PyQt5,such as #386, #404 and #427 ,the solutions of this problem are
1, choose low version of opencv-python( I choose opencv-python 4.1.2.30 to solve this problem) 2, choose opencv-python-headless
However, if I have to use opencv-python 4.6.x, and must include image display UI(that means opencv-python-headless isn't proper for the project), is there any elegant solution?