1. 系統(tǒng)介紹
本系統(tǒng)是一個基于OpenCV和 Qt Designer 的人臉識別考勤系統(tǒng),主要功能是自動識別攝像頭中的人臉,并把人臉對應(yīng)的姓名和打卡時間存儲到數(shù)據(jù)庫中,方便管理人員進行考勤管理。本系統(tǒng)使用 face_recognition 庫進行人臉識別,使用 PyQt5 開發(fā)界面,然后把界面與代碼進行整合。
2. 系統(tǒng)架構(gòu)
系統(tǒng)主要由以下幾個模塊組成:
用戶界面:使用 PyQt5 設(shè)計界面,包括攝像頭畫面、人臉識別結(jié)果、打卡時間等。
攝像頭模塊:使用 OpenCV 庫獲取攝像頭視頻流,實時顯示在用戶界面中。
人臉識別模塊:使用 face_recognition 庫進行人臉識別,并將結(jié)果顯示在用戶界面中。
數(shù)據(jù)庫模塊:使用 sqlite3 庫進行數(shù)據(jù)存儲,把人臉對應(yīng)的姓名和打卡時間存儲到數(shù)據(jù)庫中。
3. 開發(fā)步驟
3.1 安裝必要的庫
本系統(tǒng)需要的主要庫有:
PyQt5:用于設(shè)計用戶界面。
OpenCV:用于獲取攝像頭視頻流。
face_recognition:用于進行人臉識別。
sqlite3:用于進行數(shù)據(jù)存儲。
可以通過以下命令安裝:
pip install pyqt5 opencv-python face_recognition sqlite3
3.2 設(shè)計用戶界面
使用 Qt Designer 設(shè)計用戶界面。用戶界面應(yīng)該包括以下幾個部分:
攝像頭畫面:用于實時顯示攝像頭視頻流。
人臉識別結(jié)果:用于顯示識別出的人臉及對應(yīng)的姓名。
打卡時間:用于顯示打卡時間。
打卡按鈕:用于手動打卡。
可以參考下面的截圖:
文章來源:http://www.zghlxwxcb.cn/news/detail-459508.html
3.3 編寫代碼
3.3.1 導(dǎo)入庫
import sys
import cv2
import face_recognition
import sqlite3
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QPushButton
3.3.2 連接數(shù)據(jù)庫
conn = sqlite3.connect('attendance.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS attendance
(name TEXT, time TEXT)''')
conn.commit()
3.3.3 定義主窗口類
class MainWindow(QDialog):
def __init__(self):
super().__init__()
self.camera_label = QLabel(self)
self.camera_label.resize(640, 480)
self.camera_label.move(20, 20)
self.result_label = QLabel(self)
self.result_label.resize(300, 300)
self.result_label.move(700, 20)
self.time_label = QLabel(self)
self.time_label.resize(300, 50)
self.time_label.move(700, 350)
self.button = QPushButton('打卡', self)
self.button.resize(100, 50)
self.button.move(700, 420)
self.button.clicked.connect(self.check_attendance)
self.timer = QTimer(self)
self.timer.timeout.connect(self.show_camera)
self.timer.start(30)
self.video_capture = cv2.VideoCapture(0)
self.setGeometry(100, 100, 1024, 480)
self.setWindowTitle('人臉識別考勤系統(tǒng)')
3.3.4 實時顯示攝像頭畫面
def show_camera(self):
ret, frame = self.video_capture.read()
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.display_image(rgb_frame, self.camera_label)
3.3.5 進行人臉識別
def face_recognition(self, frame):
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_faces, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_faces, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_names[best_match_index]
self.display_image(frame, self.result_label)
self.result_label.setText(name)
if name != 'Unknown':
c.execute("INSERT INTO attendance VALUES (?, datetime('now', 'localtime'))", (name,))
conn.commit()
3.3.6 手動打卡
def check_attendance(self):
name = self.result_label.text()
if name != 'Unknown':
c.execute("INSERT INTO attendance VALUES (?, datetime('now', 'localtime'))", (name,))
conn.commit()
3.3.7 顯示打卡時間
def show_time(self):
c.execute("SELECT * FROM attendance ORDER BY time DESC")
result = c.fetchone()
if result:
name, time = result
self.time_label.setText(f"{name} 打卡時間:{time}")
3.3.8 顯示圖片
def display_image(self, img, label):
qformat = QImage.Format_Indexed8
if len(img.shape) == 3:
if img.shape[2] == 4:
qformat = QImage.Format_RGBA8888
else:
qformat = QImage.Format_RGB888
img = QImage(img, img.shape[1], img.shape[0], qformat)
img = img.rgbSwapped()
label.setPixmap(QPixmap.fromImage(img))
label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
3.3.9 運行主程序
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
4. 總結(jié)
本文介紹了如何使用 Python 和 Qt Designer 開發(fā)人臉識別考勤系統(tǒng)。該系統(tǒng)可以自動識別攝像頭中的人臉,并把人臉對應(yīng)的姓名和打卡時間存儲到數(shù)據(jù)庫中,方便管理人員進行考勤管理。希望本文對您有所幫助。文章來源地址http://www.zghlxwxcb.cn/news/detail-459508.html
到了這里,關(guān)于OpenCV+ Qt Designer 開發(fā)人臉識別考勤系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!