6.数据处理-视频抽帧处理成图片

#-*- coding: utf-8 -*-
# 本脚本的功能:将一个文件夹下的所有.avi视频截取帧保寸为图像
#Auth:cao
import os, sys,argparse, cv2
from datetime import datetime

def checkpath(path):
    if not os.path.exists(path):
        print("错误的路径: {}".format(path))
        sys.exit()

def makepath(path):
    if not os.path.exists(path):
        os.makedirs(path)

if __name__ == "__main__":
    # ==================================================================================================================
    # 输入参数,不能有中文路径
    # input_path: 视频的存放路径
    # output_path: 图片的保存路径
    # ==================================================================================================================
    parse = argparse.ArgumentParser()
    parse.add_argument("--input_path", type=str, default= 'D:\\Project\\videos\\', help="...")#F:\videoE:\CUTOUTIMAGE\image\11111111
    parse.add_argument("--output_path", type=str, default='D:\\Project\\videos\\', help="")#F:\OUTPUTSample
    parse.add_argument("--imgnumber_pervideo", type = int, default = 1000000, help = "")
    parse.add_argument("--skip_frame", type = int, default = 5, help = "")
    # ==================================================================================================================
    #E:\jiao_video\avi
    flags, unparsed = parse.parse_known_args(sys.argv[1:])
    input_path = flags.input_path
    output_path = flags.output_path
    imgnumber_pervideo = flags.imgnumber_pervideo
    skip_frame = flags.skip_frame

    checkpath(input_path)
    makepath(output_path)
    #save_path = output_path

    files = os.listdir(input_path)
    video_type = ['avi', 'mp4', 'MP4', 'AVI', 'mov']
    for file in files:
        if len(file.split('.')) > 1 and file.split('.')[-1] in video_type:
            file_name = file.split('.')[0]

            # if file_name != 'CourtTest07':
            #     continue

            file_path = os.path.join(input_path, file)
            print(file_path)
            cap = cv2.VideoCapture(file_path)

            save_path = os.path.join(output_path, file_name)
            makepath(save_path)

            count = 0
            n = 0
            success = True
            while (success and n < imgnumber_pervideo):
                count += 1
                success, frame = cap.read()
                if 1 == count%skip_frame:
                    #success, frame = cap.read()
                    n += 1
                    print(n)
                    time = str(datetime.now())
                    time = '_'.join(time.split(' ')).split('.')[0]
                    time = ''.join(time.split(':'))
                    time = ''.join(time.split('-'))
                    img_save_name =file_name + time + '_' + '_'+str(count).zfill(7)+'.jpg'
                    cv2.imwrite(os.path.join(save_path, img_save_name), frame)
                    #cv2.imwrite(os.path.join(output_path, img_save_name), frame)


            cap.release()
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小帅之狗腿子

一条New_Worker

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值