Article Directory
1. Camera display
Summary 1: Face Classroom Sign-in Management System (Summary One) pyqt5 interface design
The implementation flowchart is roughly as follows:
Second, the camera operation class camera implementation
effect: Complete the camera's collection function, when the camera needs to complete a function, call a function of the camera object to complete
Realize function: 1. Turn on the camera; 2. Obtain real-time data of the camera; 3. Convert the data into a data format that can be displayed on the interface
-
Create a new camera.py file and introduce the header file:
import cv2 import numpy as np from PyQt5.QtGui import QPixmap, QImage
-
Define the camera class, the functions implemented are:
-
Turn on the camera
# 1、打开摄像头 def __init__(self): # VideoCapture类对摄像头或调用摄像头进行读取操作 # 0表示打开默认摄像头 self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 定义一个多维数组,用于存储画面数据 self.currentframe = np.array([])
-
Get real-time data from the camera
# 2、读取摄像头数据 def read_camera(self): ret, frame = self.cap.read() if not ret: print("获取摄像头失败!") return None return frame
-
Convert the data into a data format that the interface can display
# 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
-
Turn off the camera
# 关闭摄像头 def camera_close(self): # 释放VideoCapture 对象 self.cap.release()
-
Three, the interface class myWindow
Display the screen to the interface
-
The display screen needs to be displayed cyclically, so timer timing is required. The timer generates a signal every time interval, and is associated with a slot function. When the signal is generated, the slot function is actively called
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)
-
Clicking to start sign-in (actionopen) will generate a signal, and the signal is associated with the slot function (on_actionopen), and clicking start sign-in will realize the function in the slot function. The "groove associated with the signal" should be achieved during object initialization, and should therefore be __init__ function implementations
Format: signal object. signal. connect (slot function)
# 信号与槽的关联 # 指定对象:self.actionopen、信号:triggered、关联(槽函数):connect(self.on_actionopen) self.actionopen.triggered.connect(self.on_actionopen) # 关联启动签到
Click to close and sign in (actionclose) to close the camera
-
On_actionclose function implementation:
def on_actionclose(self): # 关闭摄像头 self.camera_data.camera_close() # 关闭定时器 self.timeshow.stop() self.label.setText('TextLabel')
-
The association of signals and slots is added in the __init__ function :
self.actionclose.triggered.connect(self.on_actionclose) # 关联关闭签到
Get the date and add it to the interface
-
Get the system time and date and display it in the label control on the interface
# 获取日期,并添加到界面中 def get_datetime(self): qdatetime = QDateTime.currentDateTime() self.label_2.setText(qdatetime.toString("ddd yyyy/MM/dd hh:mm:ss"))
-
Add a timer in the __init__ function , and the associated slot function (get_datetime) realizes dynamic binding time
# 创建定时器对象,用于实时显示时间 self.datetime = QTimer() # 每500ms获取一次系统时间 self.datetime.start(500) self.datetime.timeout.connect(self.get_datetime)
Complete code
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)
Gain: Master the use of classes to implement camera operation functions, the association of pyqt signals and slots, timer operations, etc.
Summary three portal: face classroom sign-in management system (summary three) Baidu face detection API call