test_jpeg.py revision 87b68b020dd72c4cdcf3b8c1f9196c060f947991
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 os.path 19import Image 20import shutil 21import numpy 22import math 23 24def main(): 25 """Test that converted YUV images and device JPEG images look the same. 26 """ 27 NAME = os.path.basename(__file__).split(".")[0] 28 29 THRESHOLD_MAX_RMS_DIFF = 0.1 30 31 req = its.objects.capture_request( { 32 "android.control.mode": 0, 33 "android.control.aeMode": 0, 34 "android.control.awbMode": 0, 35 "android.control.afMode": 0, 36 "android.sensor.frameDuration": 0, 37 "android.sensor.sensitivity": 100, 38 "android.sensor.exposureTime": 10*1000*1000 39 }) 40 41 with its.device.ItsSession() as cam: 42 props = cam.get_camera_properties() 43 44 # YUV 45 size = props['android.scaler.availableProcessedSizes'][0] 46 req["outputSurface"] = size 47 req["outputSurface"]["format"] = "yuv" 48 fname, w, h, cap_md = cam.do_capture(req) 49 img = its.image.load_yuv420_to_rgb_image(fname, w, h) 50 its.image.write_image(img, "%s_fmt=yuv.jpg" % (NAME)) 51 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 52 rgb0 = its.image.compute_image_means(tile) 53 54 # JPEG 55 size = props['android.scaler.availableJpegSizes'][0] 56 req["outputSurface"] = size 57 req["outputSurface"]["format"] = "jpg" 58 fname, w, h, cap_md = cam.do_capture(req) 59 img = numpy.array(Image.open(fname)).reshape(w,h,3) / 255.0 60 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) 61 rgb1 = its.image.compute_image_means(tile) 62 shutil.copy2(fname, "%s_fmt=jpg.jpg" % (NAME)) 63 64 rms_diff = math.sqrt( 65 sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0) 66 print "RMS difference:", rms_diff 67 assert(rms_diff < THRESHOLD_MAX_RMS_DIFF) 68 69if __name__ == '__main__': 70 main() 71 72