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 os.path
16import its.caps
17import its.device
18import its.image
19import its.objects
20import its.target
21
22from matplotlib import pylab
23import matplotlib.pyplot
24import numpy
25
26BURST_LEN = 50
27BURSTS = 5
28COLORS = ["R", "G", "B"]
29FRAMES = BURST_LEN * BURSTS
30NAME = os.path.basename(__file__).split(".")[0]
31SPREAD_THRESH = 0.03
32
33
34def main():
35    """Take long bursts of images and check that they're all identical.
36
37    Assumes a static scene. Can be used to idenfity if there are sporadic
38    frames that are processed differently or have artifacts. Uses manual
39    capture settings.
40    """
41
42    with its.device.ItsSession() as cam:
43
44        # Capture at the smallest resolution.
45        props = cam.get_camera_properties()
46        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
47                             its.caps.per_frame_control(props))
48        debug = its.caps.debug_mode()
49
50        _, fmt = its.objects.get_fastest_manual_capture_settings(props)
51        e, s = its.target.get_target_exposure_combos(cam)["minSensitivity"]
52        req = its.objects.manual_capture_request(s, e)
53        w, h = fmt["width"], fmt["height"]
54
55        # Capture bursts of YUV shots.
56        # Get the mean values of a center patch for each.
57        # Also build a 4D array, which is an array of all RGB images.
58        r_means = []
59        g_means = []
60        b_means = []
61        imgs = numpy.empty([FRAMES, h, w, 3])
62        for j in range(BURSTS):
63            caps = cam.do_capture([req]*BURST_LEN, [fmt])
64            for i, cap in enumerate(caps):
65                n = j*BURST_LEN + i
66                imgs[n] = its.image.convert_capture_to_rgb_image(cap)
67                tile = its.image.get_image_patch(imgs[n], 0.45, 0.45, 0.1, 0.1)
68                means = its.image.compute_image_means(tile)
69                r_means.append(means[0])
70                g_means.append(means[1])
71                b_means.append(means[2])
72
73        # Dump all images if debug
74        if debug:
75            print "Dumping images"
76            for i in range(FRAMES):
77                its.image.write_image(imgs[i], "%s_frame%03d.jpg"%(NAME, i))
78
79        # The mean image.
80        img_mean = imgs.mean(0)
81        its.image.write_image(img_mean, "%s_mean.jpg"%(NAME))
82
83        # Plot means vs frames
84        frames = range(FRAMES)
85        pylab.title(NAME)
86        pylab.plot(frames, r_means, "-ro")
87        pylab.plot(frames, g_means, "-go")
88        pylab.plot(frames, b_means, "-bo")
89        pylab.ylim([0, 1])
90        pylab.xlabel("frame number")
91        pylab.ylabel("RGB avg [0, 1]")
92        matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
93
94        # PASS/FAIL based on center patch similarity.
95        for plane, means in enumerate([r_means, g_means, b_means]):
96            spread = max(means) - min(means)
97            msg = "%s spread: %.5f, SPREAD_THRESH: %.3f" % (
98                    COLORS[plane], spread, SPREAD_THRESH)
99            print msg
100            assert spread < SPREAD_THRESH, msg
101
102if __name__ == "__main__":
103    main()
104
105