PyQt5 - Playing image sequence in QGraphicsView (M

2019-02-19 08:11发布

问题:

I'm trying to process an image sequence and make a video of the results using OpenCV and PyQt5. I've got some code that loops through a directory, reads in the images, and tries to display them on a QGraphicsView.

def on_start(self):
    for f in self.image_list:
        img = cv2.imread(f)
        img = cv2qimage(img, False)
        self.scene.set_qimage(img)

self.scene inherits from QGraphicsScene.

  def set_qimage(self, qimage):
        self.pixmap = QPixmap.fromImage(qimage)
        self.addPixmap(self.pixmap)

The problem is everytime I call addPixmap() the image is just added on top of all the other images and soon I run out of memory and everything crashes. The current code doesn't include any of the processing steps, it just converts the numpy ndarry to a QImage and adds the QPixmap to the scene.

What is the proper way to update the QGraphicsScene so that I can stream a sequence of images?

回答1:

Every time you use addPixmap() you are creating a new QGraphicsPixmapItem adding memory unnecessarily. The solution is to create a QGraphicsPixmapItem and reuse it. In addition the processing task can block the main thread so you must use a thread to do the heavy task and send the QImage through signals.

class ProcessWorker(QObject):
    imageChanged = pyqtSignal(QImage)

    def doWork(self):
        for f in self.image_list:
            img = cv2.imread(f)
            img = cv2qimage(img, False)
            self.imageChanged.emit(img)
            QThread.msleep(1)

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        lay = QVBoxLayout(self)
        gv = QGraphicsView()
        lay.addWidget(gv)
        scene = QGraphicsScene(self)
        gv.setScene(scene)
        self.pixmap_item = QGraphicsPixmapItem()
        scene.addItem(self.pixmap_item)

        self.workerThread = QThread()
        self.worker = ProcessWorker()
        self.worker.moveToThread(self.workerThread)
        self.workerThread.finished.connect(self.worker.deleteLater)
        self.workerThread.started.connect(self.worker.doWork)
        self.worker.imageChanged.connect(self.setImage)
        self.workerThread.start()


    @pyqtSlot(QImage)
    def setImage(self, image):
        pixmap = QPixmap.fromImage(image)
        self.pixmap_item.setPixmap(pixmap)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())