Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any plan to solve conflicts betwen opencv-python and PyQt5 #736

Open
hitbuyi opened this issue Oct 12, 2022 · 4 comments
Open

Any plan to solve conflicts betwen opencv-python and PyQt5 #736

hitbuyi opened this issue Oct 12, 2022 · 4 comments
Labels

Comments

@hitbuyi
Copy link

hitbuyi commented Oct 12, 2022

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?

@asmorkalov
Copy link
Collaborator

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.

@bstivers
Copy link

bstivers commented Dec 3, 2022

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.

@porterpan
Copy link

porterpan commented Jun 7, 2023

@bstivers @asmorkalov, Here are some methods,you can try it

trouble

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

identify

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
image,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
image

besides, i find opencv-python source have other code snap, ARG QT_VERSION=5.15.0
image

how fix it

  1. install opencv-python==4.xx and install PyQt5==5.15.0, eg:
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 script

import 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
  1. or install with source code , Modify the qt version in the source code, then install ......manual build opencv-python

@MusYi
Copy link

MusYi commented Jan 22, 2024

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.

image

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.

Here is my test environment and test code

System environment

$ 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

Test code

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_())

Running result

Uncomment the code and run the pure opencv2 code cv2.py
image

Comment the code in cv2/config-3.py and run the opencv2 with pyqt5 code qt-cv2.py:
image

kxing28 pushed a commit to Evanyl/screw-sorter-sw that referenced this issue Mar 25, 2024
hardcode data_collection_backend.py to use the correct xcb file

Issue arises from pyqt5 finding xcb dependency within the cv2 folder
instead of its own pyqt5 folder.

My old bandaid fix involved renaming
/home/screwsorter/.local/lib/python3.9/site-packages/cv2/qt/plugins/platforms
to platforms_old

But that prevented usage of cv.imshow(). This new fix means we can use
both cv2.imshow() and our pyqt5 application GUI.

https://stackoverflow.com/questions/68417682/qt-and-opencv-app-not-working-in-virtual-environment
opencv/opencv-python#736
kxing28 pushed a commit to Evanyl/screw-sorter-sw that referenced this issue Mar 25, 2024
hardcode data_collection_backend.py to use the correct xcb file

Issue arises from pyqt5 finding xcb dependency within the cv2 folder
instead of its own pyqt5 folder.

My old bandaid fix involved renaming
/home/screwsorter/.local/lib/python3.9/site-packages/cv2/qt/plugins/platforms
to platforms_old

But that prevented usage of cv.imshow(). This new fix means we can use
both cv2.imshow() and our pyqt5 application GUI.

https://stackoverflow.com/questions/68417682/qt-and-opencv-app-not-working-in-virtual-environment
opencv/opencv-python#736
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants