|
32 | 32 | _CV2_FACE_SCALE_FACTOR = 1.05 # 5% step for resizing image to find face |
33 | 33 | _CV2_FACE_MIN_NEIGHBORS = 4 # recommended 3-6: higher for less faces |
34 | 34 | _CV2_GREEN = (0, 1, 0) |
35 | | -_CV2_RED = (1, 0, 0) |
36 | | -_FACE_CENTER_MATCH_TOL_X = 10 # 10 pixels or ~1.5% in 640x480 image |
37 | | -_FACE_CENTER_MATCH_TOL_Y = 20 # 20 pixels or ~4% in 640x480 image |
38 | | -_FACE_CENTER_MIN_LOGGING_DIST = 50 |
39 | 35 | _FD_MODE_OFF, _FD_MODE_SIMPLE, _FD_MODE_FULL = 0, 1, 2 |
40 | | -_MIN_NUM_FACES_ALIGNED = 2 |
41 | | -_MIN_CENTER_DELTA = 15 |
42 | 36 | _NAME = os.path.splitext(os.path.basename(__file__))[0] |
43 | 37 | _NUM_FACES = 3 |
44 | 38 | _NUM_TEST_FRAMES = 20 |
45 | 39 | _TEST_REQUIRED_MPC = 34 |
46 | 40 | _W, _H = 640, 480 |
47 | 41 |
|
48 | 42 |
|
49 | | -def eliminate_duplicate_centers(coordinates_list): |
50 | | - """Checks center coordinates of OpenCV's face rectangles |
51 | | -
|
52 | | - Method makes sure that the list of face rectangles' centers do not |
53 | | - contain duplicates from the same face. |
54 | | -
|
55 | | - Args: |
56 | | - coordinates_list: list; coordinates of face rectangles' centers |
57 | | - Returns: |
58 | | - non_duplicate_list: list; coordinates of face rectangles' centers |
59 | | - without duplicates on the same face |
60 | | - """ |
61 | | - output = set() |
62 | | - |
63 | | - for i, xy1 in enumerate(coordinates_list): |
64 | | - for j, xy2 in enumerate(coordinates_list): |
65 | | - if distance.euclidean(xy1, xy2) < _MIN_CENTER_DELTA: |
66 | | - continue |
67 | | - if xy1 not in output: |
68 | | - output.add(xy1) |
69 | | - else: |
70 | | - output.add(xy2) |
71 | | - return list(output) |
72 | | - |
73 | | - |
74 | | -def match_face_locations(faces_cropped, faces_opencv, mode, img, img_name): |
75 | | - """Assert face locations between two methods. |
76 | | -
|
77 | | - Method determines if center of opencv face boxes is within face detection |
78 | | - face boxes. Using math.hypot to measure the distance between the centers, |
79 | | - as math.dist is not available for python versions before 3.8. |
80 | | -
|
81 | | - Args: |
82 | | - faces_cropped: list of lists with (l, r, t, b) for each face. |
83 | | - faces_opencv: list of lists with (x, y, w, h) for each face. |
84 | | - mode: int indicating face detection mode |
85 | | - img: np image array |
86 | | - img_name: text string with path to image file |
87 | | - """ |
88 | | - # turn faces_opencv into list of center locations |
89 | | - faces_opencv_center = [(x+w//2, y+h//2) for (x, y, w, h) in faces_opencv] |
90 | | - cropped_faces_centers = [ |
91 | | - ((l+r)//2, (t+b)//2) for (l, r, t, b) in faces_cropped] |
92 | | - faces_opencv_center.sort(key=lambda t: [t[1], t[0]]) |
93 | | - cropped_faces_centers.sort(key=lambda t: [t[1], t[0]]) |
94 | | - logging.debug('cropped face centers: %s', str(cropped_faces_centers)) |
95 | | - logging.debug('opencv face center: %s', str(faces_opencv_center)) |
96 | | - faces_opencv_centers = [] |
97 | | - num_centers_aligned = 0 |
98 | | - |
99 | | - # eliminate duplicate openCV face rectangles' centers the same face |
100 | | - faces_opencv_centers = eliminate_duplicate_centers(faces_opencv_center) |
101 | | - logging.debug('opencv face centers: %s', str(faces_opencv_centers)) |
102 | | - |
103 | | - for (x, y) in faces_opencv_centers: |
104 | | - for (x1, y1) in cropped_faces_centers: |
105 | | - centers_dist = math.hypot(x-x1, y-y1) |
106 | | - if centers_dist < _FACE_CENTER_MIN_LOGGING_DIST: |
107 | | - logging.debug('centers_dist: %.3f', centers_dist) |
108 | | - if (abs(x-x1) < _FACE_CENTER_MATCH_TOL_X and |
109 | | - abs(y-y1) < _FACE_CENTER_MATCH_TOL_Y): |
110 | | - num_centers_aligned += 1 |
111 | | - |
112 | | - # If test failed, save image with green AND OpenCV red rectangles |
113 | | - image_processing_utils.write_image(img, img_name) |
114 | | - if num_centers_aligned < _MIN_NUM_FACES_ALIGNED: |
115 | | - for (x, y, w, h) in faces_opencv: |
116 | | - cv2.rectangle(img, (x, y), (x+w, y+h), _CV2_RED, 2) |
117 | | - image_processing_utils.write_image(img, img_name) |
118 | | - logging.debug('centered: %s', str(num_centers_aligned)) |
119 | | - raise AssertionError(f'Mode {mode} face rectangles in wrong location(s)!. ' |
120 | | - f'Found {num_centers_aligned} rectangles near cropped ' |
121 | | - f'face centers, expected {_MIN_NUM_FACES_ALIGNED}') |
122 | | - |
123 | | - |
124 | 43 | def check_face_bounding_box(rect, aw, ah, index): |
125 | 44 | """Checks face bounding box is within the active array area. |
126 | 45 |
|
@@ -187,32 +106,6 @@ def check_face_landmarks(face, fd_mode, index): |
187 | 106 | raise AssertionError(f'Unknown face detection mode: {fd_mode}.') |
188 | 107 |
|
189 | 108 |
|
190 | | -def correct_faces_for_crop(faces, img, crop): |
191 | | - """Correct face rectangles for sensor crop. |
192 | | -
|
193 | | - Args: |
194 | | - faces: list of dicts with face information |
195 | | - img: np image array |
196 | | - crop: dict of crop region size with 'top, right, left, bottom' as keys |
197 | | - Returns: |
198 | | - list of face locations (left, right, top, bottom) corrected |
199 | | - """ |
200 | | - faces_corrected = [] |
201 | | - cw, ch = crop['right'] - crop['left'], crop['bottom'] - crop['top'] |
202 | | - logging.debug('crop region: %s', str(crop)) |
203 | | - w = img.shape[1] |
204 | | - h = img.shape[0] |
205 | | - for rect in [face['bounds'] for face in faces]: |
206 | | - logging.debug('rect: %s', str(rect)) |
207 | | - left = int(round((rect['left'] - crop['left']) * w / cw)) |
208 | | - right = int(round((rect['right'] - crop['left']) * w / cw)) |
209 | | - top = int(round((rect['top'] - crop['top']) * h / ch)) |
210 | | - bottom = int(round((rect['bottom'] - crop['top']) * h / ch)) |
211 | | - faces_corrected.append([left, right, top, bottom]) |
212 | | - logging.debug('faces_corrected: %s', str(faces_corrected)) |
213 | | - return faces_corrected |
214 | | - |
215 | | - |
216 | 109 | class NumFacesTest(its_base_test.ItsBaseTest): |
217 | 110 | """Test face detection with different skin tones. |
218 | 111 | """ |
@@ -281,7 +174,8 @@ def test_num_faces(self): |
281 | 174 |
|
282 | 175 | # draw boxes around faces in green |
283 | 176 | crop_region = cap['metadata']['android.scaler.cropRegion'] |
284 | | - faces_cropped = correct_faces_for_crop(faces, img, crop_region) |
| 177 | + faces_cropped = opencv_processing_utils.correct_faces_for_crop( |
| 178 | + faces, img, crop_region) |
285 | 179 | for (l, r, t, b) in faces_cropped: |
286 | 180 | cv2.rectangle(img, (l, t), (r, b), _CV2_GREEN, 2) |
287 | 181 |
|
@@ -315,8 +209,8 @@ def test_num_faces(self): |
315 | 209 | faces_opencv = opencv_processing_utils.find_opencv_faces( |
316 | 210 | img, _CV2_FACE_SCALE_FACTOR, _CV2_FACE_MIN_NEIGHBORS) |
317 | 211 | if fd_mode: # non-zero value for ON |
318 | | - match_face_locations(faces_cropped, faces_opencv, |
319 | | - fd_mode, img, img_name) |
| 212 | + opencv_processing_utils.match_face_locations( |
| 213 | + faces_cropped, faces_opencv, img, img_name) |
320 | 214 |
|
321 | 215 | if not faces: |
322 | 216 | continue |
|
0 commit comments