test_param_sensitivity.py revision 67b689684c4600930ccfa9cc7cec8d7a75b6ed7a
1# Copyright 2013 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 pylab
19import os.path
20import matplotlib
21import matplotlib.pyplot
22
23def main():
24    """Test that the android.sensor.sensitivity parameter is applied.
25    """
26    NAME = os.path.basename(__file__).split(".")[0]
27
28    # Pass/fail thresholds.
29    THRESHOLD_MAX_MIN_DIFF = 0.3
30    THRESHOLD_MAX_MIN_RATIO = 2.0
31
32    NUM_STEPS = 5
33
34    req = its.objects.capture_request( {
35        "android.control.mode": 0,
36        "android.control.aeMode": 0,
37        "android.control.awbMode": 0,
38        "android.control.afMode": 0,
39        "android.sensor.frameDuration": 0,
40        "android.sensor.exposureTime": 2*1000*1000
41        })
42
43    sensitivities = None
44    r_means = []
45    g_means = []
46    b_means = []
47
48    with its.device.ItsSession() as cam:
49        props = cam.get_camera_properties()
50        sens_range = props['android.sensor.info.sensitivityRange']
51        sensitivities = range(sens_range[0],
52                              sens_range[1]+1,
53                              int((sens_range[1] - sens_range[0]) / NUM_STEPS))
54        for s in sensitivities:
55            req["captureRequest"]["android.sensor.sensitivity"] = s
56            fname, w, h, cap_md = cam.do_capture(req)
57            img = its.image.load_yuv420_to_rgb_image(fname, w, h)
58            its.image.write_image(
59                    img, "%s_iso=%04d.jpg" % (NAME, s))
60            tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
61            rgb_means = its.image.compute_image_means(tile)
62            r_means.append(rgb_means[0])
63            g_means.append(rgb_means[1])
64            b_means.append(rgb_means[2])
65
66    # Draw a plot.
67    pylab.plot(sensitivities, r_means, 'r')
68    pylab.plot(sensitivities, g_means, 'g')
69    pylab.plot(sensitivities, b_means, 'b')
70    pylab.ylim([0,1])
71    matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
72
73    # Test for pass/fail: just check that that images get brighter by an amount
74    # that is more than could be expected by random noise. Don't assume the
75    # curve has any specific shape or gradient. This test is just checking that
76    # the sensitivity parameter actually does something. Note the intensity
77    # may be clamped to 0 or 1 for part of the ramp, so only test that the
78    # brightness difference between the first and last samples are above a
79    # given threshold.
80    for means in [r_means, g_means, b_means]:
81        for i in range(len(means)-1):
82            assert(means[i] <= means[i+1])
83        assert(means[-1] - means[0] > THRESHOLD_MAX_MIN_DIFF)
84        assert(means[-1] / means[0] > THRESHOLD_MAX_MIN_RATIO)
85
86if __name__ == '__main__':
87    main()
88
89