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        its.caps.skip_unless(its.caps.face_detect(props))
36        mono_camera = its.caps.mono_camera(props)
37        fd_modes = props['android.statistics.info.availableFaceDetectModes']
38        a = props['android.sensor.info.activeArraySize']
39        aw, ah = a['right'] - a['left'], a['bottom'] - a['top']
40
41        if its.caps.read_3a(props):
42            _, _, _, _, _ = cam.do_3a(get_results=True,
43                                      mono_camera=mono_camera)
44
45        for fd_mode in fd_modes:
46            assert FD_MODE_OFF <= fd_mode <= FD_MODE_FULL
47            req = its.objects.auto_capture_request()
48            req['android.statistics.faceDetectMode'] = fd_mode
49            fmt = {'format': 'yuv', 'width': W, 'height': H}
50            caps = cam.do_capture([req]*NUM_TEST_FRAMES, fmt)
51            for i, cap in enumerate(caps):
52                md = cap['metadata']
53                assert md['android.statistics.faceDetectMode'] == fd_mode
54                faces = md['android.statistics.faces']
55
56                # 0 faces should be returned for OFF mode
57                if fd_mode == FD_MODE_OFF:
58                    assert not faces
59                    continue
60                # Face detection could take several frames to warm up,
61                # but should detect the correct number of faces in last frame
62                if i == NUM_TEST_FRAMES - 1:
63                    img = its.image.convert_capture_to_rgb_image(cap,
64                                                                 props=props)
65                    fnd_faces = len(faces)
66                    print 'Found %d face(s), expected %d.' % (fnd_faces,
67                                                              NUM_FACES)
68                    # draw boxes around faces
69                    for rect in [face['bounds'] for face in faces]:
70                        top_left = (int(round(rect['left']*W/aw)),
71                                    int(round(rect['top']*H/ah)))
72                        bot_rght = (int(round(rect['right']*W/aw)),
73                                    int(round(rect['bottom']*H/ah)))
74                        cv2.rectangle(img, top_left, bot_rght, (0, 1, 0), 2)
75                        img_name = '%s_fd_mode_%s.jpg' % (NAME, fd_mode)
76                        its.image.write_image(img, img_name)
77                    assert fnd_faces == NUM_FACES
78                if not faces:
79                    continue
80
81                print 'Frame %d face metadata:' % i
82                print '  Faces:', faces
83                print ''
84
85                # Reasonable scores for faces
86                face_scores = [face['score'] 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                face_rectangles = [face['bounds'] for face in faces]
91                for rect in face_rectangles:
92                    assert rect['top'] < rect['bottom']
93                    assert rect['left'] < rect['right']
94                    assert 0 <= rect['top'] <= ah
95                    assert 0 <= rect['bottom'] <= ah
96                    assert 0 <= rect['left'] <= aw
97                    assert 0 <= rect['right'] <= aw
98
99if __name__ == '__main__':
100    main()
101