python opencv 获取fps 帧率

#!/usr/bin/env python

import cv2
import time

if __name__ == '__main__' :

    # Start default camera
    video = cv2.VideoCapture(0);

    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

    # With webcam get(CV_CAP_PROP_FPS) does not work.
    # Let's see for ourselves.

    if int(major_ver)  < 3 :
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
        print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
    else :
        fps = video.get(cv2.CAP_PROP_FPS)
        print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))

    # Number of frames to capture
    num_frames = 120;

    print("Capturing {0} frames".format(num_frames))

    # Start time
    start = time.time()

    # Grab a few frames
    for i in range(0, num_frames) :
        ret, frame = video.read()

    # End time
    end = time.time()

    # Time elapsed
    seconds = end - start
    print ("Time taken : {0} seconds".format(seconds))

    # Calculate frames per second
    fps  = num_frames / seconds
    print("Estimated frames per second : {0}".format(fps))

    # Release video
    video.release()

注意

cap.get(cv2.CAP_PROP_FPS) 有时会获取到异常值 180000.0,所以当异常时使用时间差的方式获取
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在使用PythonOpenCV获取视频的帧率fps)时,可以使用`cv2.VideoCapture`函数来打开视频文件或者摄像头。在打开视频文件后,可以使用`get`方法获取视频的帧率属性,示例如下: ```python import cv2 video_name = "video.ts" vc = cv2.VideoCapture(video_name) # 读入视频文件 video_fps = int(vc.get(cv2.CAP_PROP_FPS)) # 视频帧率 print(video_fps) vc.release() ``` 在上述代码中,`cv2.VideoCapture`函数用于打开视频文件,然后使用`get`方法获取视频的帧率属性,最后通过`int`函数将帧率转换为整数类型并打印出来。请注意,需要在使用完视频后调用`release`方法释放资源。 希望对你有所帮助! #### 引用[.reference_title] - *1* *2* [【PythonPython-OpenCV实时处理视频](https://blog.csdn.net/weixin_42147967/article/details/126754172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [使用python-opencv获取视频数据](https://blog.csdn.net/qq_35451572/article/details/104179024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值