12
12
print ("pip install git+https://github.com/ageitgey/face_recognition_models" )
13
13
quit ()
14
14
15
+ face_detector = dlib .get_frontal_face_detector ()
16
+
15
17
predictor_model = face_recognition_models .pose_predictor_model_location ()
16
18
pose_predictor = dlib .shape_predictor (predictor_model )
17
19
@@ -39,6 +41,17 @@ def _css_to_rect(css):
39
41
return dlib .rectangle (css [3 ], css [0 ], css [1 ], css [2 ])
40
42
41
43
44
+ def _trim_css_to_bounds (css , image_shape ):
45
+ """
46
+ Make sure a tuple in (top, right, bottom, left) order is within the bounds of the image.
47
+
48
+ :param css: plain tuple representation of the rect in (top, right, bottom, left) order
49
+ :param image_shape: numpy shape of the image array
50
+ :return: a trimmed plain tuple representation of the rect in (top, right, bottom, left) order
51
+ """
52
+ return max (css [0 ], 0 ), min (css [1 ], image_shape [1 ]), min (css [2 ], image_shape [0 ]), max (css [3 ], 0 )
53
+
54
+
42
55
def _face_distance (faces , face_to_compare ):
43
56
"""
44
57
Given a list of face encodings, compared them to a known face encoding and get a euclidean distance
@@ -70,7 +83,6 @@ def _raw_face_locations(img, number_of_times_to_upsample=1):
70
83
:param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
71
84
:return: A list of dlib 'rect' objects of found face locations
72
85
"""
73
- face_detector = dlib .get_frontal_face_detector ()
74
86
return face_detector (img , number_of_times_to_upsample )
75
87
76
88
@@ -82,7 +94,7 @@ def face_locations(img, number_of_times_to_upsample=1):
82
94
:param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
83
95
:return: A list of tuples of found face locations in css (top, right, bottom, left) order
84
96
"""
85
- return [_rect_to_css (face ) for face in _raw_face_locations (img , number_of_times_to_upsample )]
97
+ return [_trim_css_to_bounds ( _rect_to_css (face ), img . shape ) for face in _raw_face_locations (img , number_of_times_to_upsample )]
86
98
87
99
88
100
def _raw_face_landmarks (face_image , face_locations = None ):
0 commit comments