【发布时间】:2019-07-24 12:59:38
【问题描述】:
我有一个装满 mp4 剪辑的文件夹(超过 200 个)。我想获取所有这些剪辑,提取它们的帧并将它们发送到另一个文件夹以存储所有帧。这是我到目前为止所拥有的(部分代码)但它只有在我有一个 mp4 文件时才有效文件夹:
import cv2 # for capturing videos
import math # for mathematical operations
import matplotlib.pyplot as plt # for plotting the images
import pandas as pd
from keras.preprocessing import image # for preprocessing the images
import numpy as np # for mathematical operations
from keras.utils import np_utils
from skimage.transform import resize # for resizing images
count = 0
videoFile = "sample_vid.mp4"
cap = cv2.VideoCapture(videoFile) # capturing the video from the given path
frameRate = cap.get(5) #frame rate
x=1
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(frameRate) == 0):
filename ="frame%d.jpg" % count;count+=1
cv2.imwrite(filename, frame)
cap.release()
print ("Done!")
再次,我在处理 python 中的文件目录和循环它时遇到了一些麻烦,以便它遍历另一个文件夹中的所有文件并将提取的帧发送到另一个文件夹。
【问题讨论】:
-
你有什么错误?
-
我不知道如何在我的代码中使用不同的目录。对于同一目录中的单个文件,该功能是可靠的,我只是想获取它,以便它遍历所有文件并将它们传输到一个文件夹。
-
所以你需要遍历所有文件夹
-
不,只有一个包含所有 mp4 文件的文件夹。我需要从该 mp4 文件文件夹中提取帧并将它们发送到另一个文件夹
标签: python python-3.x opencv scikit-image cv2