-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (89 loc) · 4.74 KB
/
main.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
import os.path
import datetime
import pickle
import subprocess
import tkinter as tk
import cv2
from PIL import Image, ImageTk
import face_recognition
import util
#from test import test
class App:
def __init__(self):
self.main_window = tk.Tk()
self.main_window.geometry("1200x520+350+100")
self.login_button_main_window = util.get_button(self.main_window, 'login', 'green', self.login)
self.login_button_main_window.place(x=750, y=200)
#self.logout_button_main_window = util.get_button(self.main_window, 'logout', 'red', self.logout)
#self.logout_button_main_window.place(x=750, y=300)
self.register_new_user_button_main_window = util.get_button(self.main_window, 'register new user', 'gray',
self.register_new_user, fg='black')
self.register_new_user_button_main_window.place(x=750, y=400)
self.webcam_label = util.get_img_label(self.main_window)
self.webcam_label.place(x=10, y=0, width=700, height=500)
self.add_webcam(self.webcam_label)
self.db_dir = './db'
if not os.path.exists(self.db_dir):
os.mkdir(self.db_dir)
self.log_path = './log.txt'
def add_webcam(self, label):
if 'cap' not in self.__dict__:
self.cap = cv2.VideoCapture(2)
self._label = label
self.process_webcam()
def process_webcam(self):
ret, frame = self.cap.read()
self.most_recent_capture_arr = frame
img_ = cv2.cvtColor(self.most_recent_capture_arr, cv2.COLOR_BGR2RGB)
self.most_recent_capture_pil = Image.fromarray(img_)
imgtk = ImageTk.PhotoImage(image=self.most_recent_capture_pil)
self._label.imgtk = imgtk
self._label.configure(image=imgtk)
self._label.after(20, self.process_webcam)
def login(self):
unknown_image_path= './.tmp.jpg'
cv2.imwrite(unknown_image_path, self.most_recent_capture_arr)
output = subprocess.check_output(['face_recognition', self.db_dir, unknown_image_path])
name=output.decode('utf-8').split(',')[1][:-3]
if name in ['unknown_person', 'no_person_found']:
util.msg_box('Ups...', 'Unknown user. Please register new user or try again.')
else:
util.msg_box('Welcome back!', 'Welcome, {}.'.format(name))
with open(self.log_path, 'a') as f:
f.write('{},{},in\n'.format(name, datetime.datetime.now()))
f.close()
f.close()
os.remove(unknown_image_path)
def register_new_user(self):
self.register_new_user_window = tk.Toplevel(self.main_window)
self.register_new_user_window.geometry("1200x520+370+120")
self.accept_button_register_new_user_window = util.get_button(self.register_new_user_window, 'Accept', 'green', self.accept_register_new_user)
self.accept_button_register_new_user_window.place(x=750, y=300)
self.try_again_button_register_new_user_window = util.get_button(self.register_new_user_window, 'Try again', 'red', self.try_again_register_new_user)
self.try_again_button_register_new_user_window.place(x=750, y=400)
self.capture_label = util.get_img_label(self.register_new_user_window)
self.capture_label.place(x=10, y=0, width=700, height=500)
self.add_img_to_label(self.capture_label)
self.entry_text_register_new_user = util.get_entry_text(self.register_new_user_window)
self.entry_text_register_new_user.place(x=750, y=150)
self.text_label_register_new_user = util.get_text_label(self.register_new_user_window, 'Please, \ninput username:')
self.text_label_register_new_user.place(x=750, y=70)
def try_again_register_new_user(self):
self.register_new_user_window.destroy()
def add_img_to_label(self, label):
imgtk = ImageTk.PhotoImage(image=self.most_recent_capture_pil)
label.imgtk = imgtk
label.configure(image=imgtk)
self.register_new_user_capture = self.most_recent_capture_arr.copy()
def start(self):
self.main_window.mainloop()
def accept_register_new_user(self):
name = self.entry_text_register_new_user.get(1.0, "end-1c")
embeddings = face_recognition.face_encodings(self.register_new_user_capture)[0]
file = open(os.path.join(self.db_dir, '{}.pickle'.format(name)), 'wb')
pickle.dump(embeddings, file)
util.msg_box('Success!', 'User was registered successfully !')
self.register_new_user_window.destroy()
if __name__ == "__main__":
app = App()
app.start()