blob: 5aa8620ebb83875e69620604980be264c16e652a [file] [log] [blame]
Clemenz Portmannac8a0852017-11-30 16:54:28 -08001# 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]
Clemenz Portmann81568142018-05-16 13:12:50 -070025PATCH_SIZE = 0.0625 # 1/16 x 1/16 in center of image
26PATCH_LOC = (1-PATCH_SIZE)/2
27THRESH_DIFF = 0.06
Clemenz Portmannac8a0852017-11-30 16:54:28 -080028THRESH_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()
Clemenz Portmann9d1f0d32018-09-25 17:50:23 -070038 its.caps.skip_unless(its.caps.per_frame_control(props) and
Clemenz Portmann9d1f0d32018-09-25 17:50:23 -070039 its.caps.logical_multi_camera(props))
Clemenz Portmannac8a0852017-11-30 16:54:28 -080040 ids = its.caps.logical_multi_camera_physical_ids(props)
Clemenz Portmannac8a0852017-11-30 16:54:28 -080041 for i in ids:
42 physical_props = cam.get_camera_properties_by_id(i)
Clemenz Portmann32754b72018-07-10 11:47:01 -070043 its.caps.skip_unless(not its.caps.mono_camera(physical_props))
Takumi Yoshida83a9e732020-04-17 20:27:48 +090044 its.caps.skip_unless(its.caps.backward_compatible(physical_props))
Clemenz Portmannac8a0852017-11-30 16:54:28 -080045 yuv_sizes[i] = its.objects.get_available_output_sizes(
Clemenz Portmann642ed5f2019-03-13 17:13:09 -070046 'yuv', physical_props)
47 if i == ids[0]: # get_available_output_sizes returns sorted list
Clemenz Portmannac8a0852017-11-30 16:54:28 -080048 yuv_match_sizes = yuv_sizes[i]
49 else:
50 list(set(yuv_sizes[i]).intersection(yuv_match_sizes))
Clemenz Portmann32754b72018-07-10 11:47:01 -070051
52 # find matched size for captures
Clemenz Portmannac8a0852017-11-30 16:54:28 -080053 yuv_match_sizes.sort()
54 w = yuv_match_sizes[-1][0]
55 h = yuv_match_sizes[-1][1]
Clemenz Portmann32754b72018-07-10 11:47:01 -070056 print 'Matched YUV size: (%d, %d)' % (w, h)
57
58 # do 3a and create requests
Clemenz Portmannee5058b2019-03-27 16:47:26 -070059 avail_fls = sorted(props['android.lens.info.availableFocalLengths'],
60 reverse=True)
Clemenz Portmann32754b72018-07-10 11:47:01 -070061 cam.do_3a()
62 reqs = []
63 for i, fl in enumerate(avail_fls):
64 reqs.append(its.objects.auto_capture_request())
65 reqs[i]['android.lens.focalLength'] = fl
Clemenz Portmannee5058b2019-03-27 16:47:26 -070066 if i > 0:
67 # Calculate the active sensor region for a non-cropped image
68 zoom = avail_fls[0] / fl
69 a = props['android.sensor.info.activeArraySize']
70 ax, ay = a['left'], a['top']
71 aw, ah = a['right'] - a['left'], a['bottom'] - a['top']
72
73 # Calculate a center crop region.
74 assert zoom >= 1
75 cropw = aw / zoom
76 croph = ah / zoom
77 crop_region = {
78 'left': aw / 2 - cropw / 2,
79 'top': ah / 2 - croph / 2,
80 'right': aw / 2 + cropw / 2,
81 'bottom': ah / 2 + croph / 2
82 }
83 reqs[i]['android.scaler.cropRegion'] = crop_region
Clemenz Portmannac8a0852017-11-30 16:54:28 -080084
85 # capture YUVs
Clemenz Portmann32754b72018-07-10 11:47:01 -070086 y_means = {}
87 msg = ''
88 fmt = [{'format': 'yuv', 'width': w, 'height': h}]
89 caps = cam.do_capture(reqs, fmt)
Clemenz Portmann7b8ab0b2019-03-27 14:11:16 -070090 if not isinstance(caps, list):
91 caps = [caps] # handle canonical case where caps is not list
Clemenz Portmannac8a0852017-11-30 16:54:28 -080092
Clemenz Portmann32754b72018-07-10 11:47:01 -070093 for i, fl in enumerate(avail_fls):
94 img = its.image.convert_capture_to_rgb_image(caps[i], props=props)
95 its.image.write_image(img, '%s_yuv_fl=%s.jpg' % (NAME, fl))
96 y, _, _ = its.image.convert_capture_to_planes(caps[i], props=props)
97 y_mean = its.image.compute_image_means(
98 its.image.get_image_patch(y, PATCH_LOC, PATCH_LOC,
99 PATCH_SIZE, PATCH_SIZE))[0]
100 print 'y[%s]: %.3f' % (fl, y_mean)
101 msg += 'y[%s]: %.3f, ' % (fl, y_mean)
102 y_means[fl] = y_mean
Clemenz Portmannac8a0852017-11-30 16:54:28 -0800103
Clemenz Portmannac8a0852017-11-30 16:54:28 -0800104 # compare YUVs
Clemenz Portmann32754b72018-07-10 11:47:01 -0700105 msg += 'TOL=%.5f' % THRESH_DIFF
106 assert np.isclose(max(y_means.values()), min(y_means.values()),
107 rtol=THRESH_DIFF), msg
Clemenz Portmannac8a0852017-11-30 16:54:28 -0800108
109
110if __name__ == '__main__':
111 main()