PyQt5/PyQt

failed to start because no Qt platform plugin could be initialized

HasarinduPerera opened this issue · 4 comments

Environment : / 环境

  • OS: Ubuntu 20.04.6 LTS
  • Python 3.8 X64
  • PyQt5 [e.g. 5.10.1]
import cv2
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt, QTimer
from PyQt5 import QtCore

class VideoWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowState(Qt.WindowFullScreen)  # Set fullscreen mode

        # Create a label to display the video stream
        self.label = QLabel(self)
        self.setCentralWidget(self.label)

        # Initialize the video capture
        self.cap = cv2.VideoCapture(0)
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 187)

        # Start updating the frame
        self.update_frame()

    def resizeEvent(self, event):
        # Adjust the label size to fit the window
        self.label.setGeometry(0, 0, event.size().width(), event.size().height())

    def update_frame(self):
        # Get the frame from the video capture
        ret, frame = self.cap.read()

        if ret:
            # Convert the OpenCV BGR frame to RGB
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            # Rotate the frame by 90 degrees
            frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)

            # Resize the frame to fit the window size
            resized_frame = cv2.resize(frame, (self.label.width(), self.label.height()))

            # Convert the resized frame to QImage format
            image = QImage(resized_frame.data, resized_frame.shape[1], resized_frame.shape[0], QImage.Format_RGB888)

            # Convert QImage to QPixmap
            pixmap = QPixmap.fromImage(image)

            # Update the label with the new pixmap
            self.label.setPixmap(pixmap)

        # Schedule the next frame update
        QtCore.QTimer.singleShot(1, self.update_frame)

if __name__ == "__main__":
    # Create the QApplication
    app = QApplication([])

    # Create the VideoWindow
    window = VideoWindow()
    window.showFullScreen()

    # Start the QApplication event loop
    app.exec_()

    # Release the video capture
    window.cap.release()


I am trying to run this code but it gives an error, I tried reinstalling, exporting plugin variable, but nothing works.

QObject::moveToThread: Current thread (0x1100d810) is not the object's thread (0x11517c30).
Cannot move to target thread (0x1100d810)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/user/.local/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.

Aborted

TIA

export QT_DEBUG_PLUGINS=1
before run python script. maybe see some missing dependency libraries about xcb

This was the result. Any suggestions? (The result was very long, this is the only part regarding xcb and also the last part)

TIA

QFactoryLoader::QFactoryLoader() looking at "/usr/lib/aarch64-linux-gnu/qt5/plugins/platforms/libqxcb.so"
Found metadata in lib /usr/lib/aarch64-linux-gnu/qt5/plugins/platforms/libqxcb.so, metadata=
{
    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": {
        "Keys": [
            "xcb"
        ]
    },
    "archreq": 0,
    "className": "QXcbIntegrationPlugin",
    "debug": false,
    "version": 331520
}


Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/platforms" ...
loaded library "/home/user/.local/lib/python3.9/site-packages/cv2/qt/plugins/platforms/libqxcb.so"
QObject::moveToThread: Current thread (0x3a4f250) is not the object's thread (0x3f5dbe0).
Cannot move to target thread (0x3a4f250)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/user/.local/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.

Aborted

maybe different version Qtxcb problem

loaded library "/home/user/.local/lib/python3.9/site-packages/cv2/qt/plugins/platforms/libqxcb.so"

/usr/lib/aarch64-linux-gnu/qt5/plugins/platforms/libqxcb.so, metadata=

image
Normally, PyQt5 will use the plugin library in its own directory.

check your environment variable in your python scripts at first.

import os
print(os.environ.get('PATH'))
print(os.environ.get('LD_LIBRARY_PATH'))