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 30_COLORS = ('R', 'G', 'B') 31_EXP_MULT_FACTORS = (0.8, 0.9, 1.0, 1.1, 1.2) # vary exposure +/- 20% 32_NAME = os.path.splitext(os.path.basename(__file__))[0] 33_PATCH_H = 0.1 # center 10% 34_PATCH_W = 0.1 35_PATCH_X = 0.5 - _PATCH_W/2 36_PATCH_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 name_with_log_path = os.path.join(log_path, _NAME) 56 57 # check SKIP conditions 58 camera_properties_utils.skip_unless( 59 camera_properties_utils.compute_target_exposure(props)) 60 61 # Load chart for scene 62 its_session_utils.load_scene( 63 cam, props, self.scene, self.tablet, 64 its_session_utils.CHART_DISTANCE_NO_SCALING) 65 66 # Create requests 67 sync_latency = camera_properties_utils.sync_latency(props) 68 largest_yuv = capture_request_utils.get_largest_yuv_format(props) 69 match_ar = (largest_yuv['width'], largest_yuv['height']) 70 fmt = capture_request_utils.get_near_vga_yuv_format( 71 props, match_ar=match_ar) 72 e, s = target_exposure_utils.get_target_exposure_combos( 73 log_path, cam)['midExposureTime'] 74 75 # Do captures & process images 76 for i, e_mult in enumerate(_EXP_MULT_FACTORS): 77 req = capture_request_utils.manual_capture_request( 78 s, e * e_mult, 0.0, True, props) 79 cap = its_session_utils.do_capture_with_latency( 80 cam, req, sync_latency, fmt) 81 img = image_processing_utils.convert_capture_to_rgb_image(cap) 82 image_processing_utils.write_image( 83 img, f'{name_with_log_path}_frame{i}.jpg') 84 patch = image_processing_utils.get_image_patch( 85 img, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 86 rgb_means = image_processing_utils.compute_image_means(patch) 87 logging.debug('RGB means: %s', str(rgb_means)) 88 exp_times.append(e * e_mult) 89 r_means.append(rgb_means[0]) 90 g_means.append(rgb_means[1]) 91 b_means.append(rgb_means[2]) 92 93 # Draw plot 94 pylab.figure(_NAME) 95 for ch, means in enumerate([r_means, g_means, b_means]): 96 pylab.plot(exp_times, means, '-'+'rgb'[ch]+'o') 97 pylab.ylim([0, 1]) 98 pylab.title(_NAME) 99 pylab.xlabel('Exposure times (ns)') 100 pylab.ylabel('RGB means') 101 matplotlib.pyplot.savefig(f'{name_with_log_path}_plot_means.png') 102 103 # Assert each shot is brighter than previous. 104 for ch, means in enumerate([r_means, g_means, b_means]): 105 for i in range(len(_EXP_MULT_FACTORS)-1): 106 if means[i+1] <= means[i]: 107 raise AssertionError(f'{_COLORS[ch]} not increasing in brightness! ' 108 f'{_COLORS[ch]}[i+1]: {means[i+1]:.4f}, ' 109 f'{_COLORS[ch]}[i]: {means[i]:.4f}') 110 111if __name__ == '__main__': 112 test_runner.main() 113 114