1# Copyright 2018 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 exposure times on RAW images.""" 15 16 17import logging 18import os.path 19import matplotlib 20from matplotlib import pylab 21from mobly import test_runner 22import numpy as np 23 24import its_base_test 25import camera_properties_utils 26import capture_request_utils 27import image_processing_utils 28import its_session_utils 29 30BLK_LVL_RTOL = 0.1 31BURST_LEN = 10 # break captures into burst of BURST_LEN requests 32COLORS = ['R', 'Gr', 'Gb', 'B'] 33EXP_LONG_THRESH = 1E6 # 1ms 34EXP_MULT_SHORT = pow(2, 1.0/3) # Test 3 steps per 2x exposure 35EXP_MULT_LONG = pow(10, 1.0/3) # Test 3 steps per 10x exposure 36IMG_DELTA_THRESH = 0.99 # Each shot must be > 0.99*previous 37IMG_SAT_RTOL = 0.01 # 1% 38IMG_STATS_GRID = 9 # find used to find the center 11.11% 39NAME = os.path.splitext(os.path.basename(__file__))[0] 40NS_TO_MS_FACTOR = 1.0E-6 41NUM_COLORS = len(COLORS) 42NUM_ISO_STEPS = 5 43 44 45def create_test_exposure_list(e_min, e_max): 46 """Create the list of exposure values to test.""" 47 e_list = [] 48 mult = 1.0 49 while e_min*mult < e_max: 50 e_list.append(int(e_min*mult)) 51 if e_min*mult < EXP_LONG_THRESH: 52 mult *= EXP_MULT_SHORT 53 else: 54 mult *= EXP_MULT_LONG 55 if e_list[-1] < e_max*IMG_DELTA_THRESH: 56 e_list.append(int(e_max)) 57 return e_list 58 59 60def define_raw_stats_fmt(props): 61 """Define format with active array width and height.""" 62 aax = props['android.sensor.info.preCorrectionActiveArraySize']['left'] 63 aay = props['android.sensor.info.preCorrectionActiveArraySize']['top'] 64 aaw = props['android.sensor.info.preCorrectionActiveArraySize']['right']-aax 65 aah = props['android.sensor.info.preCorrectionActiveArraySize']['bottom']-aay 66 return {'format': 'rawStats', 67 'gridWidth': aaw // IMG_STATS_GRID, 68 'gridHeight': aah // IMG_STATS_GRID} 69 70 71def create_plot(exps, means, sens, log_path): 72 """Create plots R, Gr, Gb, B vs exposures. 73 74 Args: 75 exps: array of exposure times in ms 76 means: array of means for RAW captures 77 sens: int value for ISO gain 78 log_path: path to write plot file 79 Returns: 80 None 81 """ 82 # means[0] is black level value 83 r = [m[0] for m in means[1:]] 84 gr = [m[1] for m in means[1:]] 85 gb = [m[2] for m in means[1:]] 86 b = [m[3] for m in means[1:]] 87 pylab.figure('%s_%s' % (NAME, sens)) 88 pylab.plot(exps, r, 'r.-') 89 pylab.plot(exps, b, 'b.-') 90 pylab.plot(exps, gr, 'g.-') 91 pylab.plot(exps, gb, 'k.-') 92 pylab.xscale('log') 93 pylab.yscale('log') 94 pylab.title('%s ISO=%d' % (NAME, sens)) 95 pylab.xlabel('Exposure time (ms)') 96 pylab.ylabel('Center patch pixel mean') 97 matplotlib.pyplot.savefig( 98 '%s_s=%d.png' % (os.path.join(log_path, NAME), sens)) 99 pylab.clf() 100 101 102def assert_increasing_means(means, exps, sens, black_levels, white_level): 103 """Assert that each image increases unless over/undersaturated. 104 105 Args: 106 means: COLORS means for set of images 107 exps: exposure times in ms 108 sens: ISO gain value 109 black_levels: COLORS black_level values 110 white_level: full scale value 111 Returns: 112 None 113 """ 114 allow_under_saturated = True 115 for i in range(1, len(means)): 116 prev_mean = means[i-1] 117 mean = means[i] 118 119 if np.isclose(max(mean), white_level, rtol=IMG_SAT_RTOL): 120 logging.debug('Saturated: white_level %f, max_mean %f', 121 white_level, max(mean)) 122 break 123 124 if allow_under_saturated and np.allclose( 125 mean, black_levels, rtol=BLK_LVL_RTOL): 126 # All channel means are close to black level 127 continue 128 129 allow_under_saturated = False 130 # Check pixel means are increasing (with small tolerance) 131 for ch, color in enumerate(COLORS): 132 e_msg = 'ISO=%d, %s, exp %3fms mean: %.2f, %s mean: %.2f, TOL=%.f%%' % ( 133 sens, color, exps[i-1], mean[ch], 134 'black level' if i == 1 else 'exp_time %.3fms'%exps[i-2], 135 prev_mean[ch], IMG_DELTA_THRESH*100) 136 if mean[ch] <= prev_mean[ch] * IMG_DELTA_THRESH: 137 raise AssertionError(e_msg) 138 139 140class RawExposureTest(its_base_test.ItsBaseTest): 141 """Capture RAW images with increasing exp time and measure pixel values.""" 142 143 def test_raw_exposure(self): 144 logging.debug('Starting %s', NAME) 145 with its_session_utils.ItsSession( 146 device_id=self.dut.serial, 147 camera_id=self.camera_id, 148 hidden_physical_id=self.hidden_physical_id) as cam: 149 props = cam.get_camera_properties() 150 props = cam.override_with_hidden_physical_camera_props(props) 151 camera_properties_utils.skip_unless( 152 camera_properties_utils.raw16(props) and 153 camera_properties_utils.manual_sensor(props) and 154 camera_properties_utils.per_frame_control(props) and 155 not camera_properties_utils.mono_camera(props)) 156 log_path = self.log_path 157 158 # Load chart for scene 159 its_session_utils.load_scene( 160 cam, props, self.scene, self.tablet, self.chart_distance) 161 162 # Create list of exposures 163 e_min, e_max = props['android.sensor.info.exposureTimeRange'] 164 e_test = create_test_exposure_list(e_min, e_max) 165 e_test_ms = [e*NS_TO_MS_FACTOR for e in e_test] 166 167 # Capture with rawStats to reduce capture times 168 fmt = define_raw_stats_fmt(props) 169 170 # Create sensitivity range from min to max analog sensitivity 171 sens_min, _ = props['android.sensor.info.sensitivityRange'] 172 sens_max = props['android.sensor.maxAnalogSensitivity'] 173 sens_step = (sens_max - sens_min) // NUM_ISO_STEPS 174 white_level = float(props['android.sensor.info.whiteLevel']) 175 black_levels = [image_processing_utils.get_black_level( 176 i, props) for i in range(NUM_COLORS)] 177 178 # Do captures with exposure list over sensitivity range 179 for s in range(sens_min, sens_max, sens_step): 180 # Break caps into bursts and do captures 181 burst_len = BURST_LEN 182 caps = [] 183 reqs = [capture_request_utils.manual_capture_request( 184 s, e, 0) for e in e_test] 185 # Eliminate burst len==1. Error because returns [[]], not [{}, ...] 186 while len(reqs) % burst_len == 1: 187 burst_len -= 1 188 # Break caps into bursts 189 for i in range(len(reqs) // burst_len): 190 caps += cam.do_capture(reqs[i*burst_len:(i+1)*burst_len], fmt) 191 last_n = len(reqs) % burst_len 192 if last_n: 193 caps += cam.do_capture(reqs[-last_n:], fmt) 194 195 # Extract means for each capture 196 means = [] 197 means.append(black_levels) 198 for i, cap in enumerate(caps): 199 mean_image, _ = image_processing_utils.unpack_rawstats_capture(cap) 200 mean = mean_image[IMG_STATS_GRID // 2, IMG_STATS_GRID // 2] 201 logging.debug('ISO=%d, exp_time=%.3fms, mean=%s', 202 s, (e_test[i] * NS_TO_MS_FACTOR), str(mean)) 203 means.append(mean) 204 205 # Create plot 206 create_plot(e_test_ms, means, s, log_path) 207 208 # Each shot mean should be brighter (except under/overexposed scene) 209 assert_increasing_means(means, e_test_ms, s, black_levels, white_level) 210 211if __name__ == '__main__': 212 test_runner.main() 213