程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python face recognition attendance system

編輯:Python

Preface

This project is IOT Design of laboratory personnel check-in and attendance , The system realizes the function :

  • Personnel face recognition and sign in / Sign off

  • Attendance time calculation

  • Save attendance data as CSV Format (Excel form )

PS: This system 2D Face recognition , Save the tedious face recognition training part , Simple and fast

The project is a beta version , The official version will add more functions , Ongoing update ..... I'll put the beta project address at the end

Project renderings

Login interface
Main interface display diagram :
Check in function display

Sign out function display
Background check-in data record
Check in or not / Regression judgment

Project environment

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

compiler

Pycham 2021.1.3

**Python edition  3.9.12**

Anaconda

Auxiliary development QT-designer

Project configuration

Code section

Core code

「MainWindow.py」UI File loading :

class Ui_Dialog(QDialog):
    def __init__(self):
        super(Ui_Dialog, self).__init__()
        loadUi("mainwindow.ui", self)       ## load QTUI file
        self.runButton.clicked.connect(self.runSlot)
        self._new_window = None
        self.Videocapture_ = None

Camera call :

def refreshAll(self):
        print(" Currently, two people are called to detect the camera number (0 Built in camera for notebook ,1 by USB External camera ):")
        self.Videocapture_ = "0"

「OutWindow.py」 Get current system time

class Ui_OutputDialog(QDialog):
    def __init__(self):
        super(Ui_OutputDialog, self).__init__()
        loadUi("./outputwindow.ui", self)   ## Load output form UI
        ##datetime  Time module
        now = QDate.currentDate()
        current_date = now.toString('ddd dd MMMM yyyy')  ## Time format
        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

##  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 face recognition is 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

Check in and save data

## csv Tables save data
        def mark_attendance(name):
            """
            :param name:  Face recognition part
            :return:
            """
            if self.ClockInButton.isChecked():
                self.ClockInButton.setEnabled(False)
                with open('Attendance.csv', 'a') as f:
                        if (name != 'unknown'):         ## Check in judgment : Whether it is a recognized face
                            buttonReply = QMessageBox.question(self, ' welcome  ' + name, ' Start checking in ' ,
                                                               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(' Sign in ')
                                self.HoursLabel.setText(' Start check-in timing ')
                                self.MinLabel.setText('')
                                self.Time1 = datetime.datetime.now()
                                self.ClockInButton.setEnabled(True)
                            else:
                                print(' Check in operation failed ')
                                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, ' Hi  ' + name, ' Confirm to sign out ?',
                                                              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(' Sign off ')
                                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(' Sign out operation failed ')
                                self.ClockOutButton.setEnabled(True)
Project directory structure

Postscript

  • Because the system does not carry out face training and establish a model , The system has a high false recognition rate , Low security

  • Poor system optimization , The number of frames captured by the camera is low (8-9), Backstage possession is high ,CPU High utilization rate

  • Save the data CSV Format , Low security

Official version improvement

  • Join in TensorFlow Deep learning , Improve the safety and accuracy of face recognition system

  • Join in MySQL database , More secure check-in data , Not easy to modify

  • Beautify and optimize UI Design

The source code is in the official account Python researcher The background to reply Attendance attendance obtain ~

Recommended reading

  • Python Make visualizations GUI Interface , One click to automatically classify and manage files !

  • 5 That's ok Python Code crawling 3000+ Information of listed companies

  • Python Office automation : The strongest and most detailed in the whole network PDF Document operation manual !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved