python实现视频分帧效果

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现视频分帧的具体代码,供大家参考,具体内容如下

import cv2 
vidcap = cv2.VideoCapture('005.avi') 
success,image = vidcap.read() 
count = 0 
success = True 
while success: 
 success,image = vidcap.read() 
 cv2.imwrite("frame%d.jpg" % count, image)  # save frame as JPEG file 
 if cv2.waitKey(10) == 27:      
  break 
 count += 1 

python tools:将视频的每一帧提取并保存

# coding=utf-8 
 
import os 
import cv2 
 
videos_src_path = "/home/wgp/视频/" 
video_formats = [".MP4", ".MOV"] 
frames_save_path = "/home/wgp/视频/" 
width = 320 
height = 240 
time_interval = 50 
 
 
def video2frame(video_src_path, formats, frame_save_path, frame_width, frame_height, interval): 
 """ 
 将视频按固定间隔读取写入图片 
 :param video_src_path: 视频存放路径 
 :param formats: 包含的所有视频格式 
 :param frame_save_path: 保存路径 
 :param frame_width: 保存帧宽 
 :param frame_height: 保存帧高 
 :param interval: 保存帧间隔 
 :return: 帧图片 
 """ 
 videos = os.listdir(video_src_path) 
 
 def filter_format(x, all_formats): 
  if x[-4:] in all_formats: 
   return True 
  else: 
   return False 
 
 videos = filter(lambda x: filter_format(x, formats), videos) 
 
 for each_video in videos: 
  print "正在读取视频:", each_video 
 
  each_video_name = each_video[:-4] 
  os.mkdir(frame_save_path + each_video_name) 
  each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + "/" 
 
  each_video_full_path = os.path.join(video_src_path, each_video) 
 
  cap = cv2.VideoCapture(each_video_full_path) 
  frame_index = 0 
  frame_count = 0 
  if cap.isOpened(): 
   success = True 
  else: 
   success = False 
   print("读取失败!") 
 
  while(success): 
   success, frame = cap.read() 
   print "---> 正在读取第%d帧:" % frame_index, success 
 
   if frame_index % interval == 0: 
    resize_frame = cv2.resize(frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA) 
    # cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_index, resize_frame) 
    cv2.imwrite(each_video_save_full_path + "%d.jpg" % frame_count, resize_frame) 
    frame_count += 1 
 
   frame_index += 1 
 
 cap.release() 
 
 
if __name__ == '__main__': 
 video2frame(videos_src_path, video_formats, frames_save_path, width, height, time_interval) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中函数参数匹配模型详解

当我们的函数接收参数为任意个,或者不能确定参数个数时,我们,可以利用 * 来定义任意数目的参数,这个函数调用时,其所有不匹配的位置参数会被赋值为元组,我们可以在函数利用循环或索...

Python for Informatics 第11章 正则表达式(一)

正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描...

Python常用知识点汇总

Python常用知识点汇总

1、Set基本数据类型 a、set集合,是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set ob...

python实现二维插值的三维显示

python实现二维插值的三维显示

本文实例为大家分享了二维插值的三维显示具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- """ 演示二维插值。 """ # -*- coding:...

python通过apply使用元祖和列表调用函数实例

本文实例讲述了python通过apply使用元祖和列表调用函数的方法。分享给大家供大家参考。具体实现方法如下: def my_fuc(a, b): print a, b atup...