-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRecognizer.py
99 lines (74 loc) · 2.86 KB
/
Recognizer.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import cv2
from FaceDetection.face_detection import face
from keras.models import load_model
import numpy as np
import os
from embedding import emb
from MongoDB.retrieve_pymongo_data import database
import warnings
warnings.filterwarnings("ignore")
def Recognition(subject):
label=None
people=sorted(os.listdir('people'))
lecture=subject
person=None
def Create_labels():
people=sorted(os.listdir('people'))
students={}
attendance_count={}
for i in people:
students[(int(i[0])-1)]=i[1:]
attendance_count[(int(i[0])-1)]=0
return students,attendance_count
people,attendance_count=Create_labels()
completed_label="Attendance is Completed"
e=emb()
fd=face()
model=load_model('Model/Face_recognition.MODEL')
data=database() ##### Intitalising the Mongo Database
color=(0, 255, 0)
cap=cv2.VideoCapture(0)
ret=True
while ret:
ret,frame=cap.read()
frame=cv2.flip(frame,1)
det,coor=fd.detectFace(frame)
if(det is not None):
for i in range(len(det)):
detected=det[i]
k=coor[i]
f=detected
detected=cv2.resize(detected,(160,160))
detected=detected.astype('float')/255.0
detected=np.expand_dims(detected,axis=0)
feed=e.calculate(detected)
feed=np.expand_dims(feed,axis=0)
prediction=model.predict(feed)[0]
result=int(np.argmax(prediction))
if(np.max(prediction)>.85):
for i in people:
if(result==i):
label=people[i]
if(attendance_count[i]<30):
attendance_count[i]=attendance_count[i]+1
if(attendance_count[i]==30):
data.update(label,lecture)
person=i
else:
label='unknown'
try:
if(int(attendance_count[person])>=30):
cv2.putText(frame,completed_label,(k[0],k[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
cv2.rectangle(frame,(k[0],k[1]),(k[0]+k[2],k[1]+k[3]),(0,255,0),3)
else:
cv2.putText(frame,label,(k[0],k[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
cv2.rectangle(frame,(k[0],k[1]),(k[0]+k[2],k[1]+k[3]),(255,0,0),3)
except:
pass
cv2.imshow('Say Cheese and Press "Q" to Quite',frame)
if(cv2.waitKey(1) & 0XFF==ord('q')):
break
cap.release()
cv2.destroyAllWindows()
data.export_csv(lecture)
return