Is there any way or function in OpenCV that allows us to play any video with a fixed frame rate(fps)? Different videos may have different frame rates but by using OpenCV library can we play them by a fixed frame rate that we define?
Thanks in advance.
Find frame rate (frames per second-fps) in OpenCV (Python/C++) 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.
Use threading to obtain higher FPS The “secret” to obtaining higher FPS when processing video streams with OpenCV is to move the I/O (i.e., the reading of frames from the camera sensor) to a separate thread. You see, accessing your webcam/USB camera using the cv2.
Take a look at this article. It shows how to play back AVI files with OpenCV. Here, the frame rate is read using
int fps = ( int ) cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
and the delay is set via
key = cvWaitKey( 1000 / fps );
Hence, by controlling the fps
variable, you can get the play back rate you want.
int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int delay = 1000 / fps;
while (true) {
clock_t startTime = clock();
capture.read(frame);
process();
imshow("video", frame);
while (clock() - startTime < delay) {
waitKey(1);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With