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