5

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?

GradStudent
  • 166
  • 1
  • 3
  • 12

2 Answers2

16

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)
Aditya
  • 533
  • 3
  • 11
alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80
2

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.

Multimedia Mike
  • 12,660
  • 5
  • 46
  • 62
  • Even if it is not precise, should I be getting values atleast in the same range? How do I determine at what frame rate the video is being read? – GradStudent Jun 23 '14 at 17:34
  • A lot of this surely comes down to the vagaries of running on a non realtime system and the unpredictability of when your process gets scheduled to run after yielding the CPU to block on input. – Multimedia Mike Jun 23 '14 at 20:00
  • 1
    So if I want to process the stored video at a certain frame rate, how do I do that? – GradStudent Jun 23 '14 at 23:55
  • Not sure if I understand your goal. Are you trying to capture frames, process them, and then display them back to the user on a real-time deadline? If you just want to, e.g., process the captured frames and store them to disc, then these timings are less interesting. In general, you can trust that the hardware is capturing the frames at a steady 25 frames/sec. The OS/language interpreter might introduce a bit of delay when delivering them to your program. – Multimedia Mike Jun 25 '14 at 15:51