GradStudent
GradStudent

Reputation: 186

Frame rate of video Python

Code:

    import cv2
    import numpy as np
    import sys
    import webcolors
    import time

    cam=cv2.VideoCapture('video2.avi')
    _, fo = cam.read()
    framei = cv2.cvtColor(fo, cv2.COLOR_BGR2GRAY)
    bg_avg = np.float32(framei)
    video_width = int(cam.get(3))
    video_height = int(cam.get(4))
    fr = int(cam.get(5))
    print("frame rate of stored video:::",fr)

    while(cam.isOpened): 
           f,img=cam.read()
           start_fps=time.time()
           .
           .
           .
           k = cv2.waitKey(20)
           if(k == 27):
               break
         endtime_fps=time.time()
         diff_fps=endtime_fps-start_fps
         print("Frame rate::",1/diff_fps)

With every iteration, this prints a different frame rate like: 31.249936670193268, 76.92300920661702, 142.85290010558222, 166.67212398172063, 200.00495922941204, 38.46150460330851... etc with some values being repeated a few times. Now the value of frame rate for the stored video is 25. So what is the actual frame rate at which it is being read?

Upvotes: 6

Views: 16583

Answers (2)

alyssaeliyah
alyssaeliyah

Reputation: 2244

You can get FPS(Frames Per Second) using the code below:

import cv2
cam = cv2.VideoCapture('video2.avi')
fps = cam.get(cv2.CAP_PROP_FPS)

Upvotes: 18

Multimedia Mike
Multimedia Mike

Reputation: 13216

I'm not certain, but I think this might come down to your timing method. I don't think Python's time.time() method guarantees enough precision to provide the real-time profiling information you desire.

Upvotes: 2

Related Questions