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.device 17import its.objects 18import os.path 19 20def main(): 21 """Test face detection. 22 """ 23 NAME = os.path.basename(__file__).split(".")[0] 24 NUM_TEST_FRAMES = 20 25 FD_MODE_OFF = 0 26 FD_MODE_SIMPLE = 1 27 FD_MODE_FULL = 2 28 29 with its.device.ItsSession() as cam: 30 props = cam.get_camera_properties() 31 fd_modes = props['android.statistics.info.availableFaceDetectModes'] 32 a = props['android.sensor.info.activeArraySize'] 33 aw, ah = a['right'] - a['left'], a['bottom'] - a['top'] 34 cam.do_3a() 35 for fd_mode in fd_modes: 36 assert(FD_MODE_OFF <= fd_mode <= FD_MODE_FULL) 37 req = its.objects.auto_capture_request() 38 req['android.statistics.faceDetectMode'] = fd_mode 39 caps = cam.do_capture([req]*NUM_TEST_FRAMES) 40 for i,cap in enumerate(caps): 41 md = cap['metadata'] 42 assert(md['android.statistics.faceDetectMode'] == fd_mode) 43 faces = md['android.statistics.faces'] 44 45 # 0 faces should be returned for OFF mode 46 if fd_mode == FD_MODE_OFF: 47 assert(len(faces) == 0) 48 continue 49 # Face detection could take several frames to warm up, 50 # but it should detect at least one face in last frame 51 if i == NUM_TEST_FRAMES - 1: 52 if len(faces) == 0: 53 print "Error: no face detected in mode", fd_mode 54 assert(0) 55 if len(faces) == 0: 56 continue 57 58 print "Frame %d face metadata:" % i 59 print " Faces:", faces 60 print "" 61 62 face_scores = [face['score'] for face in faces] 63 face_rectangles = [face['bounds'] for face in faces] 64 for score in face_scores: 65 assert(score >= 1 and score <= 100) 66 # Face bounds should be within active array 67 for rect in face_rectangles: 68 assert(rect['top'] < rect['bottom']) 69 assert(rect['left'] < rect['right']) 70 assert(0 <= rect['top'] <= ah) 71 assert(0 <= rect['bottom'] <= ah) 72 assert(0 <= rect['left'] <= aw) 73 assert(0 <= rect['right'] <= aw) 74 75 # Face landmarks are reported if and only if fd_mode is FULL 76 # Face ID should be -1 for SIMPLE and unique for FULL 77 if fd_mode == FD_MODE_SIMPLE: 78 for face in faces: 79 assert('leftEye' not in face) 80 assert('rightEye' not in face) 81 assert('mouth' not in face) 82 assert(face['id'] == -1) 83 elif fd_mode == FD_MODE_FULL: 84 face_ids = [face['id'] for face in faces] 85 assert(len(face_ids) == len(set(face_ids))) 86 # Face landmarks should be within face bounds 87 for face in faces: 88 left_eye = face['leftEye'] 89 right_eye = face['rightEye'] 90 mouth = face['mouth'] 91 l, r = face['bounds']['left'], face['bounds']['right'] 92 t, b = face['bounds']['top'], face['bounds']['bottom'] 93 assert(l <= left_eye['x'] <= r) 94 assert(t <= left_eye['y'] <= b) 95 assert(l <= right_eye['x'] <= r) 96 assert(t <= right_eye['y'] <= b) 97 assert(l <= mouth['x'] <= r) 98 assert(t <= mouth['y'] <= b) 99 100if __name__ == '__main__': 101 main() 102 103