【发布时间】:2021-05-05 03:57:11
【问题描述】:
我有一系列视频,我想将其中的帧保存为图像。这是我的代码:
import os, cv2
current_dir = os.getcwd()
training_videos = '../../Videos/training_videos/'
if not os.path.exists(current_dir + '/training_images/'):
os.mkdir(current_dir + '/training_images/')
for i in os.listdir(training_videos):
video = cv2.VideoCapture('../../Videos/training_videos/' + i)
currentFrame = 0
while (True):
# Capture frame-by-frame
ret, frame = video.read()
# Saves image of the current frame in jpg file
name = current_dir + '/training_images/frame' + str(currentFrame) + '.png'
print('Creating...' + name)
cv2.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
此代码有效,但不幸的是,它每毫秒需要一帧。这不是我想要的。相反,我想每 5 或 10 秒保存一帧。我考虑过添加一个时间延迟,但这不会真正起作用,因为视频不是实时流,所以 5 秒后,它只会在前一毫秒后截取屏幕截图。
【问题讨论】:
标签: python video-capture cv2