1# Copyright 2022 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 that flash is fired when lighting conditions are dark.""" 15 16 17import logging 18import os.path 19import pathlib 20 21import cv2 22from mobly import test_runner 23import numpy as np 24 25import its_base_test 26import camera_properties_utils 27import image_processing_utils 28import its_session_utils 29import lighting_control_utils 30import opencv_processing_utils 31import ui_interaction_utils 32 33_JETPACK_CAMERA_APP_PACKAGE_NAME = 'com.google.jetpackcamera' 34_MEAN_DELTA_ATOL = 15 # mean used for reflective charts 35_PATCH_H = 0.25 # center 25% 36_PATCH_W = 0.25 37_PATCH_X = 0.5 - _PATCH_W/2 38_PATCH_Y = 0.5 - _PATCH_H/2 39_TEST_NAME = os.path.splitext(os.path.basename(__file__))[0] 40 41 42class AutoFlashTest(its_base_test.UiAutomatorItsBaseTest): 43 """Test that flash is fired when lighting conditions are dark using JCA.""" 44 45 def setup_class(self): 46 super().setup_class() 47 self.ui_app = _JETPACK_CAMERA_APP_PACKAGE_NAME 48 # restart CtsVerifier to ensure that correct flags are set 49 ui_interaction_utils.force_stop_app( 50 self.dut, its_base_test.CTS_VERIFIER_PKG) 51 self.dut.adb.shell( 52 'am start -n com.android.cts.verifier/.CtsVerifierActivity') 53 54 def teardown_test(self): 55 ui_interaction_utils.force_stop_app(self.dut, self.ui_app) 56 57 def test_auto_flash(self): 58 with its_session_utils.ItsSession( 59 device_id=self.dut.serial, 60 camera_id=self.camera_id, 61 hidden_physical_id=self.hidden_physical_id) as cam: 62 props = cam.get_camera_properties() 63 props = cam.override_with_hidden_physical_camera_props(props) 64 test_name = os.path.join(self.log_path, _TEST_NAME) 65 66 # close camera after props retrieved, so that ItsTestActivity can open it 67 cam.close_camera() 68 69 # check SKIP conditions 70 first_api_level = its_session_utils.get_first_api_level(self.dut.serial) 71 facing_front = (props['android.lens.facing'] == 72 camera_properties_utils.LENS_FACING['FRONT']) 73 should_run_front = ( 74 facing_front and 75 first_api_level >= its_session_utils.ANDROID15_API_LEVEL 76 ) 77 should_run_rear = ( 78 camera_properties_utils.flash(props) and 79 first_api_level >= its_session_utils.ANDROID13_API_LEVEL 80 ) 81 camera_properties_utils.skip_unless(should_run_front or should_run_rear) 82 83 # establish connection with lighting controller 84 arduino_serial_port = lighting_control_utils.lighting_control( 85 self.lighting_cntl, self.lighting_ch) 86 87 # turn OFF lights to darken scene 88 lighting_control_utils.set_lighting_state( 89 arduino_serial_port, self.lighting_ch, 'OFF') 90 91 # take capture with no flash as baseline 92 path = pathlib.Path( 93 cam.do_jca_capture( 94 self.dut, 95 self.log_path, 96 flash='OFF', 97 facing=props['android.lens.facing'], 98 ) 99 ) 100 no_flash_capture_path = path.with_name( 101 f'{path.stem}_no_flash{path.suffix}' 102 ) 103 os.rename(path, no_flash_capture_path) 104 cv2_no_flash_image = cv2.imread(str(no_flash_capture_path)) 105 y = opencv_processing_utils.convert_to_y(cv2_no_flash_image, 'BGR') 106 # Add a color channel dimension for interoperability 107 y = np.expand_dims(y, axis=2) 108 patch = image_processing_utils.get_image_patch( 109 y, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H 110 ) 111 no_flash_mean = image_processing_utils.compute_image_means(patch)[0] 112 image_processing_utils.write_image(y, f'{test_name}_no_flash_Y.jpg') 113 logging.debug('No flash frames Y mean: %.4f', no_flash_mean) 114 115 # take capture with auto flash enabled 116 logging.debug('Taking capture with auto flash enabled.') 117 path = pathlib.Path( 118 cam.do_jca_capture( 119 self.dut, 120 self.log_path, 121 flash='AUTO', 122 facing=props['android.lens.facing'] 123 ) 124 ) 125 auto_flash_capture_path = path.with_name( 126 f'{path.stem}_auto_flash{path.suffix}' 127 ) 128 os.rename(path, auto_flash_capture_path) 129 cv2_auto_flash_image = cv2.imread(str(auto_flash_capture_path)) 130 y = opencv_processing_utils.convert_to_y(cv2_auto_flash_image, 'BGR') 131 # Add a color channel dimension for interoperability 132 y = np.expand_dims(y, axis=2) 133 patch = image_processing_utils.get_image_patch( 134 y, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H 135 ) 136 flash_mean = image_processing_utils.compute_image_means(patch)[0] 137 image_processing_utils.write_image(y, f'{test_name}_auto_flash_Y.jpg') 138 logging.debug('Flash frames Y mean: %.4f', flash_mean) 139 140 # confirm correct behavior 141 mean_delta = flash_mean - no_flash_mean 142 if mean_delta <= _MEAN_DELTA_ATOL: 143 raise AssertionError(f'mean FLASH-OFF: {mean_delta:.3f}, ' 144 f'ATOL: {_MEAN_DELTA_ATOL}') 145 146 # turn lights back ON 147 lighting_control_utils.set_lighting_state( 148 arduino_serial_port, self.lighting_ch, 'ON') 149 150if __name__ == '__main__': 151 test_runner.main() 152