1# Copyright 2015 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.edge.mode param behavior for reprocessing reqs.""" 15 16 17import logging 18import math 19import os 20 21from matplotlib import pyplot as plt 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 30import opencv_processing_utils 31 32_EDGE_MODES = {'OFF': 0, 'FAST': 1, 'HQ': 2, 'ZSL': 3} 33_EDGE_MODES_VALUES = list(_EDGE_MODES.values()) 34_NAME = os.path.splitext(os.path.basename(__file__))[0] 35_NUM_SAMPLES = 4 36_PLOT_COLORS = {'yuv': 'r', 'private': 'g', 'none': 'b'} 37_SHARPNESS_RTOL = 0.15 38 39 40def check_edge_modes(sharpness): 41 """Check that the sharpness for the different edge modes is correct.""" 42 logging.debug('Verify HQ is sharper than OFF') 43 if sharpness[_EDGE_MODES['HQ']] < sharpness[_EDGE_MODES['OFF']]: 44 raise AssertionError(f"HQ < OFF! HQ: {sharpness[_EDGE_MODES['HQ']]:.5f}, " 45 f"OFF: {sharpness[_EDGE_MODES['OFF']]:.5f}") 46 47 logging.debug('Verify ZSL is similar to OFF') 48 if not math.isclose(sharpness[_EDGE_MODES['ZSL']], 49 sharpness[_EDGE_MODES['OFF']], rel_tol=_SHARPNESS_RTOL): 50 raise AssertionError(f"ZSL: {sharpness[_EDGE_MODES['ZSL']]:.5f}, " 51 f"OFF: {sharpness[_EDGE_MODES['OFF']]:.5f}, " 52 f'RTOL: {_SHARPNESS_RTOL}') 53 54 logging.debug('Verify OFF is not sharper than FAST') 55 if (sharpness[_EDGE_MODES['FAST']] <= 56 sharpness[_EDGE_MODES['OFF']] * (1.0-_SHARPNESS_RTOL)): 57 raise AssertionError(f"FAST: {sharpness[_EDGE_MODES['FAST']]:.5f}, " 58 f"OFF: {sharpness[_EDGE_MODES['OFF']]:.5f}, " 59 f'RTOL: {_SHARPNESS_RTOL}') 60 61 logging.debug('Verify FAST is not sharper than HQ') 62 if (sharpness[_EDGE_MODES['HQ']] <= 63 sharpness[_EDGE_MODES['FAST']] * (1.0-_SHARPNESS_RTOL)): 64 raise AssertionError(f"FAST: {sharpness[_EDGE_MODES['FAST']]:.5f}, " 65 f"HQ: {sharpness[_EDGE_MODES['HQ']]:.5f}, " 66 f'RTOL: {_SHARPNESS_RTOL}') 67 68 69def do_capture_and_determine_sharpness( 70 cam, edge_mode, sensitivity, exp, fd, out_surface, chart, 71 name_with_log_path, reprocess_format=None): 72 """Return sharpness of the output images and the capture result metadata. 73 74 Processes a capture request with a given edge mode, sensitivity, exposure 75 time, focus distance, output surface parameter, and reprocess format 76 (None for a regular request.) 77 78 Args: 79 cam: An open device session. 80 edge_mode: Edge mode for the request as defined in android.edge.mode 81 sensitivity: Sensitivity for the request as defined in 82 android.sensor.sensitivity 83 exp: Exposure time for the request as defined in 84 android.sensor.exposureTime. 85 fd: Focus distance for the request as defined in 86 android.lens.focusDistance 87 out_surface: Specifications of the output image format and size. 88 chart: object containing chart information 89 name_with_log_path: file name & location to save files 90 reprocess_format: (Optional) The reprocessing format. If not None, 91 reprocessing will be enabled. 92 93 Returns: 94 Object containing reported edge mode and the sharpness of the output 95 image, keyed by the following strings: 96 'edge_mode' 97 'sharpness' 98 """ 99 100 req = capture_request_utils.manual_capture_request(sensitivity, exp) 101 req['android.lens.focusDistance'] = fd 102 req['android.edge.mode'] = edge_mode 103 if reprocess_format: 104 req['android.reprocess.effectiveExposureFactor'] = 1.0 105 106 sharpness_list = [] 107 caps = cam.do_capture([req]*_NUM_SAMPLES, [out_surface], reprocess_format) 108 for n in range(_NUM_SAMPLES): 109 y, _, _ = image_processing_utils.convert_capture_to_planes(caps[n]) 110 chart.img = image_processing_utils.get_image_patch( 111 y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm) 112 if n == 0: 113 image_processing_utils.write_image( 114 chart.img, 115 f'{name_with_log_path}_reprocess_fmt_{reprocess_format}_edge={edge_mode}.jpg') 116 edge_mode_res = caps[n]['metadata']['android.edge.mode'] 117 sharpness_list.append( 118 image_processing_utils.compute_image_sharpness(chart.img)*255) 119 logging.debug('Sharpness list for edge mode %d: %s', 120 edge_mode, str(sharpness_list)) 121 return {'edge_mode': edge_mode_res, 'sharpness': np.mean(sharpness_list)} 122 123 124class ReprocessEdgeEnhancementTest(its_base_test.ItsBaseTest): 125 """Test android.edge.mode param applied when set for reprocessing requests. 126 127 Capture non-reprocess images for each edge mode and calculate their 128 sharpness as a baseline. 129 130 Capture reprocessed images for each supported reprocess format and edge_mode 131 mode. Calculate the sharpness of reprocessed images and compare them against 132 the sharpess of non-reprocess images. 133 """ 134 135 def test_reprocess_edge_enhancement(self): 136 logging.debug('Edge modes: %s', str(_EDGE_MODES)) 137 with its_session_utils.ItsSession( 138 device_id=self.dut.serial, 139 camera_id=self.camera_id, 140 hidden_physical_id=self.hidden_physical_id) as cam: 141 props = cam.get_camera_properties() 142 props = cam.override_with_hidden_physical_camera_props(props) 143 name_with_log_path = os.path.join(self.log_path, _NAME) 144 145 # Check skip conditions 146 camera_properties_utils.skip_unless( 147 camera_properties_utils.read_3a(props) and 148 camera_properties_utils.per_frame_control(props) and 149 camera_properties_utils.edge_mode(props, 0) and 150 (camera_properties_utils.yuv_reprocess(props) or 151 camera_properties_utils.private_reprocess(props))) 152 153 # Load chart for scene 154 its_session_utils.load_scene( 155 cam, props, self.scene, self.tablet, self.chart_distance) 156 157 # Initialize chart class and locate chart in scene 158 chart = opencv_processing_utils.Chart( 159 cam, props, self.log_path, distance=self.chart_distance) 160 161 # If reprocessing is supported, ZSL edge mode must be available 162 if not camera_properties_utils.edge_mode(props, _EDGE_MODES['ZSL']): 163 raise AssertionError('ZSL android.edge.mode not available!') 164 165 reprocess_formats = [] 166 if camera_properties_utils.yuv_reprocess(props): 167 reprocess_formats.append('yuv') 168 if camera_properties_utils.private_reprocess(props): 169 reprocess_formats.append('private') 170 171 out_surface = capture_request_utils.get_largest_format('jpeg', props) 172 logging.debug('image W: %d, H: %d', 173 out_surface['width'], out_surface['height']) 174 175 # Get proper sensitivity, exposure time, and focus distance. 176 mono_camera = camera_properties_utils.mono_camera(props) 177 s, e, _, _, fd = cam.do_3a(get_results=True, mono_camera=mono_camera) 178 179 # Get the sharpness for each edge mode for regular requests 180 sharpness_regular = [] 181 edge_mode_reported_regular = [] 182 for edge_mode in _EDGE_MODES.values(): 183 # Skip unavailable modes 184 if not camera_properties_utils.edge_mode(props, edge_mode): 185 edge_mode_reported_regular.append(edge_mode) 186 sharpness_regular.append(0) 187 continue 188 ret = do_capture_and_determine_sharpness( 189 cam, edge_mode, s, e, fd, out_surface, chart, name_with_log_path) 190 edge_mode_reported_regular.append(ret['edge_mode']) 191 sharpness_regular.append(ret['sharpness']) 192 193 # Initialize plot 194 plt.figure('reprocess_result') 195 plt.title(_NAME) 196 plt.xlabel('Edge Enhance Mode') 197 plt.ylabel('Sharpness') 198 plt.xticks(_EDGE_MODES_VALUES) 199 plt.plot(_EDGE_MODES_VALUES, sharpness_regular, 200 f"-{_PLOT_COLORS['none']}o", label='None') 201 logging.debug('Sharpness for edge modes with regular request: %s', 202 str(sharpness_regular)) 203 204 # Get sharpness for each edge mode and reprocess format 205 sharpnesses_reprocess = [] 206 edge_mode_reported_reprocess = [] 207 208 for reprocess_format in reprocess_formats: 209 # List of sharpness 210 sharpnesses = [] 211 edge_mode_reported = [] 212 for edge_mode in range(4): 213 # Skip unavailable modes 214 if not camera_properties_utils.edge_mode(props, edge_mode): 215 edge_mode_reported.append(edge_mode) 216 sharpnesses.append(0) 217 continue 218 219 ret = do_capture_and_determine_sharpness( 220 cam, edge_mode, s, e, fd, out_surface, chart, name_with_log_path, 221 reprocess_format) 222 edge_mode_reported.append(ret['edge_mode']) 223 sharpnesses.append(ret['sharpness']) 224 225 sharpnesses_reprocess.append(sharpnesses) 226 edge_mode_reported_reprocess.append(edge_mode_reported) 227 228 # Add to plot and log results 229 plt.plot(_EDGE_MODES_VALUES, sharpnesses, 230 f'-{_PLOT_COLORS[reprocess_format]}o', 231 label=reprocess_format) 232 logging.debug('Sharpness for edge modes w/ %s reprocess fmt: %s', 233 reprocess_format, str(sharpnesses)) 234 # Finalize plot 235 plt.legend(numpoints=1, fancybox=True) 236 plt.savefig(f'{name_with_log_path}_plot.png') 237 logging.debug('Check regular requests') 238 check_edge_modes(sharpness_regular) 239 240 for reprocess_format in range(len(reprocess_formats)): 241 logging.debug('Check reprocess format: %s', reprocess_format) 242 check_edge_modes(sharpnesses_reprocess[reprocess_format]) 243 244 # Check reprocessing doesn't make everyting worse 245 hq_div_off_reprocess = ( 246 sharpnesses_reprocess[reprocess_format][_EDGE_MODES['HQ']] / 247 sharpnesses_reprocess[reprocess_format][_EDGE_MODES['OFF']]) 248 hq_div_off_regular = ( 249 sharpness_regular[_EDGE_MODES['HQ']] / 250 sharpness_regular[_EDGE_MODES['OFF']]) 251 logging.debug('Verify reprocess HQ ~= reg HQ relative to OFF') 252 if hq_div_off_reprocess < hq_div_off_regular*(1-_SHARPNESS_RTOL): 253 raise AssertionError( 254 f'HQ/OFF_{reprocess_format}: {hq_div_off_reprocess:.4f}, ' 255 f'HQ/OFF_reg: {hq_div_off_regular:.4f}, RTOL: {_SHARPNESS_RTOL}') 256 257 258if __name__ == '__main__': 259 test_runner.main() 260 261