Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. from PyQt5.QtWidgets import QApplication, QWidget,\
  2.     QLabel, QMainWindow, QGridLayout, QPushButton
  3.  
  4. from PyQt5.QtMultimedia import QAbstractVideoSurface, QAbstractVideoBuffer, QVideoFrame, QCamera
  5. from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
  6. from PyQt5.QtGui import QImage, QPixmap
  7.  
  8. class frameGrabber(QAbstractVideoSurface):
  9.     draw_image = pyqtSignal(QImage)
  10.     process_image = pyqtSignal(QImage)
  11.  
  12.     def __init__(self, parent=None):
  13.         super().__init__(parent)
  14.  
  15.     def supportedPixelFormats(self,handleType=QAbstractVideoBuffer.NoHandle):
  16.          formats = [QVideoFrame.PixelFormat()]
  17.          if (handleType == QAbstractVideoBuffer.NoHandle):
  18.              formats.append(QVideoFrame.Format_RGB24)
  19.          return formats
  20.  
  21.     def present(self, frame:QVideoFrame)->bool:
  22.         if (self.surfaceFormat().pixelFormat() != frame.pixelFormat() or
  23.             self.surfaceFormat().frameSize() != frame.size()):
  24.                 self.setError(QAbstractVideoSurface.IncorrectFormatError)
  25.                 self.stop()
  26.                 return False
  27.         self.currentFrame = frame
  28.         img = QImage(self.currentFrame.bits(),
  29.             self.currentFrame.width(), self.currentFrame.height(), self.currentFrame.bytesPerLine(),
  30.             QImage.Format_RGB888)
  31.         self.draw_image.emit(img)
  32.         return True
  33.  
  34. class mainwin(QMainWindow):
  35.     def __init__(self, parent=None):
  36.         super(mainwin, self).__init__(parent)
  37.         self.initUI()
  38.         self._grabber = frameGrabber(self)
  39.         self._grabber.draw_image.connect(self.drawImage)
  40.         self._cam = QCamera(QCamera.FrontFace)
  41.         self._cam.setViewfinder(self._grabber)
  42.         self._cam.start()
  43.  
  44.     def initUI(self):
  45.         self._central = QWidget(self)
  46.         grid = QGridLayout(self)
  47.         self._lbl_img = QLabel("img_lbl")
  48.         self._lbl_fps = QLabel("fps:")
  49.         self._lbl_cps = QLabel("cps:")
  50.         self._btn_start = QPushButton("start")
  51.         grid.addWidget(self._lbl_img, 0,0)
  52.         grid.addWidget(self._lbl_fps, 1,0)
  53.         grid.addWidget(self._lbl_cps, 1,1)
  54.         grid.addWidget(self._btn_start, 1,2)
  55.         self.setCentralWidget(self._central)
  56.         self._central.setLayout(grid)
  57.  
  58.     @pyqtSlot(QImage)
  59.     def drawImage(self, img:QImage):
  60.         px = QPixmap.fromImage(img)
  61.         self._lbl_img.setPixmap(px)
  62.  
  63. app = QApplication([])
  64. win = mainwin()
  65. win.show()
  66. app.exec_(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement