133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
简化的相机性能测试
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import time
|
|
import logging
|
|
|
|
# 添加项目路径
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from devices.utils.config_manager import ConfigManager
|
|
from devices.camera_manager import CameraManager
|
|
|
|
# 设置日志级别
|
|
logging.basicConfig(
|
|
level=logging.DEBUG,
|
|
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
)
|
|
|
|
def test_camera_init_time():
|
|
"""
|
|
测试相机初始化时间
|
|
"""
|
|
print("相机初始化性能测试")
|
|
print("=" * 50)
|
|
|
|
try:
|
|
# 创建管理器
|
|
config_manager = ConfigManager()
|
|
camera_manager = CameraManager(None, config_manager)
|
|
|
|
print("\n开始相机初始化测试...")
|
|
|
|
# 记录总时间
|
|
total_start = time.time()
|
|
|
|
# 执行初始化
|
|
success = camera_manager.initialize()
|
|
|
|
total_time = (time.time() - total_start) * 1000
|
|
|
|
print(f"\n初始化结果: {'成功' if success else '失败'}")
|
|
print(f"总耗时: {total_time:.1f}ms ({total_time/1000:.1f}秒)")
|
|
|
|
# 性能评估
|
|
if total_time < 1000:
|
|
print("性能评级: 优秀 ⭐⭐⭐ (< 1秒)")
|
|
elif total_time < 3000:
|
|
print("性能评级: 良好 ⭐⭐ (< 3秒)")
|
|
elif total_time < 5000:
|
|
print("性能评级: 一般 ⭐ (< 5秒)")
|
|
else:
|
|
print("性能评级: 需要优化 ❌ (> 5秒)")
|
|
|
|
if success:
|
|
# 测试校准
|
|
print("\n测试校准性能...")
|
|
calibrate_start = time.time()
|
|
calibrate_success = camera_manager.calibrate()
|
|
calibrate_time = (time.time() - calibrate_start) * 1000
|
|
|
|
print(f"校准结果: {'成功' if calibrate_success else '失败'}")
|
|
print(f"校准耗时: {calibrate_time:.1f}ms")
|
|
|
|
# 测试首帧获取
|
|
if camera_manager.cap:
|
|
print("\n测试首帧获取...")
|
|
frame_start = time.time()
|
|
ret, frame = camera_manager.cap.read()
|
|
frame_time = (time.time() - frame_start) * 1000
|
|
|
|
if ret and frame is not None:
|
|
print(f"首帧获取成功 - 耗时: {frame_time:.1f}ms, 帧大小: {frame.shape}")
|
|
del frame
|
|
else:
|
|
print(f"首帧获取失败 - 耗时: {frame_time:.1f}ms")
|
|
|
|
# 获取设备信息
|
|
print("\n设备信息:")
|
|
device_info = camera_manager.get_device_info()
|
|
for key, value in device_info.items():
|
|
if key in ['width', 'height', 'fps', 'fourcc']:
|
|
print(f" {key}: {value}")
|
|
|
|
# 清理
|
|
camera_manager.cleanup()
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ 测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
def analyze_performance_bottlenecks():
|
|
"""
|
|
分析性能瓶颈
|
|
"""
|
|
print("\n" + "=" * 50)
|
|
print("性能瓶颈分析")
|
|
print("=" * 50)
|
|
|
|
print("\n根据测试结果,主要性能瓶颈可能包括:")
|
|
print("1. 相机打开 (CAP_PROP设置) - 通常耗时3-4秒")
|
|
print("2. 分辨率设置 - 可能耗时5-6秒")
|
|
print("3. FPS设置 - 可能耗时2-3秒")
|
|
print("4. 首帧读取 - 通常耗时300-400ms")
|
|
|
|
print("\n优化建议:")
|
|
print("• 使用更快的相机后端 (如DirectShow)")
|
|
print("• 减少不必要的属性设置")
|
|
print("• 使用较低的分辨率进行初始化")
|
|
print("• 启用OpenCV优化")
|
|
print("• 设置合适的缓冲区大小")
|
|
|
|
def main():
|
|
print("相机启动性能优化测试")
|
|
print("目标: 将启动时间从10+秒优化到3秒以内")
|
|
|
|
# 执行测试
|
|
test_camera_init_time()
|
|
|
|
# 分析结果
|
|
analyze_performance_bottlenecks()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("测试完成")
|
|
print("=" * 50)
|
|
|
|
if __name__ == '__main__':
|
|
main() |