• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, debug, name_with_log_path):
41
42  """Return if image is flipped or mirrored.
43
44  Args:
45   cam : An open its session.
46   props : Properties of cam.
47   fmt : dict,Capture format.
48   chart: Object with chart properties.
49   debug: boolean,whether to run test in debug mode or not.
50   name_with_log_path: file with log_path to save the captured image.
51
52  Returns:
53    boolean: True if flipped, False if not
54  """
55  # get a local copy of the chart template
56  template = cv2.imread(opencv_processing_utils.CHART_FILE, cv2.IMREAD_ANYDEPTH)
57
58  # take img, crop chart, scale and prep for cv2 template match
59  cam.do_3a()
60  req = capture_request_utils.auto_capture_request()
61  cap = cam.do_capture(req, fmt)
62  y, _, _ = image_processing_utils.convert_capture_to_planes(cap, props)
63  y = image_processing_utils.rotate_img_per_argv(y)
64  patch = image_processing_utils.get_image_patch(y, chart.xnorm, chart.ynorm,
65                                                 chart.wnorm, chart.hnorm)
66  patch = 255 * opencv_processing_utils.gray_scale_img(patch)
67  patch = opencv_processing_utils.scale_img(
68      patch.astype(np.uint8), chart.scale)
69
70  # check image has content
71  if np.max(patch)-np.min(patch) < 255/8:
72    raise AssertionError('Image patch has no content! Check setup.')
73
74  # save full images if in debug
75  if debug:
76    image_processing_utils.write_image(template[:, :, np.newaxis] / 255.0,
77                                       f'{name_with_log_path}_template.jpg')
78
79  # save patch
80  image_processing_utils.write_image(patch[:, :, np.newaxis] / 255.0,
81                                     f'{name_with_log_path}_scene_patch.jpg')
82
83  # crop center areas and strip off any extra rows/columns
84  template = image_processing_utils.get_image_patch(
85      template, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H)
86  patch = image_processing_utils.get_image_patch(
87      patch, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H)
88  patch = patch[0:min(patch.shape[0], template.shape[0]),
89                0:min(patch.shape[1], template.shape[1])]
90  comp_chart = patch
91
92  # determine optimum orientation
93  opts = []
94  for orientation in _CHART_ORIENTATIONS:
95    if orientation == 'flip':
96      comp_chart = np.flipud(patch)
97    elif orientation == 'mirror':
98      comp_chart = np.fliplr(patch)
99    elif orientation == 'rotate':
100      comp_chart = np.flipud(np.fliplr(patch))
101    correlation = cv2.matchTemplate(comp_chart, template, cv2.TM_CCOEFF)
102    _, opt_val, _, _ = cv2.minMaxLoc(correlation)
103    if debug:
104      cv2.imwrite(f'{name_with_log_path}_{orientation}.jpg', comp_chart)
105    logging.debug('%s correlation value: %d', orientation, opt_val)
106    opts.append(opt_val)
107
108  # determine if 'nominal' or 'rotated' is best orientation
109  if not (opts[0] == max(opts) or opts[3] == max(opts)):
110    raise AssertionError(
111        f'Optimum orientation is {_CHART_ORIENTATIONS[np.argmax(opts)]}')
112  # print warning if rotated
113  if opts[3] == max(opts):
114    logging.warning('Image is rotated 180 degrees. Tablet might be rotated.')
115
116
117class FlipMirrorTest(its_base_test.ItsBaseTest):
118  """Test to verify if the image is flipped or mirrored."""
119
120  def test_flip_mirror(self):
121    """Test if image is properly oriented."""
122
123    logging.debug('Starting %s', _NAME)
124
125    with its_session_utils.ItsSession(
126        device_id=self.dut.serial,
127        camera_id=self.camera_id,
128        hidden_physical_id=self.hidden_physical_id) as cam:
129      props = cam.get_camera_properties()
130      props = cam.override_with_hidden_physical_camera_props(props)
131      debug = self.debug_mode
132      name_with_log_path = os.path.join(self.log_path, _NAME)
133
134      # check SKIP conditions
135      camera_properties_utils.skip_unless(
136          not camera_properties_utils.mono_camera(props))
137
138      # load chart for scene
139      its_session_utils.load_scene(
140          cam, props, self.scene, self.tablet, self.chart_distance)
141
142      # initialize chart class and locate chart in scene
143      chart = opencv_processing_utils.Chart(
144          cam, props, self.log_path, distance=self.chart_distance)
145      fmt = {'format': 'yuv', 'width': _VGA_W, 'height': _VGA_H}
146
147      # test that image is not flipped, mirrored, or rotated
148      test_flip_mirror_impl(cam, props, fmt, chart, debug, name_with_log_path)
149
150
151if __name__ == '__main__':
152  test_runner.main()
153