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