Ask Your Question
0

How to use FPS module

asked 2020-04-05 16:04:48 -0600

hernancrespo gravatar image

updated 2020-04-05 16:05:50 -0600

Hi, First I've created a video using

import cv2
import numpy as np
import glob

img_array = []
for filename in glob.glob('C:/Users/mustafa/Downloads/highway/highway/input/*.jpg'):
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width,height)
    img_array.append(img)

out = cv2.VideoWriter('project.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 24, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

As you see, I set FPS of video as 24.

Secondly, when I try to use FPS module to measure FPS of video that I've created.

from imutils.video import FPS
import cv2
import numpy as np

cap = cv2.VideoCapture('p.mp4')
fps = FPS().start()

if (cap.isOpened()== False): 
  print("Error opening video stream or file")

while(cap.isOpened()):

  ret, frame = cap.read()
  if ret == True:

    cv2.imshow('Frame',frame)
    fps.update()

    if cv2.waitKey(25) & 0xFF == ord('q'):
      break

  else: 
    break
cap.release()
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cv2.destroyAllWindows()

I see this output:

[INFO] approx. FPS: 38.06
[INFO] elapsed time: 44.67

Also the length of original video is 70 seconds. Why I see different results?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-04-06 02:27:31 -0600

berak gravatar image

updated 2020-04-06 04:05:57 -0600

the numbers you see all make sense. the FPS module is unfortunately not part of the opencv codebase, but assuming it "just works" --

if you wait for 25 millis in waitKey() you get 1000 / 25 = 40 FPS, minus some for image drawing, so 38.06 looks quite exact.

to play at (about) 24fps, try adjust the wait time to 41 millis, so you get 1000 / 41 = 24.3. (and again, with a bit of additional time spent for drawing, it should get quiite exact)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-04-05 16:04:48 -0600

Seen: 3,098 times

Last updated: Apr 06 '20