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

yipeiwu_com7年前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获取基金网站网页内容、使用BeautifulSoup库分析html操作示例

本文实例讲述了Python获取基金网站网页内容、使用BeautifulSoup库分析html操作。分享给大家供大家参考,具体如下: 利用 urllib包 获取网页内容 #引入包 fr...

Python散点图与折线图绘制过程解析

Python散点图与折线图绘制过程解析

这篇文章主要介绍了Python散点图与折线图绘制过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在数据分析的过程中,经常需要将...

对python 命令的-u参数详解

缘起: 今天在看arcface的训练代码,在shell脚本中运行python 命令时后面加了-u 参数(python -u xx.py),于是对这个参数进行了下小研究。 准备知识 用网上...

Python发送Email方法实例

本文以实例形式展示了Python发送Email功能的实现方法,有不错的实用价值的技巧,且功能较为完善。具体实现方法如下: 主要功能代码如下: #/usr/bin/env python...

python导出hive数据表的schema实例代码

本文研究的主要问题是python语言导出hive数据表的schema,分享了实现代码,具体如下。 为了避免运营提出无穷无尽的查询需求,我们决定将有查询价值的数据从mysql导入hive中...