-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
316 lines (239 loc) · 10.7 KB
/
Copy pathapplication.py
File metadata and controls
316 lines (239 loc) · 10.7 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
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
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog as Filedialog
from datetime import timedelta, datetime
import library as l
import threading
import glob
import os
W_RATIO = 1.5
W_BGCOL ='#494949'
W_FGCOL = 'lightgray'
DIR = 'data'
# generates centered window
# w the window opened
# ratio the window ratio to mantain
# returns window geometry [width]x[height]+[startx]+[starty]
def get_window_geo(w, ratio):
width = int(w.winfo_screenwidth() / ratio)
height = int(w.winfo_screenheight() / ratio)
startx = int((w.winfo_screenwidth() - width) / 2)
starty = int((w.winfo_screenheight() - height) / 2)
return "{}x{}+{}+{}".format(width, height, startx, starty)
# set defaults on window app
root = Tk()
root.geometry(get_window_geo(root, W_RATIO))
root.configure(bg=W_BGCOL)
root.title('Face Recognition - Probeta Technologies')
# render basic layout
load_frame = LabelFrame(root, text="Load Faces", bg=W_BGCOL, fg=W_FGCOL)
load_frame.place(relx=0.04, rely=0.04, relheight=0.65, relwidth=0.45)
search_frame = LabelFrame(root, text="Search Faces", bg=W_BGCOL, fg=W_FGCOL)
search_frame.place(relx=0.51, rely=0.04, relheight=0.65, relwidth=0.45)
config_frame = LabelFrame(root, text="Configurations", bg=W_BGCOL, fg=W_FGCOL)
config_frame.place(relx=0.04, rely=0.71, relheight=0.25, relwidth=0.92)
####################### search layout #######################
# search for the images prints results and more
def search_coincidences(face):
res = int(results_frame.winfo_width() * 0.95 /2)
# set ups layout
in_image_label = Label(results_frame)
in_image_label.place(relx=0, rely=0, anchor=NW)
original_img = ImageTk.PhotoImage(Image.fromarray(face).resize((res,res),1))
in_image_label.config(image=original_img)
out_image_label = Label(results_frame)
out_image_label.place(relx=1, rely=0, anchor=NE)
result_label = Label(results_frame, bg=W_BGCOL, anchor=CENTER, fg=W_FGCOL)
image_btn_var = IntVar()
image_btn = Button(results_frame, text='Quit Search', command=lambda: image_btn_var.set(1))
image_btn.place(anchor=S, relx=0.5, rely=1)
# search for coincidences
(f, name, prob) = l.search_image(face, DIR, algorithm.get() == 2)
# prints results
match_img = ImageTk.PhotoImage(Image.fromarray(f).resize((res,res),1))
out_image_label.config(image=match_img)
result_label.configure(text='Name: {} Probability: {}'.format(name, prob))
result_label.place(anchor=S, relx=0.5, rely=0.85)
image_btn.wait_variable(image_btn_var)
# cleans layout
in_image_label.destroy()
out_image_label.destroy()
result_label.destroy()
image_btn.destroy()
search_btn.place(relx=0.5, rely=0.5, anchor=CENTER)
# analizes a single image and selects only one face
def analize_directory():
# ask for image path
path = Filedialog.askdirectory( initialdir=os.getcwd(), title="Select a File")
if not path:
return
types = ('/*.png', '/*.jpg', '/*.jpeg') # the tuple of file types
list_of_items = []
for t in types:
list_of_items.extend(glob.glob(path + t))
for file in list_of_items:
faces = l.extract_face(DIR, file, confidence_factor.get())
if (len(faces) != 0):
print(file)
for face in faces:
(f, name, prob) = l.search_image(face, DIR, algorithm.get() == 2)
print(name, prob)
# analizes a single image and selects only one face
def analize_single_image():
# ask for image path
image = Filedialog.askopenfilename( initialdir=os.getcwd(), title="Select a File", filetypes=(("All Files", "*.*"), ("png files", "*.png"), ("jpg files", "*.jpg")))
if not image:
return
faces = l.extract_face(DIR, image, confidence_factor.get())
if (len(faces) == 0):
return
# set layout
search_btn.place_forget()
image_label = Label(results_frame)
image_label.pack(side=TOP)
image_btn_var = IntVar()
image_btn = Button(results_frame, text='Next Face', command=lambda: image_btn_var.set(1))
image_btn.place(anchor=SE, relx=0.9, rely=1, relwidth=0.375)
image_btn_stop = Button(results_frame, text='Search This Face', command=lambda: image_btn_var.set(2))
image_btn_stop.place(anchor=SW, relx=0.1, rely=1, relwidth=0.375)
# calculate max resolution posible for image
res = int(min(results_frame.winfo_height(), results_frame.winfo_width()) * 0.8)
# after extract get each face weait for button and save
i = 1
for face in faces:
# disables next on last or only face
if (i == len(faces)):
image_btn.configure(state=DISABLED)
i += 1
search_img = ImageTk.PhotoImage(Image.fromarray(face).resize((res,res),1))
image_label.config(image=search_img)
final_face = face
image_btn.wait_variable(image_btn_var)
if (image_btn_var.get() == 2):
break
# unloads al widgets
image_label.destroy()
image_btn.destroy()
image_btn_stop.destroy()
# searches and shows results of the face
search_coincidences(final_face)
results_frame = Frame(search_frame, bg=W_BGCOL)
results_frame.place(relx=0.1, rely=0.05, relwidth=0.8, relheight=0.9)
search_btn = Button(search_frame, text='Select Image ...', command=analize_single_image)
# search_btn = Button(search_frame, text='Select Image ...', command=analize_directory)
search_btn.place(relx=0.5, rely=0.5, anchor=CENTER)
# checks if calculated data is actualized
def deactivate_search():
eigenvector_file = os.path.join(DIR, 'eigenvector.npy')
if not os.path.exists(eigenvector_file):
return True
time = 0
files = glob.glob(DIR + '/persons/*/*.npy')
for f in files:
file_time = os.path.getmtime(f)
if file_time > time:
time = file_time
return time < os.path.getmtime(eigenvector_file)
if deactivate_search():
search_btn.configure(state=DISABLED)
####################### configurations layout #######################
# algorithm selection frame and layout
algorithm = IntVar()
alg_frame = Frame(config_frame, bg=W_BGCOL)
alg_frame.place(relx=0.05, rely=0.2, relheight=0.8, relwidth=0.2)
label = Label(alg_frame, text='Pre-Processing Algorithm', bg=W_BGCOL, fg=W_FGCOL)
label.pack(anchor=CENTER)
pca_btn = Radiobutton(alg_frame, text='PCA', variable=algorithm, value=1, bg=W_BGCOL, fg=W_FGCOL, highlightbackground=W_BGCOL, selectcolor=W_BGCOL)
pca_btn.pack(anchor=W)
kpca_btn = Radiobutton(alg_frame, text='KPCA', variable=algorithm, value=2, bg=W_BGCOL, fg=W_FGCOL, highlightbackground=W_BGCOL, selectcolor=W_BGCOL)
kpca_btn.pack(anchor=W)
algorithm.set(1)
# k selection fram and layout
k_frame = Frame(config_frame, bg=W_BGCOL)
k_frame.place(relx=0.3, rely=0.25, relheight=0.5, relwidth=0.2)
label = Label(k_frame, text='K Value', bg=W_BGCOL, fg=W_FGCOL)
label.pack(anchor=CENTER)
k_value = IntVar()
scale = Scale(k_frame, variable = k_value, resolution=1, orient=HORIZONTAL, from_=0, to=100, bg=W_BGCOL, fg=W_FGCOL, highlightbackground=W_BGCOL)
scale.pack(anchor=CENTER, fill=X)
k_value.set(60)
# confidence factor selection fram and layout
c_frame = Frame(config_frame, bg=W_BGCOL)
c_frame.place(relx=0.55, rely=0.25, relheight=0.5, relwidth=0.2)
label = Label(c_frame, text='Confidence Factor', bg=W_BGCOL, fg=W_FGCOL)
label.pack(anchor=CENTER)
confidence_factor = DoubleVar()
scale = Scale(c_frame, variable=confidence_factor, orient=HORIZONTAL, resolution=0.1, from_=0, to=1, bg=W_BGCOL, fg=W_FGCOL, highlightbackground=W_BGCOL)
scale.pack(anchor=CENTER, fill=X)
confidence_factor.set(0.2)
# calculate button
cal_frame = Frame(config_frame, bg=W_BGCOL)
cal_frame.place(relx=0.80, rely=0.1, relheight=0.8, relwidth=0.15)
def change_search_btn_name():
calculate_btn.configure(text ="Preprocess Data")
# used down below
def wrapper():
search_btn.configure(state=NORMAL)
l.calculate(DIR, algorithm.get() == 2, k_value.get())
calculate_btn.configure(text='Data Processed!')
now = datetime.now()
run_at = now + timedelta(0, 3)
delay = (run_at - now).total_seconds()
threading.Timer(delay, change_search_btn_name).start()
calculate_btn = Button(cal_frame, text ="Preprocess Data", relief=RAISED, borderwidth=0, command=wrapper)
calculate_btn.pack(side=LEFT, fill=X)
####################### loads layout #######################
# analizes and saves images and bla
def analize_images():
# get directory path
path = Filedialog.askdirectory(initialdir=os.getcwd(), title="Select a Folder or File")
if not path:
return
# get all images paths (names)
types = ('/*.png', '/*.jpg', '/*.jpeg') # the tuple of file types
images = []
for t in types:
images.extend(glob.glob(path + t))
# do only when images is not empty
if (len(images) == 0):
return
# forget button and load image label
load_btn.place_forget()
image_label = Label(face_frame)
image_label.pack(side=TOP)
image_ety_var = StringVar()
image_entry = Entry(face_frame, textvariable=image_ety_var)
image_entry.place(anchor=W, relx=0, rely=0.915, relwidth=0.7, relheight=0.07)
image_btn_var = IntVar()
image_btn = Button(face_frame, text='Save Face', command=lambda: image_btn_var.set(1))
image_btn.place(anchor=SE, relx=1, rely=.90, relwidth=0.25, relheight=0.07)
image_btn_stop = Button(face_frame, text='Quit Loading', command=lambda: image_btn_var.set(2))
image_btn_stop.place(anchor=SE, relx=1, rely=1, relwidth=0.25, relheight=0.07)
# calculate max resolution posible for image
res = int(min(face_frame.winfo_height(), face_frame.winfo_width()) * 0.8)
for image in images:
faces = l.extract_face(DIR, image, confidence_factor.get())
# after extract get each face weait for button and save
for face in faces:
load_img = ImageTk.PhotoImage(Image.fromarray(face).resize((res,res),1))
image_label.config(image=load_img)
image_btn.wait_variable(image_btn_var)
if (image_btn_var.get() == 2):
break
if (len(image_ety_var.get()) > 0):
l.save_face(face, image_ety_var.get(), DIR)
search_btn.configure(state=DISABLED)
image_ety_var.set('')
if (image_btn_var.get() == 2):
break
# when no images or quit loading
image_label.destroy()
image_btn.destroy()
image_btn_stop.destroy()
image_entry.destroy()
load_btn.place(relx=0.5, rely=0.5, anchor=CENTER)
face_frame = Frame(load_frame, bg=W_BGCOL)
face_frame.place(relx=0.1, rely=0.05, relwidth=0.8, relheight=0.9)
load_btn = Button(face_frame, text='Select Image Folder...', command=analize_images)
load_btn.place(relx=0.5, rely=0.5, anchor=CENTER)
root.mainloop()