Home | History | Annotate | Download | only in tests
      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 
     15 import its.image
     16 import its.device
     17 import its.objects
     18 import sys
     19 import numpy
     20 import Image
     21 import pprint
     22 import math
     23 import pylab
     24 import os.path
     25 import matplotlib
     26 import matplotlib.pyplot
     27 
     28 def main():
     29     """Test that the device will produce full black+white images.
     30     """
     31     NAME = os.path.basename(__file__).split(".")[0]
     32 
     33     r_means = []
     34     g_means = []
     35     b_means = []
     36 
     37     with its.device.ItsSession() as cam:
     38         # Take a shot with very low ISO and exposure time. Expect it to
     39         # be black.
     40         req = its.objects.manual_capture_request(100, 0.1)
     41         fname, w, h, cap_md = cam.do_capture(req)
     42         img = its.image.load_yuv420_to_rgb_image(fname, w, h)
     43         its.image.write_image(img, "%s_black.jpg" % (NAME))
     44         tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     45         black_means = its.image.compute_image_means(tile)
     46         r_means.append(black_means[0])
     47         g_means.append(black_means[1])
     48         b_means.append(black_means[2])
     49         print "Dark pixel means:", black_means
     50 
     51         # Take a shot with very high ISO and exposure time. Expect it to
     52         # be black.
     53         req = its.objects.manual_capture_request(10000, 1000)
     54         fname, w, h, cap_md = cam.do_capture(req)
     55         img = its.image.load_yuv420_to_rgb_image(fname, w, h)
     56         its.image.write_image(img, "%s_white.jpg" % (NAME))
     57         tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     58         white_means = its.image.compute_image_means(tile)
     59         r_means.append(white_means[0])
     60         g_means.append(white_means[1])
     61         b_means.append(white_means[2])
     62         print "Bright pixel means:", white_means
     63 
     64         # Draw a plot.
     65         pylab.plot([0,1], r_means, 'r')
     66         pylab.plot([0,1], g_means, 'g')
     67         pylab.plot([0,1], b_means, 'b')
     68         pylab.ylim([0,1])
     69         matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
     70 
     71         for val in black_means:
     72             assert(val < 0.025)
     73         for val in white_means:
     74             assert(val > 0.975)
     75 
     76 if __name__ == '__main__':
     77     main()
     78 
     79