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 works properly.""" 15 16 17import logging 18import os 19from mobly import test_runner 20import numpy as np 21 22import its_base_test 23import camera_properties_utils 24import capture_request_utils 25import image_processing_utils 26import its_session_utils 27import opencv_processing_utils 28 29EDGE_MODES = {'OFF': 0, 'FAST': 1, 'HQ': 2, 'ZSL': 3} 30NAME = os.path.splitext(os.path.basename(__file__))[0] 31NUM_SAMPLES = 4 32SHARPNESS_RTOL = 0.1 33 34 35def do_capture_and_determine_sharpness( 36 cam, edge_mode, sensitivity, exp, fd, out_surface, chart, log_path): 37 """Return sharpness of the output image and the capture result metadata. 38 39 Processes a capture request with a given edge mode, sensitivity, exposure 40 time, focus distance, output surface parameter. 41 42 Args: 43 cam: An open device session. 44 edge_mode: Edge mode for the request as defined in android.edge.mode 45 sensitivity: Sensitivity for the request as defined in 46 android.sensor.sensitivity 47 exp: Exposure time for the request as defined in 48 android.sensor.exposureTime. 49 fd: Focus distance for the request as defined in 50 android.lens.focusDistance 51 out_surface: Specifications of the output image format and size. 52 chart: object that contains chart information 53 log_path: path to write result images 54 55 Returns: 56 Object containing reported edge mode and the sharpness of the output 57 image, keyed by the following strings: 58 edge_mode 59 sharpness 60 """ 61 62 req = capture_request_utils.manual_capture_request(sensitivity, exp) 63 req['android.lens.focusDistance'] = fd 64 req['android.edge.mode'] = edge_mode 65 66 sharpness_list = [] 67 for n in range(NUM_SAMPLES): 68 cap = cam.do_capture(req, out_surface, repeat_request=req) 69 y, _, _ = image_processing_utils.convert_capture_to_planes(cap) 70 chart.img = image_processing_utils.normalize_img( 71 image_processing_utils.get_image_patch( 72 y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm)) 73 if n == 0: 74 image_processing_utils.write_image( 75 chart.img, '%s_edge=%d.jpg' % ( 76 os.path.join(log_path, NAME), edge_mode)) 77 edge_mode_res = cap['metadata']['android.edge.mode'] 78 sharpness_list.append( 79 image_processing_utils.compute_image_sharpness(chart.img)) 80 81 return {'edge_mode': edge_mode_res, 'sharpness': np.mean(sharpness_list)} 82 83 84class EdgeEnhancementTest(its_base_test.ItsBaseTest): 85 """Test that the android.edge.mode param is applied correctly. 86 87 Capture non-reprocess images for each edge mode and calculate their 88 sharpness as a baseline. 89 """ 90 91 def test_edge_enhancement(self): 92 logging.debug('Starting %s', NAME) 93 with its_session_utils.ItsSession( 94 device_id=self.dut.serial, 95 camera_id=self.camera_id, 96 hidden_physical_id=self.hidden_physical_id) as cam: 97 chart_loc_arg = self.chart_loc_arg 98 props = cam.get_camera_properties() 99 props = cam.override_with_hidden_physical_camera_props(props) 100 101 # Check skip conditions 102 camera_properties_utils.skip_unless( 103 camera_properties_utils.read_3a(props) and 104 camera_properties_utils.per_frame_control(props) and 105 camera_properties_utils.edge_mode(props, 0)) 106 107 # Load chart for scene 108 its_session_utils.load_scene( 109 cam, props, self.scene, self.tablet, self.chart_distance) 110 111 # Initialize chart class and locate chart in scene 112 chart = opencv_processing_utils.Chart( 113 cam, props, self.log_path, chart_loc=chart_loc_arg) 114 115 # Define format 116 fmt = 'yuv' 117 size = capture_request_utils.get_available_output_sizes(fmt, props)[0] 118 out_surface = {'width': size[0], 'height': size[1], 'format': fmt} 119 120 # Get proper sensitivity, exposure time, and focus distance. 121 mono_camera = camera_properties_utils.mono_camera(props) 122 s, e, _, _, fd = cam.do_3a(get_results=True, mono_camera=mono_camera) 123 124 # Get the sharpness for each edge mode for regular requests 125 sharpness_regular = [] 126 edge_mode_reported_regular = [] 127 for edge_mode in EDGE_MODES.values(): 128 # Skip unavailable modes 129 if not camera_properties_utils.edge_mode(props, edge_mode): 130 edge_mode_reported_regular.append(edge_mode) 131 sharpness_regular.append(0) 132 continue 133 134 ret = do_capture_and_determine_sharpness( 135 cam, edge_mode, s, e, fd, out_surface, chart, self.log_path) 136 edge_mode_reported_regular.append(ret['edge_mode']) 137 sharpness_regular.append(ret['sharpness']) 138 139 logging.debug('Reported edge modes: %s', edge_mode_reported_regular) 140 logging.debug('Sharpness with EE mode [0,1,2,3]: %s', 141 str(sharpness_regular)) 142 143 logging.debug('Verify HQ is sharper than OFF') 144 e_msg = 'HQ: %.3f, OFF: %.3f' % (sharpness_regular[EDGE_MODES['HQ']], 145 sharpness_regular[EDGE_MODES['OFF']]) 146 assert (sharpness_regular[EDGE_MODES['HQ']] > 147 sharpness_regular[EDGE_MODES['OFF']]), e_msg 148 149 logging.debug('Verify OFF is not sharper than FAST') 150 e_msg = 'FAST: %.3f, OFF: %.3f, RTOL: %.2f' % ( 151 sharpness_regular[EDGE_MODES['FAST']], 152 sharpness_regular[EDGE_MODES['OFF']], SHARPNESS_RTOL) 153 assert (sharpness_regular[EDGE_MODES['FAST']] > 154 sharpness_regular[EDGE_MODES['OFF']]*(1.0-SHARPNESS_RTOL)), e_msg 155 156 logging.debug('Verify FAST is not sharper than HQ') 157 e_msg = 'HQ: %.3f, FAST: %.3f, RTOL: %.2f' % ( 158 sharpness_regular[EDGE_MODES['HQ']], 159 sharpness_regular[EDGE_MODES['FAST']], SHARPNESS_RTOL) 160 assert (sharpness_regular[EDGE_MODES['HQ']] > 161 sharpness_regular[EDGE_MODES['FAST']]*(1.0-SHARPNESS_RTOL)), e_msg 162 163if __name__ == '__main__': 164 test_runner.main() 165