-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
208 lines (183 loc) · 6.89 KB
/
Copy pathGUI.py
File metadata and controls
208 lines (183 loc) · 6.89 KB
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
#first trial using tinkter
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from PIL import Image, ImageTk
import cv2
import gc
import time
import faces_recognizer
import file_handlers
import os
cap = cv2.VideoCapture(0)
PASSWORD_ICON_PATH = os.path.dirname(os.path.realpath(__file__)) + "/images/password_icon.png"
APP_WIDTH = 920
APP_HEIGHT = 534
WIDTH = int(cap.get(3))
HEIGHT = int(cap.get(4))
VALID_PASSWORD = "jayanth"
NUMBER_OF_FACES_ENCODINGS = 1
NAME_ADDED = False
PASSWORD_ADDED = False
SHOW_PASSWORD = True
RECOGNIZE = False
def add_to_database(KNOWN_FACES, name):
KNOWN_FACES[name] = faces_recognizer.KNOWN_FACES_ENCODINGS
file_handlers.create_file(name)
file_handlers.save_encodings(name)
KNOWN_FACES = file_handlers.load_known_faces()
return KNOWN_FACES
def refresh_database(name):
KNOWN_FACES = {}
for _ in range(NUMBER_OF_FACES_ENCODINGS):
_, frame = cap.read()
if frame is not None:
faces_recognizer.KNOWN_FACES_ENCODINGS, NUMBER_OF_FACES_IN_FRAME = faces_recognizer.create_face_encodings(frame)
if len(faces_recognizer.KNOWN_FACES_ENCODINGS) and NUMBER_OF_FACES_IN_FRAME==1:
KNOWN_FACES = add_to_database(KNOWN_FACES, name)
name_entry.delete(0, 'end')
password_entry.delete(0, 'end')
password_entry.focus()
name_entry["state"] = "disabled"
name_button["state"] = "disabled"
else:
messagebox.showinfo(message='Only one face can be recognized as of now.Please try again with a single person.',
title = "Invalid name")
name_entry.delete(0, 'end')
name_entry.focus()
return KNOWN_FACES
def add_new_known_face():
faces_recognizer.KNOWN_FACES = refresh_database(name = NEW_NAME.get().lower())
faces_recognizer.KNOWN_FACES = file_handlers.load_known_faces()
def display_frames_per_second(frame, start_time):
END_TIME = abs(start_time-time.time())
TOP_LEFT = (0,0)
BOTTOM_RIGHT = (116,26)
TEXT_POSITION = (8,20)
TEXT_SIZE = 0.6
FONT = cv2.FONT_HERSHEY_SIMPLEX
COLOR = (100,150,15) #BGR
cv2.rectangle(frame, TOP_LEFT, BOTTOM_RIGHT, (0,0,2), cv2.FILLED)
cv2.putText(frame, "FPS: {}".format(round(1/max(0.0333,END_TIME),1)), TEXT_POSITION, FONT, TEXT_SIZE,COLOR)
return frame
def convert_to_image(frame):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame)
return image
def recognize_faces (frame):
frame = faces_recognizer.identify_faces(frame)
return frame
def update_frame():
START_TIME = time.time()
global image
_, frame = cap.read()
if frame is not None:
frame = cv2.flip(frame, 1)
if RECOGNIZE:
frame = recognize_faces(frame)
frame = display_frames_per_second(frame, START_TIME)
image = convert_to_image(frame)
photo.paste(image)
root.after(round(10), update_frame)
def name_authentification():
global NAME_ADDED
if NEW_NAME.get().lower() in faces_recognizer.KNOWN_FACES.keys() or not len(NEW_NAME.get()):
messagebox.showinfo(message='Invalid name!\t\nPlease try again.', title = "Invalid name")
name_entry.delete(0, 'end')
name_entry.focus()
NAME_ADDED = False
if NAME_ADDED:
return True
def enter_name(*args):
global NAME_ADDED
NEW_NAME.get()
NAME_ADDED = True
if name_authentification():
add_new_known_face()
def password_authentification():
global PASSWORD_ADDED
if PASSWORD.get() != VALID_PASSWORD:
messagebox.showinfo(message='Invalid password!\t\nPlease try again.', title = "Invalid password")
password_entry.delete(0, 'end')
password_entry.focus()
PASSWORD_ADDED = True
if PASSWORD_ADDED:
name_entry["state"] = "normal"
name_button["state"] = "normal"
name_entry.focus()
def enter_password(*args):
global PASSWORD_ADDED
PASSWORD.get()
PASSWORD_ADDED = True
password_authentification()
def take_screenshot():
try:
IM = image
SAVE_PATH = filedialog.asksaveasfilename(defaultextension=".png", filetypes=(("PNG Files", "*.png"), ("All Files", "*.*")))
IM.save(SAVE_PATH)
except:
pass
def show_password():
global SHOW_PASSWORD
if SHOW_PASSWORD:
password_entry["show"] = ''
SHOW_PASSWORD = False
else :
password_entry["show"] = "*"
SHOW_PASSWORD = True
def enable_recognition():
global RECOGNIZE
if RECOGNIZE:
RECOGNIZE = False
recognition_button["bg"] = "blue"
else:
RECOGNIZE = True
recognition_button["bg"] = "green"
faces_recognizer.KNOWN_FACES = file_handlers.load_known_faces()
root = tk.Tk()
root.title("Security Cam")
root.minsize(APP_WIDTH,APP_HEIGHT)
root["bg"]="#000514"
canvas = tk.Canvas(root, width=WIDTH-5, height=HEIGHT-5,bg="black")
canvas.place(relx=0.03,rely=0.052)
recognition_button = tk.Button(canvas, text = "Recognize", command = enable_recognition,
bg = "black", fg = "white", activebackground = 'white')
recognition_button.place(relx=0.87,rely=0.93, relwidth=0.12,relheight=0.06)
recognition_button.bind(enable_recognition)
recognition_button.focus()
first_seperator = ttk.Separator(root, orient="horizontal")
first_seperator.place(relx=0.97, rely=0.055,relwidth = 0.2, anchor = "ne")
MESSAGE = tk.StringVar()
MESSAGE.set("password:jayanth")
message_label=tk.Label(root,textvariable=MESSAGE, wraplength = "5c", bg="white", fg="red")
message_label.place(relx=0.97,rely=0.080,relwidth=0.2,relheight=0.16,anchor="ne")
message_label.config(font=(None, 11))
second_seperator = ttk.Separator(root, orient="horizontal")
second_seperator.place(relx=0.97, rely=0.265,relwidth = 0.2, anchor = "ne")
PASSWORD = tk.StringVar()
password_entry = ttk.Entry(root, textvariable=PASSWORD, show="-")
password_entry.place(relx=0.93, rely=0.290,relheight=0.05,relwidth = 0.16, anchor = "ne")
password_entry.bind('<Return>', enter_password)
password_icon = tk.PhotoImage(file=PASSWORD_ICON_PATH)
show_password_button = tk.Button(root,command=show_password, image= password_icon, border=0, bg="#131113",activebackground="#131113")
show_password_button.place(relx=0.97, rely=0.290,relheight=0.05,relwidth = 0.035, anchor = "ne")
show_password_button.bind(show_password)
NEW_NAME = tk.StringVar()
name_entry = ttk.Entry(root, textvariable=NEW_NAME, state="disabled")
name_entry.place(relx=0.97, rely=0.428,relheight=0.05,relwidth = 0.2, anchor = "ne")
name_entry.bind('<Return>', enter_name)
name_button = ttk.Button(root,text="Enter your name",command=enter_name, state="disabled")
name_button.place(relx=0.97,rely=0.498,relheight=0.05,relwidth=0.2,anchor="ne")
name_button.bind(enter_name)
screenshot_button = ttk.Button(root,text="Take a screenshot",command=take_screenshot)
screenshot_button.place(relx=0.97,rely=0.895,relheight=0.05,relwidth=0.2,anchor="ne")
screenshot_button.bind(take_screenshot)
_, frame = cap.read()
if frame is not None:
image = convert_to_image(frame)
photo = ImageTk.PhotoImage(image=image)
canvas.create_image(WIDTH, HEIGHT, image=photo, anchor="se")
if __name__ == '__main__':
update_frame()
root.mainloop()
cap.release()
gc.collect()