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

showVideoMain.py文件

python 复制代码
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 文件

python 复制代码
# -*- 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 文件

python 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>719</width>
    <height>546</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>40</y>
      <width>661</width>
      <height>311</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(0, 0, 0);</string>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonSX">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>350</y>
      <width>151</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>开启摄像头</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonLZ">
    <property name="geometry">
     <rect>
      <x>580</x>
      <y>350</y>
      <width>101</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>录制</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonBC">
    <property name="geometry">
     <rect>
      <x>480</x>
      <y>350</y>
      <width>91</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>保存</string>
    </property>
   </widget>
   <widget class="QSlider" name="horizontalSlider">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>340</y>
      <width>411</width>
      <height>16</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonGSX">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>350</y>
      <width>91</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>抓拍</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonQX">
    <property name="geometry">
     <rect>
      <x>320</x>
      <y>350</y>
      <width>91</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>重拍</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonEXIT">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>440</y>
      <width>591</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>关闭主窗口</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>0</y>
      <width>661</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>相机</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>719</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
相关推荐
王哈哈^_^2 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
王哈哈^_^16 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
苏三有春4 天前
PyQt5实战——UTF-8编码器UI页面设计以及按钮连接(五)
python·pyqt
充值内卷6 天前
PyQt入门指南三十六 QInputDialog输入对话框组件
开发语言·python·pyqt
yava_free7 天前
用PyQt 5 开发的雷达基数据可视化软件
python·pyqt
充值内卷8 天前
PyQt入门指南三十五 QAction动作组件
linux·python·pyqt
星寂樱易李11 天前
python--pyQt 单选按钮控件 -QRadioButton
开发语言·python·pyqt
彭祥.13 天前
点云标注工具开发记录(四)之点云根据类别展示与加速渲染
pyqt·opengl
love_songming13 天前
Pyside6 布局管理器(3)--- QGridLayout的使用
开发语言·python·pyqt·pyside6
痛&快乐着15 天前
python-PyQt项目实战案例:制作一个视频播放器
python·pyqt