我正在尝试从数据集文件夹中存在的每个单独视频中提取帧,但不是帧,而是多个子文件夹,其名称与视频名称完全相同。请指导!
这是我的代码:
import os, sys
from os import listdir
import shutil
main_folder = r'G:\\Umar Zafar Data\\8th Semester\\FYP\\Action Detection\\DCSASS Dataset\\all_work\\'
fps = 25
for folder in listdir(main_folder):
folder_url = main_folder+folder
for file in listdir(folder_url):
file_url = folder_url+'\\'+file
if os.path.isfile(file_url):
if not os.path.isdir(file_url[:-4]):
os.mkdir(file_url[:-4])
print("Extracting frames from ", file_url)
os.system('ffmpeg -i "{}" -vf fps={} "{}/%05d.bmp"'.format(file_url, fps, file_url[:-4]))
os.remove(file_url)
在此处输出图像:
答案 0 :(得分:0)
对您的代码进行了一些修复,并且可以正常工作:
import os
import cv2
main_folder = r'.\movies'
fps = 25
for folder in os.listdir(main_folder):
folder_url = f"{main_folder}\\{folder}"
for file in os.listdir(folder_url):
file_url = f"{folder_url}\\{file}"
if os.path.isfile(file_url):
folder_url2 = file_url[:-4]
if not os.path.isdir(folder_url2):
os.mkdir(folder_url2)
print("Extracting frames from ", file_url)
cap = cv2.VideoCapture(file_url)
ret, img = cap.read()
i = 0
while ret:
cv2.imwrite(f"{folder_url2}//{i}.bmp", img)
i += 1
ret, img = cap.read()
cap.release()
请注意,您需要安装 cv2
包 (我相信您已经给了 opencv 标记),这可以通过命令提示符命令完成:
pip install opencv-python