/
face_recog.py
104 lines (77 loc) · 2.9 KB
/
face_recog.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
import face_recognition
import imutils
import pickle
import time
import cv2
import os
import csv
from datetime import datetime
from datetime import date
today = date.today()
total = int(input("\nTotal no of students in class: "))
Subject = input("\nSubject name: ")
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
data = pickle.loads(open('face_enc', "rb").read())
filename=time.strftime('%Y-%m-%d__%H-%M',time.localtime())
open('Attendance'+filename+'.csv','a+')
with open('Attendance'+filename+'.csv','r+') as f:
f.write('\t\t\t INSTITUTE ')
f.write('\n\t\t\t BRANCH : Computer Science')
f.write('\nDate : ')
f.write(str(today))
f.write('\nlecture name : ')
f.write(Subject)
f.write('\n\nName of Students,Time of Arrival')
count = 0
def markAttendance(name):
with open('Attendance'+filename+'.csv','r+') as f:
myDataList=f.readlines()
nameList=[]
for line in myDataList:
entry=line.split(',')
nameList.append(entry[0])
if name not in nameList:
global count
now=datetime.now()
dtString=now.strftime('%H:%M:%S')
count += 1
f.writelines(f'\n{name},{dtString}')
print("Streaming started")
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1 , minNeighbors=5,minSize=(60, 60),flags= cv2.CASCADE_SCALE_IMAGE)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
encodings = face_recognition.face_encodings(rgb)
names = []
for encoding in encodings:
matches = face_recognition.compare_faces(data["encodings"],encoding)
name = "unknown"
if True in matches:
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
for i in matchedIdxs:
name = data["names"][i]
counts[name] = counts.get(name, 0) + 1
name = max(counts, key=counts.get)
names.append(name)
markAttendance(name)
for ((x, y, w, h), name) in zip(faces, names):
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, name, (x, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2)
cv2.imshow("Frame", frame)
if cv2.waitKey(1) & 0xff== ord('q'):
break
absent = int(total-count)
perc=(count*100)/total
with open('Attendance'+filename+'.csv','a+') as f:
f.write('\n\n\n\n\n\t\t\tTotal no of Students : ')
f.write(str(total))
f.write('\n\t\t\tStudents Present : ')
f.write(str(count))
f.write('\n\t\t\tStudents Absent : ')
f.write(str(absent))
f.write(f'\n\t\t\tPercentage attendance of lecture : {perc}%')
video_capture.release()
cv2.destroyAllWindows()