问题是获取图像的功能仅执行一次,而不更新标签。
正确的方法是将其放置在循环中,但会导致阻塞主窗口。主窗口的这种阻塞可以通过使用
QThread类来解决,并通过信号发送
QImage来更新标签。例如:
import cv2import sysfrom PyQt5.QtWidgets import QWidget, QLabel, QApplicationfrom PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlotfrom PyQt5.QtGui import QImage, QPixmapclass Thread(QThread): changePixmap = pyqtSignal(QImage) def run(self): cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if ret: # https://stackoverflow.com/a/55468544/6622587 rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = rgbImage.shape bytesPerLine = ch * w convertToQtFormat = QImage(rgbImage.data, w, h, bytesPerLine, QImage.Format_RGB888) p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio) self.changePixmap.emit(p)class App(QWidget): def __init__(self): super().__init__() [...] self.initUI() @pyqtSlot(QImage) def setImage(self, image): self.label.setPixmap(QPixmap.fromImage(image)) def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.resize(1800, 1200) # create a label self.label = QLabel(self) self.label.move(280, 120) self.label.resize(640, 480) th = Thread(self) th.changePixmap.connect(self.setImage) th.start() self.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)