In OpenCV the class VideoCapture handles reading videos and grabbing frames from connected cameras. There is a lot of information you can find about the video file you are playing by using the get(PROPERTY_NAME) method in VideoCapture. One of the common properties you may want to know is to find frame rate or frames per second. You can download all code and example images used in this post here.
How to find frame rate of a camera / webcam in OpenCV ?
In OpenCV finding the frame rate of a connected camera / webcam is not straight forward. The documentation says that get(CAP_PROP_FPS) or get(CV_CAP_PROP_FPS) gives the frames per second. Now that is true for video files, but not for webcams. For webcams and many other connected cameras, you have to calculate the frames per second manually. You can read a certain number of frames from the video and see how much time has elapsed to calculate frames per second.
Python
#!/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()
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(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;
}
How to find frame rate of a video in OpenCV ?
If you are reading a video file you can simply use the get method to obtain frames per second. The following examples show the usage.
Python
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()
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(CAP_PROP_FPS) : " << fps << endl;
video.release();
return 0;
}
Subscribe & Download Code
If you liked this article and would like to download code (C++ and Python) and example images used in this post, please click here. Alternately, sign up to receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.Summary
In this we discussed finding the frames per second-fps in OpenCV. We also provided the Python/C++ code for practice and study.
Key Takeaways
- OpenCV class
VideoCapture
handles reading videos and grabbing frames from connected cameras. - The method
PROPERTY_NAME
helps find lot of information about the video file being played. - Common property we may want to know, frame rate or frames per second, is discussed in detail.
- When reading a video file simply use the
get
method to obtain frames per second.
i tried to play a video using python but the video is playing in greater speed than the original
i used the code.
import numpy as np
import cv2
cap = cv2.VideoCapture(‘video8.avi’)
fps =cap.get(cv2.CAP_PROP_FPS)
wd =cap.get(3)
ln =cap.get(4)
cap.set(5,6.1)
print (‘frame rate {0} wd={1} ln={2}’.format(fps,wd,ln))
ret = cap.set(3,520)
ret = cap.set(4,600)
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow(‘frame’,gray)
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
cap.release()
cv2.destroyAllWindows()
What can be the problem?
As far as I remember, you have to wait for 1000/fps milliseconds in waitkey. Waiting for 1 millisecond will give you flash-fast speed.
P.S., lets say video is playing at 30fps. Dont use wait of 1000/30 milliseconds, as this will be rounded from 33.3 to 30 milliseconds. The 0.3 millisecond rounded off from a long video can change the videolength. Trick like keeping a counter which when is incremented for each frame from 0. When it reaches 3, wait for 34 ms and set counter to 0.
Hi,
I tried to use this code to get the fps of my camera. I am getting 0.0 as my output.
`Frames per second using camera.get(cv2.cv.CV_CAP_PROP_FPS): 0.0`
I am using Macbook pro webcam, python 2.7 and opencv 2.4
If you read the article carefully, it says CV_CAP_PROP_FPS does not work for webcams and that is why we have provided another method for finding the FPS. In OpenCV 3.2, CV_CAP_PROP_FPS works for webcams as well.
My bad. Thanks.
I want to capture frames from Ps3 eye camera at 120 fps. But I am unable to capture using set capture property in opencv.
good everning to every body, please can someone tell me where to fine teh python source code of the function createBackgroundSubstractor in opencv, i want to modify it
also please i need to know how to do compressive sensing on a video with python using opencv
hi, thank you for the post. i have a question, i want to read every 5 frames from a webcam, how can i include this part in my code? :c
thanks
Just a heads-up, in OpenCV 3.2, cv2.CAP_PROP_FPS works for all types of devices now: files, webcams, even network stream URL’s.
That’s nice. I did not know that. Thanks.
Hello!
When running the script- I get this error in terminal:
File “fps.py”, line 17
print ‘Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}’.format(fps)
^
SyntaxError: invalid syntax
What should I do?
the error arrow is below the dot in xxxx {0}’.format(fps)
https://uploads.disquscdn.com/images/a33f0d4a3bc5abc0338a3bb921916c6a1f69da9762434517703629a8753c8e58.png
Hi! How can I set fps, width of frame and height of frame in my ipcamera using Opencv, because as you can see videoCapture.set() didn`t work. I have also tryed setting this using ONVIF Device Manager and still doesn`t work. Any idea?
this is the best page ever!!!!!
I am getting following error C++ example. Any idea why?. I checked my input .mp4 file is present in the path i specified.
warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl.hpp:537)
Frames per second using video.get(CV_CAP_PROP_FPS) : 0
Press any key to continue . . .
Regards
How to reduce frame rate of usb camera for opencv in cpp?
Conteúdo muito bom! Obrigado…
I change FPS rate of my WebCam using
cap.set(cv::CAP_PROP_FPS,5);
when check it with
cap.get(cv::CAP_PROP_FPS);
it seem Ok. But WebCam still works on its original fps rate (30 in my case).
How can I change fps rate of my WebCAM?