Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a facial scanning attendance system with Python Code

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use Python code to build a facial scanning attendance system, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Demand analysis

The "employee facial scanning attendance" system, developed in Python language, can add employee facial information through the camera, which involves two specific questions, one is what kind of data should be used to identify each employee's facial information, and the other is to keep the information in the database. In more detail, it also involves the design of the table; another basic requirement is to use the camera to recognize the employee's facial information to complete the attendance. This problem can basically be achieved by comparing the employee's facial data in the database with the employee's facial data in the current camera, but one problem is what to do if there are multiple faces in the camera. The expansion requirement is to export the daily attendance table, which can be divided into two parts, one is to store attendance information, and the other is to display attendance information.

The goals we hope to achieve are:

(1) according to the principle of general software interface design, all operations are implemented in the menu bar, part of the area is used to display the video stream information read by the camera in real time and processed by the program, and the other area is used for console output to print relevant information, such as prompting employees to add facial information successfully, failed to add and its reasons, and to prompt employees to sign in successfully, failed and their reasons. When adding facial information, people must interact with the program, such as entering some relevant information, when the program is blocked; but when clocking in, the program is not blocked, if you do not click to close the punch, it will always be in the clocking mode, waiting for and identifying every employee who comes to punch, which is more in line with the realistic usage scenario.

(2) set up a table to store employee information and attendance information. Each time the employee's facial information is newly entered, the employee's work number and name are required to be entered, and only the employee's facial information closest to the screen is taken. This is due to the fact that the actual clocking in is carried out in turn rather than in groups of people, and there are two modes to choose when entering. Automatic mode: once the face is recognized, the screenshot is automatically captured, and the input ends when the continuous screenshot reaches 10. Manual mode: click the menu to finish typing, you don't have to get 10. After the end of input, we begin to extract facial feature data from the employee's face just captured and save it in the database together with the previously entered employee name and other data as a row of records. If the employee's face is not captured or the facial information just captured is not the same person, this line of record will be discarded.

When checking in, three conditions must be met: the facial information has been entered, the clock-in time period is not repeated, and only if the clock-in is successful, the name and work number, date and time of the clock-in will be saved to the database as an one-line record and the successful sign-in information will be output in the console, otherwise the failure and its reasons will be output on the console.

All in all: our design goal is standardization and humanization.

Need source code can be followed, forwarded, private message editor "01" to get, as well as free Python learning video materials.

overall design

In order to accomplish the above goal 1, the interface initialization of the program is divided into three parts: the * part initializes the menu bar, the second part initializes the left console, and the third part initializes the right display panel, making the three parts independent of each other. The initialization of the data logic part is divided into two parts, the * part is the initialization of the database part, if the database / table does not exist, the relevant data is loaded, and the second part is to initialize some variables that need to be recycled. For example, the employee name, job number, screenshot number counter and so on when the new entry is completed, these data should be reset to initialization for the next entry. Writing these initialization statements as a function can improve code reuse.

The second goal mentioned above is mainly some restrictive conditions, which can be achieved by adding judgment statements, such as checking the validity of the input id:

While self.id = = ID_WORKER_UNAVIABLE: self.id = wx.GetNumberFromUser (message= "Please enter your work number (- 1 not available)", prompt= "work number", caption= "warm reminder", value=ID_WORKER_UNAVIABLE, parent=self.bmp,max=100000000,min=ID_WORKER_UNAVIABLE) for knew_id in self.knew_id: if knew_id = = self.id: self.id = ID_WORKER_UNAVIABLE wx.MessageBox (message= "work number already exists, please re-enter", caption= "warning")

Where ID_WORKER_UNAVIABLE is the initialization value of id-1, which is not available. Self.knew.id is the list of id loaded from the database. If the id is illegal (repeated or not 0,000,000,000), there will always be a new pop-up window to prompt for id.

For example, when rejecting multiple faces, only the facial information of the employee closest to the screen is processed:

If len (dets)! = 0: biggest_face = dets [0] # face maxArea = 0 for det in dets: W = det.right ()-det.left () h = det.top ()-det.bottom () if wishh > maxArea: biggest_face = det maxArea = wreckh

Dets is an array of all the faces detected, and biggest_face is the closest face to the screen.

Program block diagram:

Note: the picture can also be previewed online if you can't see it clearly.

Https://www.processon.com/view/link/5bbcc953e4b08faf8c7324a1

The design idea of this program can be divided into the following aspects.

According to the object-oriented principle, the main body of the whole program is a WAS (WorkAttendanceSystem) class, and all implementations are developed around this class.

According to the principle of separation of interface and data logic, the initialization process of WAS class includes interface initialization and data initialization, which are independent of each other.

Code encapsulation principle, statements that have been called many times are written into interfaces for calling, and there is no redundant code.

Interface isolation principle: use multiple specialized interfaces instead of a single master interface.

Function list

Note: the * arguments of all functions in the class are self, indicating that the function belongs to this class. I will not repeat it later.

Def _ init__ (self)

The constructor of the WAS class mainly completes some initialization operations, such as initializing menu, information printing panel, main display panel, initializing loading database and initializing variables for recycling.

Def initMenu (self):

Complete the initialization display of the menu and click event binding.

Def initInfoText (self):

Complete the initialization display of the left information prompt panel.

Def initGallery (self):

Complete the initialization display of the main display panel on the right.

Def initDatabase (self):

Initialize the database and establish a database connection (create a new one if the database inspurer.db does not exist). If the two tables of employee information worker_info and attendance logcat do not exist in the database, then create them in turn.

Def loadDataBase (self,type):

The module function completes the operation of reading data from the database, including reading employee information and attendance information. The second parameter, type, is used to identify whether to load employee information or attendance information. On the one hand, the interface can be unified, opening the database is the same as getting cursors and closing connections, and the two read interfaces are combined into one to improve code reuse. On the other hand, it can reduce the workload of loading, reduce IO and improve the running speed of the program; * because the last read information list is cleared before reading information, using type identification can avoid misoperation caused by reading one table to another table.

Def insertARow (self,Row,type):

The module function completes the write operation to the database, the second parameter is a record to be written, and the third parameter, type, indicates which table to write to.

Def adapt_array (self,arr):

The extracted facial feature information (list) is compressed, the entry parameters are the data to be compressed, and the exit parameters are the compressed data, which are used to write to the database.

Def convert_array (self,text):

Decompress the adult face feature information from the read data, the entrance parameters are the data to be decompressed, and the exit parameters are the decompressed data.

Def return_euclidean_distance (feature_1, feature_2):

To calculate the Euclidean distance between two faces, the entrance parameters are the feature data of the two faces, and the exit parameters are the result of the decision. An Euclidean distance greater than 0.4 is different, but not less than the same.

Def OnNewRegisterClicked (self,event):

See the meaning of the name, create a new monitoring event in the menu, the parameter event is the event information, and the other menus (OnFinishRegisterClicked,OnStartPunchCardClicked, OnEndPunchCardClicked, OnOpenLogcatClicked,OnCloseLogcatClicked) are similar, so I won't repeat them here.

Def getDateAndTime (self):

Get the current date and time and assemble it into a specific format and return it as an exit parameter.

Function call relationship: the arrow points to the callee

Online preview address:

Https://www.processon.com/view/link/5bbe0b0de4b0534c9bfbecb4

Program running result

Program main interface

New entry

We see that there is a repeated warning of face data in the information bar, and this entry is cancelled.

So we deleted the database data and started over.

The following is the printed log information (copied to the screenshot in notepad to ensure the format)

I see that it has been successfully entered.

Start clocking in.

The prompt information is printed as follows

The critical time for being late is 9:00

Show log

Only if you have entered and checked in successfully will you write it to the database, whether you are late or not.

This is the answer to the question about how to use Python code to create a facial scanning attendance system. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report