Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import face_recognition as fc
import os
import numpy as np
import cv2
from datetime import datetime
path = 'Photos'
photos = []
NameClass = []
ImgList = os.listdir(path)
for cl in ImgList:
curImg = cv2.imread(f'{path}/{cl}')
photos.append(curImg)
NameClass.append(os.path.splitext(cl)[0])
def EncodingsFind(images):
ListEncodes = []
for img in photos:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = fc.face_encodings(img)[0]
ListEncodes.append(encode)
return ListEncodes
def Attendance(name):
with open('AttendanceRecord.csv', 'r+') as f:
DataList = f.readlines()
nameList = []
for line in DataList:
gap = line.split(',')
nameList.append(gap[0])
if name not in nameList:
time = datetime.now()
dtString = time.strftime('%H:%M:%S')
f.writelines(f'\n{name},{dtString}')
ListEncodesKnown = EncodingsFind(photos)
capture = cv2.VideoCapture(0)
while True:
success, img = capture.read()
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesFrame = fc.face_locations(imgS)
encodesCurFrame = fc.face_encodings(imgS, facesFrame)
for encodeFace, faceLoc in zip(encodesCurFrame, facesFrame):
match = fc.compare_faces(ListEncodesKnown, encodeFace)
Distance = fc.face_distance(ListEncodesKnown, encodeFace)
matchIndex = np.argmin(Distance)
if match[matchIndex]:
name = NameClass[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_COMPLEX, 1, (255, 255, 255), 2)
Attendance(name)
cv2.imshow('webcam', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break