1# Copyright 2013 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 YUV & JPEG image captures have similar brightness.""" 15 16 17import logging 18import os.path 19import matplotlib 20from matplotlib import pylab 21from mobly import test_runner 22 23import its_base_test 24import camera_properties_utils 25import capture_request_utils 26import image_processing_utils 27import its_session_utils 28import target_exposure_utils 29 30NAME = os.path.splitext(os.path.basename(__file__))[0] 31PATCH_H = 0.1 # center 10% 32PATCH_W = 0.1 33PATCH_X = 0.5 - PATCH_W/2 34PATCH_Y = 0.5 - PATCH_H/2 35THRESHOLD_MAX_RMS_DIFF = 0.03 36 37 38def do_capture_and_extract_rgb_means(req, cam, size, img_type, log_path, debug): 39 """Do capture and extra rgb_means of center patch. 40 41 Args: 42 req: capture request 43 cam: camera object 44 size: [width, height] 45 img_type: string of 'yuv' or 'jpeg' 46 log_path: location for saving image 47 debug: boolean to flag saving captured images 48 49 Returns: 50 center patch RGB means 51 """ 52 out_surface = {'width': size[0], 'height': size[1], 'format': img_type} 53 cap = cam.do_capture(req, out_surface) 54 if img_type == 'jpg': 55 assert cap['format'] == 'jpeg' 56 img = image_processing_utils.decompress_jpeg_to_rgb_image(cap['data']) 57 else: 58 assert cap['format'] == img_type 59 img = image_processing_utils.convert_capture_to_rgb_image(cap) 60 assert cap['width'] == size[0] 61 assert cap['height'] == size[1] 62 63 if debug: 64 image_processing_utils.write_image(img, '%s_%s_w%d_h%d.jpg'%( 65 os.path.join(log_path, NAME), img_type, size[0], size[1])) 66 if img_type == 'jpg': 67 assert img.shape[0] == size[1] 68 assert img.shape[1] == size[0] 69 assert img.shape[2] == 3 70 patch = image_processing_utils.get_image_patch( 71 img, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 72 rgb = image_processing_utils.compute_image_means(patch) 73 logging.debug('Captured %s %dx%d rgb = %s', 74 img_type, cap['width'], cap['height'], str(rgb)) 75 return rgb 76 77 78class YuvJpegAllTest(its_base_test.ItsBaseTest): 79 """Test reported sizes & fmts for YUV & JPEG caps return similar images.""" 80 81 def test_yuv_jpeg_all(self): 82 logging.debug('Starting %s', NAME) 83 with its_session_utils.ItsSession( 84 device_id=self.dut.serial, 85 camera_id=self.camera_id, 86 hidden_physical_id=self.hidden_physical_id) as cam: 87 props = cam.get_camera_properties() 88 props = cam.override_with_hidden_physical_camera_props(props) 89 camera_properties_utils.skip_unless( 90 camera_properties_utils.compute_target_exposure(props) and 91 camera_properties_utils.per_frame_control(props)) 92 log_path = self.log_path 93 debug = self.debug_mode 94 95 # Load chart for scene 96 its_session_utils.load_scene( 97 cam, props, self.scene, self.tablet, self.chart_distance) 98 99 # Use a manual request with a linear tonemap so that the YUV and JPEG 100 # should look the same (once converted by the image_processing_utils). 101 e, s = target_exposure_utils.get_target_exposure_combos( 102 log_path, cam)['midExposureTime'] 103 req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props) 104 105 rgbs = [] 106 for size in capture_request_utils.get_available_output_sizes( 107 'yuv', props): 108 rgbs.append(do_capture_and_extract_rgb_means( 109 req, cam, size, 'yuv', log_path, debug)) 110 111 for size in capture_request_utils.get_available_output_sizes( 112 'jpg', props): 113 rgbs.append(do_capture_and_extract_rgb_means( 114 req, cam, size, 'jpg', log_path, debug)) 115 116 # Plot means vs format 117 pylab.figure(NAME) 118 pylab.title(NAME) 119 pylab.plot(range(len(rgbs)), [r[0] for r in rgbs], '-ro') 120 pylab.plot(range(len(rgbs)), [g[1] for g in rgbs], '-go') 121 pylab.plot(range(len(rgbs)), [b[2] for b in rgbs], '-bo') 122 pylab.ylim([0, 1]) 123 pylab.xlabel('format number') 124 pylab.ylabel('RGB avg [0, 1]') 125 matplotlib.pyplot.savefig( 126 '%s_plot_means.png' % os.path.join(log_path, NAME)) 127 128 # Assert all captured images are similar in RBG space 129 max_diff = 0 130 for rgb_i in rgbs[1:]: 131 rms_diff = image_processing_utils.compute_image_rms_difference( 132 rgbs[0], rgb_i) # use first capture as reference 133 max_diff = max(max_diff, rms_diff) 134 msg = 'Max RMS difference: %.4f' % max_diff 135 logging.debug('%s', msg) 136 e_msg = msg + ' spec: %.3f' % THRESHOLD_MAX_RMS_DIFF 137 assert max_diff < THRESHOLD_MAX_RMS_DIFF, e_msg 138 139if __name__ == '__main__': 140 test_runner.main() 141