from utils.get_image import get_image
import cv2
import time

# 创建摄像头实例
camera = get_image

# 添加延迟确保摄像头初始化完成
time.sleep(1.0)

frame_count = 0
start_time = time.time()

while True:
    # 获取帧数据
    frame_data = camera.get_frame()

    # 检查是否成功获取帧
    if frame_data is None:
        print("⚠️ 无法从摄像头获取帧，重试中...")
        time.sleep(0.1)  # 短暂延迟后重试
        continue

    # 提取OpenCV格式的图像
    image_rgb = frame_data[0]

    # 转换为BGR格式（OpenCV默认格式）
    image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)

    # 保存图像
    cv2.imwrite('test.jpg', image_bgr)

    # 显示帧率信息
    frame_count += 1
    elapsed = time.time() - start_time
    if elapsed > 1.0:  # 每秒更新一次帧率
        fps = frame_count / elapsed
        print(f"帧率: {fps:.1f} FPS")
        frame_count = 0
        start_time = time.time()

    # 添加短暂延迟防止CPU过载
    time.sleep(0.01)