pycapture2视频捕获和线程化

时间:2018-10-30 02:42:44

标签: python python-3.x opencv pyqt pyqt5

我使用pycapture2和PyQt5制作了一个视频捕获程序。 看来可行,但控制台显示错误:

QObject::killTimer: Timers cannot be stopped from another thread

QBasicTimer::stop: Failed. Possibly trying to stop from a different thread

我使用QTimer解决了这个问题,但是速度很慢(每秒2帧)。

我要使用QThread,如何解决此问题?

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread
import cv2
import PyCapture2
import numpy as np

class Ui_MainWindow(object):
    def __init__(self, parent=None):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(600, 400)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(150, 20, 400, 300))
        self.graphicsView.setObjectName("graphicsView")

        MainWindow.setCentralWidget(self.centralwidget)

        self.Thread = CapThread()
        self.Thread.start()     

class CapThread(QThread):
    def __init__(self):
        QThread.__init__(self)

    def run(QMainWindow):
        bus = PyCapture2.BusManager()
        uid = bus.getCameraFromIndex(0)

        c = PyCapture2.Camera()
        c.connect(uid)         

        while True:
            c.startCapture()
            img = c.retrieveBuffer()
            c.stopCapture()

            cv_img1 = np.array(img.getData(), dtype="uint8").reshape((img.getRows(), img.getCols()));
            cv_img = cv2.cvtColor(cv_img1, cv2.COLOR_BAYER_BG2BGR)
            cv_img = cv2.resize(cv_img,(380,270))

            height, width, dim = cv_img.shape
            bytesPerLine = dim * width
            image = QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
            item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap.fromImage(image))
            scene = QtWidgets.QGraphicsScene()
            scene.addItem(item)
            ui.graphicsView.setScene(scene)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

您的代码没有逻辑,例如,因为run()具有QMainWinodw作为参数,理论上您每次获取图像时都会尝试创建一个场景,这是不正确的,因为程序将消耗不必要的存储,解决方案是重用该项。最后但并非最不重要的一点是,GUI无法在另一个线程中更新,Qt指出必须通过信号将其发送到GUI线程,然后在那里进行更新:

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import cv2
import PyCapture2
import numpy as np


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(600, 400)
        scene = QtWidgets.QGraphicsScene()
        graphicsView = QtWidgets.QGraphicsView(scene)
        self.setCentralWidget(graphicsView)
        self._item = QtWidgets.QGraphicsPixmapItem()
        scene.addItem(self._item)
        thread = CapThread(self)
        thread.imageChanged.connect(self.on_imageChanged)
        thread.start()     

    @QtCore.pyqtSlot(QtGui.QImage)
    def on_imageChanged(self, image):
        pixmap = QtGui.QPixmap.fromImage(image)
        self._item.setPixmap(pixmap)


class CapThread(QtCore.QThread):
    imageChanged = QtCore.pyqtSignal(QtGui.QImage)

    def run(self):
        bus = PyCapture2.BusManager()
        uid = bus.getCameraFromIndex(0)
        c = PyCapture2.Camera()
        c.connect(uid)        

        while True:
            c.startCapture()
            img = c.retrieveBuffer()
            c.stopCapture()

            cv_img1 = np.array(img.getData(), dtype="uint8").reshape((img.getRows(), img.getCols()))
            cv_img = cv2.cvtColor(cv_img1, cv2.COLOR_BAYER_BG2BGR)
            cv_img = cv2.resize(cv_img, (380,270))

            height, width, dim = cv_img.shape
            bytesPerLine = dim * width
            image = QtGui.QImage(cv_img.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
            self.imageChanged.emit(image)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())