AIHGF

OpenCV - 计算相机和视频的帧速率FPS[译]
原文:How to find frame rate or frames per second (fps) in O...
扫描右侧二维码阅读全文
06
2019/05

OpenCV - 计算相机和视频的帧速率FPS[译]

原文:How to find frame rate or frames per second (fps) in OpenCV ( Python / C++ ) ? - 2015.11.12

OpenCV 库中的 VideoCapture 类主要处理视频读取以及从连接的相机中获取图像帧.

基于VideoCapture 中的 get(PROPERTY_NAME) 方法可以获取到视频文件的很多信息. 其中,关于视频的最常用的属性是帧速率(frame rate),也叫每秒帧数(frames per second).

1. 计算相机的帧速率FPS

OpenCV 并不能很直接的得到所连接的相机(camera/webcam) 的帧速率.

在 OpenCV 的文档中,所述的是,get(CAP_PROP_FPS)get(CV_CAP_PROP_FPS) 方法给出了每秒帧数. 这对于视频文件而言是正确的,但是并不适用于 webcams. 对于webcams 以及许多其它 cameras,不得不手工计算每秒的帧数. 可以从视频中读取一定量的视频帧,然后根据所耗时间,计算得到每秒帧数.

1.1. Python 实现

#!/usr/bin/env python
#! --*-- coding:utf-8 --*--
import cv2
import time
 
if __name__ == '__main__' :
    # 启动默认相机
    video = cv2.VideoCapture(0);
     
    # 获取 OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    
    # 对于 webcam 不能采用 get(CV_CAP_PROP_FPS) 方法 
    # 而是:
    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 xrange(0, num_frames):
        ret, frame = video.read()
    # End time
    end = time.time()
 
    # Time elapsed
    seconds = end - start
    print("Time taken : {0} seconds".format(seconds))
 
    # 计算FPS,alculate frames per second
    fps  = num_frames / seconds;
    print("Estimated frames per second : {0}".format(fps))
 
    # 释放 video
    video.release()

1.2. C++ 实现

#include "opencv2/opencv.hpp"
#include <time.h>
 
using namespace cv;
using namespace std;
 
int main(int argc, char** argv)
{
    // Start default camera
    VideoCapture video(0);
     
    // With webcam get(CV_CAP_PROP_FPS) does not work.
    // Let's see for ourselves.
     
    double fps = video.get(CV_CAP_PROP_FPS);
    // If you do not care about backward compatibility
    // You can use the following instead for OpenCV 3
    // double fps = video.get(CAP_PROP_FPS);
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;
     
    // Number of frames to capture
    int num_frames = 120;
    
    // Start and end times
    time_t start, end;
    
    // Variable for storing video frames
    Mat frame;
 
    cout << "Capturing " << num_frames << " frames" << endl ;
 
    // Start time
    time(&start);
     
    // Grab a few frames
    for(int i = 0; i < num_frames; i++)
    {
        video >> frame;
    }
     
    // End Time
    time(&end);
     
    // Time elapsed
    double seconds = difftime (end, start);
    cout << "Time taken : " << seconds << " seconds" << endl;
     
    // Calculate frames per second
    fps  = num_frames / seconds;
    cout << "Estimated frames per second : " << fps << endl;
     
    // Release video
    video.release();
    return 0;
}

2. 计算视频的帧速率FPS

可以直接采用 OpenCV 的 get 方法计算视频文件的帧速率.

2.1. Python 实现

#!/usr/bin/env python
#! --*-- coding:utf-8 --*--
import cv2

if __name__ == '__main__' :
    video = cv2.VideoCapture("video.mp4");
     
    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
     
    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))
     
   video.release(); 

2.2. C++ 实现

#include "opencv2/opencv.hpp"
 
using namespace cv; 
using namespace std;
 
int main(int argc, char** argv)
{
    // Open video file
    VideoCapture video("video.mp4");
 
    double fps = video.get(CV_CAP_PROP_FPS);
 
    // For OpenCV 3, you can also use the following
    // double fps = video.get(CAP_PROP_FPS);
 
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;
     
    video.release(); 
    return 0;
} 
Last modification:May 6th, 2019 at 10:15 pm

Leave a Comment