forked from joeVenner/FaceRecognition-GUI-APP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-gui.py
201 lines (170 loc) · 9.01 KB
/
app-gui.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
from Detector import main_app
from create_classifier import train_classifer
from create_dataset import start_capture
import tkinter as tk
from tkinter import font as tkfont
from tkinter import messagebox,PhotoImage
#from PIL import ImageTk, Image
#from gender_prediction import emotion,ageAndgender
names = set()
class MainUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
global names
with open("nameslist.txt", "r") as f:
x = f.read()
z = x.rstrip().split(" ")
for i in z:
names.add(i)
self.title_font = tkfont.Font(family='Helvetica', size=16, weight="bold")
self.title("Face Recognizer")
self.resizable(False, False)
self.geometry("500x250")
self.protocol("WM_DELETE_WINDOW", self.on_closing)
self.active_name = None
container = tk.Frame(self)
container.grid(sticky="nsew")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree, PageFour):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def on_closing(self):
if messagebox.askokcancel("Quit", "Are you sure?"):
global names
f = open("nameslist.txt", "a+")
for i in names:
f.write(i+" ")
self.destroy()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
#load = Image.open("homepagepic.png")
#load = load.resize((250, 250), Image.ANTIALIAS)
render = PhotoImage(file='homepagepic.png')
img = tk.Label(self, image=render)
img.image = render
img.grid(row=0, column=1, rowspan=4, sticky="nsew")
label = tk.Label(self, text=" Home Page ", font=self.controller.title_font,fg="#263942")
label.grid(row=0, sticky="ew")
button1 = tk.Button(self, text=" Add a User ", fg="#ffffff", bg="#263942",command=lambda: self.controller.show_frame("PageOne"))
button2 = tk.Button(self, text=" Check a User ", fg="#ffffff", bg="#263942",command=lambda: self.controller.show_frame("PageTwo"))
button3 = tk.Button(self, text="Quit", fg="#263942", bg="#ffffff", command=self.on_closing)
button1.grid(row=1, column=0, ipady=3, ipadx=7)
button2.grid(row=2, column=0, ipady=3, ipadx=2)
button3.grid(row=3, column=0, ipady=3, ipadx=32)
def on_closing(self):
if messagebox.askokcancel("Quit", "Are you sure?"):
global names
with open("nameslist.txt", "w") as f:
for i in names:
f.write(i + " ")
self.controller.destroy()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
tk.Label(self, text="Enter the name", fg="#263942", font='Helvetica 12 bold').grid(row=0, column=0, pady=10, padx=5)
self.user_name = tk.Entry(self, borderwidth=3, bg="lightgrey", font='Helvetica 11')
self.user_name.grid(row=0, column=1, pady=10, padx=10)
self.buttoncanc = tk.Button(self, text="Cancel", bg="#ffffff", fg="#263942", command=lambda: controller.show_frame("StartPage"))
self.buttonext = tk.Button(self, text="Next", fg="#ffffff", bg="#263942", command=self.start_training)
self.buttoncanc.grid(row=1, column=0, pady=10, ipadx=5, ipady=4)
self.buttonext.grid(row=1, column=1, pady=10, ipadx=5, ipady=4)
def start_training(self):
global names
if self.user_name.get() == "None":
messagebox.showerror("Error", "Name cannot be 'None'")
return
elif self.user_name.get() in names:
messagebox.showerror("Error", "User already exists!")
return
elif len(self.user_name.get()) == 0:
messagebox.showerror("Error", "Name cannot be empty!")
return
name = self.user_name.get()
names.add(name)
self.controller.active_name = name
self.controller.frames["PageTwo"].refresh_names()
self.controller.show_frame("PageThree")
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
global names
self.controller = controller
tk.Label(self, text="Select user", fg="#263942", font='Helvetica 12 bold').grid(row=0, column=0, padx=10, pady=10)
self.buttoncanc = tk.Button(self, text="Cancel", command=lambda: controller.show_frame("StartPage"), bg="#ffffff", fg="#263942")
self.menuvar = tk.StringVar(self)
self.dropdown = tk.OptionMenu(self, self.menuvar, *names)
self.dropdown.config(bg="lightgrey")
self.dropdown["menu"].config(bg="lightgrey")
self.buttonext = tk.Button(self, text="Next", command=self.nextfoo, fg="#ffffff", bg="#263942")
self.dropdown.grid(row=0, column=1, ipadx=8, padx=10, pady=10)
self.buttoncanc.grid(row=1, ipadx=5, ipady=4, column=0, pady=10)
self.buttonext.grid(row=1, ipadx=5, ipady=4, column=1, pady=10)
def nextfoo(self):
if self.menuvar.get() == "None":
messagebox.showerror("ERROR", "Name cannot be 'None'")
return
self.controller.active_name = self.menuvar.get()
self.controller.show_frame("PageFour")
def refresh_names(self):
global names
self.menuvar.set('')
self.dropdown['menu'].delete(0, 'end')
for name in names:
self.dropdown['menu'].add_command(label=name, command=tk._setit(self.menuvar, name))
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.numimglabel = tk.Label(self, text="Number of images captured = 0", font='Helvetica 12 bold', fg="#263942")
self.numimglabel.grid(row=0, column=0, columnspan=2, sticky="ew", pady=10)
self.capturebutton = tk.Button(self, text="Capture Data Set", fg="#ffffff", bg="#263942", command=self.capimg)
self.trainbutton = tk.Button(self, text="Train The Model", fg="#ffffff", bg="#263942",command=self.trainmodel)
self.capturebutton.grid(row=1, column=0, ipadx=5, ipady=4, padx=10, pady=20)
self.trainbutton.grid(row=1, column=1, ipadx=5, ipady=4, padx=10, pady=20)
def capimg(self):
self.numimglabel.config(text=str("Captured Images = 0 "))
messagebox.showinfo("INSTRUCTIONS", "We will Capture 300 pic of your Face.")
x = start_capture(self.controller.active_name)
self.controller.num_of_images = x
self.numimglabel.config(text=str("Number of images captured = "+str(x)))
def trainmodel(self):
if self.controller.num_of_images < 300:
messagebox.showerror("ERROR", "No enough Data, Capture at least 300 images!")
return
train_classifer(self.controller.active_name)
messagebox.showinfo("SUCCESS", "The modele has been successfully trained!")
self.controller.show_frame("PageFour")
class PageFour(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Face Recognition", font='Helvetica 16 bold')
label.grid(row=0,column=0, sticky="ew")
button1 = tk.Button(self, text="Face Recognition", command=self.openwebcam, fg="#ffffff", bg="#263942")
#button2 = tk.Button(self, text="Emotion Detection", command=self.emot, fg="#ffffff", bg="#263942")
#button3 = tk.Button(self, text="Gender and Age Prediction", command=self.gender_age_pred, fg="#ffffff", bg="#263942")
button4 = tk.Button(self, text="Go to Home Page", command=lambda: self.controller.show_frame("StartPage"), bg="#ffffff", fg="#263942")
button1.grid(row=1,column=0, sticky="ew", ipadx=5, ipady=4, padx=10, pady=10)
#button2.grid(row=1,column=1, sticky="ew", ipadx=5, ipady=4, padx=10, pady=10)
#button3.grid(row=2,column=0, sticky="ew", ipadx=5, ipady=4, padx=10, pady=10)
button4.grid(row=1,column=1, sticky="ew", ipadx=5, ipady=4, padx=10, pady=10)
def openwebcam(self):
main_app(self.controller.active_name)
#def gender_age_pred(self):
# ageAndgender()
#def emot(self):
# emotion()
app = MainUI()
app.iconphoto(False, tk.PhotoImage(file='icon.ico'))
app.mainloop()