-
Notifications
You must be signed in to change notification settings - Fork 1
/
Recognize_Faces.py
69 lines (48 loc) · 1.81 KB
/
Recognize_Faces.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
from keras.models import load_model
import cv2
import numpy as np
from datetime import datetime
model = load_model('model-011.model')
face_clsfr=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
source=cv2.VideoCapture(0)
#data_path='Training'
#categories=os.listdir(data_path)
#labels=[i for i in range(len(categories))]
#label_dict=dict(zip(categories,labels)) #empty dictionary
#print(labels)
#print(label_dict)
labels_dict={0:'BradPitt',1:'LeoDecap'}
#color_dict={0:(0,255,0)}
def MarkAttendance(name):
with open('Attendance.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}')
while (True):
ret, img = source.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_clsfr.detectMultiScale(gray, 1.5, 5)
for (x, y, w, h) in faces:
face_img = gray[y:y + w, x:x + w]
resized = cv2.resize(face_img, (100, 100))
normalized = resized / 255.0
reshaped = np.reshape(normalized, (1, 100, 100, 1))
result = model.predict(reshaped)
label = np.argmax(result, axis=1)[0]
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
#cv2.rectangle(img, (x, y - 40), (x + w, y), color_dict[label], -1)
cv2.putText(img, labels_dict[label], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
MarkAttendance(labels_dict[label])
cv2.imshow('LIVE', img)
key = cv2.waitKey(1)
if (key == 27):
break
cv2.destroyAllWindows()
source.release()