Rella
Rella

Reputation: 66975

How to get web camera fps rate in OpenCV?

So I need to get web camera fps rate in OpenCV. Which function can do such thing for?

Upvotes: 3

Views: 16778

Answers (4)

Thiago Falcao
Thiago Falcao

Reputation: 5043

In my case, fps = video.get(cv2.CAP_PROP_FPS) did not work.

So, I found this code in this link:

https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/

import cv2
import time

if __name__ == '__main__':

    video = cv2.VideoCapture(1)

    # Find OpenCV version
    (major_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 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)

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

    # Release video
    video.release()

Upvotes: 1

LovaBill
LovaBill

Reputation: 5139

*OpenCV 2 solution:

C++: double VideoCapture::get(int propId)

E.g.

VideoCapture myvid("video.mpg");
int fps=myvid.get(CV_CAP_PROP_FPS);

Upvotes: 0

Zhe Hu
Zhe Hu

Reputation: 4007

It seems that for live webcam capture, you can set an arbitrary fps and read back that same fps, which has nothing to do with the real fps from webcam. Is it a bug?

For example:

cvSetCaptureProperty(capture,CV_CAP_PROP_FPS,500);

and later

double rates = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
printf("%f\n",rates);

will give you 500.

But if I timed it using web cam fps link, it's around the normal 30fps.

Upvotes: 2

Adi
Adi

Reputation: 1316

int cvGetCaptureProperty( CvCapture* capture, int property_id);

with property_id = CV_CAP_PROP_FPS

Upvotes: 6

Related Questions