视频(avi)转换为图片(Python代码实现)
【摘要】
下面Python代码是把视频格式是avi的视频转换为一帧一帧的图片
import osimport numpy as npimport cv2 video_src_src_path = 'data/hmdb_data_demo' #数据集路径label_name = os.listdir(video_src_src_path)labe...
下面Python代码是把视频格式是avi的视频转换为一帧一帧的图片
-
import os
-
import numpy as np
-
import cv2
-
-
video_src_src_path = 'data/hmdb_data_demo' #数据集路径
-
label_name = os.listdir(video_src_src_path)
-
label_dir = {}
-
index = 0
-
for i in label_name:
-
if i.startswith('.'):
-
continue
-
label_dir[i] = index
-
index += 1
-
video_src_path = os.path.join(video_src_src_path, i)
-
video_save_path = os.path.join(video_src_src_path, i) + '_jpg'
-
if not os.path.exists(video_save_path):
-
os.mkdir(video_save_path)
-
-
videos = os.listdir(video_src_path)
-
# 过滤出avi文件
-
videos = filter(lambda x: x.endswith('avi'), videos)
-
-
for each_video in videos:
-
each_video_name, _ = each_video.split('.')
-
if not os.path.exists(video_save_path + '/' + each_video_name):
-
os.mkdir(video_save_path + '/' + each_video_name)
-
-
each_video_save_full_path = os.path.join(video_save_path, each_video_name) + '/'
-
-
each_video_full_path = os.path.join(video_src_path, each_video)
-
-
cap = cv2.VideoCapture(each_video_full_path)
-
frame_count = 1
-
success = True
-
while success:
-
success, frame = cap.read()
-
# print('read a new frame:', success)
-
-
params = []
-
params.append(1)
-
if success:
-
cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_count, frame, params)
-
-
frame_count += 1
-
cap.release()
-
np.save('label_dir.npy', label_dir)
-
print(label_dir)
文章来源: blog.csdn.net,作者:小小谢先生,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/xiewenrui1996/article/details/108746063
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)