1. 安装opencv库

pip install opencv-python

2. 界面

3 .代码

  界面设计:
        palette = QPalette()
        icon = QPixmap('c.jpg').scaled(800, 600)
        palette.setBrush(self.backgroundRole(), QBrush(icon))
        self.setPalette(palette)

        self.resize(600, 400)
        self.label = QLabel(self)
        self.label.setFixedSize(390, 250)
        self.label.move(100, 80)
        self.label.setText("显示录像")
        self.label.setStyleSheet("QLabel{background:white;}"
                                 "QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}"
                                 )
        btn = QPushButton(self)
        btn1 = QPushButton(self)
        btn.move(10, 20)
        btn1.move(10, 60)
        btn1.setText("停止录像")
        btn.setText("录像")


刷新摄像头的显示时间,实时显示:
        self.timer = QTimer()
        self.timer.start()            # 实时刷新,不然视频不动态
        self.timer.setInterval(100)   # 设置刷新时间

    def start(self, event):
        self.cap = cv2.VideoCapture(0)      #开启摄像头
        self.timer.timeout.connect(self.capPicture)

  """ 开启视频"""使用label的setPixmap方法显示
    
        if (self.cap.isOpened()):
            # get a frame
            ret, img = self.cap.read()
            height, width, bytesPerComponent = img.shape
            bytesPerLine = bytesPerComponent * width
            # 变换彩色空间顺序
            cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
            # 转为QImage对象
            self.image = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)
            self.label.setPixmap(QPixmap.fromImage(self.image).scaled(self.label.width(), self.label.height()))

    """ 停止录像"""
        self.cap.release()
        self.label.setText(" ")

4. 代码详情地址

 https://download.csdn.net/my

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐