Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 347
GradStudent Avatar asked Jun 23 '14 08:06

GradStudent


2 Answers

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)
like image 61
alyssaeliyah Avatar answered Nov 26 '22 07:11

alyssaeliyah


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.

like image 23
Multimedia Mike Avatar answered Nov 26 '22 06:11

Multimedia Mike