简体   繁体   English

带有pyqt4对话框的opencv显示

[英]opencv with pyqt4 dialog display

I'm trying to embed video capture with my dialog box (created in pyqt4). 我正在尝试在对话框(在pyqt4中创建)中嵌入视频捕获。 For the same I tired the code below. 同样,我厌倦了下面的代码。 But it just starts the capturing and does not display anything on the dialog. 但是它只是开始捕获而在对话框上不显示任何内容。 Please help me know what's missing in the following code. 请帮助我了解以下代码中缺少的内容。 Here, self.videoFrame is a QLabel under QtGui. 在这里,self.videoFrame是QtGui下的QLabel。

def onRun(self):
        self.playing = True
        capture = cv2.VideoCapture(0)
        data1=np.array([])

        while self.playing:

            _, data = capture.read()
            data1 = cv2.cvtColor(data, cv2.cv.CV_BGR2RGB)
            qImage = QtGui.QImage(data1, data1.shape[2], data1.shape[2], 
                QtGui.QImage.Format_RGB888)
            qImage=QtGui.QPixmap.fromImage(qImage)
            self.videoFrame.setPixmap(
            qImage)
            self.videoFrame.setScaledContents(True)            
            QtGui.qApp.processEvents()
            cv2.waitKey(5)

        cv2.destroyAllWindows() 

This works: 这有效:

from PyQt4 import QtCore,QtGui
import sys
import cv2
import numpy as np

class ImageWidget(QtGui.QWidget):
    def __init__(self,parent=None):
        super(ImageWidget,self).__init__(parent)
        self.image=None

    def setImage(self,image):
        self.image=image
        sz=image.size()
        self.setMinimumSize(sz)
        self.update()

    def paintEvent(self,event):
        qp=QtGui.QPainter()
        qp.begin(self)
        if self.image:
            qp.drawImage(QtCore.QPoint(0,0),self.image)
        qp.end()

class MainWindow(QtGui.QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        self.videoFrame=ImageWidget()
        self.setCentralWidget(self.videoFrame)
        self.timer=QtCore.QTimer(self)
        self.timer.timeout.connect(self.updateImage)
        self.timer.start(30)
        self.capture = cv2.VideoCapture(0)

    def updateImage(self):
        _, img = self.capture.read()
        #img=cv2.cvtColor(img, cv.CV_BGR2RGB)
        height, width, bpc = img.shape
        bpl = bpc * width
        image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
        self.videoFrame.setImage(image)


def main():
    app=QtGui.QApplication(sys.argv)
    w=MainWindow()
    w.show()
    app.exec_()

if __name__=='__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM