-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceRecogClassConstructor.py
141 lines (114 loc) · 4.65 KB
/
FaceRecogClassConstructor.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import face_recognition
import json
import cv2
import numpy as np
import math
import pathlib
import urllib
from gtts import gTTS
import os
from urllib.request import urlopen
import mysql.connector
from datetime import datetime
with open(f'{pathlib.Path(__file__).parent.resolve()}/mysql.json') as f:
mysqldb = json.load(f)
def connectMySQL():
return mysql.connector.connect(
host=mysqldb["host"],
user=mysqldb["user"],
password=mysqldb["pass"],
database=mysqldb["database"]
)
def talk(msg):
file = gTTS(text=msg, lang="en", tld="co.in")
file.save("last-bot-message.mp3")
os.system("start last-bot-message.mp3")
return msg
def getCurrentDate():
return datetime.today().strftime('%Y-%m-%d')
class MySQLInsert:
def __init__(self):
self.mysql = connectMySQL()
pass
def hasAttendedToday(self, name, cr):
cur = self.mysql.cursor()
cur.execute(
f"select * from attendance where name='{name}' AND class='{cr}' AND time_attended='{getCurrentDate()}'")
for x in cur:
return True
return False
def insert(self, name, cr):
cur = self.mysql.cursor()
cur.execute(
f"insert into attendance (`name`, `class`, `time_attended`) values('{name}', '{cr}', '{getCurrentDate()}')")
self.mysql.commit()
class ClassAttendance:
def __new__(cls, *args, **kwargs):
return super().__new__(cls)
def __init__(self, classroom, students, min_conf):
self.classroom = classroom
self.students = students
self.min_conf = min_conf
self.mysqlInsert = MySQLInsert()
self.def_name = "Not found"
def getImageFromURL(self, url):
req = urllib.request.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
print(arr)
return cv2.imdecode(arr, -1)
def getEncodedImages(self):
imgs = []
for x in self.students:
print(x)
imgs.append(face_recognition.face_encodings(
cv2.cvtColor(self.getImageFromURL(x.img_url), cv2.COLOR_BGR2RGB))[0])
return imgs
def getConfidence(self, face_distance, face_match_threshold=0.6):
range = (1.0 - face_match_threshold)
linear_val = (1.0 - face_distance) / (range * 2.0)
if face_distance > face_match_threshold:
return str(round(linear_val * 100, 2)) + '%'
else:
value = (linear_val + ((1.0 - linear_val) *
math.pow((linear_val - 0.5) * 2, 0.2))) * 100
return (round(value, 2))
def getStudentByIndex(self, index):
i = 0
for x in self.students:
if i == index:
return x.name
i += 1
return self.def_name
def start(self):
encodedStudentImages = self.getEncodedImages()
print(encodedStudentImages)
video_capture = cv2.VideoCapture(0)
while True:
ignored, img = video_capture.read()
# print("checking video capture")
faceCurentFrame = face_recognition.face_locations(img)
encodeCurentFrame = face_recognition.face_encodings(
img, faceCurentFrame)
for encodeface, faceLoc in zip(encodeCurentFrame, faceCurentFrame):
faceDis = face_recognition.face_distance(
encodedStudentImages, encodeface)
matchIndex = np.argmin(faceDis)
confidence = self.getConfidence(faceDis[matchIndex])
detectedstudent = self.getStudentByIndex(matchIndex) if face_recognition.compare_faces(
encodedStudentImages, encodeface)[matchIndex] and confidence >= self.min_conf else self.def_name
if detectedstudent != self.def_name:
if self.mysqlInsert.hasAttendedToday(detectedstudent, self.classroom) == True:
continue
self.mysqlInsert.insert(detectedstudent, self.classroom)
# Draw rectangle around student face
y1, x2, y2, x1 = faceLoc
cv2.rectangle(img, (x1, y1), (x2, y2), (102, 0, 255), 2)
if detectedstudent != self.def_name:
talk(detectedstudent)
cv2.putText(img, f"{detectedstudent} {confidence}%" if detectedstudent != self.def_name else detectedstudent, (x1+6, y1-6),
cv2.FONT_HERSHEY_COMPLEX, 0.6, (102, 0, 255), 2)
cv2.imshow(f'Attendance - Class [{self.classroom}]', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()