1# Copyright 2023 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"""Verifies advertised FPS from device as webcam.""" 15 16import logging 17import time 18 19import cv2 20import device 21 22_FPS_TEST_DURATION = 10 # seconds 23_DEVICE_NAME = 'android' # TODO b/277159494 24_FPS_TO_TEST = [15, 30, 60] # Since device or cv2 do not retrieve the 25 # advertised FPS, the test will use these 26 # values which are known to be supported 27 28 29def initialize_device(): 30 """Gets the device index of the webcam to be tested. 31 32 Returns: 33 Device index of webcam 34 """ 35 res_device_index = -1 36 device_name = '' 37 38 device_list = device.getDeviceList() 39 for index, camera in enumerate(device_list): 40 if _DEVICE_NAME in camera[0].lower(): 41 res_device_index = index 42 device_name = camera[0] 43 break 44 45 if res_device_index < 0: 46 logging.error('No device found') 47 else: 48 logging.info('Using webcam: %s', device_name) 49 50 return res_device_index 51 52 53def initialize_resolutions(device_index): 54 """Gets list of supported resolutions from webcam. 55 56 Args: 57 device_index: index of device 58 59 Returns: 60 List of tuples of supported resolutions 61 """ 62 device_list = device.getDeviceList() 63 64 res = [] 65 if device_list: 66 res = device_list[device_index][1] 67 68 return res 69 70 71def setup_for_test_fps(dut, supported_resolutions): 72 """Sets up and runs fps testing on device. 73 74 Args: 75 dut: device under test 76 supported_resolutions: supported resolutions on device to be tested 77 78 Returns: 79 List of tuples of the fps results, where the first element is the 80 expected and the second element is the actual fps from testing 81 """ 82 results = [] 83 84 for current_resolution in supported_resolutions: 85 dut.set(cv2.CAP_PROP_FRAME_WIDTH, current_resolution[0]) 86 dut.set(cv2.CAP_PROP_FRAME_HEIGHT, current_resolution[1]) 87 88 for current_fps in _FPS_TO_TEST: 89 dut.set(cv2.CAP_PROP_FPS, current_fps) 90 91 results.append((current_fps, test_fps(dut))) 92 93 return results 94 95 96def test_fps(dut): 97 """Tests fps on device. 98 99 Args: 100 dut: device under test 101 102 Returns: 103 fps calculated from test 104 """ 105 num_frames = dut.get(cv2.CAP_PROP_FPS) * _FPS_TEST_DURATION 106 107 start_time = time.time() 108 i = num_frames 109 while i > 0: 110 ret = dut.read() 111 112 if not ret: 113 logging.error('Error while reading frame') 114 break 115 116 i -= 1 117 118 end_time = time.time() 119 120 fps = num_frames / (end_time - start_time) 121 122 return fps 123 124 125def main(): 126 device_index = initialize_device() 127 128 if device_index < 0: 129 logging.info('Supported device not found!') 130 return [] 131 132 supported_resolutions = initialize_resolutions(device_index) 133 134 if not supported_resolutions: 135 logging.error('Error retrieving formats and resolutions') 136 return [] 137 138 dut = cv2.VideoCapture(device_index, cv2.CAP_DSHOW) 139 140 res = setup_for_test_fps(dut, supported_resolutions) 141 142 dut.release() 143 print(res) 144 return res 145 146 147if __name__ == '__main__': 148 main() 149