Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Displaying real time FPS at which webcam/video file is processed...

Python – Displaying real time FPS at which webcam/video file is processed using OpenCV

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 
 

Python3




import numpy as np
import cv2
import time
  
  
# creating the videocapture object
# and reading from the input file
# Change it to 0 if reading from webcam
  
cap = cv2.VideoCapture('vid.mp4')
  
# used to record the time when we processed last frame
prev_frame_time = 0
  
# used to record the time at which we processed current frame
new_frame_time = 0
  
# Reading the video file until finished
while(cap.isOpened()):
  
    # Capture frame-by-frame
  
    ret, frame = cap.read()
  
    # if video finished or no Video Input
    if not ret:
        break
  
    # Our operations on the frame come here
    gray = frame
  
    # resizing the frame size according to our need
    gray = cv2.resize(gray, (500, 300))
  
    # font which we will be using to display FPS
    font = cv2.FONT_HERSHEY_SIMPLEX
    # time when we finish processing for this frame
    new_frame_time = time.time()
  
    # Calculating the fps
  
    # fps will be number of frame processed in given time frame
    # since their will be most of time error of 0.001 second
    # we will be subtracting it to get more accurate result
    fps = 1/(new_frame_time-prev_frame_time)
    prev_frame_time = new_frame_time
  
    # converting the fps into integer
    fps = int(fps)
  
    # converting the fps to string so that we can display it on frame
    # by using putText function
    fps = str(fps)
  
    # putting the FPS count on the frame
    cv2.putText(gray, fps, (7, 70), font, 3, (100, 255, 0), 3, cv2.LINE_AA)
  
    # displaying the frame with fps
    cv2.imshow('frame', gray)
  
    # press 'Q' if you want to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
# When everything done, release the capture
cap.release()
# Destroy the all windows now
cv2.destroyAllWindows()


Output: video file displaying live FPS in green colour.
 

 

RELATED ARTICLES

Most Popular

Recent Comments