Welcome folks today in this blog post we will be building a facial recognition attendance system in python using opencv and numpy library. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries using the pip
command as shown below
pip install opencv-python
pip install numpy
pip install face_recognition
Now you need to create the folder where you will store all the images of the students used for attendance system .The directory structure of the project is shown below
app.py
1 2 3 4 5 6 7 8 9 |
import face_recognition import cv2 import numpy as np import csv import glob import os from datetime import datetime video_capture = cv2.VideoCapture(0) |
As you can see first of all we are importing all the libraries required for this project and then we are capturing the user webcam using the opencv library. Inside it we are using the VideoCapture() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
kane_image = face_recognition.load_image_file('images/kane.jpg') kane_encoding = face_recognition.face_encodings(kane_image)[0] southee_image = face_recognition.load_image_file('images/southee.jpg') southee_encoding = face_recognition.face_encodings(southee_image)[0] conway_image = face_recognition.load_image_file('images/conway.png') conway_encoding = face_recognition.face_encodings(conway_image)[0] mithcell_image = face_recognition.load_image_file('images/mitchell.jpg') mitchell_encoding = face_recognition.face_encodings(mithcell_image)[0] known_face_encoding = [ kane_encoding, southee_encoding, conway_encoding, mitchell_encoding ] |
And now basically guys we are converting all the input images to their encodings and storing it inside the respective variables. And then we are storing these encodings inside the array variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
known_face_names = [ "kane williamson", "tim southee", "devon conway", "daryl mitchell" ] students = known_face_names.copy() face_locations = [] face_encodings = [] face_names = [] s=True now = datetime.now() current_date = now.strftime("%Y-%m-%d") f = open(current_date+".csv",'w+',newline='') lnwriter = csv.writer(f) |
And now we are declaring the names of the students inside the array. And then we are declaring some more variables for the face locations and the encodings. And then we are storing all this information inside the csv file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
while True: _,frame = video_capture.read() small_frame = cv2.resize(frame,(0,0),fx=0.25,fy=0.25) rgb_small_frame = small_frame[:,:,::-1] if s: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame,face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(known_face_encoding,face_encoding) name = "" face_distance = face_recognition.face_distance(known_face_encoding,face_encoding) best_match_index = np.argmin(face_distance) if matches[best_match_index]: name = known_face_names[best_match_index] face_names.append(name) if name in known_face_names: if name in students: students.remove(name) print(students) current_time = now.strftime("%H-%M-%S") lnwriter.writerow([name,current_time]) cv2.imshow("Attendance System",frame) if cv2.waitKey(1) & 0XFF == ord('q'): break video_capture.release() cv2.destroyAllWindows() f.close() |
And now inside this while loop we are showing the user webcam by using the read()
method to read the frames of the webcam and then we are training the machine to recognize the faces when they are shown to it. If the face matches then that student will be removed from the list and get inserted into the csv file. At last if all students are removed. You can see the csv file created