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