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 math 19import os.path 20import matplotlib 21from matplotlib import pylab 22from mobly import test_runner 23import numpy as np 24 25import its_base_test 26import camera_properties_utils 27import capture_request_utils 28import image_processing_utils 29import its_session_utils 30 31LOCKED = 3 32LUMA_LOCKED_RTOL_EV_SM = 0.05 33LUMA_LOCKED_RTOL_EV_LG = 0.10 34NAME = os.path.splitext(os.path.basename(__file__))[0] 35NUM_UNSATURATED_EVS = 3 36PATCH_H = 0.1 # center 10% 37PATCH_W = 0.1 38PATCH_X = 0.5 - PATCH_W/2 39PATCH_Y = 0.5 - PATCH_H/2 40THRESH_CONVERGE_FOR_EV = 8 # AE must converge within this num 41YUV_FULL_SCALE = 255.0 42YUV_SAT_MIN = 250.0 43YUV_SAT_TOL = 3.0 44 45 46def create_request_with_ev(ev): 47 req = capture_request_utils.auto_capture_request() 48 req['android.control.aeExposureCompensation'] = ev 49 req['android.control.aeLock'] = True 50 return req 51 52 53def extract_luma_from_capture(cap): 54 """Extract luma from capture.""" 55 y = image_processing_utils.convert_capture_to_planes(cap)[0] 56 patch = image_processing_utils.get_image_patch( 57 y, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 58 luma = image_processing_utils.compute_image_means(patch)[0] 59 return luma 60 61 62class EvCompensationBasicTest(its_base_test.ItsBaseTest): 63 """Tests that EV compensation is applied.""" 64 65 def test_ev_compensation_basic(self): 66 logging.debug('Starting %s', NAME) 67 with its_session_utils.ItsSession( 68 device_id=self.dut.serial, 69 camera_id=self.camera_id, 70 hidden_physical_id=self.hidden_physical_id) as cam: 71 props = cam.get_camera_properties() 72 props = cam.override_with_hidden_physical_camera_props(props) 73 log_path = self.log_path 74 debug = self.debug_mode 75 test_name_w_path = os.path.join(log_path, NAME) 76 77 # check SKIP conditions 78 camera_properties_utils.skip_unless( 79 camera_properties_utils.ev_compensation(props) and 80 camera_properties_utils.ae_lock(props)) 81 82 # Load chart for scene 83 its_session_utils.load_scene( 84 cam, props, self.scene, self.tablet, self.chart_distance) 85 86 # Create ev compensation changes 87 ev_per_step = capture_request_utils.rational_to_float( 88 props['android.control.aeCompensationStep']) 89 steps_per_ev = int(1.0 / ev_per_step) 90 evs = range(-2 * steps_per_ev, 2 * steps_per_ev + 1, steps_per_ev) 91 luma_locked_rtols = [LUMA_LOCKED_RTOL_EV_LG, 92 LUMA_LOCKED_RTOL_EV_SM, 93 LUMA_LOCKED_RTOL_EV_SM, 94 LUMA_LOCKED_RTOL_EV_SM, 95 LUMA_LOCKED_RTOL_EV_LG] 96 97 # Converge 3A, and lock AE once converged. skip AF trigger as 98 # dark/bright scene could make AF convergence fail and this test 99 # doesn't care the image sharpness. 100 mono_camera = camera_properties_utils.mono_camera(props) 101 cam.do_3a(ev_comp=0, lock_ae=True, do_af=False, mono_camera=mono_camera) 102 103 # Do captures and extract information 104 largest_yuv = capture_request_utils.get_largest_yuv_format(props) 105 match_ar = (largest_yuv['width'], largest_yuv['height']) 106 fmt = capture_request_utils.get_smallest_yuv_format( 107 props, match_ar=match_ar) 108 lumas = [] 109 for j, ev in enumerate(evs): 110 luma_locked_rtol = luma_locked_rtols[j] 111 # Capture a single shot with the same EV comp and locked AE. 112 req = create_request_with_ev(ev) 113 caps = cam.do_capture([req]*THRESH_CONVERGE_FOR_EV, fmt) 114 luma_locked = [] 115 for i, cap in enumerate(caps): 116 if debug: 117 img = image_processing_utils.convert_capture_to_rgb_image( 118 cap, props) 119 image_processing_utils.write_image( 120 img, f'{test_name_w_path}_ev{ev}_frame{i}.jpg') 121 if cap['metadata']['android.control.aeState'] == LOCKED: 122 ev_meta = cap['metadata']['android.control.aeExposureCompensation'] 123 logging.debug('cap EV compensation: %d', ev_meta) 124 if ev != ev_meta: 125 raise AssertionError( 126 f'EV compensation cap != req! cap: {ev_meta}, req: {ev}') 127 luma = extract_luma_from_capture(cap) 128 luma_locked.append(luma) 129 if i == THRESH_CONVERGE_FOR_EV-1: 130 lumas.append(luma) 131 if not math.isclose(min(luma_locked), max(luma_locked), 132 rel_tol=luma_locked_rtol): 133 raise AssertionError(f'AE locked lumas: {luma_locked}, ' 134 f'RTOL: {luma_locked_rtol}') 135 logging.debug('lumas in AE locked captures: %s', str(lumas)) 136 if caps[THRESH_CONVERGE_FOR_EV-1]['metadata'][ 137 'android.control.aeState'] != LOCKED: 138 raise AssertionError(f'No AE lock by {THRESH_CONVERGE_FOR_EV} frame.') 139 140 # Create plot 141 pylab.figure(NAME) 142 pylab.plot(evs, lumas, '-ro') 143 pylab.title(NAME) 144 pylab.xlabel('EV Compensation') 145 pylab.ylabel('Mean Luma (Normalized)') 146 matplotlib.pyplot.savefig(f'{test_name_w_path}_plot_means.png') 147 148 # Trim extra saturated images 149 while (lumas[-2] >= YUV_SAT_MIN/YUV_FULL_SCALE and 150 lumas[-1] >= YUV_SAT_MIN/YUV_FULL_SCALE and 151 len(lumas) > 2): 152 lumas.pop(-1) 153 logging.debug('Removed saturated image.') 154 155 # Only allow positive EVs to give saturated image 156 if len(lumas) < NUM_UNSATURATED_EVS: 157 raise AssertionError( 158 f'>{NUM_UNSATURATED_EVS-1} unsaturated images needed.') 159 min_luma_diffs = min(np.diff(lumas)) 160 logging.debug('Min of luma value difference between adjacent ev comp: %.3f', 161 min_luma_diffs) 162 163 # Assert unsaturated lumas increasing with increasing ev comp. 164 if min_luma_diffs <= 0: 165 raise AssertionError('Lumas not increasing with ev comp! ' 166 f'EVs: {list(evs)}, lumas: {lumas}') 167 168 169if __name__ == '__main__': 170 test_runner.main() 171