We will be displaying the real-time processing FPS of the video file or webcam depending upon our choice.
FPS or frame per second or frame rate can be defined as number of frames displayed per second. A video can be assumed as a collection of images or we can say frames which are displayed at some rate to produce motion. if you wish to identify the object in the video than 15 fps can be sufficient but if you wish to identify the car number which is moving at speed of 40 km/hr. on the highway then you would need at least 30 fps for hassle-free identification. Therefore, it will be good if we know how to calculate FPS in our Computer Vision projects.
Steps to Calculate Live FPS:
- The first step will be to create a VideoCapture object by using cv2.VideoCapture() .We can read video from webcam or video file depending upon our choice .
- Now we will be processing the captured footage frame by frame until capture.read() is true ( here capture represent an object and this function additionally with frame it also return bool and provide the information whether the frame has been successfully read or not ).
- For calculating FPS, we will be keeping the record of the time when last frame processed and the end time when current frame processed.So, the processing time for one frame will be time difference between current time and previous frame time .
Processing time for this frame = Current time – time when previous frame processed
So fps at the current moment will be :
FPS = 1/ (Processing time for this frame)
- Since FPS will be always Integer, we will be converting FPS to integer and after that typecasting it to string because it will be easy and faster to display the string using cv2.putText() on frame . Now with the help cv2.putText() method we will be printing the FPS on this frame and then displaying this frame with the help of cv2.imshow() function .
Code: Python code implementation of the above mentioned approach
Output: video file displaying live FPS in green colour.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Displaying the coordinates of the points clicked on the image using Python-OpenCV
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Multiple Color Detection in Real-Time using Python-OpenCV
- Detect Cat Faces in Real-Time using Python-OpenCV
- OpenCV | Displaying an Image
- MoviePy – FPS of Video File Clip
- OpenCV | Real Time Road Lane Detection
- PyQtGraph – Getting Processed Image Data from Image View
- Python VLC MediaPlayer - Getting FPS
- MoviePy – Setting FPS of the Clip
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- MoviePy – Displaying a Frame of Video Clip using inbuilt display method
- Transition from OpenCV 2 to OpenCV 3.x
- Python | Real time currency convertor using Tkinter
- Python | Real time weather detection using Tkinter
- Get Bit Coin price in real time using Python
- How to get real-time Mutual Funds Information using Python?
- Tableau - Displaying data in Worksheet
- PyCairo - Displaying Text
- Python | Get the real time currency exchange rate
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.