Face Recognition Attendance Code
Face Recognition Attendance 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]
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)
if matches[matchIndex]:
name = studentNames[matchIndex].upper()
markAttendance(name)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()