1# Copyright 2016 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 image is not flipped or mirrored.""" 15 16 17import logging 18import os 19 20import cv2 21from mobly import test_runner 22import numpy as np 23 24import its_base_test 25import camera_properties_utils 26import capture_request_utils 27import image_processing_utils 28import its_session_utils 29import opencv_processing_utils 30 31_CHART_ORIENTATIONS = ('nominal', 'flip', 'mirror', 'rotate') 32_NAME = os.path.splitext(os.path.basename(__file__))[0] 33_PATCH_H = 0.5 # center 50% 34_PATCH_W = 0.5 35_PATCH_X = 0.5 - _PATCH_W/2 36_PATCH_Y = 0.5 - _PATCH_H/2 37_VGA_W, _VGA_H = 640, 480 38 39 40def test_flip_mirror_impl(cam, props, fmt, chart, first_api_level, 41 name_with_log_path): 42 43 """Return if image is flipped or mirrored. 44 45 Args: 46 cam: An open its session. 47 props: Properties of cam. 48 fmt: dict; capture format. 49 chart: Object with chart properties. 50 first_api_level: int; first API level value. 51 name_with_log_path: file with log_path to save the captured image. 52 53 Returns: 54 boolean: True if flipped, False if not 55 """ 56 # take img, crop chart, scale and prep for cv2 template match 57 cam.do_3a() 58 req = capture_request_utils.auto_capture_request() 59 cap = cam.do_capture(req, fmt) 60 y, _, _ = image_processing_utils.convert_capture_to_planes(cap, props) 61 y = image_processing_utils.rotate_img_per_argv(y) 62 chart_patch = image_processing_utils.get_image_patch( 63 y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm) 64 image_processing_utils.write_image(chart_patch, 65 f'{name_with_log_path}_chart.jpg') 66 67 # make chart patch 2D & uint8 for cv2.matchTemplate 68 chart_patch = chart_patch[:, :, 0] 69 chart_uint8 = image_processing_utils.convert_image_to_uint8(chart_patch) 70 71 # scale chart 72 chart_uint8 = opencv_processing_utils.scale_img(chart_uint8, chart.scale) 73 74 # check image has content 75 if np.max(chart_uint8)-np.min(chart_uint8) < 255/8: 76 raise AssertionError('Image patch has no content! Check setup.') 77 78 # get a local copy of the chart template and save to results dir 79 template = cv2.imread(opencv_processing_utils.CHART_FILE, cv2.IMREAD_ANYDEPTH) 80 image_processing_utils.write_image(template[:, :, np.newaxis] / 255, 81 f'{name_with_log_path}_template.jpg') 82 83 # crop center areas, strip off any extra rows/columns, & save cropped images 84 template = image_processing_utils.get_image_patch( 85 template, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 86 center_uint8 = image_processing_utils.get_image_patch( 87 chart_uint8, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 88 center_uint8 = center_uint8[:min(center_uint8.shape[0], template.shape[0]), 89 :min(center_uint8.shape[1], template.shape[1])] 90 image_processing_utils.write_image(template[:, :, np.newaxis] / 255, 91 f'{name_with_log_path}_template_crop.jpg') 92 image_processing_utils.write_image(chart_uint8[:, :, np.newaxis] / 255, 93 f'{name_with_log_path}_chart_crop.jpg') 94 95 # determine optimum orientation 96 opts = [] 97 imgs = [] 98 for orientation in _CHART_ORIENTATIONS: 99 if orientation == 'nominal': 100 comp_chart = center_uint8 101 elif orientation == 'flip': 102 comp_chart = np.flipud(center_uint8) 103 elif orientation == 'mirror': 104 comp_chart = np.fliplr(center_uint8) 105 elif orientation == 'rotate': 106 comp_chart = np.flipud(np.fliplr(center_uint8)) 107 correlation = cv2.matchTemplate(comp_chart, template, cv2.TM_CCOEFF) 108 _, opt_val, _, _ = cv2.minMaxLoc(correlation) 109 imgs.append(comp_chart) 110 logging.debug('%s correlation value: %d', orientation, opt_val) 111 opts.append(opt_val) 112 113 # assert correct behavior 114 if opts[0] != max(opts): # 'nominal' is not best orientation 115 for i, orientation in enumerate(_CHART_ORIENTATIONS): 116 cv2.imwrite(f'{name_with_log_path}_{orientation}.jpg', imgs[i]) 117 118 if first_api_level < its_session_utils.ANDROID15_API_LEVEL: 119 if opts[3] != max(opts): # allow 'rotated' < ANDROID15 120 raise AssertionError( 121 f'Optimum orientation is {_CHART_ORIENTATIONS[np.argmax(opts)]}') 122 else: 123 logging.warning('Image rotated 180 degrees. Tablet might be rotated.') 124 else: # no rotation >= ANDROID15 125 raise AssertionError( 126 f'Optimum orientation is {_CHART_ORIENTATIONS[np.argmax(opts)]}') 127 128 129class FlipMirrorTest(its_base_test.ItsBaseTest): 130 """Test to verify if the image is flipped or mirrored.""" 131 132 def test_flip_mirror(self): 133 """Test if image is properly oriented.""" 134 with its_session_utils.ItsSession( 135 device_id=self.dut.serial, 136 camera_id=self.camera_id, 137 hidden_physical_id=self.hidden_physical_id) as cam: 138 props = cam.get_camera_properties() 139 props = cam.override_with_hidden_physical_camera_props(props) 140 name_with_log_path = os.path.join(self.log_path, _NAME) 141 first_api_level = its_session_utils.get_first_api_level(self.dut.serial) 142 143 # check SKIP conditions 144 camera_properties_utils.skip_unless( 145 not camera_properties_utils.mono_camera(props)) 146 147 # load chart for scene 148 its_session_utils.load_scene( 149 cam, props, self.scene, self.tablet, self.chart_distance) 150 151 # initialize chart class and locate chart in scene 152 chart = opencv_processing_utils.Chart( 153 cam, props, self.log_path, distance=self.chart_distance) 154 fmt = {'format': 'yuv', 'width': _VGA_W, 'height': _VGA_H} 155 156 # test that image is not flipped, mirrored, or rotated 157 test_flip_mirror_impl(cam, props, fmt, chart, first_api_level, 158 name_with_log_path) 159 160 161if __name__ == '__main__': 162 test_runner.main() 163