动态刷新图像

时间:2018-10-04 16:19:48

标签: python pyqt file-watcher

我要在文件夹中的任何这些文件更新后立即动态刷新这7张图像(messigray_ {})标签。我检查了一下,看起来必须使用一些filewatcher来完成此操作,但是我不确定如何执行此操作。任何帮助表示赞赏。

这是我的代码

import cv2,os
import pafy
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from VideoFaceDetectiondup import vidcaptureface
from PyQt5 import QtCore, QtGui, QtWidgets #works for pyqt5
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

class MainWindow(QWidget):
    def __init__(self, camera_index=0, fps=30):
        super().__init__()
        url='https://www.youtube.com/watch?v=4GulZ_6xdXg&t=1s'

        # vPafy = pafy.new(url)
        # play = vPafy.getbest(preftype="webm")

        self.capture = cv2.VideoCapture(0)
        self.dimensions = self.capture.read()[1].shape[1::-1]

        scene = QGraphicsScene(self)
        pixmap = QPixmap(*self.dimensions)
        self.pixmapItem = scene.addPixmap(pixmap)

        view = QGraphicsView(self)
        view.setScene(scene)

        timer = QTimer(self)
        timer.setInterval(int(1000/fps))
        timer.timeout.connect(self.get_frame)
        timer.start()

        # text = QLabel('facecam 1.0', self)
        # for _ in len(os.listdir(os.getcwd())):
        #     label
        label = QLabel()
        pixmap = QPixmap("static/facesimages/messigray_0.png")
        label.setPixmap(pixmap)

        label_2 = QLabel()
        pixmap_2 = QPixmap("static/facesimages/messigray_1.png")
        label_2.setPixmap(pixmap_2)

        label_3 = QLabel()
        pixmap_3 = QPixmap("static/facesimages/messigray_2.png")
        label_3.setPixmap(pixmap_3)

        label_4 = QLabel()
        pixmap_4 = QPixmap("static/facesimages/messigray_3.png")
        label_4.setPixmap(pixmap_4)

        label_5 = QLabel()
        pixmap_5 = QPixmap("static/facesimages/messigray_4.png")
        label_5.setPixmap(pixmap_5)

        label_6 = QLabel()
        pixmap_6 = QPixmap("static/facesimages/messigray_5.png")
        label_6.setPixmap(pixmap_6)

        label_7 = QLabel()
        pixmap_7 = QPixmap("static/facesimages/messigray_6.png")
        label_7.setPixmap(pixmap_7)

        label_7 = QLabel()
        pixmap_7 = QPixmap("static/facesimages/messigray_7.png")
        label_7.setPixmap(pixmap_7)


        layout = QVBoxLayout(self)        
        layout.addWidget(view)        

        layoutlab = QHBoxLayout()
        layout.addLayout(layoutlab)
        layoutlab.addWidget(label)
        layoutlab.addWidget(label_2)
        layoutlab.addWidget(label_3)
        layoutlab.addWidget(label_4)
        layoutlab.addWidget(label_5)
        layoutlab.addWidget(label_6)
        layoutlab.addWidget(label_7)

        # layout.addWidget(text)




    def get_frame(self):
        _, frame = self.capture.read()
        frame=vidcaptureface(frame)
        image = QImage(frame, *self.dimensions, QImage.Format_RGB888).rgbSwapped()
        pixmap = QPixmap.fromImage(image)
        self.pixmapItem.setPixmap(pixmap)





app = QApplication([])
win = MainWindow()
win.show()
app.exec()

0 个答案:

没有答案