opencv pyqt实现可视化员工人脸打卡系统 封装成exe文件 源代码
时间: 2024-01-25 10:04:42 浏览: 89
OpenCV+C++图像处理设计-人脸检测与识别系统DEMO源代码.zip
5星 · 资源好评率100%
以下是一个简单的实现代码,仅供参考:
```python
import cv2
import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
import sqlite3
class FaceRecognizer:
def __init__(self):
self.face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
self.recognizer = cv2.face.LBPHFaceRecognizer_create()
self.recognizer.read('trainer.yml')
self.font = cv2.FONT_HERSHEY_SIMPLEX
self.conn = sqlite3.connect('employee.db')
self.cursor = self.conn.cursor()
def recognize(self, img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
roi_gray = gray[y:y + h, x:x + w]
id_, confidence = self.recognizer.predict(roi_gray)
if confidence < 70:
self.cursor.execute("SELECT * FROM employee WHERE id=?", (id_,))
employee = self.cursor.fetchone()
name = employee[1]
cv2.putText(img, name, (x, y - 20), self.font, 2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
self.cursor.execute("INSERT INTO attendance (employee_id) VALUES (?)", (id_,))
self.conn.commit()
else:
cv2.putText(img, "Unknown", (x, y - 20), self.font, 2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
return img
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Face Recognition")
self.setGeometry(100, 100, 800, 600)
self.video_label = QLabel(self)
self.video_label.setGeometry(10, 10, 640, 480)
self.video_label.setAlignment(Qt.AlignCenter)
self.employee_label = QLabel(self)
self.employee_label.setGeometry(660, 10, 130, 50)
self.employee_label.setAlignment(Qt.AlignCenter)
self.attendance_label = QLabel(self)
self.attendance_label.setGeometry(660, 70, 130, 500)
self.attendance_label.setAlignment(Qt.AlignTop)
self.face_recognizer = FaceRecognizer()
self.timer = None
def start_video(self):
self.timer = self.video_label.startTimer(30)
def stop_video(self):
self.video_label.killTimer(self.timer)
def timerEvent(self, event):
ret, frame = self.cap.read()
if ret:
frame = cv2.flip(frame, 1)
frame = self.face_recognizer.recognize(frame)
h, w, ch = frame.shape
bytesPerLine = ch * w
convertToQtFormat = QImage(frame.data, w, h, bytesPerLine, QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.video_label.setPixmap(QPixmap.fromImage(p))
def start_camera(self):
self.cap = cv2.VideoCapture(0)
self.start_video()
def stop_camera(self):
self.stop_video()
self.cap.release()
def get_employees(self):
self.face_recognizer.cursor.execute("SELECT * FROM employee")
employees = self.face_recognizer.cursor.fetchall()
employee_text = "Employees:\n"
for employee in employees:
employee_text += f"{employee[0]} - {employee[1]}\n"
self.employee_label.setText(employee_text)
def get_attendance(self):
self.face_recognizer.cursor.execute("SELECT * FROM attendance")
attendance = self.face_recognizer.cursor.fetchall()
attendance_text = "Attendance:\n"
for record in attendance:
employee_id = record[1]
self.face_recognizer.cursor.execute("SELECT name FROM employee WHERE id=?", (employee_id,))
employee_name = self.face_recognizer.cursor.fetchone()[0]
attendance_text += f"{employee_name}\n"
self.attendance_label.setText(attendance_text)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.get_employees()
window.get_attendance()
window.start_camera()
window.show()
sys.exit(app.exec_())
```
注意,这只是一个简单的实现,还有很多需要优化的地方,比如异常处理、界面美化、性能优化等。
阅读全文