Source(侵删)
"""
Created on Tue Dec 4 16:48:57 2018
keyframes extract tool
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.
The original code comes from the link below, I optimized the code to reduce
unnecessary memory consumption.
https://blog.csdn.net/qq_21997625/article/details/81285096
@author: zyb_as
"""
import cv2
import operator
import subprocess
import numpy as np
# import matplotlib.pyplot as plt
import sys, os
from scipy.signal import argrelextrema
from musecheck import logger_error, logger_info
from musecheck.resource.conf import MUSECHECK_BASE_DIR
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError as e:
logger_error(f'matplotlib,尝试执行 pip install matplotlib')
matplotlib_path = os.path.join(MUSECHECK_BASE_DIR, 'libs', 'matplotlib-3.3.2-cp37-cp37m-manylinux1_x86_64.whl')
try:
subprocess.Popen(f'pip install matplotlib')
logger_info(f'success pip install matplotlib_path')
except Exception as e:
logger_error(f'装置依赖 matplotlib_path 出错,尝试 pip install {matplotlib_path}, ErrorInfo:{e}')
cycler_path = os.path.join(MUSECHECK_BASE_DIR, 'libs', 'cycler-0.10.0-py2.py3-none-any.whl')
certifi_path = os.path.join(MUSECHECK_BASE_DIR, 'libs', 'certifi-2020.6.20-py2.py3-none-any.whl')
kiwisolver_path = os.path.join(MUSECHECK_BASE_DIR, 'libs', 'kiwisolver-1.3.1-cp37-cp37m-manylinux1_x86_64.whl')
Pillow_path = os.path.join(MUSECHECK_BASE_DIR, 'libs', 'Pillow-8.0.1-cp37-cp37m-manylinux1_x86_64.whl')
try:
subprocess.Popen(f'pip install {cycler_path}')
subprocess.Popen(f'pip install {certifi_path}')
subprocess.Popen(f'pip install {kiwisolver_path}')
subprocess.Popen(f'pip install {Pillow_path}')
subprocess.Popen(f'pip install {matplotlib_path}')
logger_info(f'success pip install {matplotlib_path}')
except Exception as e:
logger_error(f'装置依赖 matplotlib 出错,请分割【youngzhang】, ErrorInfo:{e}')
def smooth(x, window_len=13, window='hanning'):
"""smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
import numpy as np
t = np.linspace(-2,2,0.1)
x = np.sin(t)+np.random.randn(len(t))*0.1
y = smooth(x)
see also:
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter
TODO: the window parameter could be the window itself if an array instead of a string
"""
print(len(x), window_len)
# if x.ndim != 1:
# raise ValueError, "smooth only accepts 1 dimension arrays."
#
# if x.size < window_len:
# raise ValueError, "Input vector needs to be bigger than window size."
#
# if window_len < 3:
# return x
#
# if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
# raise ValueError, "Window is on of'flat','hanning','hamming','bartlett','blackman'"
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]
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 extract_key_frame(video_path, key_frame_save_path):
# print(sys.executable)
# Setting fixed threshold criteria
USE_THRESH = False
# fixed threshold value
THRESH = 0.8
# Setting fixed threshold criteria
USE_TOP_ORDER = False
# Setting local maxima criteria
USE_LOCAL_MAXIMA = True
# Number of top sorted frames
NUM_TOP_FRAMES = 50
# Video path of the source file
video_path = video_path
# Directory to store the processed frames
save_path = key_frame_save_path
# dir = f'./key_frame_pic/{os.path.basename(videopath).split(".")[0]}/'
# smoothing window size
len_window = int(50)
print("target video :" + video_path)
print("frame save directory:" + save_path)
if not os.path.exists(save_path):
os.makedirs(save_path)
# load video and compute diff between frames
cap = cv2.VideoCapture(str(video_path))
curr_frame = None
prev_frame = None
frame_diffs = []
frames = []
success, frame = cap.read()
i = 0
while (success):
luv = cv2.cvtColor(frame, cv2.COLOR_BGR2LUV)
curr_frame = luv
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)
frame = Frame(i, diff_sum_mean)
frames.append(frame)
prev_frame = curr_frame
i = i + 1
success, frame = cap.read()
cap.release()
# compute keyframe
keyframe_id_set = set()
if USE_TOP_ORDER:
print("Using 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)
if 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)
if USE_LOCAL_MAXIMA:
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(tight=100)
plt.stem(sm_diff_array)
plt.savefig(save_path + 'plot.png')
# 要记得 close(), 否则会内存泄露
plt.close()
# save all keyframes as image
cap = cv2.VideoCapture(str(video_path))
curr_frame = None
keyframes = []
success, frame = cap.read()
idx = 0
key_frame_list = []
while (success):
if idx in keyframe_id_set:
name = str(idx) + ".jpg"
cv2.imwrite(save_path + name, frame)
key_frame_list.append(idx)
keyframe_id_set.remove(idx)
idx = idx + 1
success, frame = cap.read()
cap.release()
return key_frame_list