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 34CH_FULL_SCALE = 255 35CH_THRESH_BLACK = 6 36CH_THRESH_WHITE = CH_FULL_SCALE - 6 37CH_TOL_WHITE = 2 38COLOR_PLANES = ['R', 'G', 'B'] 39NAME = os.path.splitext(os.path.basename(__file__))[0] 40PATCH_H = 0.1 41PATCH_W = 0.1 42PATCH_X = 0.45 43PATCH_Y = 0.45 44VGA_WIDTH, VGA_HEIGHT = 640, 480 45 46 47def do_img_capture(cam, s, e, fmt, latency, cap_name, 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 log_path: 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, '%s_%s.jpg' % (os.path.join(log_path, NAME), cap_name)) 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 98 # Check SKIP conditions 99 camera_properties_utils.skip_unless( 100 camera_properties_utils.manual_sensor(props)) 101 102 # Load chart for scene 103 its_session_utils.load_scene( 104 cam, props, self.scene, self.tablet, self.chart_distance) 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 log_path = self.log_path 112 113 # Take shot with very low ISO and exp time: expect it to be black 114 s = sens_range[0] 115 e = expt_range[0] 116 black_means = do_img_capture(cam, s, e, fmt, latency, 'black', 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(cam, s, e, fmt, latency, 'white', log_path) 125 r_means.append(white_means[0]) 126 g_means.append(white_means[1]) 127 b_means.append(white_means[2]) 128 129 # Draw plot 130 pylab.title('test_black_white') 131 pylab.plot([0, 1], r_means, '-ro') 132 pylab.plot([0, 1], g_means, '-go') 133 pylab.plot([0, 1], b_means, '-bo') 134 pylab.xlabel('Capture Number') 135 pylab.ylabel('Output Values [0:255]') 136 pylab.ylim([0, 255]) 137 matplotlib.pyplot.savefig('%s_plot_means.png' % ( 138 os.path.join(log_path, NAME))) 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_TOL_WHITE): 157 raise AssertionError('channel saturation not equal! ' 158 f'RGB: {white_means}, ATOL: {CH_TOL_WHITE}') 159 160if __name__ == '__main__': 161 test_runner.main() 162 163