• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  logging.debug('e_cap: %d, s_cap: %d',
55                cap['metadata']['android.sensor.exposureTime'],
56                cap['metadata']['android.sensor.sensitivity'])
57  if img_type == 'jpg':
58    if cap['format'] != 'jpeg':
59      raise AssertionError(f"{cap['format']} != jpeg")
60    img = image_processing_utils.decompress_jpeg_to_rgb_image(cap['data'])
61  else:
62    if cap['format'] != img_type:
63      raise AssertionError(f"{cap['format']} != {img_type}")
64    img = image_processing_utils.convert_capture_to_rgb_image(cap)
65  if cap['width'] != size[0]:
66    raise AssertionError(f"{cap['width']} != {size[0]}")
67  if cap['height'] != size[1]:
68    raise AssertionError(f"{cap['height']} != {size[1]}")
69
70  if debug:
71    image_processing_utils.write_image(img, '%s_%s_w%d_h%d.jpg'%(
72        os.path.join(log_path, NAME), img_type, size[0], size[1]))
73
74  if img_type == 'jpg':
75    if img.shape[0] != size[1]:
76      raise AssertionError(f'{img.shape[0]} != {size[1]}')
77    if img.shape[1] != size[0]:
78      raise AssertionError(f'{img.shape[1]} != {size[0]}')
79    if img.shape[2] != 3:
80      raise AssertionError(f'{img.shape[2]} != 3')
81  patch = image_processing_utils.get_image_patch(
82      img, PATCH_X, PATCH_Y, PATCH_W, PATCH_H)
83  rgb = image_processing_utils.compute_image_means(patch)
84  logging.debug('Captured %s %dx%d rgb = %s',
85                img_type, cap['width'], cap['height'], str(rgb))
86  return rgb
87
88
89class YuvJpegAllTest(its_base_test.ItsBaseTest):
90  """Test reported sizes & fmts for YUV & JPEG caps return similar images."""
91
92  def test_yuv_jpeg_all(self):
93    logging.debug('Starting %s', NAME)
94    with its_session_utils.ItsSession(
95        device_id=self.dut.serial,
96        camera_id=self.camera_id,
97        hidden_physical_id=self.hidden_physical_id) as cam:
98      props = cam.get_camera_properties()
99      props = cam.override_with_hidden_physical_camera_props(props)
100      camera_properties_utils.skip_unless(
101          camera_properties_utils.compute_target_exposure(props) and
102          camera_properties_utils.per_frame_control(props))
103      log_path = self.log_path
104      debug = self.debug_mode
105
106      # Load chart for scene
107      its_session_utils.load_scene(
108          cam, props, self.scene, self.tablet, self.chart_distance)
109
110      # Use a manual request with a linear tonemap so that the YUV and JPEG
111      # should look the same (once converted by the image_processing_utils).
112      e, s = target_exposure_utils.get_target_exposure_combos(
113          log_path, cam)['midExposureTime']
114      logging.debug('e_req: %d, s_req: %d', e, s)
115      req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props)
116
117      rgbs = []
118      for size in capture_request_utils.get_available_output_sizes(
119          'yuv', props):
120        rgbs.append(do_capture_and_extract_rgb_means(
121            req, cam, size, 'yuv', log_path, debug))
122
123      for size in capture_request_utils.get_available_output_sizes(
124          'jpg', props):
125        rgbs.append(do_capture_and_extract_rgb_means(
126            req, cam, size, 'jpg', log_path, debug))
127
128      # Plot means vs format
129      pylab.figure(NAME)
130      pylab.title(NAME)
131      pylab.plot(range(len(rgbs)), [r[0] for r in rgbs], '-ro')
132      pylab.plot(range(len(rgbs)), [g[1] for g in rgbs], '-go')
133      pylab.plot(range(len(rgbs)), [b[2] for b in rgbs], '-bo')
134      pylab.ylim([0, 1])
135      pylab.xlabel('format number')
136      pylab.ylabel('RGB avg [0, 1]')
137      matplotlib.pyplot.savefig(
138          '%s_plot_means.png' % os.path.join(log_path, NAME))
139
140      # Assert all captured images are similar in RBG space
141      max_diff = 0
142      for rgb_i in rgbs[1:]:
143        rms_diff = image_processing_utils.compute_image_rms_difference_1d(
144            rgbs[0], rgb_i)  # use first capture as reference
145        max_diff = max(max_diff, rms_diff)
146      msg = 'Max RMS difference: %.4f' % max_diff
147      logging.debug('%s', msg)
148      if max_diff >= THRESHOLD_MAX_RMS_DIFF:
149        raise AssertionError(f'{msg} spec: {THRESHOLD_MAX_RMS_DIFF}')
150
151if __name__ == '__main__':
152  test_runner.main()
153