1# Copyright 2014 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import os.path 16import its.caps 17import its.device 18import its.image 19import its.objects 20 21NAME = os.path.basename(__file__).split('.')[0] 22NUM_TEST_FRAMES = 20 23FD_MODE_OFF = 0 24FD_MODE_SIMPLE = 1 25FD_MODE_FULL = 2 26W, H = 640, 480 27 28 29def main(): 30 """Test face detection. 31 """ 32 33 with its.device.ItsSession() as cam: 34 props = cam.get_camera_properties() 35 props = cam.override_with_hidden_physical_camera_props(props) 36 its.caps.skip_unless(its.caps.face_detect(props)) 37 mono_camera = its.caps.mono_camera(props) 38 fd_modes = props['android.statistics.info.availableFaceDetectModes'] 39 a = props['android.sensor.info.activeArraySize'] 40 aw, ah = a['right'] - a['left'], a['bottom'] - a['top'] 41 if its.caps.read_3a(props): 42 gain, exp, _, _, focus = cam.do_3a(get_results=True, 43 mono_camera=mono_camera) 44 print 'iso = %d' % gain 45 print 'exp = %.2fms' % (exp*1.0E-6) 46 if focus == 0.0: 47 print 'fd = infinity' 48 else: 49 print 'fd = %.2fcm' % (1.0E2/focus) 50 for fd_mode in fd_modes: 51 assert FD_MODE_OFF <= fd_mode <= FD_MODE_FULL 52 req = its.objects.auto_capture_request() 53 req['android.statistics.faceDetectMode'] = fd_mode 54 fmt = {'format': 'yuv', 'width': W, 'height': H} 55 caps = cam.do_capture([req]*NUM_TEST_FRAMES, fmt) 56 for i, cap in enumerate(caps): 57 md = cap['metadata'] 58 assert md['android.statistics.faceDetectMode'] == fd_mode 59 faces = md['android.statistics.faces'] 60 61 # 0 faces should be returned for OFF mode 62 if fd_mode == FD_MODE_OFF: 63 assert not faces 64 continue 65 # Face detection could take several frames to warm up, 66 # but it should detect at least one face in last frame 67 if i == NUM_TEST_FRAMES - 1: 68 img = its.image.convert_capture_to_rgb_image( 69 cap, props=props) 70 img = its.image.rotate_img_per_argv(img) 71 img_name = '%s_fd_mode_%s.jpg' % (NAME, fd_mode) 72 its.image.write_image(img, img_name) 73 if not faces: 74 print 'Error: no face detected in mode', fd_mode 75 assert 0 76 if not faces: 77 continue 78 79 print 'Frame %d face metadata:' % i 80 print ' Faces:', faces 81 print '' 82 83 face_scores = [face['score'] for face in faces] 84 face_rectangles = [face['bounds'] for face in faces] 85 for score in face_scores: 86 assert score >= 1 and score <= 100 87 # Face bounds should be within active array 88 for j, rect in enumerate(face_rectangles): 89 print 'Checking face rectangle %d...' % j 90 rect_t = rect['top'] 91 rect_b = rect['bottom'] 92 rect_l = rect['left'] 93 rect_r = rect['right'] 94 assert rect_t < rect_b 95 assert rect_l < rect_r 96 l_msg = 'l: %d outside of active W: 0,%d' % (rect_l, aw) 97 r_msg = 'r: %d outside of active W: 0,%d' % (rect_r, aw) 98 t_msg = 't: %d outside active H: 0,%d' % (rect_t, ah) 99 b_msg = 'b: %d outside active H: 0,%d' % (rect_b, ah) 100 # Assert same order as face landmarks below 101 assert 0 <= rect_l <= aw, l_msg 102 assert 0 <= rect_r <= aw, r_msg 103 assert 0 <= rect_t <= ah, t_msg 104 assert 0 <= rect_b <= ah, b_msg 105 106 # Face landmarks are reported if and only if fd_mode is FULL 107 # Face ID should be -1 for SIMPLE and unique for FULL 108 if fd_mode == FD_MODE_SIMPLE: 109 for face in faces: 110 assert 'leftEye' not in face 111 assert 'rightEye' not in face 112 assert 'mouth' not in face 113 assert face['id'] == -1 114 elif fd_mode == FD_MODE_FULL: 115 face_ids = [face['id'] for face in faces] 116 assert len(face_ids) == len(set(face_ids)) 117 # Face landmarks should be within face bounds 118 for k, face in enumerate(faces): 119 print 'Checking landmarks in face %d...' % k 120 l_eye = face['leftEye'] 121 r_eye = face['rightEye'] 122 mouth = face['mouth'] 123 l, r = face['bounds']['left'], face['bounds']['right'] 124 t, b = face['bounds']['top'], face['bounds']['bottom'] 125 l_eye_x, l_eye_y = l_eye['x'], l_eye['y'] 126 r_eye_x, r_eye_y = r_eye['x'], r_eye['y'] 127 mouth_x, mouth_y = mouth['x'], mouth['y'] 128 lx_msg = 'l: %d, r: %d, x: %d' % (l, r, l_eye_x) 129 ly_msg = 't: %d, b: %d, y: %d' % (t, b, l_eye_y) 130 rx_msg = 'l: %d, r: %d, x: %d' % (l, r, r_eye_x) 131 ry_msg = 't: %d, b: %d, y: %d' % (t, b, r_eye_y) 132 mx_msg = 'l: %d, r: %d, x: %d' % (l, r, mouth_x) 133 my_msg = 't: %d, b: %d, y: %d' % (t, b, mouth_y) 134 assert l <= l_eye_x <= r, lx_msg 135 assert t <= l_eye_y <= b, ly_msg 136 assert l <= r_eye_x <= r, rx_msg 137 assert t <= r_eye_y <= b, ry_msg 138 assert l <= mouth_x <= r, mx_msg 139 assert t <= mouth_y <= b, my_msg 140 141if __name__ == '__main__': 142 main() 143