Python+OpenCV face recognition check-in attendance system (beginner entry)

foreword

This project is designed for the check-in and attendance of IOT laboratory personnel. The system implements functions:
1. Personnel face recognition and complete check-in/check-out
2. Attendance time calculation
3. Save attendance data in CSV format (Excel table)

PS: This system 2D face recognition saves the tedious face recognition training part, which is simple and fast

This project is a beta version, the official version will add more functions, and it is continuously updated...
I will put the address of the beta version project at the end

Project renderings

System initialization login interface
insert image description here
main interface display diagram:
insert image description here
check-in function display
insert image description here
insert image description here
check-out function display
insert image description here
background check-in data record
insert image description here
whether to check in/out judgment
insert image description here

environment required by the project

Core environment:
OpenCV-Python 4.5.5.64
face_recognition 1.30
face_recognition_model 0.3.0
dlib 19.23.1

UI form interface:
PyQt5 5.15.4
pyqt5-plugins 5.15.4.2.2
PyQt5-Qt5 5.15.2
PyQt5-sip 12.10.1
pyqt5-tools 5.15.4.3.2

translater

Pycham 2021.1.3
insert image description here
Python version 3.9.12
insert image description here
Anaconda
insert image description here

Aided development of QT-designer

insert image description here
insert image description here

Project configuration

insert image description here

code section

core code

MainWindow.py
UI file loads:

class Ui_Dialog(QDialog):
    def __init__(self):
        super(Ui_Dialog, self).__init__()
        loadUi("mainwindow.ui", self)       #加载QTUI文件

        self.runButton.clicked.connect(self.runSlot)

        self._new_window = None
        self.Videocapture_ = None

Camera call:

    def refreshAll(self):
        print("当前调用人俩检测摄像头编号(0为笔记本内置摄像头,1为USB外置摄像头):")
        self.Videocapture_ = "0"

OutWindow.py
gets the current system time

class Ui_OutputDialog(QDialog):
    def __init__(self):
        super(Ui_OutputDialog, self).__init__()
        loadUi("./outputwindow.ui", self)   #加载输出窗体UI

        #datetime 时间模块
        now = QDate.currentDate()
        current_date = now.toString('ddd dd MMMM yyyy')  #时间格式
        current_time = datetime.datetime.now().strftime("%I:%M %p")
        self.Date_Label.setText(current_date)
        self.Time_Label.setText(current_time)

        self.image = None

Check-in time calculation

    def ElapseList(self,name):
        with open('Attendance.csv', "r") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            line_count = 2

            Time1 = datetime.datetime.now()
            Time2 = datetime.datetime.now()
            for row in csv_reader:
                for field in row:
                    if field in row:
                        if field == 'Clock In':
                            if row[0] == name:
                                Time1 = (datetime.datetime.strptime(row[1], '%y/%m/%d %H:%M:%S'))
                                self.TimeList1.append(Time1)
                        if field == 'Clock Out':
                            if row[0] == name:
                                Time2 = (datetime.datetime.strptime(row[1], '%y/%m/%d %H:%M:%S'))
                                self.TimeList2.append(Time2)

face recognition part

# 人脸识别部分
        faces_cur_frame = face_recognition.face_locations(frame)
        encodes_cur_frame = face_recognition.face_encodings(frame, faces_cur_frame)

        for encodeFace, faceLoc in zip(encodes_cur_frame, faces_cur_frame):
            match = face_recognition.compare_faces(encode_list_known, encodeFace, tolerance=0.50)
            face_dis = face_recognition.face_distance(encode_list_known, encodeFace)
            name = "unknown"    #未知人脸识别为unknown
            best_match_index = np.argmin(face_dis)
            if match[best_match_index]:
                name = class_names[best_match_index].upper()
                y1, x2, y2, x1 = faceLoc
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
                cv2.rectangle(frame, (x1, y2 - 20), (x2, y2), (0, 255, 0), cv2.FILLED)
                cv2.putText(frame, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
            mark_attendance(name)

        return frame

Sign-in data storage and judgment

# csv表格保存数据
        def mark_attendance(name):
            """
            :param name: 人脸识别部分
            :return:
            """
            if self.ClockInButton.isChecked():
                self.ClockInButton.setEnabled(False)
                with open('Attendance.csv', 'a') as f:
                        if (name != 'unknown'):         #签到判断:是否为已经识别人脸
                            buttonReply = QMessageBox.question(self, '欢迎 ' + name, '开始签到' ,
                                                               QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                            if buttonReply == QMessageBox.Yes:

                                date_time_string = datetime.datetime.now().strftime("%y/%m/%d %H:%M:%S")
                                f.writelines(f'\n{
      
      name},{
      
      date_time_string},Clock In')
                                self.ClockInButton.setChecked(False)

                                self.NameLabel.setText(name)
                                self.StatusLabel.setText('签到')
                                self.HoursLabel.setText('开始签到计时中')
                                self.MinLabel.setText('')

                                self.Time1 = datetime.datetime.now()
                                self.ClockInButton.setEnabled(True)
                            else:
                                print('签到操作失败')
                                self.ClockInButton.setEnabled(True)
            elif self.ClockOutButton.isChecked():
                self.ClockOutButton.setEnabled(False)
                with open('Attendance.csv', 'a') as f:
                        if (name != 'unknown'):
                            buttonReply = QMessageBox.question(self, '嗨呀 ' + name, '确认签退?',
                                                              QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                            if buttonReply == QMessageBox.Yes:
                                date_time_string = datetime.datetime.now().strftime("%y/%m/%d %H:%M:%S")
                                f.writelines(f'\n{
      
      name},{
      
      date_time_string},Clock Out')
                                self.ClockOutButton.setChecked(False)

                                self.NameLabel.setText(name)
                                self.StatusLabel.setText('签退')
                                self.Time2 = datetime.datetime.now()

                                self.ElapseList(name)
                                self.TimeList2.append(datetime.datetime.now())
                                CheckInTime = self.TimeList1[-1]
                                CheckOutTime = self.TimeList2[-1]
                                self.ElapseHours = (CheckOutTime - CheckInTime)
                                self.MinLabel.setText("{:.0f}".format(abs(self.ElapseHours.total_seconds() / 60)%60) + 'm')
                                self.HoursLabel.setText("{:.0f}".format(abs(self.ElapseHours.total_seconds() / 60**2)) + 'h')
                                self.ClockOutButton.setEnabled(True)
                            else:
                                print('签退操作失败')
                                self.ClockOutButton.setEnabled(True)

Project directory structure

insert image description here

postscript

Because the system does not conduct face training to establish a model, the system has a high false recognition rate and low security. The
system optimization is poor, the number of frames captured by the camera is low (8-9), the background occupation is high, and the CPU utilization rate is high.
Data storage CSV format, less secure

Official version improvements

1. Add TensorFlow deep learning to improve the security and accuracy of face recognition in the system
2. Add MySQL database to protect the check-in data more securely, and it is not easy to be modified
3. Beautify and optimize UI design

Project download

IOT face recognition check-in system beta version V0.99

Guess you like

Origin blog.csdn.net/weixin_50679163/article/details/124310679