• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2013 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 android.sensor.exposureTime parameter."""
15
16
17import logging
18import os.path
19import matplotlib
20from matplotlib import pylab
21from mobly import test_runner
22
23import its_base_test
24import camera_properties_utils
25import capture_request_utils
26import image_processing_utils
27import its_session_utils
28import target_exposure_utils
29
30COLORS = ['R', 'G', 'B']
31EXP_MULT_FACTORS = [0.8, 0.9, 1.0, 1.1, 1.2]  # vary exposure +/- 20%
32NAME = os.path.splitext(os.path.basename(__file__))[0]
33PATCH_H = 0.1  # center 10%
34PATCH_W = 0.1
35PATCH_X = 0.5 - PATCH_W/2
36PATCH_Y = 0.5 - PATCH_H/2
37
38
39class ParamExposureTimeTest(its_base_test.ItsBaseTest):
40  """Test that the android.sensor.exposureTime parameter is applied."""
41
42  def test_param_exposure_time(self):
43    logging.debug('Starting %s', NAME)
44    exp_times = []
45    r_means = []
46    g_means = []
47    b_means = []
48    with its_session_utils.ItsSession(
49        device_id=self.dut.serial,
50        camera_id=self.camera_id,
51        hidden_physical_id=self.hidden_physical_id) as cam:
52      props = cam.get_camera_properties()
53      props = cam.override_with_hidden_physical_camera_props(props)
54      log_path = self.log_path
55
56      # check SKIP conditions
57      camera_properties_utils.skip_unless(
58          camera_properties_utils.compute_target_exposure(props))
59
60      # Load chart for scene
61      its_session_utils.load_scene(
62          cam, props, self.scene, self.tablet, self.chart_distance)
63
64      # Create requests
65      sync_latency = camera_properties_utils.sync_latency(props)
66      largest_yuv = capture_request_utils.get_largest_yuv_format(props)
67      match_ar = (largest_yuv['width'], largest_yuv['height'])
68      fmt = capture_request_utils.get_smallest_yuv_format(
69          props, match_ar=match_ar)
70      e, s = target_exposure_utils.get_target_exposure_combos(
71          log_path, cam)['midExposureTime']
72
73      # Do captures & process images
74      for i, e_mult in enumerate(EXP_MULT_FACTORS):
75        req = capture_request_utils.manual_capture_request(
76            s, e * e_mult, 0.0, True, props)
77        cap = its_session_utils.do_capture_with_latency(
78            cam, req, sync_latency, fmt)
79        img = image_processing_utils.convert_capture_to_rgb_image(cap)
80        image_processing_utils.write_image(
81            img, '%s_frame%d.jpg' % (os.path.join(log_path, NAME), i))
82        patch = image_processing_utils.get_image_patch(
83            img, PATCH_X, PATCH_Y, PATCH_W, PATCH_H)
84        rgb_means = image_processing_utils.compute_image_means(patch)
85        exp_times.append(e * e_mult)
86        r_means.append(rgb_means[0])
87        g_means.append(rgb_means[1])
88        b_means.append(rgb_means[2])
89
90    # Draw plot
91    pylab.figure(NAME)
92    for ch, means in enumerate([r_means, g_means, b_means]):
93      pylab.plot(exp_times, means, '-'+'rgb'[ch]+'o')
94    pylab.ylim([0, 1])
95    pylab.title(NAME)
96    pylab.xlabel('Exposure times (ns)')
97    pylab.ylabel('RGB means')
98    plot_name = '%s_plot_means.png' % os.path.join(log_path, NAME)
99    matplotlib.pyplot.savefig(plot_name)
100
101    # Assert each shot is brighter than previous.
102    for ch, means in enumerate([r_means, g_means, b_means]):
103      for i in range(len(EXP_MULT_FACTORS)-1):
104        e_msg = '%s [i+1]: %.4f, [i]: %.4f' % (COLORS[ch], means[i+1], means[i])
105        assert means[i+1] > means[i], e_msg
106
107if __name__ == '__main__':
108  test_runner.main()
109
110