Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Attendance register - using face detection - complete project and source code

Creating an attendance system using face detection involves several steps, including setting up the environment, capturing and processing images, recognizing faces, and storing attendance records. Here’s a complete project outline and the source code for an attendance system using face detection with Python.

open_cv_face_detection_project.png (36 KB)
### Project Outline

1. **Install Necessary Libraries**:
    - OpenCV: For image processing and face detection.
    - dlib: For face recognition.
    - face_recognition: A simple face recognition library.
    - pandas: For handling attendance records.
    - NumPy: For numerical operations.

2. **Dataset Preparation**:
    - Collect images of individuals whose attendance you want to track.
    - Organize the images in a directory with each sub-directory named after the person.

3. **Face Encoding**:
    - Encode faces from the images and store these encodings.

4. **Face Detection and Recognition**:
    - Capture live video stream from a webcam.
    - Detect faces in the video frames.
    - Compare the detected faces with the stored encodings to recognize individuals.

5. **Attendance Logging**:
    - Log the attendance of recognized individuals in a CSV file.

### Source Code

Below is the source code for the face detection attendance system:

```python

import cv2

import numpy as np

import face_recognition

import os

from datetime import datetime

import pandas as pd


# Step 1: Load images and create encodings

path = 'images'  # Directory containing images

images = []

classNames = []

myList = os.listdir(path)


for cl in myList:

    curImg = cv2.imread(f'{path}/{cl}')

    images.append(curImg)

    classNames.append(os.path.splitext(cl)[0])


def findEncodings(images):

    encodeList = []

    for img in images:

        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        encode = face_recognition.face_encodings(img)[0]

        encodeList.append(encode)

    return encodeList


encodeListKnown = findEncodings(images)


# Step 2: Initialize video capture

cap = cv2.VideoCapture(0)


# Step 3: Mark attendance

def markAttendance(name):

    df = pd.read_csv('Attendance.csv')

    now = datetime.now()

    dtString = now.strftime('%H:%M:%S')

    dateString = now.strftime('%d/%m/%Y')

    

    if name not in df['Name'].values:

        df = df.append({'Name': name, 'Date': dateString, 'Time': dtString}, ignore_index=True)

        df.to_csv('Attendance.csv', index=False)

    else:

        # If name exists, update the time for today

        df.loc[df['Name'] == name, 'Time'] = dtString

        df.to_csv('Attendance.csv', index=False)


while True:

    success, img = cap.read()

    imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)

    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

    

    facesCurFrame = face_recognition.face_locations(imgS)

    encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)

    

    for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):

        matches = face_recognition.compare_faces(encodeListKnown, encodeFace)

        faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)

        

        matchIndex = np.argmin(faceDis)

        

        if matches[matchIndex]:

            name = classNames[matchIndex].upper()

            y1, x2, y2, x1 = faceLoc

            y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4

            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

            cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)

            cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

            markAttendance(name)

    

    cv2.imshow('Webcam', img)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break


cap.release()

cv2.destroyAllWindows()

```


### Instructions to Run the Project

1. **Install Required Libraries**:
 
  ```bash

    pip install opencv-python numpy face_recognition pandas

    ```


2. **Prepare Dataset**:
    - Create a folder named `images`.
    - Add subfolders for each individual with their respective images.

3. **Create an Attendance File**:
    - Create a CSV file named `Attendance.csv` with columns: `Name`, `Date`, `Time`.

4. **Run the Code**:
    - Save the code in a Python file, e.g., `attendance.py`.
    - Run the script using `python attendance.py`.

5. **Using the System**:
    - The system will use the webcam to capture faces and mark attendance for recognized individuals in the CSV file.

This project can be further enhanced by adding features like email notifications, a graphical user interface, or integrating with a database for better record management.

caa May 23 2024 291 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 11
Newest Member: Jhilam