-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd new emploey.py
287 lines (245 loc) · 11.2 KB
/
add new emploey.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
283
284
285
286
287
import PySimpleGUI as sg
import os
import pandas as pd
from sklearn.preprocessing import Normalizer
import numpy as np
from PIL import Image
from mtcnn.mtcnn import MTCNN
import tensorflow as tf
import sys
from pathlib import Path
sg.change_look_and_feel('DarkBlue2')
if getattr(sys, 'frozen', False):
root_path = sys._MEIPASS
facenet_path = os.path.join(root_path, 'facenet_keras.h5')
icon_path = os.path.join(root_path, 'WAI.ico')
image_path = os.path.join(root_path, 'WAIcheck.png')
else:
facenet_path = 'facenet_keras.h5'
icon_path = 'WAI.ico'
image_path = 'WAIcheck.png'
sg.popup_animated(image_path,
background_color='black',
message='Please Wait, Loading…',
icon=icon_path )
# load facenet model
tf.keras.backend.clear_session()
facenet = tf.keras.models.load_model(facenet_path)
sg.popup_animated(image_source=None,icon=icon_path )
def extract_face(filename, required_size=(160, 160)):
# load image from file
image = Image.open(filename)
# convert to RGB, if needed
image = image.convert('RGB')
# convert to array
pixels = np.asarray(image)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
results = detector.detect_faces(pixels)
# extract the bounding box from the first face
x1, y1, width, height = results[0]['box']
# bug fix
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
# extract the face
face = pixels[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = np.asarray(image)
return face_array
def get_embedding(face_pixels, facenet):
# scale pixel values
face_pixels = face_pixels.astype('float32')
# standardize pixel values across channels (global)
mean, std = face_pixels.mean(), face_pixels.std()
face_pixels = (face_pixels - mean) / std
# transform face into one sample
samples = np.expand_dims(face_pixels, axis=0)
yhat = facenet.predict(samples)
return yhat[0]
dont_allow_save = True
dont_allow_save_del = True
dont_allow_dep_del = True
dont_select_id = True
program_folder=os.path.join(Path.home(),"who_am_i")
if not os.path.exists(program_folder):
os.makedirs(program_folder)
if not os.path.isfile(os.path.join(program_folder, "Database.pkl")):
Database = pd.DataFrame(columns=['Name', 'ID',
"Department", "Embedding"])
Database.to_pickle(os.path.join(program_folder, "Database.pkl"))
else:
Database = pd.read_pickle(os.path.join(program_folder, "Database.pkl"))
Department_ids = Database.Department.unique().tolist()
id_in_depar = []
tab1_layout = [[sg.Text(key='-EXPAND-0',
pad=(0, 10))],
[sg.Text("Name:",
justification='right',
size=(11, None)),
sg.In(size=(25, 1),
enable_events=True,
key="-Name-"), ],
[sg.Text("ID:",
justification='right',
size=(11, None)),
sg.In(size=(25, 1),
enable_events=True,
key="-ID-"), ],
[sg.Text("Department:",
justification='right',
size=(11, None)),
sg.Combo(values=Department_ids,
size=(25, 1),
enable_events=True,
key="-Depar-")],
[sg.Text("Upload Image:",
justification='right',
size=(11, None)),
sg.In(size=(25, 1),
enable_events=True,
key="-Image-PATH-",
disabled=True),
sg.FileBrowse(file_types=(("Image Files", "*.jpg"),
("PNG Files", "*.png"))), ],
[sg.Button('Save',
bind_return_key=True,
size=(20, None),
pad=(140, 0),
disabled=dont_allow_save)],
[sg.Text(key='-EXPAND-3',
pad=(0, 10))]]
tab2_layout = [[sg.Text(key='-EXPAND-2',
pad=(0, 20))],
[sg.Text("Department:",
justification='right',
size=(10, None),
pad=(20, 0)),
sg.Combo(values=Department_ids,
size=(25, 1),
enable_events=True,
readonly=True,
key="-depardelete-")],
[sg.Text("ID:",
justification='right',
size=(10, None),
pad=(20, 0)),
sg.Combo(values=id_in_depar,
readonly=True,
size=(25, 1),
enable_events=True,
disabled=dont_select_id,
key="-IDdelete-")],
[sg.Button('Delete from Database',
bind_return_key=True,
pad=(160, 0),
disabled=dont_allow_save_del)], ]
tab3_layout = [[sg.Text(key='-EXPAND-',
pad=(0, 10))],
[sg.Text("Department to Delete:",
justification='right',
pad=(160, 0))],
[sg.Combo(values=Department_ids,
size=(25, 1),
enable_events=True,
pad=(120, 0),
readonly=True,
key="-departocompletedelete-")],
[sg.Button('Delete Department from Database',
pad=(120, 0),
bind_return_key=True,
disabled=dont_allow_dep_del)], ]
# ----- Full layout -----
layout = [[sg.TabGroup([[sg.Tab('Add New Employee',
tab1_layout),
sg.Tab('Delete Employee',
tab2_layout),
sg.Tab('Delete Department',
tab3_layout)]])]]
window = sg.Window("Human Resource Managment",
layout, icon=icon_path)
# Run the Event Loop
while True:
event, values = window.read()
if event == "-departocompletedelete-":
if values["-departocompletedelete-"]:
dont_allow_dep_del = False
window.Element('Delete Department from Database').Update(disabled=dont_allow_dep_del)
if event == 'Delete Department from Database':
confirm_delete = sg.popup_ok_cancel('Are you sure?',icon=icon_path)
if confirm_delete == 'OK':
Database = Database[Database.Department != values["-departocompletedelete-"]]
Database.to_pickle(os.path.join(program_folder, "Database.pkl"))
Department_ids = Database.Department.unique().tolist()
window.Element("-Depar-").Update(values=Department_ids)
window.Element("-depardelete-").Update(values=Department_ids)
window.Element("-departocompletedelete-").Update(values=Department_ids)
window.Element("-Depar-").Update("")
window.Element("-depardelete-").Update("")
window.Element("-departocompletedelete-").Update("")
sg.popup("Department " + values["-departocompletedelete-"] + " deleted",icon=icon_path)
if event == "-depardelete-":
dont_select_id = False
window.Element("-IDdelete-").Update(disabled=dont_select_id)
id_in_depar = Database.ID[Database.Department == values["-depardelete-"]].tolist()
window.Element("-IDdelete-").Update("")
window.Element("-IDdelete-").Update(values=id_in_depar)
if event == "-IDdelete-":
dont_allow_save_del = False
window.Element("Delete from Database").Update(disabled=dont_allow_save_del)
if event == 'Delete from Database':
confirm = sg.popup_ok_cancel('Are you sure?',icon=icon_path)
if confirm == 'OK':
Database = Database[Database.ID != values["-IDdelete-"]]
id_in_depar = Database.ID[Database.Department == values["-depardelete-"]].tolist()
Database.to_pickle(os.path.join(program_folder, "Database.pkl"))
window.Element("-IDdelete-").Update(values=id_in_depar)
window.Element("-IDdelete-").Update("")
dont_allow_save_del = True
window.Element("Delete from Database").Update(disabled=dont_allow_save_del)
if (event == "-Name-" or event == "-ID-" or event == "-Depar-" or event == "-Image-PATH-") and \
(values["-Name-"] and values["-ID-"] and values["-Depar-"] and values["-Image-PATH-"]):
dont_allow_save = False
window.Element("Save").Update(disabled=dont_allow_save)
if event == 'Save':
if values["-Depar-"] in Department_ids and \
values["-ID-"] in Database.ID[Database.Department == values["-Depar-"]].tolist():
sg.popup("ID ERROR:", "This ID already registered in database",icon=icon_path)
window.Element("-ID-").Update("")
continue
try:
face_array = extract_face(values["-Image-PATH-"], required_size=(160, 160))
embedded_face = get_embedding(face_array, facenet)
in_encoder = Normalizer(norm='l2')
embedded = np.expand_dims(embedded_face, axis=0)
embedded = in_encoder.transform(embedded)
except:
sg.popup("Image ERROR:", "Try another picture",icon=icon_path)
window.Element("-Image-PATH-").Update("")
continue
row = pd.DataFrame([[values["-Name-"],
values["-ID-"],
values["-Depar-"],
embedded]],
columns=['Name', 'ID',
"Department", "Embedding"])
Database = Database.append(row, ignore_index=True)
Database.to_pickle(os.path.join(program_folder, "Database.pkl"))
sg.popup(values["-Name-"] + " added to " + values["-Depar-"],icon=icon_path)
Department_ids = Database.Department.unique().tolist()
window.Element("-Name-").Update("")
window.Element("-ID-").Update("")
window.Element("-Image-PATH-").Update("")
window.Element("-depardelete-").Update(values=Department_ids)
window.Element("-departocompletedelete-").Update(values=Department_ids)
window.Element("-depardelete-").Update("")
window.Element("-IDdelete-").Update("")
dont_allow_save_del = True
window.Element("Delete from Database").Update(disabled=dont_allow_save_del)
dont_allow_save = True
window.Element("Save").Update(disabled=dont_allow_save)
if event == sg.WIN_CLOSED:
break
window.close()