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