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
15import os
16
17import its.caps
18import its.cv2image
19import its.device
20import its.image
21import its.objects
22import numpy as np
23
24NUM_TRYS = 2
25NUM_STEPS = 6
26SHARPNESS_TOL = 0.1
27POSITION_TOL = 0.1
28FRAME_TIME_TOL = 10  # ms
29VGA_WIDTH = 640
30VGA_HEIGHT = 480
31NAME = os.path.basename(__file__).split('.')[0]
32CHART_FILE = os.path.join(os.environ['CAMERA_ITS_TOP'], 'pymodules', 'its',
33                          'test_images', 'ISO12233.png')
34CHART_HEIGHT = 13.5  # cm
35CHART_DISTANCE = 30.0  # cm
36CHART_SCALE_START = 0.65
37CHART_SCALE_STOP = 1.35
38CHART_SCALE_STEP = 0.025
39
40
41def test_lens_position(cam, props, fmt, sensitivity, exp, chart):
42    """Return fd, sharpness, lens state of the output images.
43
44    Args:
45        cam: An open device session.
46        props: Properties of cam
47        fmt: dict; capture format
48        sensitivity: Sensitivity for the 3A request as defined in
49            android.sensor.sensitivity
50        exp: Exposure time for the 3A request as defined in
51            android.sensor.exposureTime
52        chart: Object with chart properties
53
54    Returns:
55        Dictionary of results for different focal distance captures
56        with static lens positions and moving lens positions
57        d_static, d_moving
58    """
59
60    # initialize variables and take data sets
61    data_static = {}
62    data_moving = {}
63    white_level = int(props['android.sensor.info.whiteLevel'])
64    min_fd = props['android.lens.info.minimumFocusDistance']
65    hyperfocal = props['android.lens.info.hyperfocalDistance']
66    fds_f = np.arange(hyperfocal, min_fd, (min_fd-hyperfocal)/(NUM_STEPS-1))
67    fds_f = np.append(fds_f, min_fd)
68    fds_f = fds_f.tolist()
69    fds_b = list(reversed(fds_f))
70    fds_fb = list(fds_f)
71    fds_fb.extend(fds_b)  # forward and back
72    # take static data set
73    for i, fd in enumerate(fds_fb):
74        req = its.objects.manual_capture_request(sensitivity, exp)
75        req['android.lens.focusDistance'] = fd
76        cap = its.image.stationary_lens_cap(cam, req, fmt)
77        data = {'fd': fds_fb[i]}
78        data['loc'] = cap['metadata']['android.lens.focusDistance']
79        print ' focus distance (diopters): %.3f' % data['fd']
80        print ' current lens location (diopters): %.3f' % data['loc']
81        y, _, _ = its.image.convert_capture_to_planes(cap, props)
82        chart.img = its.image.normalize_img(its.image.get_image_patch(
83                y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm))
84        its.image.write_image(chart.img, '%s_stat_i=%d_chart.jpg' % (NAME, i))
85        data['sharpness'] = white_level*its.image.compute_image_sharpness(
86                chart.img)
87        print 'Chart sharpness: %.1f\n' % data['sharpness']
88        data_static[i] = data
89    # take moving data set
90    reqs = []
91    for i, fd in enumerate(fds_f):
92        reqs.append(its.objects.manual_capture_request(sensitivity, exp))
93        reqs[i]['android.lens.focusDistance'] = fd
94    caps = cam.do_capture(reqs, fmt)
95    for i, cap in enumerate(caps):
96        data = {'fd': fds_f[i]}
97        data['loc'] = cap['metadata']['android.lens.focusDistance']
98        data['lens_moving'] = (cap['metadata']['android.lens.state']
99                               == 1)
100        timestamp = cap['metadata']['android.sensor.timestamp'] * 1E-6
101        if i == 0:
102            timestamp_init = timestamp
103        timestamp -= timestamp_init
104        data['timestamp'] = timestamp
105        print ' focus distance (diopters): %.3f' % data['fd']
106        print ' current lens location (diopters): %.3f' % data['loc']
107        y, _, _ = its.image.convert_capture_to_planes(cap, props)
108        y = its.image.rotate_img_per_argv(y)
109        chart.img = its.image.normalize_img(its.image.get_image_patch(
110                y, chart.xnorm, chart.ynorm, chart.wnorm, chart.hnorm))
111        its.image.write_image(chart.img, '%s_move_i=%d_chart.jpg' % (NAME, i))
112        data['sharpness'] = white_level*its.image.compute_image_sharpness(
113                chart.img)
114        print 'Chart sharpness: %.1f\n' % data['sharpness']
115        data_moving[i] = data
116    return data_static, data_moving
117
118
119def main():
120    """Test if focus position is properly reported for moving lenses."""
121    print '\nStarting test_lens_position.py'
122    # check skip conditions
123    with its.device.ItsSession() as cam:
124        props = cam.get_camera_properties()
125        its.caps.skip_unless(not its.caps.fixed_focus(props))
126        its.caps.skip_unless(its.caps.read_3a(props) and
127                             its.caps.lens_calibrated(props))
128    # initialize chart class
129    chart = its.cv2image.Chart(CHART_FILE, CHART_HEIGHT, CHART_DISTANCE,
130                               CHART_SCALE_START, CHART_SCALE_STOP,
131                               CHART_SCALE_STEP)
132
133    with its.device.ItsSession() as cam:
134        mono_camera = its.caps.mono_camera(props)
135        fmt = {'format': 'yuv', 'width': VGA_WIDTH, 'height': VGA_HEIGHT}
136
137        # Get proper sensitivity and exposure time with 3A
138        s, e, _, _, _ = cam.do_3a(get_results=True, mono_camera=mono_camera)
139
140        # Get sharpness for each focal distance
141        d_stat, d_move = test_lens_position(cam, props, fmt, s, e, chart)
142        print 'Lens stationary'
143        for k in sorted(d_stat):
144            print ('i: %d\tfd: %.3f\tlens location (diopters): %.3f \t'
145                   'sharpness: %.1f' % (k, d_stat[k]['fd'],
146                                        d_stat[k]['loc'],
147                                        d_stat[k]['sharpness']))
148        print 'Lens moving'
149        for k in sorted(d_move):
150            print ('i: %d\tfd: %.3f\tlens location (diopters): %.3f \t'
151                   'sharpness: %.1f  \tlens_moving: %r \t'
152                   'timestamp: %.1fms' % (k, d_move[k]['fd'],
153                                          d_move[k]['loc'],
154                                          d_move[k]['sharpness'],
155                                          d_move[k]['lens_moving'],
156                                          d_move[k]['timestamp']))
157
158        # assert static reported location/sharpness is close
159        print 'Asserting static lens locations/sharpness are similar'
160        for i in range(len(d_stat)/2):
161            j = 2 * NUM_STEPS - 1 - i
162            rw_msg = 'fd_write: %.3f, fd_read: %.3f, RTOL: %.2f' % (
163                    d_stat[i]['fd'], d_stat[i]['loc'], POSITION_TOL)
164            fr_msg = 'loc_fwd: %.3f, loc_rev: %.3f, RTOL: %.2f' % (
165                    d_stat[i]['loc'], d_stat[j]['loc'], POSITION_TOL)
166            s_msg = 'sharpness_fwd: %.3f, sharpness_rev: %.3f, RTOL: %.2f' % (
167                    d_stat[i]['sharpness'], d_stat[j]['sharpness'],
168                    SHARPNESS_TOL)
169            assert np.isclose(d_stat[i]['loc'], d_stat[i]['fd'],
170                              rtol=POSITION_TOL), rw_msg
171            assert np.isclose(d_stat[i]['loc'], d_stat[j]['loc'],
172                              rtol=POSITION_TOL), fr_msg
173            assert np.isclose(d_stat[i]['sharpness'], d_stat[j]['sharpness'],
174                              rtol=SHARPNESS_TOL), s_msg
175        # assert moving frames approximately consecutive with even distribution
176        print 'Asserting moving frames are consecutive'
177        times = [v['timestamp'] for v in d_move.itervalues()]
178        diffs = np.gradient(times)
179        assert np.isclose(np.amin(diffs), np.amax(diffs), atol=FRAME_TIME_TOL)
180        # assert reported location/sharpness is correct in moving frames
181        print 'Asserting moving lens locations/sharpness are similar'
182        for i in range(len(d_move)):
183            m_msg = 'static: %.3f, moving: %.3f, RTOL: %.2f' % (
184                    d_stat[i]['loc'], d_move[i]['loc'], POSITION_TOL)
185            assert np.isclose(d_stat[i]['loc'], d_move[i]['loc'],
186                              rtol=POSITION_TOL), m_msg
187            if d_move[i]['lens_moving'] and i > 0:
188                if d_stat[i]['sharpness'] > d_stat[i-1]['sharpness']:
189                    assert (d_stat[i]['sharpness']*(1.0+SHARPNESS_TOL) >
190                            d_move[i]['sharpness'] >
191                            d_stat[i-1]['sharpness']*(1.0-SHARPNESS_TOL))
192                else:
193                    assert (d_stat[i-1]['sharpness']*(1.0+SHARPNESS_TOL) >
194                            d_move[i]['sharpness'] >
195                            d_stat[i]['sharpness']*(1.0-SHARPNESS_TOL))
196            elif not d_move[i]['lens_moving']:
197                assert np.isclose(
198                        d_stat[i]['sharpness'], d_move[i]['sharpness'],
199                        rtol=SHARPNESS_TOL)
200            else:
201                raise its.error.Error('Lens is moving at frame 0!')
202
203if __name__ == '__main__':
204    main()
205
206