-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
335 lines (274 loc) · 11.4 KB
/
app.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import streamlit as st
import cv2
import os
from my_utils import alignment_procedure
from mtcnn import MTCNN
import glob
import ArcFace
import numpy as np
import keras
from keras import layers, Sequential
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
from keras.models import load_model
st.title('Face Recognition System')
os.makedirs('data', exist_ok=True)
name_list = os.listdir('data')
# Data Collection
# st.sidebar.title('Data Collection')
webcam_channel = st.sidebar.selectbox(
'Webcam Channel:',
('Select Channel', '0', '1', '2', '3')
)
name_person = st.text_input('Name of the Person:')
img_number = st.number_input('Number of Images:', 50)
FRAME_WINDOW = st.image([])
if not webcam_channel == 'Select Channel':
take_img = st.button('Take Images')
if take_img:
if len(name_list) != 0:
for i in name_list:
if i == name_person:
st.warning('The Name is Already Exist!!')
break
os.mkdir(f'data/{name_person}')
st.success(f'{name_person} added Successfully')
if len(os.listdir(f'data/{name_person}')) == 0:
face_classifier = cv2.CascadeClassifier(
'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(int(webcam_channel))
count = 0
while True:
success, img = cap.read()
if not success:
st.error('[INFO] Cam NOT working!!')
break
# Save Image
cv2.imwrite(f'data/{name_person}/{count}.jpg', img)
st.success(f'[INFO] Successfully Saved {count}.jpg')
count += 1
faces = face_classifier.detectMultiScale(img)
for (x, y, w, h) in faces:
cv2.rectangle(
img, (x, y), (x+w, y+h),
(0, 255, 0), 2
)
FRAME_WINDOW.image(img, channels='BGR')
if count == img_number:
st.success(f'[INFO] Collected {img_number} Images')
break
FRAME_WINDOW.image([])
cap.release()
cv2.destroyAllWindows()
else:
st.warning('[INFO] Select Camera Channel')
# 2nd Stage - Normalize Image Data
st.sidebar.title('Normalize Image Data')
if st.sidebar.button('Normalize'):
path_to_dir = "data"
path_to_save = 'norm_data'
Flage = True
detector = MTCNN()
class_list_update = []
if os.path.exists(path_to_save):
class_list_save = os.listdir(path_to_save)
class_list_dir = os.listdir(path_to_dir)
class_list_update = list(set(class_list_dir) ^ set(class_list_save))
else:
os.makedirs(path_to_save)
if len(class_list_update) == 0:
if (set(class_list_dir) == set(class_list_save)):
Flage = False
else:
class_list = os.listdir(path_to_dir)
else:
class_list = class_list_update
if Flage:
class_list = sorted(class_list)
for name in class_list:
st.success(f"[INFO] Class '{name}' Started Normalising")
img_list = glob.glob(os.path.join(path_to_dir, name) + '/*')
# Create Save Folder
save_folder = os.path.join(path_to_save, name)
os.makedirs(save_folder, exist_ok=True)
for img_path in img_list:
img = cv2.imread(img_path)
detections = detector.detect_faces(img)
if len(detections) > 0:
right_eye = detections[0]['keypoints']['right_eye']
left_eye = detections[0]['keypoints']['left_eye']
bbox = detections[0]['box']
norm_img_roi = alignment_procedure(
img, left_eye, right_eye, bbox)
# Save Norm ROI
cv2.imwrite(
f'{save_folder}/{os.path.split(img_path)[1]}', norm_img_roi)
# st.success(f'[INFO] Successfully Normalised {img_path}')
else:
st.warning(f'[INFO] Not detected Eyes in {img_path}')
st.success(
f"[INFO] All Normalised Images from '{name}' Saved in '{path_to_save}'")
st.success(
f'[INFO] Successfully Normalised All Images from {len(os.listdir(path_to_dir))} Classes\n')
else:
st.warning('[INFO] Already Normalized All Data..')
# 3rd Stage - Train Model
st.sidebar.title('Train Model')
if st.sidebar.button('Train Model'):
path_to_dir = "norm_data"
path_to_save = 'model.h5'
# Load ArcFace Model
model = ArcFace.loadModel()
target_size = model.layers[0].input_shape[0][1:3]
# Variable for store img Embedding
x = []
y = []
names = os.listdir(path_to_dir)
names = sorted(names)
class_number = len(names)
for name in names:
img_list = glob.glob(os.path.join(path_to_dir, name) + '/*')
img_list = sorted(img_list)
st.success(f'[INFO] Started Embedding {name} Class')
for img_path in img_list:
img = cv2.imread(img_path)
img_resize = cv2.resize(img, target_size)
# what this line doing? must?
img_pixels = tf.keras.preprocessing.image.img_to_array(img_resize)
img_pixels = np.expand_dims(img_pixels, axis=0)
img_norm = img_pixels/255 # normalize input in [0, 1]
img_embedding = model.predict(img_norm)[0]
x.append(img_embedding)
y.append(name)
st.success(f'[INFO] Completed Embedding {name} Class')
st.success('[INFO] All Image Data Embedding Completed...')
# Model Training
# DataFrame
df = pd.DataFrame(x, columns=np.arange(512))
df['names'] = y
x = df.copy()
y = x.pop('names')
y, _ = y.factorize()
x = x.astype('float64')
y = keras.utils.to_categorical(y)
# Train Deep Neural Network
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.2,
random_state=0)
model = Sequential([
layers.Dense(1024, activation='relu', input_shape=[512]),
layers.Dense(512, activation='relu'),
layers.Dense(class_number, activation="softmax")
])
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Add a checkpoint callback to store the checkpoint that has the highest
# validation accuracy.
checkpoint_path = path_to_save
checkpoint = keras.callbacks.ModelCheckpoint(checkpoint_path,
monitor='val_accuracy',
verbose=1,
save_best_only=True,
mode='max')
earlystopping = keras.callbacks.EarlyStopping(monitor='val_accuracy',
patience=20)
st.success('[INFO] Model Training Started ...')
# Start training
history = model.fit(x_train, y_train,
epochs=200,
batch_size=16,
validation_data=(x_test, y_test),
callbacks=[checkpoint, earlystopping])
st.success('[INFO] Model Training Completed')
st.success(f'[INFO] Model Successfully Saved in ./{path_to_save}')
# Plot History
metric_loss = history.history['loss']
metric_val_loss = history.history['val_loss']
metric_accuracy = history.history['accuracy']
metric_val_accuracy = history.history['val_accuracy']
# Construct a range object which will be used as x-axis (horizontal plane) of the graph.
epochs = range(len(metric_loss))
# Plot the Graph.
plt.plot(epochs, metric_loss, 'blue', label=metric_loss)
plt.plot(epochs, metric_val_loss, 'red', label=metric_val_loss)
plt.plot(epochs, metric_accuracy, 'blue', label=metric_accuracy)
plt.plot(epochs, metric_val_accuracy, 'green', label=metric_val_accuracy)
# Add title to the plot.
plt.title(str('Model Metrics'))
# Add legend to the plot.
plt.legend(['loss', 'val_loss', 'accuracy', 'val_accuracy'])
# If the plot already exist, remove
plot_png = os.path.exists('metrics.png')
if plot_png:
os.remove('metrics.png')
plt.savefig('metrics.png', bbox_inches='tight')
else:
plt.savefig('metrics.png', bbox_inches='tight')
st.success('[INFO] Successfully Saved metrics.png')
# 4th Stage - Inference
st.sidebar.title('Inference')
# Confidence
threshold = st.sidebar.slider('Model Confidence:', 0.01, 0.99, 0.6)
if st.sidebar.button('Run/Stop'):
class_names = os.listdir('data')
class_names = sorted(class_names)
if not webcam_channel == 'Select Channel':
path_saved_model = "model.h5"
cap = cv2.VideoCapture(int(webcam_channel))
# Load MTCNN
detector = MTCNN()
arcface_model = ArcFace.loadModel()
target_size = arcface_model.layers[0].input_shape[0][1:3]
# Load saved FaceRecognition Model
face_rec_model = load_model(path_saved_model, compile=True)
while True:
success, img = cap.read()
if not success:
st.warning('[INFO] Error with Camera')
break
detections = detector.detect_faces(img)
if len(detections) > 0:
for detect in detections:
right_eye = detect['keypoints']['right_eye']
left_eye = detect['keypoints']['left_eye']
bbox = detect['box']
xmin, ymin, xmax, ymax = int(bbox[0]), int(bbox[1]), \
int(bbox[2]+bbox[0]), int(bbox[3]+bbox[1])
norm_img_roi = alignment_procedure(
img, left_eye, right_eye, bbox)
img_resize = cv2.resize(norm_img_roi, target_size)
# what this line doing? must?
img_pixels = tf.keras.preprocessing.image.img_to_array(img_resize)
img_pixels = np.expand_dims(img_pixels, axis=0)
img_norm = img_pixels/255 # normalize input in [0, 1]
img_embedding = arcface_model.predict(img_norm)[0]
data = pd.DataFrame(
[img_embedding], columns=np.arange(512))
predict = face_rec_model.predict(data)[0]
# print(predict)
if max(predict) > threshold:
pose_class = class_names[predict.argmax()]
else:
pose_class = 'Unkown Person'
# Show Result
cv2.rectangle(
img, (xmin, ymin), (xmax, ymax),
(0, 255, 0), 2
)
cv2.putText(
img, f'{pose_class}',
(xmin, ymin-10), cv2.FONT_HERSHEY_PLAIN,
2, (255, 0, 255), 2
)
else:
st.warning('[INFO] Eyes Not Detected!!')
FRAME_WINDOW.image(img, channels='BGR')
FRAME_WINDOW.image([])
st.success('[INFO] Inference on Videostream is Ended...')
else:
st.warning('[INFO] Select Camera Channel')