Issue
I'm trying to embed video capture with my dialog box (created in 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.
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()
Solution
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()
Answered By - Photon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.