• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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.path
16
17import its.caps
18import its.device
19import its.image
20import its.objects
21import its.target
22
23import numpy as np
24NAME = os.path.basename(__file__).split('.')[0]
25PATCH_SIZE = 0.0625  # 1/16 x 1/16 in center of image
26PATCH_LOC = (1-PATCH_SIZE)/2
27THRESH_DIFF = 0.06
28THRESH_GAIN = 0.1
29THRESH_EXP = 0.05
30
31
32def main():
33    """Test both cameras give similar RBG values for gray patch."""
34
35    yuv_sizes = {}
36    with its.device.ItsSession() as cam:
37        props = cam.get_camera_properties()
38        its.caps.skip_unless(its.caps.per_frame_control(props) and
39                             its.caps.logical_multi_camera(props))
40        ids = its.caps.logical_multi_camera_physical_ids(props)
41        for i in ids:
42            physical_props = cam.get_camera_properties_by_id(i)
43            its.caps.skip_unless(not its.caps.mono_camera(physical_props))
44            yuv_sizes[i] = its.objects.get_available_output_sizes(
45                    'yuv', physical_props)
46            if i == ids[0]:  # get_available_output_sizes returns sorted list
47                yuv_match_sizes = yuv_sizes[i]
48            else:
49                list(set(yuv_sizes[i]).intersection(yuv_match_sizes))
50
51        # find matched size for captures
52        yuv_match_sizes.sort()
53        w = yuv_match_sizes[-1][0]
54        h = yuv_match_sizes[-1][1]
55        print 'Matched YUV size: (%d, %d)' % (w, h)
56
57        # do 3a and create requests
58        avail_fls = sorted(props['android.lens.info.availableFocalLengths'],
59                           reverse=True)
60        cam.do_3a()
61        reqs = []
62        for i, fl in enumerate(avail_fls):
63            reqs.append(its.objects.auto_capture_request())
64            reqs[i]['android.lens.focalLength'] = fl
65            if i > 0:
66                # Calculate the active sensor region for a non-cropped image
67                zoom = avail_fls[0] / fl
68                a = props['android.sensor.info.activeArraySize']
69                ax, ay = a['left'], a['top']
70                aw, ah = a['right'] - a['left'], a['bottom'] - a['top']
71
72                # Calculate a center crop region.
73                assert zoom >= 1
74                cropw = aw / zoom
75                croph = ah / zoom
76                crop_region = {
77                        'left': aw / 2 - cropw / 2,
78                        'top': ah / 2 - croph / 2,
79                        'right': aw / 2 + cropw / 2,
80                        'bottom': ah / 2 + croph / 2
81                }
82                reqs[i]['android.scaler.cropRegion'] = crop_region
83
84        # capture YUVs
85        y_means = {}
86        msg = ''
87        fmt = [{'format': 'yuv', 'width': w, 'height': h}]
88        caps = cam.do_capture(reqs, fmt)
89        if not isinstance(caps, list):
90            caps = [caps]  # handle canonical case where caps is not list
91
92        for i, fl in enumerate(avail_fls):
93            img = its.image.convert_capture_to_rgb_image(caps[i], props=props)
94            its.image.write_image(img, '%s_yuv_fl=%s.jpg' % (NAME, fl))
95            y, _, _ = its.image.convert_capture_to_planes(caps[i], props=props)
96            y_mean = its.image.compute_image_means(
97                    its.image.get_image_patch(y, PATCH_LOC, PATCH_LOC,
98                                              PATCH_SIZE, PATCH_SIZE))[0]
99            print 'y[%s]: %.3f' % (fl, y_mean)
100            msg += 'y[%s]: %.3f, ' % (fl, y_mean)
101            y_means[fl] = y_mean
102
103        # compare YUVs
104        msg += 'TOL=%.5f' % THRESH_DIFF
105        assert np.isclose(max(y_means.values()), min(y_means.values()),
106                          rtol=THRESH_DIFF), msg
107
108
109if __name__ == '__main__':
110    main()
111