This system automatically:
✔ Detects faces
✔ Recognizes known students/employees
✔ Marks attendance into CSV
✔ Stores date/time for each entry
✔ Prevents duplicate attendance for the same day
Perfect for office, classroom, labs, events, coaching centers, etc.
🧠 Tech Stack
| Component | Library |
|---|---|
| Face Detection | OpenCV |
| Face Recognition | face_recognition |
| Data Storage | CSV |
| Camera Capture | OpenCV |
| Encoding Faces | dlib (used internally by face_recognition) |
⚙️ Install Required Libraries
Run:
pip install opencv-python
pip install face_recognition
pip install numpy
pip install pandas
👉 face_recognition requires CMake + dlib
If error comes, I will give you auto installer for dlib.
📁 Folder Structure
FaceAttendance/
│
├── images/
│ ├── Sameer.jpg
│ ├── John.jpg
│ └── Ayesha.jpg
│
├── attendance.csv
│
└── face_attendance.py
You must store clear front-facing photos in /images.
🧩 FULL WORKING CODE (face_attendance.py)
import cv2
import numpy as np
import face_recognition
import os
import pandas as pd
from datetime import datetime
path = 'images'
images = []
classNames = []
myList = os.listdir(path)
print("Loading images...")
for cls in myList:
curImg = cv2.imread(f'{path}/{cls}')
images.append(curImg)
classNames.append(os.path.splitext(cls)[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
def markAttendance(name):
df = pd.read_csv('attendance.csv')
today = datetime.now().strftime('%Y-%m-%d')
time_now = datetime.now().strftime('%H:%M:%S')
if not ((df['Name'] == name) & (df['Date'] == today)).any():
new_entry = pd.DataFrame([[name, today, time_now]],
columns=['Name', 'Date', 'Time'])
df = pd.concat([df, new_entry], ignore_index=True)
df.to_csv('attendance.csv', index=False)
print(f"Attendance marked for {name}")
else:
print(f"{name} already marked today")
encodeListKnown = findEncodings(images)
print("Encoding Complete")
cap = cv2.VideoCapture(0)
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), (x2, y2+35), (0,255,0), cv2.FILLED)
cv2.putText(img, name, (x1+6, y2+28), cv2.FONT_HERSHEY_SIMPLEX,
1, (255,255,255), 2)
markAttendance(name)
cv2.imshow('Face Attendance', img)
if cv2.waitKey(1) == 27:
break
📄 Create attendance.csv (Empty File)
Create attendance.csv with this header:
Name,Date,Time
🎥 How It Works
- Loads all face images from
/images/ - Converts them into encodings
- Starts webcam
- Detects a face
- Compares with known encodings
- Identifies person
- Marks attendance in
attendance.csv - Prevents duplicate attendance for the same day

Leave a Reply