-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict_face.py
259 lines (206 loc) Β· 7.22 KB
/
predict_face.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
import cv2
import pickle
import face_recognition
import argparse
import numpy as np
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help="Path to image for the prediction")
ap.add_argument("-v", "--video", help="Path to video")
ap.add_argument("-e", "--encode", help="Path saved encodings")
args = vars(ap.parse_args())
if args['encode'] is None:
prepared_data_path = "data/prepared_data/saved_encodings_1"
else:
prepared_data_path = args['encode']
MAX_HEIGHT = 800
MAX_WIDTH = 800
saved_encodings = None
# Name if face not Identified
UNKNOWN = "unknown"
def is_image_big(image):
"""
Check if image height or width greater than MAX HEIGHT or MAX WIDTH
:param image:
:return:
"""
if image.shape[0] > MAX_HEIGHT or image.shape[1] > MAX_WIDTH:
return True
return False
def resize_image(image):
"""
Resizing image smaller for faster processing
:param image:
:return: resized_image, product (which can be multiplied to bring back to original shape)
"""
height = image.shape[0]
width = image.shape[1]
if width > height:
diff_product = MAX_WIDTH / width
product = width / MAX_WIDTH
else:
diff_product = MAX_HEIGHT / height
product = height / MAX_HEIGHT
new_height = int(diff_product * height)
new_width = int(diff_product * width)
image = cv2.resize(image, (new_width, new_height))
return image, product
def detect_face(image, method="hog"):
"""
Given an image searches for face
:param image:
:param method: 1) cnn: more accurate but slow, good for training
2) hog: less accurate but fast, good for real time predicting
:return: coordinates list of the faces [( X1, Y2, X2, Y1 ) ,.... ]- format
"""
bounding_boxes = face_recognition.face_locations(image, model=method)
return bounding_boxes
def get_encodings(image, bounding_boxes):
"""
Given an image and the coordinates gets 128 features
:param image:
:param bounding_boxes:
:return: 128 features of each cooridinates
"""
encodings = face_recognition.face_encodings(image, bounding_boxes)
return encodings
def load_encodings():
"""
Restore Encodings from the saved directory
:return:
"""
global saved_encodings
saved_encodings = pickle.loads(open(prepared_data_path, "rb").read())
def identify_person(face_encodings, tolerance=0.5):
"""
checks if given encodings matches with saved encodings
:param face_encodings:
:param tolerance: lesser the tolerance more strict of matching face
:return:
"""
names = []
for encoding in face_encodings:
results = face_recognition.compare_faces(saved_encodings["encodings"], encoding, tolerance=tolerance)
results = np.array(results)
name = UNKNOWN
indices = list(np.where(results)[0])
if len(indices):
result_names = [saved_encodings["names"][i] for i in indices]
name = max(set(result_names), key=result_names.count)
names.append(name)
return names
def format_results(names, face_coordinates, product):
"""
Mapping names with the face coordinates,
:param names:
:param face_coordinates:
:param diff_product: Product which will be mutiplied to bring back to original image shape
:return: Dictionary {name: coordinates}
Coordinates format [ x1, y1, x2, y2] where x1 = top, y1 = left
"""
name_coords = {}
unknown_counter = 1
for name, coords in zip(names, face_coordinates):
x1 = int(coords[0] * product)
y2 = int(coords[1] * product)
x2 = int(coords[2] * product)
y1 = int(coords[3] * product)
if name == UNKNOWN:
name = name + "_" + str(unknown_counter)
name_coords[name] = [x1, y1, x2, y2]
unknown_counter += 1
else:
name_coords[name] = [x1, y1, x2, y2]
return name_coords
def predict(image):
"""
Locates multiple face and guesses name from an image
:param image:q
:return: dictionary of {name: coordinates}
Coordinates format [ x1, y1, x2, y2] where x1 = top, y1 = left
"""
global saved_encodings
# Image needs to be in RGB to get encodings
if len(image.shape) < 3:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
product = 1
if is_image_big(image):
image, product = resize_image(image)
# Loading saved faces encodings
if saved_encodings is None:
load_encodings()
face_coordinates = detect_face(image)
face_encodings = get_encodings(image, face_coordinates)
names = identify_person(face_encodings)
result = format_results(names, face_coordinates, product)
return result
def add_name_to_image(image, name_coords):
"""
Draw bounding box on face and add name text
:param image:
:param name_coords:
:return:
"""
if image.shape[0] > 2000 or image.shape[1] > 2000:
rect_line_thickness = 5
text_line_thickness = 3
text_size = 4
elif image.shape[0] > 1000 or image.shape[1] > 1000:
rect_line_thickness = 3
text_line_thickness = 2
text_size = 2
elif image.shape[0] > 5000 or image.shape[1] > 5000:
rect_line_thickness = 2
text_line_thickness = 2
text_size = 1
else:
rect_line_thickness = 1
text_line_thickness = 1
text_size = 0.5
for name, coords in name_coords.items():
if UNKNOWN in name:
cv2.rectangle(image, (coords[1], coords[0]), (coords[3], coords[2]), (0, 0, 255), rect_line_thickness)
cv2.putText(image, name, (coords[1], coords[0]), cv2.FONT_HERSHEY_SIMPLEX, text_size, (0, 0, 255), text_line_thickness)
else:
cv2.rectangle(image, (coords[1], coords[0]), (coords[3], coords[2]), (0, 255, 0), rect_line_thickness)
cv2.putText(image, name, (coords[1], coords[0]), cv2.FONT_HERSHEY_SIMPLEX, text_size, (0, 0, 255), text_line_thickness)
return image
def process_image(image):
name_coords = predict(image)
image = add_name_to_image(image, name_coords)
if is_image_big(image):
image, _ = resize_image(image)
cv2.imshow('vid', image)
cv2.waitKey(0)
def process_video(video, skips=15):
"""
Predicts face from the video
:param video: video object (cv2.VideoCapture)
:param skips: Skips N frames in each iteration
:return:
"""
while True:
for i in range(skips):
flag, image = video.read()
flag, image = video.read()
if not flag:
break
name_coords = predict(image)
if name_coords:
image = add_name_to_image(image, name_coords)
if is_image_big(image):
image, _ = resize_image(image)
cv2.imshow('vid', image)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
if __name__ == "__main__":
if args["video"] is not None:
video = cv2.VideoCapture(args["video"])
process_video(video)
elif args["image"] is not None:
image = cv2.imread(args["image"])
process_image(image)
else:
print("Please provide path to image or video:")
print("-i image_path")
print("-v video_path")