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>
相关推荐
WeeJot嵌入式3 天前
PyQT开发与实践:全面掌握跨平台桌面应用开发
人工智能·pyqt
懷淰メ4 天前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
小狮子安度因4 天前
PyQt的安装和再PyCharm中的配置
ide·pycharm·pyqt
GIS 数据栈11 天前
博客摘录「 pyqt 为新建子线程传参以及子线程返回数据到主线程」2023年12月7日
笔记·python·pyqt·多线程·多线程通信
西木九14 天前
解决:WSL2可视化opencv和pyqt冲突:QObject::moveToThread
python·opencv·pyqt
充值内卷15 天前
PyQt入门指南五十一 文档与注释规范
开发语言·python·pyqt
王哈哈^_^18 天前
【数据集】【YOLO】【目标检测】树木倒塌识别数据集 9957 张,YOLO道路树木断裂识别算法实战训练教程!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
王哈哈^_^18 天前
【数据集】【YOLO】【目标检测】航拍船只识别数据集 3550 张,YOLO航拍水面船只识别算法实战训练教程!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
王哈哈^_^18 天前
【数据集】【YOLO】【目标检测】摔跤识别数据集 5097 张,YOLO行人摔倒识别算法实战训练教程!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
充值内卷18 天前
PyQt入门指南四十四 打印与预览功能的实现
开发语言·python·pyqt