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    """Take long bursts of images and check that they're all identical.
27
28    Assumes a static scene. Can be used to idenfity if there are sporadic
29    frames that are processed differently or have artifacts, or if 3A isn't
30    stable, since this test converges 3A at the start but doesn't lock 3A
31    throughout capture.
32    """
33    NAME = os.path.basename(__file__).split(".")[0]
34
35    BURST_LEN = 6
36    BURSTS = 2
37    FRAMES = BURST_LEN * BURSTS
38
39    DELTA_THRESH = 0.1
40
41    with its.device.ItsSession() as cam:
42
43        # Capture at full resolution.
44        props = cam.get_camera_properties()
45        its.caps.skip_unless(its.caps.manual_sensor(props) and
46                             its.caps.awb_lock(props))
47        w,h = its.objects.get_available_output_sizes("yuv", props)[0]
48
49        # Converge 3A prior to capture.
50        cam.do_3a(lock_ae=True, lock_awb=True)
51
52        # After 3A has converged, lock AE+AWB for the duration of the test.
53        req = its.objects.fastest_auto_capture_request(props)
54        req["android.blackLevel.lock"] = True
55        req["android.control.awbLock"] = True
56        req["android.control.aeLock"] = True
57
58        # Capture bursts of YUV shots.
59        # Build a 4D array, which is an array of all RGB images after down-
60        # scaling them by a factor of 4x4.
61        imgs = numpy.empty([FRAMES,h/4,w/4,3])
62        for j in range(BURSTS):
63            caps = cam.do_capture([req]*BURST_LEN)
64            for i,cap in enumerate(caps):
65                n = j*BURST_LEN + i
66                imgs[n] = its.image.downscale_image(
67                        its.image.convert_capture_to_rgb_image(cap), 4)
68
69        # Dump all images.
70        print "Dumping images"
71        for i in range(FRAMES):
72            its.image.write_image(imgs[i], "%s_frame%03d.jpg"%(NAME,i))
73
74        # The mean image.
75        img_mean = imgs.mean(0)
76        its.image.write_image(img_mean, "%s_mean.jpg"%(NAME))
77
78        # Compute the deltas of each image from the mean image; this test
79        # passes if none of the deltas are large.
80        print "Computing frame differences"
81        delta_maxes = []
82        for i in range(FRAMES):
83            deltas = (imgs[i] - img_mean).reshape(h*w*3/16)
84            delta_max_pos = numpy.max(deltas)
85            delta_max_neg = numpy.min(deltas)
86            delta_maxes.append(max(abs(delta_max_pos), abs(delta_max_neg)))
87        max_delta_max = max(delta_maxes)
88        print "Frame %d has largest diff %f" % (
89                delta_maxes.index(max_delta_max), max_delta_max)
90        assert(max_delta_max < DELTA_THRESH)
91
92if __name__ == '__main__':
93    main()
94
95