-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.py
266 lines (211 loc) · 8.19 KB
/
detector.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
import os
import cv2
import numpy as np
from utility import ProgressBar
class Detector:
face_cascade = cv2.CascadeClassifier("Cascade/frontal_face.xml")
face_rectangle_parameters = {
"scaleFactor" : 1.1,
"minNeighbors" : 4
}
model_types = {
"LBPH" : cv2.face.LBPHFaceRecognizer_create(),
"Eigen" : cv2.face.EigenFaceRecognizer_create(),
"Fisher" : cv2.face.FisherFaceRecognizer_create()
}
candidate_names = []
def __init__(self, model_type):
self.load_candidate_names()
self.model = self.model_types.get(model_type, False)
if not self.model:
raise ValueError("Invalid Model Type.")
def update_face_rectangle_parameteres(self, scaleFactor = None, minNeighbors = None):
self.face_rectangle_parameters = {
"scaleFactor" : scaleFactor if scaleFactor is not None else self.face_rectangle_parameters["scaleFactor"],
"minNeighbors" : minNeighbors if minNeighbors is not None else self.face_rectangle_parameters["minNeighbors"]
}
def get_face_rectangle(self, image):
grayed_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_rectangle = self.face_cascade.detectMultiScale(
grayed_image,
self.face_rectangle_parameters["scaleFactor"],
self.face_rectangle_parameters["minNeighbors"]
)
# If no face is detected, returning x, y, width and height of rectangle as NoneType
if not len(face_rectangle):
return None, None, None, None
# Returning rectangle of only the first face that is detected
return face_rectangle[0]
def is_face_rectangle_detected(self, face_rectangle):
return all(parameter is not None for parameter in face_rectangle)
def make_box_indicator(self, image, label_one = "", label_two = ""):
rectangle_color = (0, 255, 0)
rectangle_thickness = 2
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.5
font_color = (0, 255, 0)
font_thickness = 1
face_rectangle = self.get_face_rectangle(image)
if not self.is_face_rectangle_detected(face_rectangle):
return image
(
face_rect_x,
face_rect_y,
face_rect_width,
face_rect_height
) = self.get_face_rectangle(image)
cv2.rectangle(
image,
(face_rect_x, face_rect_y),
(face_rect_x + face_rect_width, face_rect_y + face_rect_height),
rectangle_color,
thickness = rectangle_thickness
)
cv2.putText(
image,
label_one,
(
face_rect_x,
face_rect_y + face_rect_height + 20
),
font,
font_scale,
font_color,
font_thickness,
cv2.LINE_AA
)
cv2.putText(
image,
label_two,
(
face_rect_x,
face_rect_y + face_rect_height + 40
),
font,
font_scale,
font_color,
font_thickness,
cv2.LINE_AA
)
return image
def label_name(self, image):
candidate_name, confidence = self.predict(image)
confidence = int(round(confidence / 1000, 2)) if confidence is not None else None
# Just for showcase
confidence = confidence + 50 if confidence is not None else None
image = self.make_box_indicator(
image,
candidate_name,
f"Algo's Confidence : { str(confidence) } %"
)
return image
def detect_face(self, image):
output_image_width = 224
output_image_height = 224
face_rectangle = self.get_face_rectangle(image)
# Return nothing, if no face detected, that is,
if not self.is_face_rectangle_detected(face_rectangle):
return np.zeros((
output_image_width,
output_image_height
))
(
face_rect_x,
face_rect_y,
face_rect_width,
face_rect_height
) = face_rectangle
cropped_face_image = image[
face_rect_y : face_rect_y + face_rect_height,
face_rect_x : face_rect_x + face_rect_width
]
cropped_face_image = cv2.resize(cropped_face_image, (
output_image_width,
output_image_height
))
grayed_cropped_face_image = cv2.cvtColor(cropped_face_image, cv2.COLOR_BGR2GRAY)
return grayed_cropped_face_image
def is_image_blank(self, image):
if np.all((image == 0)):
return True
return False
def save_model(self, model_name):
self.model.save(model_name)
def load_model(self, model_name):
self.model.read(model_name)
def load_candidate_names(self):
for candidate_name in os.listdir("Database"):
self.candidate_names.append(candidate_name)
def train_model(self):
faces = []
names = []
# Names are encoded according to the index of names in self.candidate_names global variable
encoded_names_index = []
for candidate_name in os.listdir("Database"):
candidate_location = os.path.join("Database", candidate_name)
for image_index, image in enumerate(os.listdir(candidate_location)):
# Progress Bar
total_images = len(os.listdir(candidate_location))
progress_bar = ProgressBar(f"Loading Image Sample of {candidate_name}")
progress_bar.set_progress(image_index, total_images)
progress_bar.print_loader()
image_location = os.path.join(candidate_location, image)
image = cv2.imread(image_location)
face = self.detect_face(image)
# Don't include this sample in training data if no face is detected
if self.is_image_blank(face):
continue
faces.append(face)
names.append(candidate_name)
for name in names:
encoded_name_index = self.candidate_names.index(name)
encoded_names_index.append(encoded_name_index)
# Training faces piece-wise one at a time
for index, face in enumerate(faces):
total = len(faces)
progress = index
progress_bar = ProgressBar("Training Dataset")
progress_bar.set_progress(progress, total)
progress_bar.print_loader()
self.model.train(
[ face ],
np.array(encoded_names_index[index]),
)
def predict(self, image):
face = self.detect_face(image)
# If no face detected, return candidate name and confidence as NoneType
if self.is_image_blank(face):
return None, None
encoded_name_index, confidence = self.model.predict(face)
candidate_name = self.candidate_names[encoded_name_index]
return candidate_name, confidence
def test_model(self):
test_images = []
for test_image in os.listdir("Testing"):
image_location = os.path.join("Testing", test_image)
image = cv2.imread(image_location)
test_images.append(image)
output = []
for test_image in test_images:
predicted_name, confidence = self.predict(test_image)
output.append([
predicted_name,
confidence
])
return output
def pretty_print_test_results(self, results):
index = 1
for result in results:
candidate_name = result[0]
confidence = result[1]
if candidate_name == None:
candidate_name = "Can't Detect"
if confidence == None:
confidence = "Can't Detect"
print(f"""
{index}
--------------------
Candidate Name : {candidate_name}
Confidence : {confidence}
""")
index += 1