pyqt 调用摄像头,实现抓拍、录屏的功能

 showVideoMain.py文件

import  sys
from showVideo import Ui_MainWindow
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from  PyQt5.QtWidgets import *
import cv2
import os

class videoShow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.setWindowTitle("相机")
        self.setWindowFlags(Qt.FramelessWindowHint )
        self.camera_path = 0
        self.n = 0
        self.m = 0
        self.capture = cv2.VideoCapture(self.camera_path)
        # fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
        # video_writer = cv2.VideoWriter('image/myself.mp4',fourcc,35,(960,540))

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.show_vid)#连接计时器触发函数,作用相当while语句不断show video
        # self.timer.start(30)

    def show_vid(self):
        flag,self.frame = self.capture.read()
        # 此时视频的某一帧图像存在frame中
        if flag is False:
            return
        frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
        height, width = frame.shape[:2]

        pixMap = QImage(frame, width, height, QImage.Format_RGB888)  # 定义pixMap,将BGR格式转化成RGB
        pixMap = QPixmap.fromImage(pixMap)

        ratio = max(width / self.label.width(), height / self.label.height())
        pixMap.setDevicePixelRatio(ratio)  # 根据比例显示图片
        self.label.setAlignment(Qt.AlignCenter)  # 设置图片居中显示
        self.label.setPixmap(pixMap)

    @pyqtSlot()
    def on_pushButtonSX_clicked(self):
        self.timer.start(30)
    @pyqtSlot()
    def on_pushButtonGSX_clicked(self):
        self.timer.stop()
    @pyqtSlot()
    def on_pushButtonQX_clicked(self):
        self.timer.start(30)
    @pyqtSlot()
    def on_pushButtonBC_clicked(self):
        self.n
        new_path = 'image/myself'+ str(self.n) +  '.png'
        self.n += 1
        print(new_path+"保存成功")
        cv2.imwrite(new_path,self.frame)
    @pyqtSlot()
    def on_pushButtonLZ_clicked(self):
        fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
        # 初始化创建 video_writer 变量用来即将保存的录制的视频
        video_newpath = 'image/selfvideo'+str(self.m)+'.mp4'
        video_writer = cv2.VideoWriter(video_newpath, fourcc, 25, (960, 540))  # 视频地址,视频录制方式,录制视频的尺寸
        video_writer.write(self.frame)
    @pyqtSlot()
    def on_pushButtonEXIT_clicked(self):
        QApp = QApplication.instance()
        QApp.quit()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = videoShow()
    ui.show()
    sys.exit(app.exec_())

showVideo.py 文件

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'showVideo.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(719, 546)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 40, 661, 311))
        self.label.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.label.setText("")
        self.label.setObjectName("label")
        self.pushButtonSX = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonSX.setGeometry(QtCore.QRect(20, 350, 151, 81))
        self.pushButtonSX.setObjectName("pushButtonSX")
        self.pushButtonLZ = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonLZ.setGeometry(QtCore.QRect(580, 350, 101, 81))
        self.pushButtonLZ.setObjectName("pushButtonLZ")
        self.pushButtonBC = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonBC.setGeometry(QtCore.QRect(480, 350, 91, 81))
        self.pushButtonBC.setObjectName("pushButtonBC")
        self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider.setGeometry(QtCore.QRect(140, 340, 411, 16))
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.pushButtonGSX = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonGSX.setGeometry(QtCore.QRect(230, 350, 91, 81))
        self.pushButtonGSX.setObjectName("pushButtonGSX")
        self.pushButtonQX = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonQX.setGeometry(QtCore.QRect(320, 350, 91, 81))
        self.pushButtonQX.setObjectName("pushButtonQX")
        self.pushButtonEXIT = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonEXIT.setGeometry(QtCore.QRect(50, 440, 591, 61))
        self.pushButtonEXIT.setObjectName("pushButtonEXIT")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 0, 661, 41))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 719, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButtonSX.setText(_translate("MainWindow", "开启摄像头"))
        self.pushButtonLZ.setText(_translate("MainWindow", "录制"))
        self.pushButtonBC.setText(_translate("MainWindow", "保存"))
        self.pushButtonGSX.setText(_translate("MainWindow", "抓拍"))
        self.pushButtonQX.setText(_translate("MainWindow", "重拍"))
        self.pushButtonEXIT.setText(_translate("MainWindow", "关闭主窗口"))
        self.pushButton.setText(_translate("MainWindow", "相机"))

showVideo.ui  文件



 MainWindow
 
  
   
    0
    0
    719
    546
   
  
  
   MainWindow
  
  
   
    
     
      20
      40
      661
      311
     
    
    
     background-color: rgb(0, 0, 0);
    
    
     
    
   
   
    
     
      20
      350
      151
      81
     
    
    
     开启摄像头
    
   
   
    
     
      580
      350
      101
      81
     
    
    
     录制
    
   
   
    
     
      480
      350
      91
      81
     
    
    
     保存
    
   
   
    
     
      140
      340
      411
      16
     
    
    
     Qt::Horizontal
    
   
   
    
     
      230
      350
      91
      81
     
    
    
     抓拍
    
   
   
    
     
      320
      350
      91
      81
     
    
    
     重拍
    
   
   
    
     
      50
      440
      591
      61
     
    
    
     关闭主窗口
    
   
   
    
     
      20
      0
      661
      41
     
    
    
     相机
    
   
  
  
   
    
     0
     0
     719
     22
    
   
  
  
 
 
 

你可能感兴趣的:(Python,pyqt)