【问题标题】:Extract frames from video into specific folder从视频中提取帧到特定文件夹
【发布时间】:2019-04-06 20:36:00
【问题描述】:

我想从 3 个视频中提取帧到 3 个不同的文件夹中。每个文件夹都有相应视频文件的帧。我只能访问第三个视频的目标。如何也提取前 2 个视频的帧

到目前为止,我已经根据视频文件创建了具有名称的文件夹。开发了帧提取代码,但只能从最后一个视频中提取。下面是我的代码

import cv2
import glob
from glob import glob
import os
import shutil

def extractFrames(m,n):

    if not os.path.exists:
        os.makedirs(n)

    vid_files=glob(m)
    print(vid_files)

    for v_f in range(len(vid_files)):
        v1=os.path.basename(vid_files[v_f])
        print(v1)
        vid_name = os.path.splitext(v1)[0]
        print(vid_name)
        output = n +'\\video_' + vid_name
        os.makedirs(output)
        print(output)





    vidcap = cv2.VideoCapture(vid_files[v_f])
    print(vidcap)
    success,image = vidcap.read()
    seconds = 2
    fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
    multiplier = fps * seconds
    count=0

    while success:
        img_name = vid_name + '_f' + str(count) + ".jpg"
        image_path = output + "/" + img_name
        frameId = int(round(vidcap.get(1)))
        success,image = vidcap.read()
        if frameId % multiplier == 0:
            cv2.imwrite(filename = image_path, img = image)
            count+=1


    vidcap.release()
    cv2.destroyAllWindows()

    print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
    return output


x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")

z=extractFrames(x,y)

如果有 3 个视频,即 video1,video2,video3。我想将相应的帧提取到它们的特定文件夹中,即 video1 文件夹、video2 文件夹、video3 文件夹。目前,我只能将第三个视频的帧提取到文件夹 video3 中。我怎样才能为video1和video2做到这一点

【问题讨论】:

  • 您缺少的一件事是if not os.path.exists: 中的参数。这应该指定哪个目录:if not os.path.exists(n):.
  • @NielsHenkens 我试过这个,看起来这与从视频中提取帧到特定文件夹无关。如何将视频帧提取到相应的文件夹中。我认为这与循环有关

标签: python opencv


【解决方案1】:

vidcap = ... 向下部分的缩进已关闭。因此只使用 for 循环中的最后一个文件。

import cv2
import glob
from glob import glob
import os
import shutil

def extractFrames(m,n):
if not os.path.exists:
        os.makedirs(n)

    vid_files=glob(m)
    print(vid_files)

    for v_f in range(len(vid_files)):
        v1=os.path.basename(vid_files[v_f])
        print(v1)
        vid_name = os.path.splitext(v1)[0]
        print(vid_name)
        output = n +'\\video_' + vid_name
        os.makedirs(output)
        print(output)


        vidcap = cv2.VideoCapture(vid_files[v_f])
        print(vidcap)
        success,image = vidcap.read()
        seconds = 2
        fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
        multiplier = fps * seconds
        count=0

        while success:
            img_name = vid_name + '_f' + str(count) + ".jpg"
            image_path = output + "/" + img_name
            frameId = int(round(vidcap.get(1)))
            success,image = vidcap.read()
            if frameId % multiplier == 0:
                cv2.imwrite(filename = image_path, img = image)
                count+=1

        vidcap.release()
        cv2.destroyAllWindows()

        print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
    return output # indent this less

x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")

z=extractFrames(x,y)

【讨论】:

  • 我之前也尝试过这种方法。这将创建文件夹并仅为第一个视频文件提取帧
  • 如果你尝试打印变量 vidcap 它只有一个输出。我不明白如何递归地从所有视频文件中读取和提取帧。我做错了什么?
  • 我的错,我还在return 语句中添加了一个缩进。现在在上面的编辑中修复它。你可以试试那个吗?
  • 工作得很好。非常感谢您宝贵的时间和帮助兄弟:)
猜你喜欢
  • 1970-01-01
  • 2019-03-29
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 2020-08-24
  • 2018-12-30
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多