人脸课堂签到管理系统(总结二) 摄像头显示

5 篇文章 0 订阅
3 篇文章 0 订阅

一、摄像头显示

总结一:人脸课堂签到管理系统(总结一) pyqt5界面设计

实现流程图大致如下:

二、摄像头操作类 camera 实现

作用: 完成摄像头的采集功能,当需要摄像头完成一个功能时,就调用摄像头对象的一个函数去完成

实现功能: 1、打开摄像头;2、获取摄像头的实时数据;3、把数据转换成界面能显示的数据格式

  1. 新建一个 camera.py 文件,引入头文件:

    import cv2
    import numpy as np
    from PyQt5.QtGui import QPixmap, QImage
    
  2. 定义 camera 类,实现的功能有:

    • 打开摄像头

          # 1、打开摄像头
          def __init__(self):
              # VideoCapture类对摄像头或调用摄像头进行读取操作
              # 0表示打开默认摄像头
              self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
              # 定义一个多维数组,用于存储画面数据
              self.currentframe = np.array([])
      
    • 获取摄像头的实时数据

          # 2、读取摄像头数据
          def read_camera(self):
              ret, frame = self.cap.read()
              if not ret:
                  print("获取摄像头失败!")
                  return None
              return frame
      
    • 把数据转换成界面能显示的数据格式

          # 3、把数据转换成界面能显示的数据格式
          def camera_toPic(self):
              frame = self.read_camera()
              # 摄像头图片BGR,需转为为RGB
              self.currentframe = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
              # 转换成界面能显示的格式
              # a.获取画面的宽高
              height, width = self.currentframe.shape[:2]
              # b.创建Qimage类对象(使用摄像头画面数据)
              img = QImage(self.currentframe, width, height, QImage.Format_RGB888)
              qpixmap = QPixmap.fromImage(img)
              return qpixmap
      
    • 关闭摄像头

          # 关闭摄像头
          def camera_close(self):
              # 释放VideoCapture 对象
              self.cap.release()
      

三、界面类 myWindow

显示画面到界面中

  1. 显示画面需要循环一直显示,因此需要定时器定时,定时器每间隔一段时间就产生一个信号,并且关联上一个槽函数,当信号产生时就会主动调用该槽函数

        def on_actionopen(self):
            # 启动摄像头
            self.camera_data = camera()
            # 启动定时器,进行定时,每个多长时间获取摄像头信号进行显示
            self.timeshow = QTimer(self)
            self.timeshow.start(50)
            # 每隔50毫秒将产生一个信号timeout
            self.timeshow.timeout.connect(self.show_camera)
    
        def show_camera(self):
            # 获取摄像头转换数据
            img = self.camera_data.camera_toPic()
            # 显示画面
            self.label.setPixmap(img)
    
  2. 点击启动签到(actionopen)将会产生一个信号,并且信号关联到槽函数(on_actionopen),实现点击启动签到就会实现槽函数中的功能。该 “信号与槽的关联” 应该在对象初始化时实现,因此应在 __init__函数 中实现

    格式:信号对象.信号.connect(槽函数)

    # 信号与槽的关联
    # 指定对象:self.actionopen、信号:triggered、关联(槽函数):connect(self.on_actionopen)
    self.actionopen.triggered.connect(self.on_actionopen)   # 关联启动签到
    

点击关闭签到(actionclose)将关闭摄像头

  1. on_actionclose 函数实现:

        def on_actionclose(self):
            # 关闭摄像头
            self.camera_data.camera_close()
            # 关闭定时器
            self.timeshow.stop()
            self.label.setText('TextLabel')
    
  2. 信号与槽的关联,在 __init__ 函数 中添加:

    self.actionclose.triggered.connect(self.on_actionclose) # 关联关闭签到
    

获取日期,并添加到界面中

  1. 获取系统时间日期,并显示到界面的 label 控件中

        # 获取日期,并添加到界面中
        def get_datetime(self):
            qdatetime = QDateTime.currentDateTime()
            self.label_2.setText(qdatetime.toString("ddd    yyyy/MM/dd    hh:mm:ss"))
    
  2. __init__ 函数 中添加一个计时器,关联槽函数(get_datetime)实现动态绑定时间

    # 创建定时器对象,用于实时显示时间
    self.datetime = QTimer()
    # 每500ms获取一次系统时间
    self.datetime.start(500)
    self.datetime.timeout.connect(self.get_datetime)
    

完整代码

from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import QTimer,QDateTime
from MainWindow import Ui_MainWindow
from camera import camera

class myWindow(Ui_MainWindow, QMainWindow):
    def __init__(self):  # 对象的初始化方法
        super(myWindow, self).__init__()  # 通过super()来调用父类的__init__()函数
        self.setupUi(self)  # 创建界面内容
        # 信号与槽的关联
        # 指定对象:self.actionopen、信号:triggered、关联(槽函数):connect(self.on_actionopen)
        self.actionopen.triggered.connect(self.on_actionopen)   # 关联启动签到
        self.actionclose.triggered.connect(self.on_actionclose) # 关联关闭签到
        # 创建定时器对象,用于实时显示时间
        self.datetime = QTimer()
        # 每500ms获取一次系统时间
        self.datetime.start(500)
        self.datetime.timeout.connect(self.get_datetime)

    # 获取日期,并添加到界面中
    def get_datetime(self):
        qdatetime = QDateTime.currentDateTime()
        self.label_2.setText(qdatetime.toString("ddd    yyyy/MM/dd    hh:mm:ss"))

    def on_actionclose(self):
        # 关闭摄像头
        self.camera_data.camera_close()
        # 关闭定时器
        self.timeshow.stop()
        self.label.setText('TextLabel')

    def on_actionopen(self):
        # 启动摄像头
        self.camera_data = camera()
        # 启动定时器,进行定时,每个多长时间获取摄像头信号进行显示
        self.timeshow = QTimer(self)
        self.timeshow.start(50)
        # 每隔50毫秒将产生一个信号timeout
        self.timeshow.timeout.connect(self.show_camera)

    def show_camera(self):
        # 获取摄像头转换数据
        img = self.camera_data.camera_toPic()
        # 显示画面
        self.label.setPixmap(img)

收获:掌握了用类来实现摄像头操作功能,pyqt信号与槽的关联,定时器操作等
总结三传送门:人脸课堂签到管理系统(总结三) 百度人脸检测API的调用

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值