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

yipeiwu_com6年前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自动发送测试报告邮件功能的实现

python自动发送测试报告邮件功能的实现

自动化发邮件功能也是自动化测试项目中的重要需求之一。在自动化脚本运行完成之后,邮箱就可以收到最新的测试报告结果,把这种主动的且不及时的查看变成被动且及时的查收,就方便多了。 首先我们需要...

Python3实现购物车功能

Python3实现购物车功能

本文实例为大家分享了Python3实现购物车功能的具体代码,供大家参考,具体内容如下 购物车要求: 1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表...

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换) Python3 JSON 数据解析...

python Pillow图像处理方法汇总

这篇文章主要介绍了python Pillow图像处理方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Pillow中文文档:ht...

使用Python和Prometheus跟踪天气的使用方法

开源监控系统 Prometheus 集成了跟踪多种类型的时间序列数据,但如果没有集成你想要的数据,那么很容易构建一个。一个经常使用的例子使用云端提供商的自定义集成,它使用提供商的 API...