python - 读取帧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)
如果有人能在这方面帮助我,我们将不胜感激。
解决方案
我认为您可以使用此关系来计算间隔:
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)
推荐阅读
- excel - 从位于不同文件夹中的 Excel 模板创建新工作簿
- ios - 如何让我的应用仅在纵向模式下适合所有 iPhone 尺寸
- laravel - 负载对于 laravel_session 解密无效
- java - 从流下载时出现角度奇怪的行为
- c - 如何调试产生意外输出的 C 程序?
- vb.net - 重定向 Console.Write
- matlab - 在 MATLAB 中使用极坐标图函数有些奇怪
- javascript - 如何将表单提交发布到 URL 并在提交时重定向到不同的 URL
- javascript - Google 脚本触发器仅在周一/周三/周五运行
- dynamics-crm - 如何以编程方式检索在 Dynamics 365 中导入的记录?