1# Copyright 2019 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 camera will produce full black & full white images.""" 15 16 17import logging 18import math 19import os.path 20import matplotlib 21from matplotlib import pylab 22 23 24from mobly import test_runner 25import numpy as np 26 27import its_base_test 28import camera_properties_utils 29import capture_request_utils 30import image_processing_utils 31import its_session_utils 32 33_ANDROID10_API_LEVEL = 29 34_CH_FULL_SCALE = 255 35_CH_THRESH_BLACK = 6 36_CH_THRESH_WHITE = _CH_FULL_SCALE - 6 37_CH_ATOL_WHITE = 2 38_COLOR_PLANES = ['R', 'G', 'B'] 39_NAME = os.path.splitext(os.path.basename(__file__))[0] 40_PATCH_H = 0.1 41_PATCH_W = 0.1 42_PATCH_X = 0.5 - _PATCH_W/2 43_PATCH_Y = 0.5 - _PATCH_H/2 44_VGA_WIDTH, _VGA_HEIGHT = 640, 480 45 46 47def do_img_capture(cam, s, e, fmt, latency, cap_name, name_with_log_path): 48 """Do the image captures with the defined parameters. 49 50 Args: 51 cam: its_session open for camera 52 s: sensitivity for request 53 e: exposure in ns for request 54 fmt: format of request 55 latency: number of frames for sync latency of request 56 cap_name: string to define the capture 57 name_with_log_path: NAME with path for plot directory 58 59 Returns: 60 means values of center patch from capture 61 """ 62 63 req = capture_request_utils.manual_capture_request(s, e) 64 cap = its_session_utils.do_capture_with_latency(cam, req, latency, fmt) 65 img = image_processing_utils.convert_capture_to_rgb_image(cap) 66 image_processing_utils.write_image( 67 img, f'{name_with_log_path}_{cap_name}.jpg') 68 patch = image_processing_utils.get_image_patch( 69 img, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 70 means = image_processing_utils.compute_image_means(patch) 71 means = [m * _CH_FULL_SCALE for m in means] 72 logging.debug('%s pixel means: %s', cap_name, str(means)) 73 r_exp = cap['metadata']['android.sensor.exposureTime'] 74 r_iso = cap['metadata']['android.sensor.sensitivity'] 75 logging.debug('%s shot write values: sens = %d, exp time = %.4fms', 76 cap_name, s, (e / 1000000.0)) 77 logging.debug('%s shot read values: sens = %d, exp time = %.4fms', 78 cap_name, r_iso, (r_exp / 1000000.0)) 79 return means 80 81 82class BlackWhiteTest(its_base_test.ItsBaseTest): 83 """Test that device will prodoce full black + white images. 84 """ 85 86 def test_black_white(self): 87 r_means = [] 88 g_means = [] 89 b_means = [] 90 91 with its_session_utils.ItsSession( 92 device_id=self.dut.serial, 93 camera_id=self.camera_id, 94 hidden_physical_id=self.hidden_physical_id) as cam: 95 props = cam.get_camera_properties() 96 props = cam.override_with_hidden_physical_camera_props(props) 97 name_with_log_path = os.path.join(self.log_path, _NAME) 98 99 # Check SKIP conditions 100 camera_properties_utils.skip_unless( 101 camera_properties_utils.manual_sensor(props)) 102 103 # Load chart for scene 104 its_session_utils.load_scene( 105 cam, props, self.scene, self.tablet, 106 its_session_utils.CHART_DISTANCE_NO_SCALING) 107 108 # Initialize params for requests 109 latency = camera_properties_utils.sync_latency(props) 110 fmt = {'format': 'yuv', 'width': _VGA_WIDTH, 'height': _VGA_HEIGHT} 111 expt_range = props['android.sensor.info.exposureTimeRange'] 112 sens_range = props['android.sensor.info.sensitivityRange'] 113 114 # Take shot with very low ISO and exp time: expect it to be black 115 s = sens_range[0] 116 e = expt_range[0] 117 black_means = do_img_capture( 118 cam, s, e, fmt, latency, 'black', name_with_log_path) 119 r_means.append(black_means[0]) 120 g_means.append(black_means[1]) 121 b_means.append(black_means[2]) 122 123 # Take shot with very high ISO and exp time: expect it to be white. 124 s = sens_range[1] 125 e = expt_range[1] 126 white_means = do_img_capture( 127 cam, s, e, fmt, latency, 'white', name_with_log_path) 128 r_means.append(white_means[0]) 129 g_means.append(white_means[1]) 130 b_means.append(white_means[2]) 131 132 # Draw plot 133 pylab.title('test_black_white') 134 pylab.plot([0, 1], r_means, '-ro') 135 pylab.plot([0, 1], g_means, '-go') 136 pylab.plot([0, 1], b_means, '-bo') 137 pylab.xlabel('Capture Number') 138 pylab.ylabel('Output Values [0:255]') 139 pylab.ylim([0, 255]) 140 matplotlib.pyplot.savefig(f'{name_with_log_path}_plot_means.png') 141 142 # Assert blacks below CH_THRESH_BLACK 143 for ch, mean in enumerate(black_means): 144 if mean >= _CH_THRESH_BLACK: 145 raise AssertionError(f'{_COLOR_PLANES[ch]} black: {mean:.1f}, ' 146 f'THRESH: {_CH_THRESH_BLACK}') 147 148 # Assert whites above CH_THRESH_WHITE 149 for ch, mean in enumerate(white_means): 150 if mean <= _CH_THRESH_WHITE: 151 raise AssertionError(f'{_COLOR_PLANES[ch]} white: {mean:.1f}, ' 152 f'THRESH: {_CH_THRESH_WHITE}') 153 154 # Assert channels saturate evenly (was test_channel_saturation) 155 first_api_level = its_session_utils.get_first_api_level(self.dut.serial) 156 if first_api_level > _ANDROID10_API_LEVEL: 157 if not math.isclose( 158 np.amin(white_means), np.amax(white_means), abs_tol=_CH_ATOL_WHITE): 159 raise AssertionError('channel saturation not equal! ' 160 f'RGB: {white_means}, ATOL: {_CH_ATOL_WHITE}') 161 162if __name__ == '__main__': 163 test_runner.main() 164 165