zoukankan      html  css  js  c++  java
  • python opencv PyQt5

    import cv2
    import numpy as np
    import sys
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    import datetime
    
    
    class Video():
        def __init__(self, capture):
            self.capture = capture
            capture.set(3,960) # set Width
            capture.set(4,2560) # set Height
            self.currentFrame = np.array([])
    
        def captureFrame(self):
            ret, readFrame = self.capture.read()
            return readFrame
    
        def captureNextFrame(self):
            ret, readFrame = self.capture.read()
            if (ret == True):
    
                readFrame=cv2.resize(readFrame, (int(960 / 4), int(2560 / 4)))
                
                #cv2.waitKey(1)
                self.currentFrame = cv2.cvtColor(readFrame, cv2.COLOR_BGR2RGB)
    
        def convertFrame(self):
            try:
                height, width = self.currentFrame.shape[:2]
                #print(height, width)
                img = QImage(self.currentFrame, width, height, QImage.Format_RGB888)
                img = QPixmap.fromImage(img)
                #self.previousFrame = self.currentFrame
                return img
            except:
                return None
    
    
    class win(QMainWindow):
        def __init__(self, parent=None):
            super(win,self).__init__()
            self.setGeometry(250, 80, 960, 2560)
            self.setWindowTitle('camera')
            self.video = Video(cv2.VideoCapture(1))
            print(self.video)
            self._timer = QTimer(self)
            self._timer.timeout.connect(self.play)
            self._timer.start(2)
            self.update()
            self.videoFrame = QLabel('VideoCapture')
            self.videoFrame.setAlignment(Qt.AlignCenter)
            self.setCentralWidget(self.videoFrame)
            self.ret, self.capturedFrame = self.video.capture.read()
    
        def play(self):
            try:
                nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                print(nowTime)
                self.video.captureNextFrame()
                self.videoFrame.setPixmap(self.video.convertFrame())
                self.videoFrame.setScaledContents(True)
            except TypeError:
                print('No Frame')
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        win = win()
        win.show()
        sys.exit(app.exec_())
    

      

  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/ahuo/p/11247294.html
Copyright © 2011-2022 走看看