-
Notifications
You must be signed in to change notification settings - Fork 31
/
generate_summary.py
57 lines (47 loc) · 2.76 KB
/
generate_summary.py
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
# -*- coding: utf-8 -*-
import numpy as np
from knapsack_implementation import knapSack
def generate_summary(all_shot_bound, all_scores, all_nframes, all_positions):
""" Generate the automatic machine summary, based on the video shots; the frame importance scores; the number of
frames in the original video and the position of the sub-sampled frames of the original video.
:param list[np.ndarray] all_shot_bound: The video shots for all the -original- testing videos.
:param list[np.ndarray] all_scores: The calculated frame importance scores for all the sub-sampled testing videos.
:param list[np.ndarray] all_nframes: The number of frames for all the -original- testing videos.
:param list[np.ndarray] all_positions: The position of the sub-sampled frames for all the -original- testing videos.
:return: A list containing the indices of the selected frames for all the -original- testing videos.
"""
all_summaries = []
for video_index in range(len(all_scores)):
# Get shots' boundaries
shot_bound = all_shot_bound[video_index] # [number_of_shots, 2]
frame_init_scores = all_scores[video_index]
n_frames = all_nframes[video_index]
positions = all_positions[video_index]
# Compute the importance scores for the initial frame sequence (not the sub-sampled one)
frame_scores = np.zeros(n_frames, dtype=np.float32)
if positions.dtype != int:
positions = positions.astype(np.int32)
if positions[-1] != n_frames:
positions = np.concatenate([positions, [n_frames]])
for i in range(len(positions) - 1):
pos_left, pos_right = positions[i], positions[i + 1]
if i == len(frame_init_scores):
frame_scores[pos_left:pos_right] = 0
else:
frame_scores[pos_left:pos_right] = frame_init_scores[i]
# Compute shot-level importance scores by taking the average importance scores of all frames in the shot
shot_imp_scores = []
shot_lengths = []
for shot in shot_bound:
shot_lengths.append(shot[1] - shot[0] + 1)
shot_imp_scores.append((frame_scores[shot[0]:shot[1] + 1].mean()).item())
# Select the best shots using the knapsack implementation
final_shot = shot_bound[-1]
final_max_length = int((final_shot[1] + 1) * 0.15)
selected = knapSack(final_max_length, shot_lengths, shot_imp_scores, len(shot_lengths))
# Select all frames from each selected shot (by setting their value in the summary vector to 1)
summary = np.zeros(final_shot[1] + 1, dtype=np.int8)
for shot in selected:
summary[shot_bound[shot][0]:shot_bound[shot][1] + 1] = 1
all_summaries.append(summary)
return all_summaries