import cv2

# 打开摄像头
cap = cv2.VideoCapture(0)

# 设置摄像头的分辨率
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

# 初始化计时器
fps = 0
start_time = cv2.getTickCount()

while True:
    # 读取一帧图像
    ret, frame = cap.read()

    if ret:
        # 显示图像
        cv2.imshow('frame', frame)

        # 计算每秒的帧数
        fps += 1
        if cv2.getTickCount() - start_time >= cv2.getTickFrequency():
            print("FPS: ", fps)
            fps = 0
            start_time = cv2.getTickCount()

    # 等待用户按下q键退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头并关闭窗口
cap.release()
cv2.destroyAllWindows()