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