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 EV compensation is applied.""" 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 30LINEAR_TONEMAP_CURVE = [0.0, 0.0, 1.0, 1.0] 31LOCKED = 3 32LUMA_DELTA_THRESH = 0.05 33LUMA_LOCKED_TOL = 0.05 34NAME = os.path.splitext(os.path.basename(__file__))[0] 35PATCH_H = 0.1 # center 10% 36PATCH_W = 0.1 37PATCH_X = 0.5 - PATCH_W/2 38PATCH_Y = 0.5 - PATCH_H/2 39THRESH_CONVERGE_FOR_EV = 8 # AE must converge within this num auto reqs for EV 40YUV_FULL_SCALE = 255.0 41YUV_SAT_MIN = 250.0 42YUV_SAT_TOL = 3.0 43 44 45def create_request_with_ev(ev): 46 req = capture_request_utils.auto_capture_request() 47 req['android.control.aeExposureCompensation'] = ev 48 req['android.control.aeLock'] = True 49 # Use linear tonemap to avoid brightness being impacted by tone curves. 50 req['android.tonemap.mode'] = 0 51 req['android.tonemap.curve'] = {'red': LINEAR_TONEMAP_CURVE, 52 'green': LINEAR_TONEMAP_CURVE, 53 'blue': LINEAR_TONEMAP_CURVE} 54 return req 55 56 57def extract_luma_from_capture(cap): 58 """Extract luma from capture.""" 59 y = image_processing_utils.convert_capture_to_planes(cap)[0] 60 patch = image_processing_utils.get_image_patch( 61 y, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 62 luma = image_processing_utils.compute_image_means(patch)[0] 63 return luma 64 65 66def create_ev_comp_changes(props): 67 """Create the ev compensation steps and shifts from control params.""" 68 ev_compensation_range = props['android.control.aeCompensationRange'] 69 range_min = ev_compensation_range[0] 70 range_max = ev_compensation_range[1] 71 ev_per_step = capture_request_utils.rational_to_float( 72 props['android.control.aeCompensationStep']) 73 logging.debug('ev_step_size_in_stops: %d', ev_per_step) 74 steps_per_ev = int(round(1.0 / ev_per_step)) 75 ev_steps = range(range_min, range_max + 1, steps_per_ev) 76 ev_shifts = [pow(2, step * ev_per_step) for step in ev_steps] 77 return ev_steps, ev_shifts 78 79 80class EvCompensationAdvancedTest(its_base_test.ItsBaseTest): 81 """Tests that EV compensation is applied.""" 82 83 def test_ev_compensation_advanced(self): 84 logging.debug('Starting %s', NAME) 85 with its_session_utils.ItsSession( 86 device_id=self.dut.serial, 87 camera_id=self.camera_id, 88 hidden_physical_id=self.hidden_physical_id) as cam: 89 props = cam.get_camera_properties() 90 props = cam.override_with_hidden_physical_camera_props(props) 91 log_path = self.log_path 92 93 # check SKIP conditions 94 camera_properties_utils.skip_unless( 95 camera_properties_utils.ev_compensation(props) and 96 camera_properties_utils.manual_sensor(props) and 97 camera_properties_utils.manual_post_proc(props) and 98 camera_properties_utils.per_frame_control(props)) 99 100 # Load chart for scene 101 its_session_utils.load_scene( 102 cam, props, self.scene, self.tablet, self.chart_distance) 103 104 # Create ev compensation changes 105 ev_steps, ev_shifts = create_ev_comp_changes(props) 106 107 # Converge 3A, and lock AE once converged. skip AF trigger as 108 # dark/bright scene could make AF convergence fail and this test 109 # doesn't care the image sharpness. 110 mono_camera = camera_properties_utils.mono_camera(props) 111 cam.do_3a(ev_comp=0, lock_ae=True, do_af=False, mono_camera=mono_camera) 112 113 # Create requests and capture 114 largest_yuv = capture_request_utils.get_largest_yuv_format(props) 115 match_ar = (largest_yuv['width'], largest_yuv['height']) 116 fmt = capture_request_utils.get_smallest_yuv_format( 117 props, match_ar=match_ar) 118 lumas = [] 119 for ev in ev_steps: 120 # Capture a single shot with the same EV comp and locked AE. 121 req = create_request_with_ev(ev) 122 caps = cam.do_capture([req]*THRESH_CONVERGE_FOR_EV, fmt) 123 for cap in caps: 124 if cap['metadata']['android.control.aeState'] == LOCKED: 125 lumas.append(extract_luma_from_capture(cap)) 126 break 127 if caps[THRESH_CONVERGE_FOR_EV-1]['metadata'][ 128 'android.control.aeState'] != LOCKED: 129 raise AssertionError('AE does not reach locked state in ' 130 f'{THRESH_CONVERGE_FOR_EV} frames.') 131 logging.debug('lumas in AE locked captures: %s', str(lumas)) 132 133 i_mid = len(ev_steps) // 2 134 luma_normal = lumas[i_mid] / ev_shifts[i_mid] 135 expected_lumas = [min(1.0, luma_normal*shift) for shift in ev_shifts] 136 137 # Create plot 138 pylab.figure(NAME) 139 pylab.plot(ev_steps, lumas, '-ro', label='measured', alpha=0.7) 140 pylab.plot(ev_steps, expected_lumas, '-bo', label='expected', alpha=0.7) 141 pylab.title(NAME) 142 pylab.xlabel('EV Compensation') 143 pylab.ylabel('Mean Luma (Normalized)') 144 pylab.legend(loc='lower right', numpoints=1, fancybox=True) 145 matplotlib.pyplot.savefig( 146 '%s_plot_means.png' % os.path.join(log_path, NAME)) 147 148 luma_diffs = [expected_lumas[i]-lumas[i] for i in range(len(ev_steps))] 149 max_diff = max(abs(i) for i in luma_diffs) 150 avg_diff = abs(np.array(luma_diffs)).mean() 151 logging.debug( 152 'Max delta between modeled and measured lumas: %.4f', max_diff) 153 logging.debug( 154 'Avg delta between modeled and measured lumas: %.4f', avg_diff) 155 if max_diff > LUMA_DELTA_THRESH: 156 raise AssertionError(f'Max delta between modeled and measured ' 157 f'lumas: {max_diff:.3f}, ' 158 f'TOL: {LUMA_DELTA_THRESH}.') 159 160 161if __name__ == '__main__': 162 test_runner.main() 163