Skip to content

Commit 3dd3991

Browse files
authored
Update main.py
1 parent 20dc48f commit 3dd3991

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

attendance-automator/main.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
import cv2
2-
from face_recognition.api import face_encodings
32
import numpy as np
43
import face_recognition
54
import os
65
from datetime import datetime
76

8-
path= 'images'
9-
10-
images=[]
11-
12-
personName=[]
13-
14-
myList=os.listdir(path)
7+
path = 'images'
8+
images = []
9+
personName = []
10+
myList = os.listdir(path)
1511

1612
print(myList)
1713

1814
for cu_img in myList:
19-
current_Img=cv2.imread(f'{path}/{cu_img}')
15+
current_Img = cv2.imread(f'{path} / {cu_img}')
2016
images.append(current_Img)
2117
personName.append(os.path.splitext(cu_img)[0])
2218

2319
print(personName)
2420

2521

2622
def faceEncodings(images):
27-
encodeList=[]
23+
encodeList = []
2824

2925
for img in images:
30-
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
31-
encode=face_recognition.face_encodings(img)[0]
26+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
27+
encode = face_recognition.face_encodings(img)[0]
3228
encodeList.append(encode)
3329

3430
return encodeList
3531

36-
encodeListKnown = (faceEncodings(images))
3732

33+
encodeListKnown = (faceEncodings(images))
3834
print("All Encoding Complete!!!!!")
3935

4036

4137
def attendance(name):
42-
with open('Attendance.csv','r+') as f:
38+
with open('Attendance.csv', 'r+') as f:
4339
myDataList = f.readlines()
4440
nameList = []
4541
for line in myDataList:
@@ -50,37 +46,35 @@ def attendance(name):
5046
time_now = datetime.now()
5147
tStr = time_now.strftime('%H:%M:%S')
5248
dStr = time_now.strftime('%d/%m/%Y')
53-
f.writelines(f'{name},{tStr},{dStr}')
49+
f.writelines(f'{name}, {tStr}, {dStr}')
5450

5551

56-
cap= cv2.VideoCapture(0)
52+
cap = cv2.VideoCapture(0)
5753

5854
while True:
5955

60-
ret,frame = cap.read()
61-
faces = cv2.resize(frame,(0,0),None,0.25,0.25)
62-
faces= cv2.cvtColor(faces,cv2.COLOR_BGR2RGB)
63-
56+
ret, frame = cap.read()
57+
faces = cv2.resize(frame, (0, 0), None, 0.25, 0.25)
58+
faces = cv2.cvtColor(faces, cv2.COLOR_BGR2RGB)
6459

6560
facesCurrentFrame = face_recognition.face_locations(faces)
66-
encodesCurrentFrame = face_recognition.face_encodings(faces,facesCurrentFrame)
61+
encodesCurrentFrame = face_recognition.face_encodings(faces, facesCurrentFrame)
6762

68-
69-
for encodeFace,faceLoc in zip(encodesCurrentFrame, facesCurrentFrame):
70-
matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
71-
faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
63+
for encodeFace, faceLoc in zip(encodesCurrentFrame, facesCurrentFrame):
64+
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
65+
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
7266

7367
matchIndex = np.argmin(faceDis)
7468

7569
if matches[matchIndex]:
7670
name = personName[matchIndex].upper()
77-
# print(name)
78-
y1,x2,y2,x1 = faceLoc
7971

80-
y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
81-
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)
82-
cv2.rectangle(frame,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
83-
cv2.putText(frame,name,(x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX,1,(255,0,0), 1)
72+
y1, x2, y2, x1 = faceLoc
73+
74+
y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
75+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
76+
cv2.rectangle(frame, (x1, y2-35), (x2, y2), (0, 255, 0), cv2.FILLED)
77+
cv2.putText(frame, name, (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 1)
8478
attendance(name)
8579
cv2.imshow("Camera", frame)
8680

0 commit comments

Comments
 (0)