# Smart Attendance System Using Face Recognition :::section{.abstract} ## Overview The Smart Attendance System Using Face Recognition is a software application that automates attendance-taking using facial recognition technology. It eliminates the need for **manual attendance-taking**, which is time-consuming and prone to errors. This system enables the recognition of a person's identity using a camera and compares it with the database to record attendance. ::: :::section{.main} ## What are we building? We are building a Smart Attendance System Using Face Recognition that can automatically take attendance using facial recognition technology. The system will use a camera to capture the face of each person and match it with the database to identify them. The system will store attendance records for each person in a excel file and generates a report. ![face-recognition-attendance-system](https://i.imgur.com/0GNy1U0.png) ### Pre-requisites To build this system, we need a basic understanding of programming languages such as **Python and knowledge of facial recognition** technology. Additionally, we require a computer with a webcam, internet connectivity, and a Python IDE. We also need to have libraries like OpenCV, face_recognition, and NumPy pre-installed on our computer. ### How are we going to build this? We will build this face detection attendance system using Python programming language and **facial recognition** technology. We will use the OpenCV library to capture images from a webcam, detect faces, and extract facial features. We will then use the face_recognition library to recognize faces and compare them with the database to identify people. Finally, we will store the attendance records in a database and generate reports using NumPy. ### Final Output ![Sample_Output](https://i.imgur.com/tXvIjBu.png) ::: :::section{.main} ## Requirements **Hardware Requirements:** A computer with a webcam and internet connectivity is needed to run the system. The webcam should have a resolution of at least 720p to capture clear images. **Software Requirements:** The system will be built using Python programming language. The required software includes a Python IDE like PyCharm or Spyder, OpenCV library, face_recognition library, and NumPy library. **Facial Recognition Algorithm:** The system will use a facial recognition algorithm to recognize faces. The algorithm should be able to detect faces and extract facial features accurately. **User Interface:** The system should have a user-friendly interface to capture images, display results, and generate reports. **Attendance Management:** The system should be able to manage attendance records for each person, including the date and time of attendance. Overall, the Smart Attendance System Using Face Recognition should be reliable, efficient, and easy to use. It should automate attendance-taking and provide accurate and timely attendance records. ::: :::section{.main} ## Building the Smart Attendance Management System Using Face Recognition Here's a sample code for building the Smart Attendance Management System Using Face Recognition step by step: ### **Data Aquisition** ```python # Import necessary libraries import cv2 # Initialize the camera cap = cv2.VideoCapture(0) # Capture the image while True: ret, frame = cap.read() cv2.imshow("frame", frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` This code captures an image using the webcam and displays it in a window. Press 'q' to exit the window. ### **Methodology** The overall method combines several **computer vision and deep learning techniques** to perform real-time face recognition and attendance marking. It uses OpenCV's face detection algorithm and a pre-trained deep learning model for face recognition. The attendance log is stored in **JSON format** for easy access and manipulation. The code provides a user-friendly interface by displaying the video stream with the recognized names of individuals and a bounding box around their faces. The entire process is automated and requires minimal manual intervention. By using face recognition technology, the system can accurately recognize individuals and mark their attendance, reducing the chances of errors or malpractices. It is a reliable and efficient way of managing attendance in various settings. ### **Algorithm** The deep learning algorithm that are were using for the smart Attendance Management System is the **face recognition** model. This model uses a **deep convolutional neural network (CNN)** to extract features from facial images and learn to map these features to a unique embedding vector for each individual. The model is trained on a large dataset of facial images using a supervised learning approach, where it learns to minimize the difference between the predicted embedding vector and the true identity of the individual. The pre-trained face recognition model used in the above code is based on the ResNet architecture and has been trained on a large-scale face recognition dataset called VGGFace2. ![face-recognition-cnn-architecture](https://i.imgur.com/3qJDdCa.png) ### **Image Aquisition** ```python # Import necessary libraries import cv2 # Load the image img = cv2.imread("image.jpg") # Convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to the image blur = cv2.GaussianBlur(gray, (5, 5), 0) # Apply adaptive thresholding to the image thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) # Apply morphological operations to the image kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) dilate = cv2.dilate(thresh, kernel, iterations=3) erode = cv2.erode(dilate, kernel, iterations=3) # Display the image cv2.imshow("image", erode) cv2.waitKey(0) cv2.destroyAllWindows() ``` This code loads an image, preprocesses it by converting it to **grayscale,** applying Gaussian blur, adaptive thresholding, and morphological operations. The final processed image is displayed in a window. ### **Dataset Creation** ```python # Import necessary libraries import cv2 import os # Initialize the camera cap = cv2.VideoCapture(0) # Create a directory for storing the dataset if not os.path.exists("dataset"): os.makedirs("dataset") # Create a dataset count = 0 while True: ret, frame = cap.read() cv2.imshow("frame", frame) if cv2.waitKey(1) == ord('q'): break if cv2.waitKey(1) == ord('s'): count += 1 filename = "dataset/user_" + str(count) + ".jpg" cv2.imwrite(filename, frame) cap.release() cv2.destroyAllWindows() ``` This code captures multiple images using the webcam and stores them in a directory called "dataset". ### **Storing the Face Embeddings** ```python # Import necessary libraries import face_recognition import os import numpy as np # Create a directory for storing the embeddings if not os.path.exists("embeddings"): os.makedirs("embeddings") # Load the images image_paths = [os.path.join("dataset", f) for f in os.listdir("dataset")] images = [] for image_path in image_paths: image = face_recognition.load_image_file(image_path) images.append(image) # Compute the face embeddings embeddings = [] for image in images: face_locations = face_recognition.face_locations(image) face_encodings = face_recognition.face_encodings(image, face_locations) if len(face_encodings) == 1: embeddings.append(face_encodings[0]) # Save the embeddings np.savetxt("embeddings/embeddings.txt", embeddings) ``` This code loads the images from the "dataset" directory, computes the face embeddings using the face_recognition library, and stores them in a directory called "embeddings" in a text file named "embeddings.txt". ### **Building the Smart Attendance system using Face Recognition** ```python # Import necessary libraries import cv2 import face_recognition import numpy as np import os import json from datetime import datetime ``` This is the import section of the code where we import all the necessary libraries required for the project. The **cv2 library** is used for capturing video frames from the camera, the **face_recognition library** is used for face detection and recognition, the numpy library is used for scientific computing, the os library is used for working with directories, the **json library** is used for reading and writing data in JSON format, and the datetime library is used for recording the date and time of attendance. ```python # Load the embeddings embeddings = np.loadtxt("embeddings/embeddings.txt") ``` This line of code loads the precomputed embeddings from the "embeddings.txt" file. These embeddings were generated in a previous step using the images in the "dataset" directory. ```python # Initialize the camera cap = cv2.VideoCapture(0) ``` This line of code initializes the camera for capturing frames. The argument "0" specifies that the default camera should be used. If you have multiple cameras connected to your computer, you can change this value to select a different camera. ```python # Load the attendance log if not os.path.exists("attendance.json"): with open("attendance.json", "w") as f: json.dump({}, f) with open("attendance.json", "r") as f: attendance = json.load(f) ``` This code block loads the attendance log from the "attendance.json" file. If the file doesn't exist, it creates an empty file and initializes it with an empty JSON object. ```python # Recognize the faces and mark attendance while True: ret, frame = cap.read() face_locations = face_recognition.face_locations(frame) face_encodings = face_recognition.face_encodings(frame, face_locations) for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognition.compare_faces(embeddings, face_encoding) if True in matches: index = matches.index(True) name = "user_" + str(index+1) if name not in attendance: attendance[name] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open("attendance.json", "w") as f: json.dump(attendance, f) cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(frame, name, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) cv2.imshow("frame", frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` This code block is the heart of the attendance system. It captures frames from the camera, detects faces in the frames, compares the detected faces with the precomputed embeddings, and marks the attendance of the recognized faces. * The **while True:** loop runs indefinitely, capturing frames from the camera and processing them. * The **cap.read() function** reads a frame from the camera and returns a boolean value (ret) and the frame itself (frame). * The **face_recognition.face_locations()** function detects the locations of all the faces in the frame. * The **face_recognition.face_encodings()** function generates embeddings for all the faces in the frame. * The **zip()** function combines the face locations and embeddings into tuples that we can iterate over. * The **face_recognition.compare_faces()** function compares the embeddings of the detected faces with the precomputed embeddings and returns a list of boolean values indicating whether each detected face matches any of the precomputed embeddings. * If a detected face matches a precomputed embedding, the **matches.index(True)** function returns the index of the matching embedding, which we use to determine the name of the recognized user. * If the user's name is not already in the attendance log, we add a new entry with the current date and time using **datetime.now().strftime("%Y-%m-%d %H:%M:%S")**. * Finally, we draw a green rectangle around the detected face and display the name of the recognized user above the rectangle using **cv2.rectangle()** and **cv2.putText()**. * The **cv2.imshow()** function displays the processed frame on the screen. * The **cv2.waitKey(1)** function waits for a key press. If the pressed key is 'q', the break statement exits the while loop. * The **cap.release()** function releases the camera and frees up system resources. * The **cv2.destroyAllWindows()** function destroys all the windows created by OpenCV. * Overall, this code block performs the real-time face recognition and attendance marking by detecting faces, comparing them with the precomputed embeddings, and adding new entries to the attendance log if necessary. ### **Updating the attendance to an excel file** Here's an example code snippet to store the attendance data into an Excel file using the Pandas library: ```python import pandas as pd # Load the attendance log from the JSON file with open('attendance.json', 'r') as f: attendance_log = json.load(f) # Convert the attendance log to a Pandas dataframe df = pd.DataFrame(attendance_log) # Write the attendance data to an Excel file writer = pd.ExcelWriter('attendance_log.xlsx') df.to_excel(writer, index=False) writer.save() ``` This code first loads the attendance log from the JSON file using the json library, then converts the log to a Pandas dataframe using the **pd.DataFrame()** function. The df dataframe is then written to an Excel file using the **pd.ExcelWriter() and df.to_excel()** functions, and finally saved using the **writer.save()** function. The **index=False parameter** in **to_excel()** is used to exclude the row index from the output. ::: :::section{.main} ## Output ![Sample_Output](https://i.imgur.com/tXvIjBu.png) ![Spreadsheet-with-attendance](https://i.imgur.com/R4ZmqTa.png) ::: :::section{.summary} ## Conclusion * In conclusion, the Smart Attendance Management System using Face Recognition is a highly innovative and efficient solution for attendance management in various institutions. * The system uses state-of-the-art computer vision and deep learning algorithms to accurately recognize individuals and mark their attendance in real-time. * This eliminates the need for manual attendance management, which is prone to errors and can be time-consuming. * The project also offers a user-friendly interface that displays live video streams and attendance logs, making it easy to use and understand. * Overall, this project has great potential to revolutionize attendance management systems in various institutions and improve their efficiency and accuracy. ## MCQs **1. What is the purpose of the Smart Attendance Management System using Face Recognition?** a) To automate the attendance management process b) To store images of individuals c) To control access to a building d) To monitor employee performance **Answer: a) To automate the attendance management process** **2. What deep learning algorithm is used in the Smart Attendance Management System using Face Recognition?** a) Convolutional Neural Network (CNN) b) Recurrent Neural Network (RNN) c) Generative Adversarial Network (GAN) d) Support Vector Machine (SVM) **Answer: a) Convolutional Neural Network (CNN)** **3. What does the attendance log in the Smart Attendance Management System contain?** a) The name and time of attendance of each recognized individual b) A record of all images captured by the camera c) A list of all individuals who were not recognized d) A report of all individuals who arrived late **Answer: a) The name and time of attendance of each recognized individual** :::