-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceRecogWithGui.py
282 lines (218 loc) · 11.5 KB
/
FaceRecogWithGui.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
import serial
import threading
import cv2
from deepface import DeepFace
import tkinter as tk
from tkinter import messagebox
# Global variables =========================================================================================================
loop = True
# Tkinter code =============================================================================================================
# Button functions for PinPage
def send_pin():
entered_pin = "p" + PinEntry.get() + "\n"
ser.write(entered_pin.encode('utf-8'))
# Button functions for SettingPage
def exit():
close_SettingPage()
def shutdown():
global loop
loop = False
def dissarm():
diss = "d\n"
ser.write(diss.encode('utf-8'))
def runtest():
test = "r\n"
ser.write(test.encode('utf-8'))
def delface():
pass
def regface():
pass
def changepin():
newpin = "r" + ChangePinEntry.get() + "\n"
ser.write(newpin.encode('utf-8'))
def dissarmedstate():
state = "sd\n"
ser.write(state.encode('utf-8'))
def homestate():
state = "sh\n"
ser.write(state.encode('utf-8'))
def awaystate():
state = "sa\n"
ser.write(state.encode('utf-8'))
# General button functions
def open_SettingPage():
PinPage.withdraw() # Hide the first window
SettingPage.deiconify() # Show the second window
def close_SettingPage():
SettingPage.withdraw() # Hide the second window
PinPage.deiconify() # Show the first window
PinEntry.delete(0, 'end') # Clear the password entry field
# PinPage window
PinPage = tk.Tk()
PinPage.title("PIN Page")
PinPage.geometry('640x480')
PinPage.grid_propagate(False)
for x in range(32):
tk.Grid.columnconfigure(PinPage, x, weight=1, uniform='row')
tk.Label(PinPage, width = 1, bg = '#101f2d').grid(row = 0, column = x, sticky = "NSEW")
for y in range(24):
tk.Grid.rowconfigure(PinPage, y, weight=1, uniform='row')
tk.Label(PinPage, width = 1, bg = '#101f2d').grid(row = y, column = 0, sticky = "NSEW")
PinPage.configure(background='#101f2d')
PinPage.resizable(False, False)
PinEntryLabel = tk.Label(PinPage, text = "Enter PIN:", font = ('Consolas', 26), width = 1, height = 1, fg = '#000000', bg = '#3a5a78')
PinEntryLabel.grid(row = 0, column = 0, columnspan = 32, rowspan = 7, sticky = "NSEW")
PinEntry = tk.Entry(PinPage, width = 1, bg = '#FFFFFF', font = ('Consolas', 22), justify = 'center')
PinEntry.grid(row = 11, column = 3, columnspan = 26, rowspan = 3, sticky = "NSEW")
PinEntry.insert(0, "")
PinEnteryButton = tk.Button(PinPage, text = "Enter", font = ('Consolas', 22), width = 1, height = 1, fg = '#000000', command = send_pin, bg = '#3a5a78')
PinEnteryButton.grid(row = 16, column = 11, columnspan = 10, rowspan = 3, sticky = "NSEW")
# SettingPage window
SettingPage = tk.Toplevel()
SettingPage.title("Setting Page")
SettingPage.geometry('1280x720')
SettingPage.grid_propagate(False)
for x in range(64):
tk.Grid.columnconfigure(SettingPage, x, weight=1, uniform='row')
tk.Label(SettingPage, width = 1, bg = '#101f2d').grid(row = 0, column = x, sticky = "NSEW")
for y in range(36):
tk.Grid.rowconfigure(SettingPage, y, weight=1, uniform='row')
tk.Label(SettingPage, width = 1, bg = '#101f2d').grid(row = y, column = 0, sticky = "NSEW")
SettingPage.configure(background='#101f2d')
SettingPage.resizable(False, False)
TopPanelLabel = tk.Label(SettingPage, text = "Configuration Panel", font = ('Consolas', 26), width = 1, height = 1, fg = '#000000', bg = '#3a5a78')
TopPanelLabel.grid(row = 0, column = 0, columnspan = 64, rowspan = 4, sticky = "NSEW")
StateLabel = tk.Label(SettingPage, text = "Operational State:", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', bg = '#3a5a78')
StateLabel.grid(row = 5, column = 3, columnspan = 10, rowspan = 3, sticky = "NSEW")
ChangePinLabel = tk.Label(SettingPage, text = "Change Pin:", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', bg = '#3a5a78')
ChangePinLabel.grid(row = 5, column = 19, columnspan = 10, rowspan = 3, sticky = "NSEW")
RegFaceLabel = tk.Label(SettingPage, text = "Register Face:", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', bg = '#3a5a78')
RegFaceLabel.grid(row = 5, column = 35, columnspan = 10, rowspan = 3, sticky = "NSEW")
DelFaceLabel = tk.Label(SettingPage, text = "Delete Face:", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', bg = '#3a5a78')
DelFaceLabel.grid(row = 5, column = 51, columnspan = 10, rowspan = 3, sticky = "NSEW")
ChangePinEntry = tk.Entry(SettingPage, width = 1, bg = '#FFFFFF', font = ('Consolas', 16), justify = 'center')
ChangePinEntry.grid(row = 10, column = 20, columnspan = 8, rowspan = 2, sticky = "NSEW")
ChangePinEntry.insert(0, "")
ExitButton = tk.Button(SettingPage, text = "Exit", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', command = exit, bg = '#e6695b')
ExitButton.grid(row = 31, column = 54, columnspan = 7, rowspan = 4, sticky = "NSEW")
ShutDownButton = tk.Button(SettingPage, text = "Shutdown", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', command = shutdown, bg = '#e6695b')
ShutDownButton.grid(row = 31, column = 44, columnspan = 7, rowspan = 4, sticky = "NSEW")
DissarmButton = tk.Button(SettingPage, text = "Dissarm", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', command = dissarm, bg = '#e6695b')
DissarmButton.grid(row = 31, column = 34, columnspan = 7, rowspan = 4, sticky = "NSEW")
RunTestButton = tk.Button(SettingPage, text = "Run Test", font = ('Consolas', 18), width = 1, height = 1, fg = '#000000', command = runtest, bg = '#668db2')
RunTestButton.grid(row = 31, column = 3, columnspan = 7, rowspan = 4, sticky = "NSEW")
DeleteFaceButton = tk.Button(SettingPage, text = "Delete", font = ('Consolas', 16), width = 1, height = 1, fg = '#000000', command = delface, bg = '#668db2')
DeleteFaceButton.grid(row = 10, column = 52, columnspan = 8, rowspan = 2, sticky = "NSEW")
RegisterFaceButton = tk.Button(SettingPage, text = "Register", font = ('Consolas', 16), width = 1, height = 1, fg = '#000000', command = regface, bg = '#668db2')
RegisterFaceButton.grid(row = 10, column = 36, columnspan = 8, rowspan = 2, sticky = "NSEW")
ChangePinButton = tk.Button(SettingPage, text = "Change", font = ('Consolas', 16), width = 1, height = 1, fg = '#000000', command = changepin, bg = '#668db2')
ChangePinButton.grid(row = 13, column = 20, columnspan = 8, rowspan = 2, sticky = "NSEW")
DissarmedStateButton = tk.Button(SettingPage, text = "Dissarmed", font = ('Consolas', 16), width = 1, height = 1, fg = '#000000', command = dissarmedstate, bg = '#668db2')
DissarmedStateButton.grid(row = 10, column = 4, columnspan = 8, rowspan = 2, sticky = "NSEW")
HomeStateButton = tk.Button(SettingPage, text = "Home ", font = ('Consolas', 16), width = 1, height = 1, fg = '#000000', command = homestate, bg = '#668db2')
HomeStateButton.grid(row = 13, column = 4, columnspan = 8, rowspan = 2, sticky = "NSEW")
AwayStateButton = tk.Button(SettingPage, text = "Away", font = ('Consolas', 16), width = 1, height = 1, fg = '#000000', command = awaystate, bg = '#668db2')
AwayStateButton.grid(row = 16, column = 4, columnspan = 8, rowspan = 2, sticky = "NSEW")
# Hide the second window initially
SettingPage.withdraw()
# Poppup tkinter window
def create_pin_popup():
# Create a new Tkinter window for the PIN popup
pin_window = tk.Toplevel()
pin_window.title("Enter PIN")
# Function to validate the entered PIN
def validate_pin():
entered_pin = pin_entry.get()
if entered_pin == "1234": # Replace "1234" with your desired PIN
print("PIN entered successfully!")
pin_window.destroy() # Close the PIN popup window
else:
print("Incorrect PIN!")
# Label and entry widget for entering the PIN
pin_label = tk.Label(pin_window, text="Enter PIN:")
pin_label.pack()
pin_entry = tk.Entry(pin_window, show="*") # Use "*" to hide the PIN characters
pin_entry.pack()
# Button to submit the PIN
submit_button = tk.Button(pin_window, text="Submit", command=validate_pin)
submit_button.pack()
# Serial communication =====================================================================================================
ser = serial.Serial("/dev/ttyACM0", 9600, timeout = 1)
ser.flush()
def received_serial_command(command):
if command[0] == 'p':
if command[1] == 'y':
open_SettingPage()
elif command[1] == 'n':
messagebox.showerror("Error", "Incorrect PIN!")
if command[0] == 'f':
face = "f" + face_match + "\n"
ser.write(face.encode('utf-8'))
# Facial recognition code ==================================================================================================
# Load the pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
counter = 0
face_match = "f"
face_detected = False
refrence_img = cv2.imread("/home/willnatsan/uni/acs233-burglar-alarm/pics/Miki.jpg")
# Face functions
def detect_faces(frame):
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Return True if at least one face is detected, False otherwise
return len(faces) > 0
def check_face(frame):
if face_detected:
global face_match
try:
if DeepFace.verify(frame, refrence_img.copy())['verified']:
face_match = "y"
else:
face_match = "n"
except ValueError:
face_match = "n"
else:
face_match = "f"
# Main Loop ================================================================================================================
while loop:
# serial
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').strip()
print(line)
received_serial_command(line)
# Running tkinter window
try:
PinPage.update() # Manually update the application
except tk.TclError:
break # Exit the loop if the main window is closed
# Running facial recognition
ret, frame = cap.read()
if ret:
if counter % 30 == 0:
face_detected = detect_faces(frame)
try:
threading.Thread(target=check_face, args=(frame.copy(),)).start()
except ValueError:
pass
counter = 0
counter += 1
if face_match == "y":
cv2.putText(frame, "MATCH!", (20, 450), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
elif face_match == "n":
cv2.putText(frame, "NO MATCH!", (20, 450), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
elif face_match == "f":
cv2.putText(frame, "NO Face!", (20, 450), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
cv2.imshow("video", frame)
# Break out of while loop when 'q' is pressed
key = cv2.waitKey(1)
if key == ord("q"):
loop = False
# Closs all open windows
PinPage.destroy()
cap.release()
cv2.destroyAllWindows()