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