1# Copyright 2014 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 its.image
16import its.device
17import its.objects
18import its.caps
19import os.path
20import numpy
21from matplotlib import pylab
22import matplotlib
23import matplotlib.pyplot
24
25def main():
26    """Test 3A lock + YUV burst (using auto settings).
27
28    This is a test that is designed to pass even on limited devices that
29    don't have MANUAL_SENSOR or PER_FRAME_CONTROLS. The test checks
30    YUV image consistency while the frame rate check is in CTS.
31    """
32    NAME = os.path.basename(__file__).split(".")[0]
33
34    BURST_LEN = 8
35    SPREAD_THRESH_MANUAL_SENSOR = 0.01
36    SPREAD_THRESH = 0.03
37    FPS_MAX_DIFF = 2.0
38
39    with its.device.ItsSession() as cam:
40        props = cam.get_camera_properties()
41        its.caps.skip_unless(its.caps.ae_lock(props) and
42                             its.caps.awb_lock(props))
43
44        # Converge 3A prior to capture.
45        cam.do_3a(do_af=True, lock_ae=True, lock_awb=True)
46
47        fmt = its.objects.get_largest_yuv_format(props)
48
49        # After 3A has converged, lock AE+AWB for the duration of the test.
50        req = its.objects.fastest_auto_capture_request(props)
51        req["android.control.awbLock"] = True
52        req["android.control.aeLock"] = True
53
54        # Capture bursts of YUV shots.
55        # Get the mean values of a center patch for each.
56        r_means = []
57        g_means = []
58        b_means = []
59        caps = cam.do_capture([req]*BURST_LEN, fmt)
60        for i,cap in enumerate(caps):
61            img = its.image.convert_capture_to_rgb_image(cap)
62            its.image.write_image(img, "%s_frame%d.jpg"%(NAME,i))
63            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
64            means = its.image.compute_image_means(tile)
65            r_means.append(means[0])
66            g_means.append(means[1])
67            b_means.append(means[2])
68
69        # Pass/fail based on center patch similarity.
70        for means in [r_means, g_means, b_means]:
71            spread = max(means) - min(means)
72            print "Patch mean spread", spread, \
73                    " (min/max: ",  min(means), "/", max(means), ")"
74            threshold = SPREAD_THRESH_MANUAL_SENSOR \
75                    if its.caps.manual_sensor(props) else SPREAD_THRESH
76            assert(spread < threshold)
77
78if __name__ == '__main__':
79    main()
80
81