blob: f42552d39a54e451080c2bd3148ac31c0e264b4a [file] [log] [blame]
Yin-Chia Yeh0e0276f2015-06-03 15:27:06 -07001# 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
Yin-Chia Yehd32a08d2016-10-05 17:06:55 -070028 W, H = 640, 480
Yin-Chia Yeh0e0276f2015-06-03 15:27:06 -070029
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']
Clemenz Portmannf7f23ee2016-08-18 13:00:59 -070035 gain, exp, _, _, focus = cam.do_3a(get_results=True)
36 print 'iso = %d' % gain
37 print 'exp = %.2fms' % (exp*1.0E-6)
Yin-Chia Yeh04a63332016-09-12 14:52:47 -070038 if focus == 0.0:
39 print 'fd = infinity'
40 else:
41 print 'fd = %.2fcm' % (1.0E2/focus)
Yin-Chia Yeh0e0276f2015-06-03 15:27:06 -070042 for fd_mode in fd_modes:
43 assert(FD_MODE_OFF <= fd_mode <= FD_MODE_FULL)
44 req = its.objects.auto_capture_request()
45 req['android.statistics.faceDetectMode'] = fd_mode
Yin-Chia Yehd32a08d2016-10-05 17:06:55 -070046 fmt = {"format":"yuv", "width":W, "height":H}
47 caps = cam.do_capture([req]*NUM_TEST_FRAMES, fmt)
Yin-Chia Yeh0e0276f2015-06-03 15:27:06 -070048 for i,cap in enumerate(caps):
49 md = cap['metadata']
50 assert(md['android.statistics.faceDetectMode'] == fd_mode)
51 faces = md['android.statistics.faces']
Yin-Chia Yeh0e0276f2015-06-03 15:27:06 -070052
53 # 0 faces should be returned for OFF mode
54 if fd_mode == FD_MODE_OFF:
55 assert(len(faces) == 0)
56 continue
57 # Face detection could take several frames to warm up,
58 # but it should detect at least one face in last frame
59 if i == NUM_TEST_FRAMES - 1:
Yin-Chia Yehd32a08d2016-10-05 17:06:55 -070060 img = its.image.convert_capture_to_rgb_image(cap, props=props)
61 img_name = "%s_fd_mode_%s.jpg" % (NAME, fd_mode)
62 its.image.write_image(img, img_name)
Yin-Chia Yeh0e0276f2015-06-03 15:27:06 -070063 if len(faces) == 0:
64 print "Error: no face detected in mode", fd_mode
65 assert(0)
66 if len(faces) == 0:
67 continue
68
69 print "Frame %d face metadata:" % i
70 print " Faces:", faces
71 print ""
72
73 face_scores = [face['score'] for face in faces]
74 face_rectangles = [face['bounds'] for face in faces]
75 for score in face_scores:
76 assert(score >= 1 and score <= 100)
77 # Face bounds should be within active array
78 for rect in face_rectangles:
79 assert(rect['top'] < rect['bottom'])
80 assert(rect['left'] < rect['right'])
81 assert(0 <= rect['top'] <= ah)
82 assert(0 <= rect['bottom'] <= ah)
83 assert(0 <= rect['left'] <= aw)
84 assert(0 <= rect['right'] <= aw)
85
86 # Face landmarks are reported if and only if fd_mode is FULL
87 # Face ID should be -1 for SIMPLE and unique for FULL
88 if fd_mode == FD_MODE_SIMPLE:
89 for face in faces:
90 assert('leftEye' not in face)
91 assert('rightEye' not in face)
92 assert('mouth' not in face)
93 assert(face['id'] == -1)
94 elif fd_mode == FD_MODE_FULL:
95 face_ids = [face['id'] for face in faces]
96 assert(len(face_ids) == len(set(face_ids)))
97 # Face landmarks should be within face bounds
98 for face in faces:
99 left_eye = face['leftEye']
100 right_eye = face['rightEye']
101 mouth = face['mouth']
102 l, r = face['bounds']['left'], face['bounds']['right']
103 t, b = face['bounds']['top'], face['bounds']['bottom']
104 assert(l <= left_eye['x'] <= r)
105 assert(t <= left_eye['y'] <= b)
106 assert(l <= right_eye['x'] <= r)
107 assert(t <= right_eye['y'] <= b)
108 assert(l <= mouth['x'] <= r)
109 assert(t <= mouth['y'] <= b)
110
111if __name__ == '__main__':
112 main()
113