1、描述
将一个视频流按帧数截取大量的图片
2、用途
AI的数据集制作,得到大量的图片,之后将其打标签
3、案例文件截图
4、代码实现:
import cv2
import argparse
import os# 边里该文件夹下的文件名称
def read_directory(directory_name):file_list = []for filename in os.listdir(directory_name):str = directory_name+'/'+filenamefile_list.append(str)return file_list# 设置处理的帧数与文件位置,并整合。
def parse_args(input,output):parser = argparse.ArgumentParser(description='Process pic')parser.add_argument('--input', help='video to process', dest='input', default=None, type=str)parser.add_argument('--output', help='pic to store', dest='output', default=None, type=str)# default为间隔多少帧截取一张图片parser.add_argument('--skip_frame', dest='skip_frame', help='skip number of video', default=50, type=int)# input为输入视频的路径 ,output为输出存放图片的路径args = parser.parse_args(['--input', input, '--output', output])return args# 处理帧数函数
def process_video(i,i_video, o_video, num):cap = cv2.VideoCapture(i_video)num_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT)print("该视频的总帧数为:",num_frame)expand_name = '.jpg'if not cap.isOpened():print("检查路径名")cnt = 0count = 0while 1:ret, frame = cap.read()cnt += 1if cnt % num == 0:count += 1cv2.imwrite(os.path.join(o_video, str(i)+'+'+str(count) + expand_name), frame)if not ret:breakif __name__ == '__main__':# 命名不要带中文,可能报错intput = read_directory("./video_01")output = './video_img'print("该目录下共有:",len(intput),"个视频")i = 1for input_i in intput:args = parse_args(input_i,output)print("开始输出第",i,"个视频")process_video(i,args.input, args.output, args.skip_frame)print("第", i, "个视频处理完毕")print("-----------------------------------")i = i + 1print("汇总",len(intput),"个视频处理完毕")