【问题标题】:How to add a delay when saving video frames as images将视频帧保存为图像时如何添加延迟
【发布时间】: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


    【解决方案1】:

    你可以用time.sleep(5)试试看:

    import os, cv2
    import time
    framesToSkip = 120
    
    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):
            # To stop duplicate images
            currentFrame += 1
            if currentFrame % framesToSkip != 0:
                continue
            # 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)
            time.sleep(5)
    
            
    

    它将暂停 5 秒并保存下一帧。

    【讨论】:

    • 时间好像没有暂停功能
    • 抱歉是sleep
    • 是的,睡眠不起作用。这与我指出的问题相同。如果这是直播,您的方法会奏效,但事实并非如此。这是一个本地视频,当你sleep n 秒时,它会从它离开的地方继续播放。
    • 嗯,对不起,你需要保留一些帧,我会调整代码
    • 同样的问题。不跳过任何内容。
    【解决方案2】:

    可能有一种更有效的方法,但您可以通过使用视频的帧速率然后每FRAME_RATE * 5 帧处理图像来做到这一点。

    您的代码看起来像这样(如果这不起作用,请告诉我,因为我没有在我的 PC 上运行它):

    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)
        fps = video.get(cv2.CAP_PROP_FPS)
    
        currentFrame = 0
    
        while video.isOpened():
            # Capture frame-by-frame
            ret, frame = video.read()
    
            if (video):
                # Write current frame
                name = current_dir + '/training_images/frame' + str(currentFrame) + '.png'
                print('Creating...' + name)
                cv2.imwrite(name, frame)
    
                currentFrame += fps * 5
                # Skip to next 5 seconds
                video.set(cv2.CAP_PROP_POS_FRAMES, currentFrame)
    
    

    【讨论】:

    • 这只会创建第一个图像,仅此而已。我的猜测是它总是会覆盖第一张图片。
    • @Axiumin_ 您可以在 while 循环的顶部移动两个帧计数器。
    猜你喜欢
    • 2021-05-05
    • 2011-04-04
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多