• 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 android.lens.state when lens is moving."""
15
16
17import copy
18import logging
19import math
20import os
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
32_FRAME_ATOL_MS = 10
33_LENS_INTRINSIC_CAL_FX_IDX = 0
34_LENS_INTRINSIC_CAL_FY_IDX = 1
35_LENS_INTRINSIC_CAL_RTOL = 0.01
36_MIN_AF_FD_RTOL = 0.2  # AF value must 20% larger than min_fd
37_NAME = os.path.splitext(os.path.basename(__file__))[0]
38_NUM_FRAMES_PER_FD = 12
39_POSITION_RTOL = 0.10  # 10%
40_SHARPNESS_RTOL = 0.10  # 10%
41_START_FRAME = 1  # start on second frame
42_VGA_WIDTH, _VGA_HEIGHT = 640, 480
43
44
45def take_caps_and_determine_sharpness(
46    cam, props, fmt, gain, exp, af_fd, chart, log_path):
47  """Return fd, sharpness, lens state of the output images.
48
49  Args:
50    cam: An open device session.
51    props: Properties of cam
52    fmt: dict; capture format
53    gain: Sensitivity for the request as defined in android.sensor.sensitivity
54    exp: Exposure time for the request as defined in
55         android.sensor.exposureTime
56    af_fd: Focus distance for the request as defined in
57           android.lens.focusDistance
58    chart: Object that contains chart information
59    log_path: log_path to save the captured image
60
61  Returns:
62    Object containing reported sharpness of the output image, keyed by
63    the following string:
64        'sharpness'
65  """
66
67  # initialize variables and take data sets
68  data_set = {}
69  white_level = int(props['android.sensor.info.whiteLevel'])
70  min_fd = props['android.lens.info.minimumFocusDistance']
71  fds = [af_fd] * _NUM_FRAMES_PER_FD + [min_fd] * _NUM_FRAMES_PER_FD
72  reqs = []
73  for i, fd in enumerate(fds):
74    reqs.append(capture_request_utils.manual_capture_request(gain, exp))
75    reqs[i]['android.lens.focusDistance'] = fd
76  caps = cam.do_capture(reqs, fmt)
77  caps = caps[_START_FRAME:]
78  for i, cap in enumerate(caps):
79    data = {'fd': fds[i+_START_FRAME]}
80    data['frame_num'] = i + _START_FRAME
81    data['loc'] = cap['metadata']['android.lens.focusDistance']
82    data['lens_moving'] = (cap['metadata']['android.lens.state']
83                           == 1)
84    data['lens_intrinsic_calibration'] = (
85        cap['metadata']['android.lens.intrinsicCalibration'])
86    timestamp = cap['metadata']['android.sensor.timestamp'] * 1E-6
87    if i == 0:
88      timestamp_init = timestamp
89    timestamp -= timestamp_init
90    data['timestamp'] = timestamp
91    y, _, _ = image_processing_utils.convert_capture_to_planes(cap, props)
92    chart.img = image_processing_utils.normalize_img(
93        image_processing_utils.get_image_patch(
94            y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm))
95    image_processing_utils.write_image(
96        chart.img, f'{os.path.join(log_path, _NAME)}_i={i}.jpg')
97    data['sharpness'] = (
98        white_level * image_processing_utils.compute_image_sharpness(chart.img))
99    data_set[i+_START_FRAME] = data
100  return data_set
101
102
103class LensMovementReportingTest(its_base_test.ItsBaseTest):
104  """Test if focus distance is properly reported.
105
106  Do unit step of focus distance and check sharpness correlates.
107  """
108
109  def test_lens_movement_reporting(self):
110    with its_session_utils.ItsSession(
111        device_id=self.dut.serial,
112        camera_id=self.camera_id,
113        hidden_physical_id=self.hidden_physical_id) as cam:
114      props = cam.get_camera_properties()
115      props = cam.override_with_hidden_physical_camera_props(props)
116
117      # Check skip conditions
118      camera_properties_utils.skip_unless(
119          not camera_properties_utils.fixed_focus(props) and
120          camera_properties_utils.read_3a(props) and
121          camera_properties_utils.lens_approx_calibrated(props))
122      lens_calibrated = camera_properties_utils.lens_calibrated(props)
123      logging.debug('lens_calibrated: %d', lens_calibrated)
124
125      # Load scene
126      its_session_utils.load_scene(
127          cam, props, self.scene, self.tablet, self.chart_distance)
128
129      # Initialize chart class and locate chart in scene
130      chart = opencv_processing_utils.Chart(
131          cam, props, self.log_path, distance=self.chart_distance)
132
133      # Get proper sensitivity, exposure time, and focus distance with 3A.
134      mono_camera = camera_properties_utils.mono_camera(props)
135      s, e, _, _, af_fd = cam.do_3a(get_results=True, mono_camera=mono_camera)
136
137      # Get sharpness for each focal distance
138      fmt = {'format': 'yuv', 'width': _VGA_WIDTH, 'height': _VGA_HEIGHT}
139      frame_data = take_caps_and_determine_sharpness(
140          cam, props, fmt, s, e, af_fd, chart, self.log_path)
141      for k in sorted(frame_data):
142        logging.debug(
143            'i: %d\tfd: %.3f\tdiopters: %.3f \tsharpness: %.1f  \t'
144            'lens_state: %d \ttimestamp: %.1fms\t cal: %s',
145            frame_data[k]['frame_num'], frame_data[k]['fd'],
146            frame_data[k]['loc'], frame_data[k]['sharpness'],
147            frame_data[k]['lens_moving'], frame_data[k]['timestamp'],
148            np.around(frame_data[k]['lens_intrinsic_calibration'], 2))
149
150      # Assert frames are consecutive
151      frame_diffs = np.gradient([v['timestamp'] for v in frame_data.values()])
152      delta_diffs = np.amax(frame_diffs) - np.amin(frame_diffs)
153      if not math.isclose(delta_diffs, 0, abs_tol=_FRAME_ATOL_MS):
154        raise AssertionError(f'Timestamp gradient(ms): {delta_diffs:.1f}, '
155                             f'ATOL: {_FRAME_ATOL_MS}')
156
157      # Remove data when lens is moving
158      frame_data_non_moving = copy.deepcopy(frame_data)
159      for k in sorted(frame_data_non_moving):
160        if frame_data_non_moving[k]['lens_moving']:
161          del frame_data_non_moving[k]
162
163      # Split data into min_fd and af data for processing
164      data_min_fd = {}
165      data_af_fd = {}
166      for k in sorted(frame_data_non_moving):
167        if frame_data_non_moving[k]['fd'] == props[
168            'android.lens.info.minimumFocusDistance']:
169          data_min_fd[k] = frame_data_non_moving[k]
170        if frame_data_non_moving[k]['fd'] == af_fd:
171          data_af_fd[k] = frame_data_non_moving[k]
172
173      logging.debug('Assert reported locs are close for af_fd captures')
174      min_loc = min([v['loc'] for v in data_af_fd.values()])
175      max_loc = max([v['loc'] for v in data_af_fd.values()])
176      if not math.isclose(min_loc, max_loc, rel_tol=_POSITION_RTOL):
177        raise AssertionError(f'af_fd[loc] min: {min_loc:.3f}, max: '
178                             f'{max_loc:.3f}, RTOL: {_POSITION_RTOL}')
179
180      logging.debug('Assert reported sharpness is close at af_fd')
181      min_sharp = min([v['sharpness'] for v in data_af_fd.values()])
182      max_sharp = max([v['sharpness'] for v in data_af_fd.values()])
183      if not math.isclose(min_sharp, max_sharp, rel_tol=_SHARPNESS_RTOL):
184        raise AssertionError(f'af_fd[sharpness] min: {min_sharp:.3f}, '
185                             f'max: {max_sharp:.3f}, RTOL: {_SHARPNESS_RTOL}')
186
187      logging.debug('Assert reported loc is close to assign loc for af_fd')
188      first_key = min(data_af_fd.keys())  # find 1st non-moving frame
189      loc = data_af_fd[first_key]['loc']
190      fd = data_af_fd[first_key]['fd']
191      if not math.isclose(loc, fd, rel_tol=_POSITION_RTOL):
192        raise AssertionError(f'af_fd[loc]: {loc:.3f}, af_fd[fd]: {fd:.3f}, '
193                             f'RTOL: {_POSITION_RTOL}')
194
195      logging.debug('Assert reported locs are close for min_fd captures')
196      min_loc = min([v['loc'] for v in data_min_fd.values()])
197      max_loc = max([v['loc'] for v in data_min_fd.values()])
198      if not math.isclose(min_loc, max_loc, rel_tol=_POSITION_RTOL):
199        raise AssertionError(f'min_fd[loc] min: {min_loc:.3f}, max: '
200                             f'{max_loc:.3f}, RTOL: {_POSITION_RTOL}')
201
202      logging.debug('Assert reported sharpness is close at min_fd')
203      min_sharp = min([v['sharpness'] for v in data_min_fd.values()])
204      max_sharp = max([v['sharpness'] for v in data_min_fd.values()])
205      if not math.isclose(min_sharp, max_sharp, rel_tol=_SHARPNESS_RTOL):
206        raise AssertionError(f'min_fd[sharpness] min: {min_sharp:.3f}, '
207                             f'max: {max_sharp:.3f}, RTOL: {_SHARPNESS_RTOL}')
208
209      logging.debug('Assert reported loc is close to assigned loc for min_fd')
210      last_key = max(data_min_fd.keys())  # find last (non-moving) frame
211      loc = data_min_fd[last_key]['loc']
212      fd = data_min_fd[last_key]['fd']
213      if not math.isclose(loc, fd, rel_tol=_POSITION_RTOL):
214        raise AssertionError(f'min_fd[loc]: {loc:.3f}, min_fd[fd]: {fd:.3f}, '
215                             f'RTOL: {_POSITION_RTOL}')
216
217      logging.debug('Assert AF focus distance > minimum focus distance')
218      min_fd = data_min_fd[last_key]['fd']
219      if af_fd > min_fd * (1 + _MIN_AF_FD_RTOL):
220        raise AssertionError(f'AF focus distance > min focus distance! af: '
221                             f'{af_fd}, min: {min_fd}, RTOL: {_MIN_AF_FD_RTOL}')
222
223      # Check LENS_INTRINSIC_CALIBRATION
224      if (its_session_utils.get_first_api_level(self.dut.serial) >=
225          its_session_utils.ANDROID15_API_LEVEL and
226          camera_properties_utils.intrinsic_calibration(props)):
227        logging.debug('Assert LENS_INTRINSIC_CALIBRATION changes with lens '
228                      'location on non-moving frames.')
229        last_af_frame_cal = data_af_fd[max(data_af_fd.keys())][
230            'lens_intrinsic_calibration']
231        first_min_frame_cal = data_min_fd[min(data_min_fd.keys())][
232            'lens_intrinsic_calibration']
233        logging.debug('Last AF frame cal: %s', last_af_frame_cal)
234        logging.debug('1st min_fd frame cal: %s', first_min_frame_cal)
235        if (math.isclose(first_min_frame_cal[_LENS_INTRINSIC_CAL_FX_IDX],
236                         last_af_frame_cal[_LENS_INTRINSIC_CAL_FX_IDX],
237                         rel_tol=_LENS_INTRINSIC_CAL_RTOL) and
238            math.isclose(first_min_frame_cal[_LENS_INTRINSIC_CAL_FY_IDX],
239                         last_af_frame_cal[_LENS_INTRINSIC_CAL_FY_IDX],
240                         rel_tol=_LENS_INTRINSIC_CAL_RTOL)):
241          raise AssertionError(
242              'LENS_INTRINSIC_CALIBRAION[f_x, f_y] not changing with lens '
243              f'movement! AF lens location: {last_af_frame_cal}, '
244              f'min fd lens location: {first_min_frame_cal}, '
245              f'RTOL: {_LENS_INTRINSIC_CAL_RTOL}')
246
247if __name__ == '__main__':
248  test_runner.main()
249