1
1
import cv2
2
- from face_recognition .api import face_encodings
3
2
import numpy as np
4
3
import face_recognition
5
4
import os
6
5
from datetime import datetime
7
6
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 )
15
11
16
12
print (myList )
17
13
18
14
for cu_img in myList :
19
- current_Img = cv2 .imread (f'{ path } / { cu_img } ' )
15
+ current_Img = cv2 .imread (f'{ path } / { cu_img } ' )
20
16
images .append (current_Img )
21
17
personName .append (os .path .splitext (cu_img )[0 ])
22
18
23
19
print (personName )
24
20
25
21
26
22
def faceEncodings (images ):
27
- encodeList = []
23
+ encodeList = []
28
24
29
25
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 ]
32
28
encodeList .append (encode )
33
29
34
30
return encodeList
35
31
36
- encodeListKnown = (faceEncodings (images ))
37
32
33
+ encodeListKnown = (faceEncodings (images ))
38
34
print ("All Encoding Complete!!!!!" )
39
35
40
36
41
37
def attendance (name ):
42
- with open ('Attendance.csv' ,'r+' ) as f :
38
+ with open ('Attendance.csv' , 'r+' ) as f :
43
39
myDataList = f .readlines ()
44
40
nameList = []
45
41
for line in myDataList :
@@ -50,37 +46,35 @@ def attendance(name):
50
46
time_now = datetime .now ()
51
47
tStr = time_now .strftime ('%H:%M:%S' )
52
48
dStr = time_now .strftime ('%d/%m/%Y' )
53
- f .writelines (f'{ name } ,{ tStr } ,{ dStr } ' )
49
+ f .writelines (f'{ name } , { tStr } , { dStr } ' )
54
50
55
51
56
- cap = cv2 .VideoCapture (0 )
52
+ cap = cv2 .VideoCapture (0 )
57
53
58
54
while True :
59
55
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 )
64
59
65
60
facesCurrentFrame = face_recognition .face_locations (faces )
66
- encodesCurrentFrame = face_recognition .face_encodings (faces ,facesCurrentFrame )
61
+ encodesCurrentFrame = face_recognition .face_encodings (faces , facesCurrentFrame )
67
62
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 )
72
66
73
67
matchIndex = np .argmin (faceDis )
74
68
75
69
if matches [matchIndex ]:
76
70
name = personName [matchIndex ].upper ()
77
- # print(name)
78
- y1 ,x2 ,y2 ,x1 = faceLoc
79
71
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 )
84
78
attendance (name )
85
79
cv2 .imshow ("Camera" , frame )
86
80
0 commit comments