当前位置:网站首页>视频提取关键帧工具类KeyFramesExtractUtils.py,动态支持三种取帧方式,关键参数可配置,代码经过优化处理,效果和性能更好。
视频提取关键帧工具类KeyFramesExtractUtils.py,动态支持三种取帧方式,关键参数可配置,代码经过优化处理,效果和性能更好。
2022-07-21 19:49:00 【君临天下tjm】
同步上一期视频关键帧提取方法:
项目中可以直接导入该工具类KeyFramesExtractUtils.py,三种取帧方式通过参数选择,method可以取"use_top_order"、"use_thresh"、"use_local_maxima"中的任何一种,第三种方式效果最佳,默认也是"use_local_maxima"。
优化点:
key_frame = 10 # 所隔帧数 cap.set(cv2.CAP_PROP_POS_FRAMES, j) # 这里的cv2.CAP_PROP_POS_FRAMES参数是说:取第j帧之后的那一帧 j += key_frame ...... gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
视频提取关键帧工具类KeyFramesExtractUtils.py完整代码如下:
# -*- coding: utf-8 -*-
"""
this key frame extract algorithm is based on interframe difference.
The principle is very simple
First, we load the video and compute the interframe difference between each frames
Then, we can choose one of these three methods to extract keyframes, which are
all based on the difference method:
1. use the difference order
The first few frames with the largest average interframe difference
are considered to be key frames.
2. use the difference threshold
The frames which the average interframe difference are large than the
threshold are considered to be key frames.
3. use local maximum
The frames which the average interframe difference are local maximum are
considered to be key frames.
It should be noted that smoothing the average difference value before
calculating the local maximum can effectively remove noise to avoid
repeated extraction of frames of similar scenes.
After a few experiment, the third method has a better key frame extraction effect.
"""
import cv2
import operator
import numpy as np
import matplotlib.pyplot as plt
import sys
import time
from scipy.signal import argrelextrema
# USE_LOCAL_MAXIMA=True,效果还行,时间有点长,54张图,包括一张差异趋势图
# 使用cv2.CAP_PROP_POS_FRAMES间隔取帧,主要消耗CPU,效率比内存高,效果更好
# 视频提取关键帧工具类
class KeyFramesExtractUtils:
# 初始化
def __init__(self, video_path=None, save_path=None):
self.video_path = video_path
self.save_path = save_path
# 提取关键帧
def extract_keyframe(self, method="use_local_maxima"):
print(sys.executable)
print("method===>", method)
# fixed threshold value
thresh = 0.6
# Number of top sorted frames
num_top_frames = 50
# smoothing window size
len_window = int(50)
print("target video: " + self.video_path)
print("frame save directory: " + self.save_path)
# load video and compute diff between frames
cap = cv2.VideoCapture(str(self.video_path))
curr_frame = None
prev_frame = None
frame_diffs = []
frames = []
video_frames = []
key_frame = 10 # 所隔帧数
t0_start = time.time()
k = 0
j = 0
while True:
if key_frame >= 1:
t1_start = time.time()
print("j======>", j)
cap.set(cv2.CAP_PROP_POS_FRAMES, j) # 这里的cv2.CAP_PROP_POS_FRAMES参数是说:取第j帧之后的那一帧
j += key_frame
success, frame = cap.read()
if not success:
print("第一次视频帧读取完毕!")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
curr_frame = gray
if curr_frame is not None and prev_frame is not None:
# logic here
diff = cv2.absdiff(curr_frame, prev_frame)
diff_sum = np.sum(diff)
diff_sum_mean = diff_sum / (diff.shape[0] * diff.shape[1])
frame_diffs.append(diff_sum_mean)
temp_frame = Frame(k, diff_sum_mean)
frames.append(temp_frame)
video_frames.append(frame)
k = k + 1
prev_frame = curr_frame
t1_end = time.time()
print("计算一次差分耗时:", (t1_end - t1_start), "s")
cap.release()
t0_end = time.time()
print("计算共耗时:", (t0_end - t0_start), "s")
t_start = time.time()
# compute keyframe
keyframe_id_set = set()
if method == "use_top_order":
print("---------------Using use_top_order---------------")
# sort the list in descending order
frames.sort(key=operator.attrgetter("diff"), reverse=True)
for keyframe in frames[:num_top_frames]:
keyframe_id_set.add(keyframe.id)
elif method == "use_thresh":
print("---------------Using Threshold---------------")
for i in range(1, len(frames)):
if rel_change(np.float(frames[i - 1].diff), np.float(frames[i].diff)) >= thresh:
keyframe_id_set.add(frames[i].id)
else:
print("---------------Using Local Maxima---------------")
diff_array = np.array(frame_diffs)
sm_diff_array = smooth(diff_array, len_window)
frame_indexes = np.asarray(argrelextrema(sm_diff_array, np.greater))[0]
for i in frame_indexes:
keyframe_id_set.add(frames[i - 1].id)
plt.figure(figsize=(40, 20))
plt.locator_params(numticks=100)
plt.stem(sm_diff_array)
plt.savefig(self.save_path + 'plot.png')
for idx in keyframe_id_set:
name = "keyframe_" + str(idx) + ".jpg"
cv2.imwrite(self.save_path + name, video_frames[idx])
t_end = time.time()
print("取帧共耗时:", (t_end - t_start), "s")
class Frame:
"""class to hold information about each frame
"""
def __init__(self, id, diff):
self.id = id
self.diff = diff
def __lt__(self, other):
if self.id == other.id:
return self.id < other.id
return self.id < other.id
def __gt__(self, other):
return other.__lt__(self)
def __eq__(self, other):
return self.id == other.id and self.id == other.id
def __ne__(self, other):
return not self.__eq__(other)
def rel_change(a, b):
x = (b - a) / max(a, b)
print(x)
return x
def smooth(x, window_len=13, window='hanning'):
"""smooth the data using a window with requested size.
"""
print(len(x), window_len)
s = np.r_[2 * x[0] - x[window_len:1:-1],
x, 2 * x[-1] - x[-1:-window_len:-1]]
# print(len(s))
if window == 'flat': # moving average
w = np.ones(window_len, 'd')
else:
w = getattr(np, window)(window_len)
y = np.convolve(w / w.sum(), s, mode='same')
return y[window_len - 1:-window_len + 1]
if __name__ == "__main__":
keyFrame = KeyFramesExtractUtils(video_path="e956ed44fffe4bfb97cf23474fb48ef3.avi", save_path="./extract_result/")
keyFrame.extract_keyframe(method="use_local_maxima")
# 计算共耗时: 307.6547501087189 s
# 取帧共耗时: 22.484756231307983 s
提取的关键帧如下:
检测结果总结:视频帧的提取率约为1.66‰,21m15s的视频,耗时约330s(即5分30秒)。
边栏推荐
- 2022 音视频技术风向标
- 如何获得 Apache 官方域名邮箱?专访 Apache Linkis 五位新晋 Committer
- 通达信上开户安全吗?
- 2022 latest Hubei construction eight members (Mechanics) simulated examination question bank and answers
- Request and response description
- 2022.7.21 特殊矩阵压缩
- Request 和Response 说明
- Live broadcast preview | new design ideas and production methods brought by NFT, aigc/ugc tools and metauniverse products
- Dota2参议院[贪心与队列]
- MySQL:字符集与比较规则
猜你喜欢
pango logos 双启动
How to do the interface test in JSON format?
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
加载RainCitySpaces的标签信息并进行显示
The latest Hubei construction special worker (construction elevator) simulation question bank and answers in 2022
unordered_map的使用
第十六篇,STM32中的ADC模数转换,CAD数模转换和DMA直接内存访问
【Unity3D】血条(HP)
【活动报名】给你的代码叠个 Buff!点击“茶”收好礼
基于ROS的机器人模型建立及3D仿真【物理/机械意义】
随机推荐
2022.7.21 特殊矩阵压缩
How to change the console font to console?
New product release: VGA display driver module for bus video monitoring
BigDecimal精度的哪些坑
Halcon series (2): hyperboxes
面试三(多进程调用同一个动态库问题)
控制台字体怎么改为console?
华泰证券网上开户安全吗,VIP账户也可以办理吗
cron表达式详解
Etcdv3 practice · common operation model encapsulation and detailed implementation
面试复盘四
Request 和Response 说明
Response response byte data
Energy principle and variational method note 02: variational problems variational and differential operations can exchange ordered Euler equations
Servlet Chapter 1
Summary of five methods of window function
女性健康养生资讯网类织梦模板(带手机端)【测试可搭建】
leetcode 92. Reverse Linked List II(链表逆序II)
C#实现汉字转拼音
Servlet optimization