Qt and opencv app not working in virtual environment
Question:
I created a GUI app using pyqt5 and opencv. The app works fine without activating the virtual env but when I activate the virtual env and run the app it shows this error:
QObject::moveToThread: Current thread (0x125b2f0) is not the object's thread (0x189e780).
Cannot move to target thread (0x125b2f0)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/deepak/Desktop/SampleApp/lib/python3.9/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.
Aborted
I tried running an example pyqt5 code (without importing opencv) and another code (only using opencv) both worked fine in the virtual env.
Operating System: Parrot OS 4.11
Python Version: 3.9.2
Answers:
The problem is that the version of Qt with which opencv was compiled is not similar to the one used by PyQt5 causing a conflict.
A possible solution is to indicate to use the Qt plugins used by PyQt5.
import os
from pathlib import Path
import PyQt5
from PyQt5.QtWidgets import QWidget # others imports
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PyQt5.__file__).resolve().parent / "Qt5" / "plugins"
)
# ...
For PySide2:
import os
from pathlib import Path
import PySide2
from PySide2.QtWidgets import QWidget # others imports
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PySide2.__file__).resolve().parent / "Qt" / "plugins"
)
# ...
Update:
A better option is to use QLibraryInfo to get the plugins folder path:
import os
from PyQt5.QtCore import QLibraryInfo
# from PySide2.QtCore import QLibraryInfo
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(
QLibraryInfo.PluginsPath
)
I solved the issue by downgrading the cv2 version, simply run
python3 -m pip install opencv-contrib-python==4.1.0.25
I used to have the 4.5.3.56
A simple solution would be to remove all the openCV you have in your environment
pip uninstall opencv-python opencv-python-headless opencv-contrib-python
then just install
pip install opencv-python-headless
I found a way to solve this problem, by pip install opencv-python-headless instead of opencv-python. but with opencv-python-headless, it cause opencv problem when use cv2.imshow or cv2.namedWindow to try to open a image window.
I got this error
Traceback (most recent call last):
File "", line 1, in
cv2.error: OpenCV(4.5.4) /tmp/pip-req-build-4x5kub8r/opencv/modules/highgui/src/window.cpp:1257: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvNamedWindow’
I created a GUI app using pyqt5 and opencv. The app works fine without activating the virtual env but when I activate the virtual env and run the app it shows this error:
QObject::moveToThread: Current thread (0x125b2f0) is not the object's thread (0x189e780).
Cannot move to target thread (0x125b2f0)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/deepak/Desktop/SampleApp/lib/python3.9/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.
Aborted
I tried running an example pyqt5 code (without importing opencv) and another code (only using opencv) both worked fine in the virtual env.
Operating System: Parrot OS 4.11
Python Version: 3.9.2
The problem is that the version of Qt with which opencv was compiled is not similar to the one used by PyQt5 causing a conflict.
A possible solution is to indicate to use the Qt plugins used by PyQt5.
import os
from pathlib import Path
import PyQt5
from PyQt5.QtWidgets import QWidget # others imports
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PyQt5.__file__).resolve().parent / "Qt5" / "plugins"
)
# ...
For PySide2:
import os
from pathlib import Path
import PySide2
from PySide2.QtWidgets import QWidget # others imports
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PySide2.__file__).resolve().parent / "Qt" / "plugins"
)
# ...
Update:
A better option is to use QLibraryInfo to get the plugins folder path:
import os
from PyQt5.QtCore import QLibraryInfo
# from PySide2.QtCore import QLibraryInfo
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(
QLibraryInfo.PluginsPath
)
I solved the issue by downgrading the cv2 version, simply run
python3 -m pip install opencv-contrib-python==4.1.0.25
I used to have the 4.5.3.56
A simple solution would be to remove all the openCV you have in your environment
pip uninstall opencv-python opencv-python-headless opencv-contrib-python
then just install
pip install opencv-python-headless
I found a way to solve this problem, by pip install opencv-python-headless instead of opencv-python. but with opencv-python-headless, it cause opencv problem when use cv2.imshow or cv2.namedWindow to try to open a image window.
I got this error
Traceback (most recent call last):
File "", line 1, in
cv2.error: OpenCV(4.5.4) /tmp/pip-req-build-4x5kub8r/opencv/modules/highgui/src/window.cpp:1257: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvNamedWindow’