-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
425 lines (323 loc) · 13.4 KB
/
demo.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from Utils.db_utils import *
from Utils.utils import *
from kivy.config import Config
Config.set('graphics', 'window_state', 'maximized')
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.graphics.texture import Texture
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.graphics import Color, Rectangle
import sys
import datetime
import cv2
kivy.require("1.11.1")
global usernames
global embeddings
global attendance
global camera_index
global show_attendance_obj
camera_index = 0
attendance = dict()
class Home_Page(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 2
self.padding = [100, 100, 100, 100]
self.add_widget(Image(source = 'Attendance.jpg'))
buttons = GridLayout(cols = 2)
buttons.spacing = [20, 20]
button_1 = Button(text='TAKE \nATTENDANCE', font_size = 30, italic = True, background_color = [1, 255, 1, 1])
button_2 = Button(text='SHOW \nATTENDANCE', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
button_3 = Button(text='ADD \nSTUDENT', font_size = 30, italic = True, background_color = [1, 1, 255, 1])
button_4 = Button(text='REMOVE \nSTUDENT', font_size = 30, italic = True, background_color = [100, 140, 0, 1])
button_1.bind(on_press = self.take_attendance)
button_2.bind(on_press = self.show_attendance)
button_3.bind(on_press = self.add_student)
button_4.bind(on_press = self.remove_student)
buttons.add_widget(button_1)
buttons.add_widget(button_2)
buttons.add_widget(button_3)
buttons.add_widget(button_4)
self.add_widget(buttons)
def take_attendance(self, instance):
global usernames, embeddings
embeddings, usernames = readAllBlobData()
UI_interface.screen_manager.current = "Attendance"
def show_attendance(self, instance):
global usernames, embeddings
global show_attendance_obj
embeddings, usernames = readAllBlobData()
show_attendance_obj.show()
UI_interface.screen_manager.current = "Show_Attendance"
def add_student(self, instance):
UI_interface.screen_manager.current = "Add_Student"
def remove_student(self, instance):
global remove_student_obj
remove_student_obj.show()
UI_interface.screen_manager.current = "Remove_Student"
class Attendance_Page(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.output_path = 'Output/'
self.flag = 0
self.cols = 2
self.padding = [100, 100, 100, 100]
self.spacing = [20, 20]
self.begin = Button(text='BEGIN', font_size = 30, italic = True, background_color = [1, 255, 1, 1])
self.begin.bind(on_press = self.start)
self.add_widget(self.begin)
self.back = Button(text='GO BACK', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
self.back.bind(on_press = self.goback)
self.add_widget(self.back)
def start(self, instance):
global camera_index
self.flag = 1
self.img=Image()
self.layout = BoxLayout()
self.layout.add_widget(self.img)
self.remove_widget(self.begin)
self.remove_widget(self.back)
self.add_widget(self.layout)
mark = Button(text='MARK ME', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
back = Button(text='GO BACK', font_size = 30, italic = True, background_color = [1, 1, 255, 1])
self.label = Label(text='Mark Your Attendance!!', font_size = 38, color = [255, 255, 255, 1])
mark.bind(on_press = self.recognize)
back.bind(on_press = self.goback)
self.button_layout = GridLayout(rows = 3, spacing = [20, 20])
self.button_layout.add_widget(mark)
self.button_layout.add_widget(back)
self.button_layout.add_widget(self.label)
self.add_widget(self.button_layout)
self.capture = cv2.VideoCapture(camera_index)
self.event = Clock.schedule_interval(self.update, 1.0/33.0)
def update(self, instance):
_, self.frame = self.capture.read()
self.frame = extract_all_faces(self.frame)
buf1 = cv2.flip(self.frame, 0)
buf = buf1.tostring()
texture = Texture.create(size=(self.frame.shape[1], self.frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.img.texture = texture
def recognize(self, instance):
ts = datetime.datetime.now()
img_name = "{}.jpg".format(ts.strftime("%Y-%m-%d_%H-%M-%S"))
img_path = self.output_path + img_name
cv2.imwrite(img_path, self.frame)
print("[INFO] saved {}".format(img_name))
embedding, flag = generate_embedding(img_path)
if embeddings is not None:
if flag == 1:
ones_matrix = np.ones((len(usernames), 1))
embedding_matrix = np.matmul(ones_matrix, embedding.detach().numpy())
distances = calc_distance(embedding_matrix, embeddings)
if (distances[np.argmin(distances)] < 1.0000):
print(usernames[np.argmin(distances)] + ' Marked')
self.button_layout.remove_widget(self.label)
self.label = Label(text=usernames[np.argmin(distances)] + ' Marked', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
attendance[usernames[np.argmin(distances)]] = "Present"
else:
self.button_layout.remove_widget(self.label)
self.label = Label(text = 'User Not Registered', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
else:
self.button_layout.remove_widget(self.label)
self.label = Label(text='Zero/Muliple Faces Detected', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
else:
self.button_layout.remove_widget(self.label)
self.label = Label(text='No Registered Users', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
def goback(self, instance):
if self.flag == 1:
self.event.cancel()
self.capture.release()
self.remove_widget(self.layout)
self.remove_widget(self.button_layout)
self.__init__()
UI_interface.screen_manager.current = "Home"
class Add_Student(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.output_path = 'Output/'
self.cols = 2
self.flag = 0
self.padding = [100, 100, 100, 100]
self.spacing = [20, 20]
self.begin = Button(text='BEGIN', font_size = 30, italic = True, background_color = [1, 255, 1, 1])
self.begin.bind(on_press = self.start)
self.add_widget(self.begin)
self.back = Button(text='GO BACK', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
self.back.bind(on_press = self.goback)
self.add_widget(self.back)
def start(self, instance):
global camera_index
self.flag = 1
self.img=Image()
self.layout = BoxLayout()
self.layout.add_widget(self.img)
self.remove_widget(self.begin)
self.remove_widget(self.back)
self.add_widget(self.layout)
self.name = TextInput(multiline = False, size_hint = (.2, None), height = 40)
add = Button(text='ADD ME', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
back = Button(text='GO BACK', font_size = 30, italic = True, background_color = [1, 1, 255, 1])
self.label = Label(text='Add Yourself!!', font_size = 38, color = [255, 255, 255, 1])
add.bind(on_press = self.add)
back.bind(on_press = self.goback)
self.button_layout = GridLayout(rows = 4, spacing = [20, 20])
self.button_layout.add_widget(self.name)
self.button_layout.add_widget(add)
self.button_layout.add_widget(back)
self.button_layout.add_widget(self.label)
self.add_widget(self.button_layout)
self.capture = cv2.VideoCapture(camera_index)
self.event = Clock.schedule_interval(self.update, 1.0/33.0)
def update(self, instance):
_, self.frame = self.capture.read()
self.frame = extract_all_faces(self.frame)
buf1 = cv2.flip(self.frame, 0)
buf = buf1.tostring()
texture = Texture.create(size=(self.frame.shape[1], self.frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.img.texture = texture
def add(self, instance):
if len(self.name.text) != 0:
ts = datetime.datetime.now()
img_name = "{}.jpg".format(ts.strftime("%Y-%m-%d_%H-%M-%S"))
img_path = self.output_path + img_name
cv2.imwrite(img_path, self.frame)
print("[INFO] saved {}".format(img_name))
embedding, flag = generate_embedding(img_path)
if flag == 1:
insertBLOB(self.name.text, embedding)
self.button_layout.remove_widget(self.label)
self.label = Label(text=self.name.text + ' Added', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
else:
self.button_layout.remove_widget(self.label)
self.label = Label(text = 'Zero/Multiple Faces Detected', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
else:
self.button_layout.remove_widget(self.label)
self.label = Label(text = 'Enter UserName', font_size = 38, color = [255, 255, 255, 1])
self.button_layout.add_widget(self.label)
def goback(self, instance):
global usernames, embeddings
if self.flag == 1:
self.event.cancel()
self.capture.release()
self.remove_widget(self.layout)
self.remove_widget(self.button_layout)
self.__init__()
embeddings, usernames = readAllBlobData()
for username in usernames:
if username not in attendance.keys():
attendance[username] = 'Absent'
print(attendance)
UI_interface.screen_manager.current = "Home"
class Show_Attendance_Page(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.flag = 0
self.cols = 2
self.padding = [100, 100, 100, 100]
self.spacing = [20, 20]
def show(self):
self.attendance_list = GridLayout(cols = 2, rows = 42, spacing = [20, 20])
for key in attendance.keys():
self.attendance_list.add_widget(Label(text = key, font_size = 20, color = [255, 255, 255, 1]))
self.attendance_list.add_widget(Label(text = attendance[key], font_size = 20, color = [255, 255, 255, 1]))
self.add_widget(self.attendance_list)
self.back = Button(text='GO BACK', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
self.back.bind(on_press = self.goback)
self.add_widget(self.back)
def goback(self, instance):
self.remove_widget(self.back)
self.remove_widget(self.attendance_list)
UI_interface.screen_manager.current = "Home"
class Remove_Student_Page(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.flag = 0
self.cols = 2
self.padding = [100, 100, 100, 100]
self.spacing = [20, 20]
def show(self):
self.name = TextInput(multiline = False, size_hint = (.2, None), height = 40)
self.remove = Button(text='REMOVE USER', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
self.remove.bind(on_press = self.remove_student)
self.back = Button(text='GO BACK', font_size = 30, italic = True, background_color = [255, 1, 1, 1])
self.back.bind(on_press = self.goback)
self.buttons = GridLayout(cols = 1, spacing = [20, 20])
self.buttons.add_widget(self.name)
self.buttons.add_widget(self.remove)
self.buttons.add_widget(self.back)
self.add_widget(self.buttons)
self.attendance_list = GridLayout(cols = 2, rows = 42, spacing = [20, 20])
for key in attendance.keys():
self.attendance_list.add_widget(Label(text = key, font_size = 20, color = [255, 255, 255, 1]))
self.attendance_list.add_widget(Label(text = attendance[key], font_size = 20, color = [255, 255, 255, 1]))
self.add_widget(self.attendance_list)
def remove_student(self, instance):
if len(self.name.text) != 0:
deleteBlob(self.name.text)
self.remove_widget(self.attendance_list)
if self.name.text in attendance:
del attendance[self.name.text]
print(attendance)
self.attendance_list = GridLayout(cols = 2, rows = 42, spacing = [20, 20])
for key in attendance.keys():
self.attendance_list.add_widget(Label(text = key, font_size = 20, color = [255, 255, 255, 1]))
self.attendance_list.add_widget(Label(text = attendance[key], font_size = 20, color = [255, 255, 255, 1]))
self.add_widget(self.attendance_list)
def goback(self, instance):
self.remove_widget(self.buttons)
self.remove_widget(self.attendance_list)
UI_interface.screen_manager.current = "Home"
class AIAMS(App):
def build(self):
global show_attendance_obj, remove_student_obj
self.screen_manager = ScreenManager()
self.home_page = Home_Page()
screen = Screen(name='Home')
screen.add_widget(self.home_page)
self.screen_manager.add_widget(screen)
self.attendance_page = Attendance_Page()
screen = Screen(name='Attendance')
screen.add_widget(self.attendance_page)
self.screen_manager.add_widget(screen)
self.add_student = Add_Student()
screen = Screen(name='Add_Student')
screen.add_widget(self.add_student)
self.screen_manager.add_widget(screen)
show_attendance_obj = Show_Attendance_Page()
screen = Screen(name='Show_Attendance')
screen.add_widget(show_attendance_obj)
self.screen_manager.add_widget(screen)
remove_student_obj = Remove_Student_Page()
screen = Screen(name='Remove_Student')
screen.add_widget(remove_student_obj)
self.screen_manager.add_widget(screen)
return self.screen_manager
if __name__ == "__main__":
if not os.path.exists('Output/'):
os.makedirs('Output/')
if not os.path.exists('Face_Database.db'):
create_table()
embeddings, usernames = readAllBlobData()
for username in usernames:
if username not in attendance.keys():
attendance[username] = 'Absent'
UI_interface = AIAMS()
UI_interface.run()