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"""Verifies 3 faces with different skin tones are detected."""
15
16
17import logging
18import os.path
19from mobly import test_runner
20
21import its_base_test
22import camera_properties_utils
23import capture_request_utils
24import image_processing_utils
25import its_session_utils
26import cv2
27
28FD_MODE_OFF = 0
29FD_MODE_SIMPLE = 1
30FD_MODE_FULL = 2
31NAME = os.path.splitext(os.path.basename(__file__))[0]
32NUM_TEST_FRAMES = 20
33NUM_FACES = 3
34W, H = 640, 480
35
36
37def draw_face_rectangles(img, faces, aw, ah):
38  """Draw rectangles on top of image.
39
40  Args:
41    img:    image array
42    faces:  list of dicts with face information
43    aw:     int; active array width
44    ah:     int; active array height
45  Returns:
46    img with face rectangles drawn on it
47  """
48  for rect in [face['bounds'] for face in faces]:
49    top_left = (int(round(rect['left']*W/aw)),
50                int(round(rect['top']*H/ah)))
51    bot_rght = (int(round(rect['right']*W/aw)),
52                int(round(rect['bottom']*H/ah)))
53    cv2.rectangle(img, top_left, bot_rght, (0, 1, 0), 2)
54  return img
55
56
57class NumFacesTest(its_base_test.ItsBaseTest):
58  """Test face detection with different skin tones.
59  """
60
61  def test_num_faces(self):
62    """Test face detection."""
63    with its_session_utils.ItsSession(
64        device_id=self.dut.serial,
65        camera_id=self.camera_id,
66        hidden_physical_id=self.hidden_physical_id) as cam:
67      props = cam.get_camera_properties()
68      props = cam.override_with_hidden_physical_camera_props(props)
69
70      # Load chart for scene
71      its_session_utils.load_scene(
72          cam, props, self.scene, self.tablet, self.chart_distance)
73
74      # Check skip conditions
75      camera_properties_utils.skip_unless(
76          camera_properties_utils.face_detect(props))
77      mono_camera = camera_properties_utils.mono_camera(props)
78      fd_modes = props['android.statistics.info.availableFaceDetectModes']
79      a = props['android.sensor.info.activeArraySize']
80      aw, ah = a['right'] - a['left'], a['bottom'] - a['top']
81
82      if camera_properties_utils.read_3a(props):
83        _, _, _, _, _ = cam.do_3a(get_results=True, mono_camera=mono_camera)
84
85      for fd_mode in fd_modes:
86        assert FD_MODE_OFF <= fd_mode <= FD_MODE_FULL
87        req = capture_request_utils.auto_capture_request()
88        req['android.statistics.faceDetectMode'] = fd_mode
89        fmt = {'format': 'yuv', 'width': W, 'height': H}
90        caps = cam.do_capture([req]*NUM_TEST_FRAMES, fmt)
91        for i, cap in enumerate(caps):
92          md = cap['metadata']
93          assert md['android.statistics.faceDetectMode'] == fd_mode
94          faces = md['android.statistics.faces']
95
96          # 0 faces should be returned for OFF mode
97          if fd_mode == FD_MODE_OFF:
98            assert not faces
99            continue
100          # Face detection could take several frames to warm up,
101          # but should detect the correct number of faces in last frame
102          if i == NUM_TEST_FRAMES - 1:
103            img = image_processing_utils.convert_capture_to_rgb_image(
104                cap, props=props)
105            fnd_faces = len(faces)
106            logging.debug('Found %d face(s), expected %d.',
107                          fnd_faces, NUM_FACES)
108            # draw boxes around faces
109            img = draw_face_rectangles(img, faces, aw, ah)
110            # save image with rectangles
111            img_name = '%s_fd_mode_%s.jpg' % (os.path.join(self.log_path,
112                                                           NAME), fd_mode)
113            image_processing_utils.write_image(img, img_name)
114            assert fnd_faces == NUM_FACES
115          if not faces:
116            continue
117
118          logging.debug('Frame %d face metadata:', i)
119          logging.debug(' Faces: %s', str(faces))
120
121          # Reasonable scores for faces
122          face_scores = [face['score'] for face in faces]
123          for score in face_scores:
124            assert 1 <= score <= 100
125          # Face bounds should be within active array
126          face_rectangles = [face['bounds'] for face in faces]
127          for rect in face_rectangles:
128            assert rect['top'] < rect['bottom']
129            assert rect['left'] < rect['right']
130            assert 0 <= rect['top'] <= ah
131            assert 0 <= rect['bottom'] <= ah
132            assert 0 <= rect['left'] <= aw
133            assert 0 <= rect['right'] <= aw
134
135if __name__ == '__main__':
136  test_runner.main()
137