Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtWidgets import QApplication, QWidget,\
- QLabel, QMainWindow, QGridLayout, QPushButton
- from PyQt5.QtMultimedia import QAbstractVideoSurface, QAbstractVideoBuffer, QVideoFrame, QCamera
- from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
- from PyQt5.QtGui import QImage, QPixmap
- class frameGrabber(QAbstractVideoSurface):
- draw_image = pyqtSignal(QImage)
- process_image = pyqtSignal(QImage)
- def __init__(self, parent=None):
- super().__init__(parent)
- def supportedPixelFormats(self,handleType=QAbstractVideoBuffer.NoHandle):
- formats = [QVideoFrame.PixelFormat()]
- if (handleType == QAbstractVideoBuffer.NoHandle):
- formats.append(QVideoFrame.Format_RGB24)
- return formats
- def present(self, frame:QVideoFrame)->bool:
- if (self.surfaceFormat().pixelFormat() != frame.pixelFormat() or
- self.surfaceFormat().frameSize() != frame.size()):
- self.setError(QAbstractVideoSurface.IncorrectFormatError)
- self.stop()
- return False
- self.currentFrame = frame
- img = QImage(self.currentFrame.bits(),
- self.currentFrame.width(), self.currentFrame.height(), self.currentFrame.bytesPerLine(),
- QImage.Format_RGB888)
- self.draw_image.emit(img)
- return True
- class mainwin(QMainWindow):
- def __init__(self, parent=None):
- super(mainwin, self).__init__(parent)
- self.initUI()
- self._grabber = frameGrabber(self)
- self._grabber.draw_image.connect(self.drawImage)
- self._cam = QCamera(QCamera.FrontFace)
- self._cam.setViewfinder(self._grabber)
- self._cam.start()
- def initUI(self):
- self._central = QWidget(self)
- grid = QGridLayout(self)
- self._lbl_img = QLabel("img_lbl")
- self._lbl_fps = QLabel("fps:")
- self._lbl_cps = QLabel("cps:")
- self._btn_start = QPushButton("start")
- grid.addWidget(self._lbl_img, 0,0)
- grid.addWidget(self._lbl_fps, 1,0)
- grid.addWidget(self._lbl_cps, 1,1)
- grid.addWidget(self._btn_start, 1,2)
- self.setCentralWidget(self._central)
- self._central.setLayout(grid)
- @pyqtSlot(QImage)
- def drawImage(self, img:QImage):
- px = QPixmap.fromImage(img)
- self._lbl_img.setPixmap(px)
- app = QApplication([])
- win = mainwin()
- win.show()
- app.exec_(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement