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 its.image 16import its.caps 17import its.device 18import its.objects 19import os.path 20 21def main(): 22 """Test face detection. 23 """ 24 NAME = os.path.basename(__file__).split(".")[0] 25 NUM_TEST_FRAMES = 20 26 FD_MODE_OFF = 0 27 FD_MODE_SIMPLE = 1 28 FD_MODE_FULL = 2 29 30 with its.device.ItsSession() as cam: 31 props = cam.get_camera_properties() 32 fd_modes = props['android.statistics.info.availableFaceDetectModes'] 33 a = props['android.sensor.info.activeArraySize'] 34 aw, ah = a['right'] - a['left'], a['bottom'] - a['top'] 35 if its.caps.read_3a(props): 36 gain, exp, _, _, focus = cam.do_3a(get_results=True) 37 print 'iso = %d' % gain 38 print 'exp = %.2fms' % (exp*1.0E-6) 39 if focus == 0.0: 40 print 'fd = infinity' 41 else: 42 print 'fd = %.2fcm' % (1.0E2/focus) 43 for fd_mode in fd_modes: 44 assert(FD_MODE_OFF <= fd_mode <= FD_MODE_FULL) 45 req = its.objects.auto_capture_request() 46 req['android.statistics.faceDetectMode'] = fd_mode 47 max_img_size = its.objects.get_available_output_sizes("yuv", props)[0] 48 w = max_img_size[0] 49 h = max_img_size[1] 50 out_surf=None 51 if w * h > 12 * 1024 * 1024: 52 size_to_use = its.objects.get_available_output_sizes("yuv", 53 props, max_size=(4000, 3000), match_ar_size=(w, h))[0] 54 out_surf = { 55 "width": size_to_use[0], 56 "height": size_to_use[1], 57 "format": "yuv", 58 } 59 caps = cam.do_capture([req]*NUM_TEST_FRAMES, out_surfaces=out_surf) 60 for i,cap in enumerate(caps): 61 md = cap['metadata'] 62 assert(md['android.statistics.faceDetectMode'] == fd_mode) 63 faces = md['android.statistics.faces'] 64 img = its.image.convert_capture_to_rgb_image(cap, props=props) 65 img_name = "%s_fd_mode_%s.jpg" % (NAME, fd_mode) 66 its.image.write_image(img, img_name) 67 68 # 0 faces should be returned for OFF mode 69 if fd_mode == FD_MODE_OFF: 70 assert(len(faces) == 0) 71 continue 72 # Face detection could take several frames to warm up, 73 # but it should detect at least one face in last frame 74 if i == NUM_TEST_FRAMES - 1: 75 if len(faces) == 0: 76 print "Error: no face detected in mode", fd_mode 77 assert(0) 78 if len(faces) == 0: 79 continue 80 81 print "Frame %d face metadata:" % i 82 print " Faces:", faces 83 print "" 84 85 face_scores = [face['score'] for face in faces] 86 face_rectangles = [face['bounds'] for face in faces] 87 for score in face_scores: 88 assert(score >= 1 and score <= 100) 89 # Face bounds should be within active array 90 for rect in face_rectangles: 91 assert(rect['top'] < rect['bottom']) 92 assert(rect['left'] < rect['right']) 93 assert(0 <= rect['top'] <= ah) 94 assert(0 <= rect['bottom'] <= ah) 95 assert(0 <= rect['left'] <= aw) 96 assert(0 <= rect['right'] <= aw) 97 98 # Face landmarks are reported if and only if fd_mode is FULL 99 # Face ID should be -1 for SIMPLE and unique for FULL 100 if fd_mode == FD_MODE_SIMPLE: 101 for face in faces: 102 assert('leftEye' not in face) 103 assert('rightEye' not in face) 104 assert('mouth' not in face) 105 assert(face['id'] == -1) 106 elif fd_mode == FD_MODE_FULL: 107 face_ids = [face['id'] for face in faces] 108 assert(len(face_ids) == len(set(face_ids))) 109 # Face landmarks should be within face bounds 110 for face in faces: 111 left_eye = face['leftEye'] 112 right_eye = face['rightEye'] 113 mouth = face['mouth'] 114 l, r = face['bounds']['left'], face['bounds']['right'] 115 t, b = face['bounds']['top'], face['bounds']['bottom'] 116 assert(l <= left_eye['x'] <= r) 117 assert(t <= left_eye['y'] <= b) 118 assert(l <= right_eye['x'] <= r) 119 assert(t <= right_eye['y'] <= b) 120 assert(l <= mouth['x'] <= r) 121 assert(t <= mouth['y'] <= b) 122 123if __name__ == '__main__': 124 main() 125 126