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 YUV image consistency with AE and AWB locked."""
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
26
27BURST_LEN = 8
28COLORS = ['R', 'G', 'B']
29FPS_MAX_DIFF = 2.0
30NAME = os.path.splitext(os.path.basename(__file__))[0]
31PATCH_H = 0.1  # center 10%
32PATCH_W = 0.1
33PATCH_X = 0.5 - PATCH_W
34PATCH_Y = 0.5 - PATCH_H
35SPREAD_THRESH_MANUAL_SENSOR = 0.01
36SPREAD_THRESH = 0.03
37VALUE_THRESH = 0.1
38
39
40class LockedBurstTest(its_base_test.ItsBaseTest):
41  """Test 3A lock + YUV burst (using auto settings).
42
43  This is a test designed to pass even on limited devices that
44  don't have MANUAL_SENSOR or PER_FRAME_CONTROL. The test checks
45  YUV image consistency while the frame rate check is in CTS.
46  """
47
48  def test_locked_burst(self):
49    logging.debug('Starting %s', NAME)
50    with its_session_utils.ItsSession(
51        device_id=self.dut.serial,
52        camera_id=self.camera_id,
53        hidden_physical_id=self.hidden_physical_id) as cam:
54      props = cam.get_camera_properties()
55      props = cam.override_with_hidden_physical_camera_props(props)
56      mono_camera = camera_properties_utils.mono_camera(props)
57      log_path = self.log_path
58
59      # check SKIP conditions
60      camera_properties_utils.skip_unless(
61          camera_properties_utils.ae_lock(props) and
62          camera_properties_utils.awb_lock(props))
63
64      # Load chart for scene
65      its_session_utils.load_scene(
66          cam, props, self.scene, self.tablet, self.chart_distance)
67
68      # Converge 3A prior to capture.
69      cam.do_3a(do_af=True, lock_ae=True, lock_awb=True,
70                mono_camera=mono_camera)
71
72      fmt = capture_request_utils.get_largest_yuv_format(props)
73
74      # After 3A has converged, lock AE+AWB for the duration of the test.
75      logging.debug('Locking AE & AWB')
76      req = capture_request_utils.fastest_auto_capture_request(props)
77      req['android.control.awbLock'] = True
78      req['android.control.aeLock'] = True
79
80      # Capture bursts of YUV shots.
81      # Get the mean values of a center patch for each.
82      r_means = []
83      g_means = []
84      b_means = []
85      caps = cam.do_capture([req]*BURST_LEN, fmt)
86      for i, cap in enumerate(caps):
87        img = image_processing_utils.convert_capture_to_rgb_image(cap)
88        image_processing_utils.write_image(img, '%s_frame%d.jpg' % (
89            os.path.join(log_path, NAME), i))
90        patch = image_processing_utils.get_image_patch(
91            img, PATCH_X, PATCH_Y, PATCH_W, PATCH_H)
92        means = image_processing_utils.compute_image_means(patch)
93        r_means.append(means[0])
94        g_means.append(means[1])
95        b_means.append(means[2])
96
97      # Assert center patch brightness & similarity
98      for i, means in enumerate([r_means, g_means, b_means]):
99        plane = COLORS[i]
100        min_means = min(means)
101        spread = max(means) - min_means
102        logging.debug('%s patch mean spread %.5f. means = %s',
103                      plane, spread, str(means))
104        for j in range(BURST_LEN):
105          e_msg = '%s frame %d too dark! mean: %.5f, THRESH: %.2f' % (
106              plane, j, min_means, VALUE_THRESH)
107          assert min_means > VALUE_THRESH, e_msg
108          threshold = SPREAD_THRESH
109          if camera_properties_utils.manual_sensor(props):
110            threshold = SPREAD_THRESH_MANUAL_SENSOR
111          e_msg = '%s center patch spread: %.5f, THRESH: %.2f' % (
112              plane, spread, threshold)
113          assert spread < threshold, e_msg
114
115if __name__ == '__main__':
116  test_runner.main()
117
118