-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_profile.py
executable file
·234 lines (170 loc) · 7.7 KB
/
deep_profile.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
import os
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
#os.environ["CUDA_VISIBLE_DEVICES"] = ""
#os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import cv2
import dlib
import numpy as np
from datetime import datetime
from list_face import ListFace
from list_face import Face
try:
import face_recognition_models
except:
print("Please install `face_recognition_models` with this command before using `face_recognition`:")
print()
print("pip install git+https://github.com/ageitgey/face_recognition_models")
quit()
from wide_resnet import WideResNet
class DeepProfile:
def __init__(self, ignore_gender=False):
self.file_log = open("log/file_log", "a")
self.img_size = 64
self.model = WideResNet(self.img_size, depth=16, k=8)()
self.model.load_weights(os.path.join("pretrained_models", "weights.28-3.73.hdf5"))
self.image = None
self.detector = dlib.get_frontal_face_detector()
self.face_recognition_model = face_recognition_models.face_recognition_model_location()
self.face_encoder = dlib.face_recognition_model_v1(self.face_recognition_model)
self.list_face = []
self.preload_list_face = True
self.logs = []
self.ignore_gender = ignore_gender
def reset(self):
self.list_face = []
self.logs = []
self.image = None
self.preload_list_face = True
def profiles_image(self, image):
self.logs = []
self.image = image
# Detecte faces in image
faces_detected = self.detecte_faces(self.image)
# Matrix to faces in image
faces = np.empty((len(faces_detected), self.img_size, self.img_size, 3))
# Print retaatangle in face and get face in 64x64
for i, d in enumerate(faces_detected):
faces[i,:,:,:] = self.copy_face_in_image(self.image, d)
# Predict ages and genders of the detected faces
result, genders_values, genders, ages = self.predict(faces)
if result:
for i, d in enumerate(faces_detected):
face_encode = self.face_encode(self.image, d)
l_face = self.add_list_face(self.image, d, genders_values[i], genders[i], ages[i])
self.draw_box(self.image, d, l_face)
#self.draw_box(self.image, d, genders[i], ages[i])
self.preload_list_face = False
self.logs.append("Faces: {}".format(len(self.list_face)))
#for l_face in self.list_face:
# self.logs.append("Face -> : {}, {} ( {} )".format(l_face.avg_age(), l_face.avg_gender(), int(l_face.gender_confidence()*100)))
self.display_log(self.image)
return self.image
def add_list_face(self, image, face, gender_value, gender, ages):
x1, y1, x2, y2, w, h = face.left(), face.top(), face.right() + 1, face.bottom() + 1, face.width(), face.height()
# Face
face = Face([x1, y1], [x2, y2], gender_value, gender, int(ages))
now_list_face = None
# # find list face of face
# for l_face in self.list_face:
# if l_face.is_intersection(face):
# now_list_face = l_face
# break
# find list face of face, the bigest area of intersection
if len(self.list_face) > 0 and not (self.preload_list_face):
faces_area = [l_face.intersection_area(face) for l_face in self.list_face]
arg_max = np.argmax(faces_area)
if faces_area[arg_max] > 0:
now_list_face = self.list_face[arg_max]
# if not found
if now_list_face == None:
now_list_face = ListFace() #new
self.list_face.append(now_list_face)
now_list_face.add_face(face)
return now_list_face
def predict(self, faces):
results = self.model.predict(faces)
if len(results) > 0:
# Gender
predicted_genders = [self.get_gender(g[0]) for g in results[0]]
# Gender Value
predicted_genders_v = [g[0] for g in results[0]]
# Age
ages = np.arange(0, 101).reshape(101, 1)
pred_ages = results[1].dot(ages).flatten()
pred_ages = [int(age) for age in pred_ages]
return True, predicted_genders_v, predicted_genders, pred_ages
else:
return False, [], [], []
def get_gender(self, value):
return "M" if value >= 0.5 else "H"
def detecte_faces(self, image):
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
input_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# detect faces using dlib detector
detected = self.detector(input_image, 1)
return detected
def copy_face_in_image(self, image, face):
image_h, image_w, _ = np.shape(image)
x1, y1, x2, y2, w, h = face.left(), face.top(), face.right() + 1, face.bottom() + 1, face.width(), face.height()
xw1 = max(int(x1 - 0.4 * w), 0)
yw1 = max(int(y1 - 0.8 * h), 0)
xw2 = min(int(x2 + 0.4 * w), image_w - 1)
yw2 = min(int(y2 + 0.4 * h), image_h - 1)
return cv2.resize(image[yw1:yw2 + 1, xw1:xw2 + 1, :], (self.img_size, self.img_size))
def face_encode(self, image, face):
image_h, image_w, _ = np.shape(image)
x1, y1, x2, y2, w, h = face.left(), face.top(), face.right() + 1, face.bottom() + 1, face.width(), face.height()
xw1 = max(int(x1 - 0.4 * w), 0)
yw1 = max(int(y1 - 0.8 * h), 0)
xw2 = min(int(x2 + 0.4 * w), image_w - 1)
yw2 = min(int(y2 + 0.4 * h), image_h - 1)
cropped_face = image[yw1:yw2 + 1, xw1:xw2 + 1]
face_encoding = np.ones(5)#face_recognition.face_encodings(cropped_face)[0]
return face_encoding
def draw_box(self, image, face, l_face):
image_h, image_w, _ = np.shape(image)
x1, y1, x2, y2, w, h = face.left(), face.top(), face.right() + 1, face.bottom() + 1, face.width(), face.height()
#x1 = max(int(x1 - 0.4 * w), 0)
#y1 = max(int(y1 - 0.8 * h), 0)
#x2 = min(int(x2 + 0.4 * w), image_w - 1)
#y2 = min(int(y2 + 0.4 * h), image_h - 1)
if l_face.avg_gender() == 'M' or self.ignore_gender:
color = (0, 0, 255)
else:
color = (255, 0, 0)
self.draw_label(image, face, l_face, color)
self.draw_rectangle(image, (x1, y1), (x2, y2), color)
def draw_rectangle(self, image, point1, point2, color):
cv2.rectangle(image, point1, point2, color, 2, cv2.LINE_AA)
def draw_label(self, image, face, l_face, color,
font=cv2.FONT_HERSHEY_DUPLEX,
font_scale=0.5, thickness=1):
point = (face.left(), face.top())
image_h, image_w, _ = np.shape(image)
lbl_gender = "Mulher" if l_face.avg_gender() == 'M' else "Homem"
lbl_gender = l_face.avg_gender_name()
conf_gender = int(l_face.gender_confidence()*100)
label2 = "{} ({}%)".format(lbl_gender, conf_gender)
label1 = "{} anos".format(l_face.avg_age())
size = max(cv2.getTextSize(label1, font, font_scale, thickness)[0], cv2.getTextSize(label2, font, font_scale, thickness)[0])
#size = cv2.getTextSize(label1, font, font_scale, thickness)[0]
x, y = point
y -= 7
x -= 1
d = 2 if not self.ignore_gender else 1
cv2.rectangle(image, (x, y - d*size[1]-5), (x + size[0]+d, y+5), (0,0,0), cv2.FILLED)
cv2.putText(image, label1, (x, y), font, font_scale, (255, 255, 255), thickness)
if not self.ignore_gender:
cv2.putText(image, label2, (x, y-size[1]-5), font, font_scale, (255, 255, 255), thickness)
def display_log(self, image, font=cv2.FONT_HERSHEY_DUPLEX,
font_scale=0.6, thickness=1):
x = 0
y = 15
for log in self.logs:
cv2.putText(image, log, (x, y), font, font_scale, (0, 0, 0), thickness)
y = y + cv2.getTextSize(log, font, font_scale, thickness)[0][1]
def print_log(self, gender, ages, face_encode):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
face = ",".join([str(i) for i in face_encode])
output = "{}, {}, {}, {}\n".format(now, gender, ages, face)
self.file_log.write(output)