python将视频按帧读取到自定义目录

如题,首先读取视频路径,其次根据视频名称创建对应的文件夹,再逐帧将视频帧读入。

import cv2
import argparse
import sys
import os
parser = argparse.ArgumentParser(description='tranfer the vedio to img.')
parser.add_argument('-m', '--mode', choices=['folder', 'url'], default='folder')
parser.add_argument('-p', '--path', help='Specify a path [e.g. testModel]', default='E:\DATA\pose_h3.6m\S5\Videos')
parser.add_argument('-pimg', '--imgpath', help='Specify a path [e.g. testModel]', default='F:\pythonprogram\multi_task\img\S5')
args = parser.parse_args(sys.argv[1:])

if args.mode == 'folder':
    #get video
    withPath = lambda f: '{}/{}'.format(args.path,f)
    video = dict((f,cv2.imread(withPath(f))) for f in os.listdir(args.path) if os.path.isfile(withPath(f)))
for key,val in video.items():
    fram_video = '{}/{}'.format(args.path,key)
    act_cam=key[:-4]
    vc=cv2.VideoCapture('{}\{}'.format(args.path,key))
    c = 1
    # camera
    # print('{}\{}\{}.jpg'.format(args.imgpath, act_cam,str(c)))
    path = '{}\{}'.format(args.imgpath, act_cam)
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
    if vc.isOpened():
        rval, frame = vc.read()
    else:
        rval = False
    while rval:
        rval, frame = vc.read()
        cv2.imwrite('{}\\{}\\{}.jpg'.format(args.imgpath, act_cam,str(c)),frame)
    #     cv2.imwrite('C:\\Users\\65183\\Desktop\\test\\fuck\\'+str(c)+'.jpg', frame)
        c = c + 1
        cv2.waitKey(1)
    vc.release()

 

你可能感兴趣的:(python)