0% found this document useful (0 votes)
9 views2 pages

Face Recognition Attendance Code

The document contains Python code for a Face Recognition Attendance System that utilizes the face_recognition and OpenCV libraries. It captures video input, recognizes faces, and marks attendance by writing names and timestamps to a CSV file. The system processes images of students stored in a specified directory and displays the attendance camera feed with recognized names.

Uploaded by

haiderkazmi3708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Face Recognition Attendance Code

The document contains Python code for a Face Recognition Attendance System that utilizes the face_recognition and OpenCV libraries. It captures video input, recognizes faces, and marks attendance by writing names and timestamps to a CSV file. The system processes images of students stored in a specified directory and displays the attendance camera feed with recognized names.

Uploaded by

haiderkazmi3708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Face Recognition Attendance System (Python Code)

import face_recognition
import cv2
import numpy as np
import os
from datetime import datetime

path = 'Students'
images = []
studentNames = []
myList = os.listdir(path)

for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
studentNames.append(os.path.splitext(cl)[0])

def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
enc = face_recognition.face_encodings(img)
if enc:
encodeList.append(enc[0])
return encodeList

encodeListKnown = findEncodings(images)

def markAttendance(name):
with open('Attendance.csv', 'r+') as f:
data = f.readlines()
namesInFile = [line.split(',')[0] for line in data]

if name not in namesInFile:


now = datetime.now()
timeStr = now.strftime('%H:%M:%S')
f.write(f'{name},{timeStr}\n')

cap = cv2.VideoCapture(0)

while True:
success, img = cap.read()
imgSmall = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)
imgSmall = cv2.cvtColor(imgSmall, cv2.COLOR_BGR2RGB)

facesCurFrame = face_recognition.face_locations(imgSmall)
encodesCurFrame = face_recognition.face_encodings(imgSmall, facesCurFrame)

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


matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
faceDist = face_recognition.face_distance(encodeListKnown, encodeFace)
matchIndex = np.argmin(faceDist)

if matches[matchIndex]:
name = studentNames[matchIndex].upper()
markAttendance(name)

y1, x2, y2, x1 = [val * 4 for val in faceLoc]


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)

cv2.imshow('Attendance Camera', img)

if cv2.waitKey(1) == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

You might also like