读取帧时无法限制每秒帧数

2024-04-26 03:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,我正在从文件夹中读取数据,我想限制从文件夹中读取的帧速率为5fps、10fps、15fps、20fps、25fps。当我在代码下面运行时,我的代码被挂起,我怀疑我使用的下面的方法是不正确的

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)

如果有人能在这方面帮助我,我将不胜感激


Tags: pathkey代码in文件夹imgforos
1条回答
网友
1楼 · 发布于 2024-04-26 03:35:29

我想你可以用这个关系来计算时间间隔:

delay = int((1 / int(fps)) * 1000)

在这里,我用上面的公式重写了您的代码。此外,我还添加了cv2.imshow()用于显示图像,以及cv2.waitKey用于延迟

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)

相关问题 更多 >

    热门问题