PythonOpenCV实现视频分帧-创新互联

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

目前创新互联已为千余家的企业提供了网站建设、域名、网页空间、网站运营、企业网站设计、刚察网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
# 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)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:PythonOpenCV实现视频分帧-创新互联
当前网址:http://myzitong.com/article/csojoh.html