脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python OpenCV实现视频分帧

Python OpenCV实现视频分帧

2021-07-01 00:23等一杯咖啡 Python

这篇文章主要为大家详细介绍了Python OpenCV实现视频分帧,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/bskfnvjtlyzmv867/article/details/79970146

延伸 · 阅读

精彩推荐