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