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 cv2
17import its.caps
18import its.device
19import its.image
20import its.objects
21
22NAME = os.path.basename(__file__).split('.')[0]
23NUM_TEST_FRAMES = 20
24NUM_FACES = 3
25FD_MODE_OFF = 0
26FD_MODE_SIMPLE = 1
27FD_MODE_FULL = 2
28W, H = 640, 480
29
30
31def main():
32    """Test face detection."""
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
42        if its.caps.read_3a(props):
43            _, _, _, _, _ = cam.do_3a(get_results=True,
44                                      mono_camera=mono_camera)
45
46        for fd_mode in fd_modes:
47            assert FD_MODE_OFF <= fd_mode <= FD_MODE_FULL
48            req = its.objects.auto_capture_request()
49            req['android.statistics.faceDetectMode'] = fd_mode
50            fmt = {'format': 'yuv', 'width': W, 'height': H}
51            caps = cam.do_capture([req]*NUM_TEST_FRAMES, fmt)
52            for i, cap in enumerate(caps):
53                md = cap['metadata']
54                assert md['android.statistics.faceDetectMode'] == fd_mode
55                faces = md['android.statistics.faces']
56
57                # 0 faces should be returned for OFF mode
58                if fd_mode == FD_MODE_OFF:
59                    assert not faces
60                    continue
61                # Face detection could take several frames to warm up,
62                # but should detect the correct number of faces in last frame
63                if i == NUM_TEST_FRAMES - 1:
64                    img = its.image.convert_capture_to_rgb_image(cap,
65                                                                 props=props)
66                    fnd_faces = len(faces)
67                    print 'Found %d face(s), expected %d.' % (fnd_faces,
68                                                              NUM_FACES)
69                    # draw boxes around faces
70                    for rect in [face['bounds'] for face in faces]:
71                        top_left = (int(round(rect['left']*W/aw)),
72                                    int(round(rect['top']*H/ah)))
73                        bot_rght = (int(round(rect['right']*W/aw)),
74                                    int(round(rect['bottom']*H/ah)))
75                        cv2.rectangle(img, top_left, bot_rght, (0, 1, 0), 2)
76                        img_name = '%s_fd_mode_%s.jpg' % (NAME, fd_mode)
77                        its.image.write_image(img, img_name)
78                    assert fnd_faces == NUM_FACES
79                if not faces:
80                    continue
81
82                print 'Frame %d face metadata:' % i
83                print '  Faces:', faces
84                print ''
85
86                # Reasonable scores for faces
87                face_scores = [face['score'] for face in faces]
88                for score in face_scores:
89                    assert score >= 1 and score <= 100
90                # Face bounds should be within active array
91                face_rectangles = [face['bounds'] for face in faces]
92                for rect in face_rectangles:
93                    assert rect['top'] < rect['bottom']
94                    assert rect['left'] < rect['right']
95                    assert 0 <= rect['top'] <= ah
96                    assert 0 <= rect['bottom'] <= ah
97                    assert 0 <= rect['left'] <= aw
98                    assert 0 <= rect['right'] <= aw
99
100if __name__ == '__main__':
101    main()
102