为不同视频的视频帧创建新目录时出错
Error creating new directories for video frames for different videos
对于每个标题,我正在尝试编写代码来循环浏览一个文件夹中的多个视频以提取它们的帧,然后将每个视频的帧写入它们自己的新文件夹,例如视频 1 到 frames_video1,视频 2 到 frames_video2。
这是我的代码:
subclip_video_path = main_path + "\subclips"
frames_path = main_path + "\frames"
#loop through videos in file
for subclips in subclip_video_path:
currentVid = cv2.VideoCapture(subclips)
success, image = currentVid.read()
count = 0
while success:
#create new frames folder for each video
newFrameFolder = ("frames_" + subclips)
os.makedirs(newFrameFolder)
我收到这个错误:
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\pip-req-build-k8sx3e60\opencv\modules\videoio\src\cap.cpp (142) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\pip-req-build-k8sx3e60\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): P in function 'cv::icvExtractPattern'
这是什么意思?我该如何解决这个问题?
- 你不能循环遍历字符串:
for subclips in subclip_video_path:
您需要获取视频列表:
from glob import glob
sub_clip_video_path = glob("sub_clip_video_path/*.mp4")
这意味着获取所有扩展名为 .mp4 的视频文件并将其存储在 sub_clip_video_path
变量中。
我的结果:
['sub_clip_video_path/output.mp4', 'sub_clip_video_path/result.mp4']
因为我确定该目录包含两个 .mp4
扩展文件,现在我可以继续了。
- 您不需要为每一帧 re-declare
VideoCapture
。
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
success, image = currentVid.read()
count = 0
声明VideoCapture
后读取当前视频的所有帧,然后为下一个视频声明VideoCapture
。
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
image_counter = 0
while currentVid.isOpened():
.
.
- 不要使用
while success
这会造成无限循环。
如果第一帧是从视频中抓取的,那么success
变量returnsTrue
。当你说:
while success:
#create new frames folder for each video
newFrameFolder = ("frames_" + subclips)
os.makedirs(newFrameFolder)
您将为当前帧创建无限数量的文件夹。
这是我的结果:
import os
import cv2
from glob import glob
sub_clip_video_path = glob("sub_clip_video_path/*.mp4") # Each image extension is `.mp4`
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
image_counter = 0
while currentVid.isOpened():
success, image = currentVid.read()
if success:
newFrameFolder = "frames_video{}".format(count + 1)
if not os.path.exists(newFrameFolder):
os.makedirs(newFrameFolder)
image_name = os.path.join(newFrameFolder, "frame{}.png".format(image_counter + 1))
cv2.imwrite(image_name, image)
image_counter += 1
else:
break
我用glob
收集了所有视频
正在阅读当前视频时:
-
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
image_counter = 0
while currentVid.isOpened():
如果当前帧抓取成功,则声明文件夹名称。如果该文件夹不存在,请创建它。
-
if success:
newFrameFolder = "frames_video{}".format(count + 1)
if not os.path.exists(newFrameFolder):
os.makedirs(newFrameFolder)
然后声明图片名称并保存
-
image_name = os.path.join(newFrameFolder, "frame{}.png".format(image_counter + 1))
cv2.imwrite(image_name, image)
image_counter += 1
对于每个标题,我正在尝试编写代码来循环浏览一个文件夹中的多个视频以提取它们的帧,然后将每个视频的帧写入它们自己的新文件夹,例如视频 1 到 frames_video1,视频 2 到 frames_video2。
这是我的代码:
subclip_video_path = main_path + "\subclips"
frames_path = main_path + "\frames"
#loop through videos in file
for subclips in subclip_video_path:
currentVid = cv2.VideoCapture(subclips)
success, image = currentVid.read()
count = 0
while success:
#create new frames folder for each video
newFrameFolder = ("frames_" + subclips)
os.makedirs(newFrameFolder)
我收到这个错误:
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\pip-req-build-k8sx3e60\opencv\modules\videoio\src\cap.cpp (142) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\pip-req-build-k8sx3e60\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): P in function 'cv::icvExtractPattern'
这是什么意思?我该如何解决这个问题?
- 你不能循环遍历字符串:
for subclips in subclip_video_path:
您需要获取视频列表:
from glob import glob
sub_clip_video_path = glob("sub_clip_video_path/*.mp4")
这意味着获取所有扩展名为 .mp4 的视频文件并将其存储在 sub_clip_video_path
变量中。
我的结果:
['sub_clip_video_path/output.mp4', 'sub_clip_video_path/result.mp4']
因为我确定该目录包含两个 .mp4
扩展文件,现在我可以继续了。
- 您不需要为每一帧 re-declare
VideoCapture
。
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
success, image = currentVid.read()
count = 0
声明VideoCapture
后读取当前视频的所有帧,然后为下一个视频声明VideoCapture
。
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
image_counter = 0
while currentVid.isOpened():
.
.
- 不要使用
while success
这会造成无限循环。
如果第一帧是从视频中抓取的,那么success
变量returnsTrue
。当你说:
while success:
#create new frames folder for each video
newFrameFolder = ("frames_" + subclips)
os.makedirs(newFrameFolder)
您将为当前帧创建无限数量的文件夹。
这是我的结果:
import os
import cv2
from glob import glob
sub_clip_video_path = glob("sub_clip_video_path/*.mp4") # Each image extension is `.mp4`
for count, sub_clips in enumerate(sub_clip_video_path):
currentVid = cv2.VideoCapture(sub_clips)
image_counter = 0
while currentVid.isOpened():
success, image = currentVid.read()
if success:
newFrameFolder = "frames_video{}".format(count + 1)
if not os.path.exists(newFrameFolder):
os.makedirs(newFrameFolder)
image_name = os.path.join(newFrameFolder, "frame{}.png".format(image_counter + 1))
cv2.imwrite(image_name, image)
image_counter += 1
else:
break
我用
收集了所有视频glob
正在阅读当前视频时:
-
for count, sub_clips in enumerate(sub_clip_video_path): currentVid = cv2.VideoCapture(sub_clips) image_counter = 0 while currentVid.isOpened():
-
如果当前帧抓取成功,则声明文件夹名称。如果该文件夹不存在,请创建它。
-
if success: newFrameFolder = "frames_video{}".format(count + 1) if not os.path.exists(newFrameFolder): os.makedirs(newFrameFolder)
-
然后声明图片名称并保存
-
image_name = os.path.join(newFrameFolder, "frame{}.png".format(image_counter + 1)) cv2.imwrite(image_name, image) image_counter += 1
-