0% found this document useful (0 votes)
35 views3 pages

Face Recognition

This document contains code for face recognition and attendance tracking. It uses libraries like OpenCV, Dlib and Face Recognition to detect faces in images, encode facial features and compare faces. The code loads images of known users, encodes their faces and saves the encodings. For live video frames, it detects faces, encodes them and matches against the known encodings to identify users and mark their attendance by writing to a CSV file. Rectangle boxes are drawn around detected faces and names are displayed.

Uploaded by

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

Face Recognition

This document contains code for face recognition and attendance tracking. It uses libraries like OpenCV, Dlib and Face Recognition to detect faces in images, encode facial features and compare faces. The code loads images of known users, encodes their faces and saves the encodings. For live video frames, it detects faces, encodes them and matches against the known encodings to identify users and mark their attendance by writing to a CSV file. Rectangle boxes are drawn around detected faces and names are displayed.

Uploaded by

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

********************************************FACE_RECOGNITION_AND_ATTENDENCE********

****************************************

Library that used in this program and essential


1.C++ Developer Enviroment
2.Cmake
3.Dlib
4.Face-recognition
5.Numpy
6.OpenCV-Python
-----------------------------------------------------------------------------------
-----------------------------------------
1. File_Name Main.py

-----------------------------------------------------------------------------------
----------------------------------------
import cv2
import numpy as np
import face_recognition

imgRdj = face_recognition.load_image_file('Images/rdj2.jpg')
imgRdj = cv2.cvtColor(imgRdj,cv2.COLOR_BGR2RGB)
imgtest = face_recognition.load_image_file('Images/rock.jpg')

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

faceLoc = face_recognition.face_locations(imgRdj)[0]
encodeRdj = face_recognition.face_encodings(imgRdj)[0]
cv2.rectangle(imgRdj,(faceLoc[3],faceLoc[0]),(faceLoc[1],faceLoc[2]),(255,0,255),2)

faceLocTest = face_recognition.face_locations(imgtest)[0]
encodeTest = face_recognition.face_encodings(imgtest)[0]
cv2.rectangle(imgtest,(faceLocTest[3],faceLocTest[0]),
(faceLocTest[1],faceLocTest[2]),(255,0,255),2)

results = face_recognition.compare_faces([encodeRdj],encodeTest)
faceDis = face_recognition.face_distance([encodeRdj],encodeTest)
print(results, faceDis)
cv2.putText(imgtest,f'{results} {round(faceDis[0],2)}',
(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)

cv2.imshow('rdj2',imgRdj)
cv2.imshow('rddw',imgtest)
cv2.waitKey(0)
# cv2.destroyAllWindows()

-----------------------------------------------------------------------------------
-----------------------------------------

2. Second_File Attendence.py

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

path = 'AttendenceImage'
images = []
classNamaes = []
myList = os.listdir(path)
print(myList)
for cls in myList:
curImg = cv2.imread(f'{path}/{cls}')
images.append(curImg)
classNamaes.append(os.path.splitext(cls)[0])
print(classNamaes)

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

def markAttendence(name):
with open('Attendence.csv','r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now = datetime.now()
dtString = now.strftime('%H:%M:%S')
f.writelines(f'\n{name},{dtString}')

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)
encodeCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)

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


matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
# print(faceDis)
matchIndex = np.argmin(faceDis)

if matches[matchIndex]:
name = classNamaes[matchIndex].upper()
# print(name)
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_COMPLEX,1,
(255,255,255),2)
markAttendence(name)

cv2.imshow('webcam',img)
cv2.waitKey(1)
-----------------------------------------------------------------------------------
-----------------------------------------

You might also like