Reputation: 175
I have the following code in which I am reading data from folder, I want to limit the number of frame rate to 5fps, 10fps,15fps,20fps,25fps being read from a folder. When I run below code my code is hanged and I doubt the below approach I am using is not correct.
filenames = [img for img in glob.glob("video-frames/*.jpg")]
fps = 5
#calculate the interval between frame.
interval = int(1000/fps)
filenames = sorted(filenames, key=os.path.getctime) or filenames.sort(key=os.path.getctime)
images = []
for img in filenames:
n= cv2.imread(img)
time.sleep(interval)
images.append(n)
print(img)
Help is highly appreciated if someone would help me in this regard.
Upvotes: 1
Views: 115
Reputation: 678
I think you can use this relation for calculating the intervals:
delay = int((1 / int(fps)) * 1000)
Here I rewrote your code with the above formula. Also, I added cv2.imshow()
for showing images and cv2.waitKey
for making the delay.
import cv2
import os
import glob
file_names = [img for img in glob.glob("video-frames/*.jpg")]
fps = 5
# calculate the interval between frames.
delay = int((1 / int(fps)) * 1000)
file_names = sorted(file_names, key=os.path.getctime) or file_names.sort(key=os.path.getctime)
images = []
# Creating a window for showing the images
cv2.namedWindow('images', cv2.WINDOW_NORMAL)
for path in file_names:
image = cv2.imread(path)
# time.sleep(interval)
cv2.waitKey(delay)
# Show an image
cv2.imshow('images', image)
images.append(image)
print(path)
Upvotes: 1