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

yipeiwu_com5年前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实现将视频按帧读取到自定义目录就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 数据处理库 pandas 入门教程基本操作

Python 数据处理库 pandas 入门教程基本操作

pandas是一个Python语言的软件包,在我们使用Python语言进行机器学习编程的时候,这是一个非常常用的基础编程库。本文是对它的一个入门教程。 pandas提供了快速,灵活和富有...

Python中用Decorator来简化元编程的教程

少劳多得 Decorator 与 Python 之前引入的元编程抽象有着某些共同之处:即使没有这些技术,您也一样可以实现它们所提供的功能。正如 Michele Simionato 和我在...

Django处理多用户类型的方法介绍

Django处理多用户类型的方法介绍

起步 这是许多开发者在项目初期要面临的一个普遍问题。要怎样来处理多用户类型。 本文讲介绍对于不同场景和业务需求如何设计用户模型。为项目提供指导设计。 设计之前 在梳理用户设计之前,有...

安装Pycharm2019以及配置anconda教程的方法步骤

安装Pycharm2019以及配置anconda教程的方法步骤

一、获取安装包: Pycharm 官网 下载页面 :点击打开 Anconda 官网 下载页面 :点击打开 选择对应的系统和需要的版本进行下载,pycharm 分为付费专业版和社区免...

Python连接SQLServer2000的方法详解

本文实例讲述了Python连接SQLServer2000的方法。分享给大家供大家参考,具体如下: http://pymssql.sourceforge.net/  介绍PYTH...