python opencv视频流_python – PyQt显示来自opencv的视频流

尝试链接PyQt和Opencv视频源,无法理解如何应用while循环连续流式传输视频.它只需要一张照片.请任何人都可以帮助解决问题.

> PtQt = 5

> Python = 3.6.1

class App(QWidget):

def __init__(self):

super().__init__()

self.title = 'PyQt5 Video'

self.left = 100

self.top = 100

self.width = 640

self.height = 480

self.initUI()

def initUI(self):

self.setWindowTitle(self.title)

self.setGeometry(self.left, self.top, self.width, self.height)

self.resize(1800, 1200)

#create a label

label = QLabel(self)

cap = cv2.VideoCapture(0)

ret, frame = cap.read()

rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0],

QtGui.QImage.Format_RGB888)

convertToQtFormat = QtGui.QPixmap.fromImage(convertToQtFormat)

pixmap = QPixmap(convertToQtFormat)

resizeImage = pixmap.scaled(640, 480, QtCore.Qt.KeepAspectRatio)

QApplication.processEvents()

label.setPixmap(resizeImage)

self.show()

if __name__ == '__main__':

app = QApplication(sys.argv)

ex = App()

sys.exit(app.exec_())

最佳答案

问题是获取图像的功能只执行一次而不更新标签.

正确的方法是将它放在循环中,但它会导致阻塞主窗口.可以通过使用QThread类来解决主窗口的阻塞,并通过信号QImage发送以更新标签.例如:

class 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 = QtGui.QImage(rgbImage.data, w, h, bytesPerLine, QtGui.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()

点击查看更多相关文章

转载注明原文:python – PyQt显示来自opencv的视频流 - 乐贴网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值