当前位置:网站首页>Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
2022-07-03 17:43:00 【lyx4949】
This project combines the previous article be based on face_recognition The library realizes face recognition , By using Python Of tkinter Module to design the graphical interface of the attendance system , In addition, the combination of openpyxl modular , Save the student's information and attendance clock data to the table , Easy to view and call . The system has “ Brush your face and punch in ”、“ New student registration ” And so on .
One 、 Interface effect
Press... Respectively on the main interface “ New student registration ” Button and “ Brush your face and punch in ” The process effect of the button is shown in the figure :
Two 、 development environment
Windows10+python3.5.5+Vs Code( development tool )
3、 ... and 、 The development of preparation
Create a new workbook on the desktop , Name it Data.xlsx( See the end of the article , Links to complete procedures and forms are attached ) There are two small tables , Namely “ Student information sheet ” and “ Student attendance sheet ”, Fill in the following information in the first and second columns . See the link at the end of the article .
Four 、 Implementation process
1. Import related library functions
Be careful face_recognition If the library doesn't have it, it needs pip install face_recognition Installation .
import cv2 #OpenCV library
import openpyxl #openpyxl library
import os # Operating system related libraries , Standard library
import time #time library
import tkinter.messagebox #messagebox Message box module
from tkinter import * #tkinter library
import face_recognition
2. Main interface programming
*( The main interface program is placed at the end , This is explained in advance )* The main interface has a “ Brush your face and punch in ”、“ New student registration ”、“ Exit the system ” Button .
# Main interface program
top = Tk()
top.title(" Time Attendance ") # Window title
top.geometry("500x300") # Window size
# Framework components of the main interface main_frame
main_frame = Frame(top) # First floor frame components
main_frame.place(relwidth=1,relheight= 1) # Parameter set to 1 It means that the frame covers the whole window
Label(main_frame,text = " Intelligent classroom attendance system ",bg = "white",font = (" In black ",20),
width = 35,height = 2).place(x = 0,y = 0) # place “ Intelligent classroom attendance system ” label
Button(main_frame,text = " Brush your face and punch in ",bg = "green",font = (" In black ",12),width = 12,height = 2,
command = take_photo).place(x = 30,y = 150) # place “ Brush your face and punch in ” Button
Button(main_frame,text = " New student registration ",bg = "green",font = (" In black ",12),width = 12,height = 2,
command = register).place(x = 200,y = 150) # place “ New student registration ” Button
Button(main_frame,text = " Exit the system ",bg = "green",font = (" In black ",12),width = 12,height = 2,
command = top.destroy).place(x =360,y = 150) # place “ Exit the system ” Button ,destroy() Directly destroy the window
# Define file path
path = "C:/Users/Administrator/Desktop/face/" #face Folder
path0 = "C:/Users/Administrator/Desktop/" # desktop
top.mainloop() # Enter wait and process window events
3. Photo function get_photo()
This function is used to call notebook camera or other external camera . Turn on the camera , And then through capture.read() Get one of the frames , adopt cv2.imwrite() Save the image to the specified path , Parameters img Is the name of the picture taken , It also includes the designated route .
# Take photos function
def get_photo(img): #i Parameters mg, Is the name of the picture taken , It also includes the designated route
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW) # Turn on the camera , Parameters 0 It's the camera port , Replaceable
ref,frame=capture.read() # Save a frame of data in the camera
cv2.imwrite(img,frame) # Save the picture
cv2.imshow(img,frame) # Window display
cv2.waitKey(1000) # Time delay 1000ms, namely 1s
cv2.destroyAllWindows() # close window
return img # Return image data
4. Brush face punch function take_photo()
Press... On the main interface “ Brush your face and punch in ” After button , Jump to this function . By calling get_photo() Function to take a temporary picture of a student brushing his face , After image processing and encoding the picture , adopt os.listdir() Methods through face The pictures in the folder ( These pictures also need image processing and coding ), And then through face_recognition.compare_faces Compare faces , Face comparison succeeded , Get the number of the picture , By numbering num Locate the position of the verifier in the table , Then fill in the clock in time and date in the form . If the verification fails, the face flag bit flag by 0
# Brush face punch function
def take_photo():
print(" Brush your face and punch in ")
student = face_recognition.load_image_file(get_photo(path0 + "img.jpg" )) # Load face image , Parameter to call the photographing function , Get an image of the verifier
student_rgb =cv2.cvtColor(student,cv2.COLOR_BGR2RGB) # Image type conversion function
student_encode = face_recognition.face_encodings(student_rgb)[0] # Given an image , Returns each face in the image 128 Dimensional face coding
# return face What's in the folder , To delete a face from a face database face In the folder , Get the identity of the verifier
pathDir = os.listdir(path) # Each picture in the folder is returned
flag = 0 # Face marker
# Test another image
for i in pathDir: # Traverse 6 A picture
face = face_recognition.load_image_file(path + i) # Open every picture in the folder
face_rgb = cv2.cvtColor(face,cv2.COLOR_BGR2RGB) # Convert every picture in the folder
face_encode = face_recognition.face_encodings(face_rgb)[0] # Encode the image
#compare_faces Compare the face coding list with face Candidate codes in the folder , See if they match , Match successfully returns [True]
s = face_recognition.compare_faces([student_encode],face_encode,tolerance=0.49) # Parameters tolerance It is the distance between two faces that counts as a match . The smaller the value, the stricter the comparison ,0.49 It's the best one I measured
if s == [True] : # If the match is successful, the result is a Boolean list True
flag = 1 # If successful, assign the flag bit to 1
num = i[:len(i)-4] # Get the number of the picture , Get rid of “.jpg”. For example, photos “7.jpg”, here num Namely 7, Is the number of the registrant
break # Jump straight out of the loop
else:
flag = 0 # If no match is successful, the flag bit is 0
if flag ==1: # By numbering num Find the location of the verifier in the table
# Form operation
sheets = openpyxl.load_workbook(path0 + "Data.xlsx") # open “ Student information sheet ” Workbooks
sheet1 = sheets[sheets.sheetnames[0]] # obtain Data The first table in the workbook “ Student information sheet ”
max_row1 = sheet1.max_row # Get the number of rows in the last row of data in the first table
print(" The number of the verifier is :" , num) # Output comparison results
#print(type(num),type(sheet1.cell(max_row1,1).value))
for j in range(2,max_row1+1): # Traverse ‘ Student information sheet ’
if sheet1.cell(j,1).value == str(num): # Locate the position of the corresponding student in the table according to the number , For example, Jack's number is 1,num=1, from “ Student information sheet ” The first column found in is 1 That line j, That's the second line
name = sheet1.cell(j,2).value # Get student name
# Get the current clock in date 、 Time , Statistics to the student attendance sheet
date = time.strftime("%Y-%m-%d",time.localtime()) # Date of the day
Time = time.strftime("%H:%M",time.localtime()) # Check in time
# Open the student attendance sheet , Add the student's clock in date to the workbook
sheet2 = sheets[sheets.sheetnames[1]] # obtain Data The second table in the workbook “ Student attendance sheet ”
max_row2 = sheet2.max_row # Get the number of rows in the last row of the table with data
sheet2.cell(row = max_row2+1,column=1,value = date) # Write the sign in date
sheet2.cell(row = max_row2+1,column=2,value = name) # Write the students who sign in
sheet2.cell(row = max_row2+1,column=3,value = Time) # Write the check-in time
sheets.save(path0 + "Data.xlsx") # Save to Data Workbooks
print(str(sheet1.cell(j,2).value) +" Sign in successfully ," + " The clock in time is " + Time) # Broadcast sign in
tkinter.messagebox.showinfo(title=" Tips ",message=name + " Successful punch in ,"+ " The clock in time is " + Time) # Message box
break # Jump out of the current traversal loop
5. Register function register()
Press... On the main interface “ New student registration ” After button , Jump to this function . The implementation covers the original framework components main_frame, Layout new frame components second_frame, Used to place the buttons needed for the new interface 、 Tag components .“ confirm ” Button Association Message function , Used to confirm 、 Save the registration information of new students
# Register function
def register():
print(" register ")
main_frame.place_forget() # hide main_frame frame
second_frame = Frame(top) # Define the second framework second_frame, For the registration interface
second_frame.place(relwidth=1,relheight= 1)
# Lay out the components of the new interface : label , Enter text box , Button
Label(second_frame,text = " Welcome to face recognition system ",font = (" In black ",18),
width = 40,height = 2).place(x = 20,y = 0)
Label(second_frame,text = " full name ",font = (" In black ",12),
width = 20,height = 1).place(x = 50,y = 100) # Prompt text “ full name ”
name_entry = Entry(second_frame, font=(" In black ", 12), width=20)
name_entry.place(x = 240,y = 100) # Text box components , Used to fill in “ full name ”
Button(second_frame,text = " confirm ",font = (" In black ",12), width=10,
command=lambda:Message(name_entry)).place(x = 100,y = 150) # confirm button
Button(second_frame,text = " return ",font = (" In black ",12), width=10,
command=lambda:back_main(second_frame)).place(x = 300,y = 150) # Return button
6. Student information storage Message()
Click on “ confirm ” After button , This function operates the table to obtain the number of rows with the last row of data in the table max_row, As the serial number of newly registered students , As shown in the figure below . Name the freshman picture with this number “max_row.jpg”, Then call the camera to take pictures of the new students and save them to face In the folder . Put the student's name 、 Serial number filled in to “ Student information ” In the table
# Student information storage
def Message(name_entry):
# Form operation
sheets = openpyxl.load_workbook(path0 + "Data.xlsx") # open “ Student information sheet ” Workbooks
sheet1 = sheets[sheets.sheetnames[0]] # obtain Data The first table in the workbook “ Student information sheet ”
max_row1 = sheet1.max_row # Get the number of rows in the last row of data in the first table
print(" Lines :",max_row1)
name = name_entry.get() # utilize get() Method to get Entry What is entered in the component
tkinter.messagebox.showinfo(title=" Tips ",message=" Start entering face information !") # Message box
image = str(max_row1)+".jpg" # Get the number of rows from the table , Number the photos
get_photo(path + image) # Get photos of new students , Save to face Folder
# Fill in the student information , Include serial number 、 full name
sheet1.cell(max_row1 + 1,1).value = str(max_row1) # Student serial number , Photo number
print(type(sheet1.cell(max_row1 + 1,1).value))
sheet1.cell(max_row1 + 1,2).value = name # The student's name
sheets.save(path0 + "Data.xlsx")
print(" Registered successfully , Your number is "+ str(max_row1)+" Number ")
tkinter.messagebox.showinfo(title=" Tips ",message=" To complete the registration !") # Message box
7. Return button back_main() function
# Return button , From the current interface (second_frame) Back to the main interface (main_frame)
def back_main(second_frame):
second_frame.place_forget() # hide second_frame frame
main_frame.place(relwidth=1,relheight= 1) # Show main_frame frame
5、 ... and 、 remarks
Data Workbook and completion program link
Extraction code :lyx4
边栏推荐
- c# . Net tool ecosystem
- STM32实现74HC595控制
- 毕业总结
- Interviewer: why is the value nil not equal to nil?
- Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
- MinGW compile boost library
- AcWing 3438. 数制转换
- PHP processing - watermark images (text, etc.)
- PS screen printing brush 131, many illustrators have followed suit
- Golang unit test, mock test and benchmark test
猜你喜欢
Talk about the design and implementation logic of payment process
鸿蒙第四次培训
How to read the source code [debug and observe the source code]
Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
Automata and automatic line of non-standard design
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
1164 Good in C
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
随机推荐
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
POM in idea XML graying solution
Leetcode 108 converts an ordered array into a binary search tree -- recursive method
Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
link preload prefetch
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
WEB-UI自动化测试-最全元素定位方法
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
鸿蒙第四次培训
[combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
Detailed explanation of common network attacks
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
How to purchase Google colab members in China
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
vs2013已阻止安装程序,需安装IE10
How to read the source code [debug and observe the source code]
MySQL grouping query
Brief introduction to the core functions of automatic penetration testing tool
How to train mask r-cnn model with your own data
Inheritance of ES6 class