Home | History | Annotate | Download | only in scene2
      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 
     15 import its.image
     16 import its.caps
     17 import its.device
     18 import its.objects
     19 import its.error
     20 import its.target
     21 import sys
     22 import os
     23 
     24 NAME = os.path.basename(__file__).split(".")[0]
     25 STOP_AT_FIRST_FAILURE = False  # change to True to have test break @ 1st FAIL
     26 
     27 
     28 def main():
     29     """Test different combinations of output formats.
     30     
     31     Note the test does not require a specific target but does perform
     32     both automatic and manual captures so it requires a fixed scene
     33     where 3A can converge.
     34     """
     35 
     36     with its.device.ItsSession() as cam:
     37 
     38         props = cam.get_camera_properties()
     39         its.caps.skip_unless(its.caps.compute_target_exposure(props) and
     40                              its.caps.raw16(props))
     41 
     42         successes = []
     43         failures = []
     44         debug = its.caps.debug_mode()
     45 
     46         # Two different requests: auto, and manual.
     47         e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
     48         req_aut = its.objects.auto_capture_request()
     49         req_man = its.objects.manual_capture_request(s, e)
     50         reqs = [req_aut,  # R0
     51                 req_man]  # R1
     52 
     53         # 10 different combos of output formats; some are single surfaces, and
     54         # some are multiple surfaces.
     55         wyuv, hyuv = its.objects.get_available_output_sizes("yuv", props)[-1]
     56         wjpg, hjpg = its.objects.get_available_output_sizes("jpg", props)[-1]
     57         fmt_yuv_prev = {"format": "yuv", "width": wyuv, "height": hyuv}
     58         fmt_yuv_full = {"format": "yuv"}
     59         fmt_jpg_prev = {"format": "jpeg", "width": wjpg, "height": hjpg}
     60         fmt_jpg_full = {"format": "jpeg"}
     61         fmt_raw_full = {"format": "raw"}
     62         fmt_combos = [
     63             [fmt_yuv_prev],                              # F0
     64             [fmt_yuv_full],                              # F1
     65             [fmt_jpg_prev],                              # F2
     66             [fmt_jpg_full],                              # F3
     67             [fmt_raw_full],                              # F4
     68             [fmt_yuv_prev, fmt_jpg_prev],                # F5
     69             [fmt_yuv_prev, fmt_jpg_full],                # F6
     70             [fmt_yuv_prev, fmt_raw_full],                # F7
     71             [fmt_yuv_prev, fmt_jpg_prev, fmt_raw_full],  # F8
     72             [fmt_yuv_prev, fmt_jpg_full, fmt_raw_full]]  # F9
     73 
     74         if its.caps.y8(props):
     75             wy8, hy8 = its.objects.get_available_output_sizes("y8", props)[-1]
     76             fmt_y8_prev = {"format": "y8", "width": wy8, "height": hy8}
     77             fmt_y8_full = {"format": "y8"}
     78             fmt_combos.append([fmt_y8_prev])
     79             fmt_combos.append([fmt_y8_full])
     80 
     81         # Two different burst lengths: single frame, and 3 frames.
     82         burst_lens = [1,  # B0
     83                       3]  # B1
     84 
     85         # There are 2xlen(fmt_combos)x2 different combinations. Run through them all.
     86         n = 0
     87         for r,req in enumerate(reqs):
     88             for f,fmt_combo in enumerate(fmt_combos):
     89                 for b,burst_len in enumerate(burst_lens):
     90                     try:
     91                         caps = cam.do_capture([req]*burst_len, fmt_combo)
     92                         successes.append((n,r,f,b))
     93                         print "==> Success[%02d]: R%d F%d B%d" % (n,r,f,b)
     94 
     95                         # Dump the captures out to jpegs in debug mode.
     96                         if debug:
     97                             if not isinstance(caps, list):
     98                                 caps = [caps]
     99                             elif isinstance(caps[0], list):
    100                                 caps = sum(caps, [])
    101                             for c, cap in enumerate(caps):
    102                                 img = its.image.convert_capture_to_rgb_image(cap, props=props)
    103                                 its.image.write_image(img,
    104                                     "%s_n%02d_r%d_f%d_b%d_c%d.jpg"%(NAME,n,r,f,b,c))
    105 
    106                     except Exception as e:
    107                         print e
    108                         print "==> Failure[%02d]: R%d F%d B%d" % (n,r,f,b)
    109                         failures.append((n,r,f,b))
    110                         if STOP_AT_FIRST_FAILURE:
    111                             sys.exit(1)
    112                     n += 1
    113 
    114         num_fail = len(failures)
    115         num_success = len(successes)
    116         num_total = len(reqs)*len(fmt_combos)*len(burst_lens)
    117         num_not_run = num_total - num_success - num_fail
    118 
    119         print "\nFailures (%d / %d):" % (num_fail, num_total)
    120         for (n,r,f,b) in failures:
    121             print "  %02d: R%d F%d B%d" % (n,r,f,b)
    122         print "\nSuccesses (%d / %d):" % (num_success, num_total)
    123         for (n,r,f,b) in successes:
    124             print "  %02d: R%d F%d B%d" % (n,r,f,b)
    125         if num_not_run > 0:
    126             print "\nNumber of tests not run: %d / %d" % (num_not_run, num_total)
    127         print ""
    128 
    129         # The test passes if all the combinations successfully capture.
    130         assert num_fail == 0
    131         assert num_success == num_total
    132 
    133 if __name__ == '__main__':
    134     main()
    135 
    136