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 20from mobly import test_runner 21import numpy as np 22 23 24import cv2 25import its_base_test 26import camera_properties_utils 27import capture_request_utils 28import image_processing_utils 29import its_session_utils 30import opencv_processing_utils 31 32CHART_ORIENTATIONS = ['nominal', 'flip', 'mirror', 'rotate'] 33NAME = os.path.splitext(os.path.basename(__file__))[0] 34PATCH_H = 0.5 # center 50% 35PATCH_W = 0.5 36PATCH_X = 0.5 - PATCH_W/2 37PATCH_Y = 0.5 - PATCH_H/2 38VGA_W, VGA_H = 640, 480 39 40 41def test_flip_mirror_impl(cam, props, fmt, chart, debug, 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 debug: boolean,whether to run test in debug mode or not. 51 log_path: log_path to save the captured image. 52 53 Returns: 54 boolean: True if flipped, False if not 55 """ 56 # get a local copy of the chart template 57 template = cv2.imread(opencv_processing_utils.CHART_FILE, cv2.IMREAD_ANYDEPTH) 58 59 # take img, crop chart, scale and prep for cv2 template match 60 cam.do_3a() 61 req = capture_request_utils.auto_capture_request() 62 cap = cam.do_capture(req, fmt) 63 y, _, _ = image_processing_utils.convert_capture_to_planes(cap, props) 64 y = image_processing_utils.rotate_img_per_argv(y) 65 patch = image_processing_utils.get_image_patch(y, chart.xnorm, chart.ynorm, 66 chart.wnorm, chart.hnorm) 67 patch = 255 * opencv_processing_utils.gray_scale_img(patch) 68 patch = opencv_processing_utils.scale_img( 69 patch.astype(np.uint8), chart.scale) 70 71 # check image has content 72 if np.max(patch)-np.min(patch) < 255/8: 73 raise AssertionError('Image patch has no content! Check setup.') 74 75 # save full images if in debug 76 if debug: 77 image_processing_utils.write_image( 78 template[:, :, np.newaxis] / 255.0, 79 '%s_template.jpg' % os.path.join(log_path, NAME)) 80 81 # save patch 82 image_processing_utils.write_image( 83 patch[:, :, np.newaxis] / 255.0, 84 '%s_scene_patch.jpg' % os.path.join(log_path, NAME)) 85 86 # crop center areas and strip off any extra rows/columns 87 template = image_processing_utils.get_image_patch( 88 template, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 89 patch = image_processing_utils.get_image_patch( 90 patch, PATCH_X, PATCH_Y, PATCH_W, PATCH_H) 91 patch = patch[0:min(patch.shape[0], template.shape[0]), 92 0:min(patch.shape[1], template.shape[1])] 93 comp_chart = patch 94 95 # determine optimum orientation 96 opts = [] 97 for orientation in CHART_ORIENTATIONS: 98 if orientation == 'flip': 99 comp_chart = np.flipud(patch) 100 elif orientation == 'mirror': 101 comp_chart = np.fliplr(patch) 102 elif orientation == 'rotate': 103 comp_chart = np.flipud(np.fliplr(patch)) 104 correlation = cv2.matchTemplate(comp_chart, template, cv2.TM_CCOEFF) 105 _, opt_val, _, _ = cv2.minMaxLoc(correlation) 106 if debug: 107 cv2.imwrite('%s_%s.jpg' % (os.path.join(log_path, NAME), orientation), 108 comp_chart) 109 logging.debug('%s correlation value: %d', orientation, opt_val) 110 opts.append(opt_val) 111 112 # determine if 'nominal' or 'rotated' is best orientation 113 if not (opts[0] == max(opts) or opts[3] == max(opts)): 114 raise AssertionError( 115 f'Optimum orientation is {CHART_ORIENTATIONS[np.argmax(opts)]}') 116 # print warning if rotated 117 if opts[3] == max(opts): 118 logging.warning('Image is rotated 180 degrees. Tablet might be rotated.') 119 120 121class FlipMirrorTest(its_base_test.ItsBaseTest): 122 """Test to verify if the image is flipped or mirrored.""" 123 124 def test_flip_mirror(self): 125 """Test if image is properly oriented.""" 126 127 logging.debug('Starting %s', NAME) 128 129 with its_session_utils.ItsSession( 130 device_id=self.dut.serial, 131 camera_id=self.camera_id, 132 hidden_physical_id=self.hidden_physical_id) as cam: 133 props = cam.get_camera_properties() 134 props = cam.override_with_hidden_physical_camera_props(props) 135 debug = self.debug_mode 136 137 # check SKIP conditions 138 camera_properties_utils.skip_unless( 139 not camera_properties_utils.mono_camera(props)) 140 141 # load chart for scene 142 its_session_utils.load_scene( 143 cam, props, self.scene, self.tablet, self.chart_distance) 144 145 # initialize chart class and locate chart in scene 146 chart = opencv_processing_utils.Chart(cam, props, self.log_path) 147 fmt = {'format': 'yuv', 'width': VGA_W, 'height': VGA_H} 148 149 # test that image is not flipped, mirrored, or rotated 150 test_flip_mirror_impl(cam, props, fmt, chart, debug, self.log_path) 151 152 153if __name__ == '__main__': 154 test_runner.main() 155