-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload_tvsum_utils.py
139 lines (113 loc) · 4.19 KB
/
load_tvsum_utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# _*_coding:utf-8 _*_
# Author : Tao
"""
用于加载tvsum anno数据
"""
import os
import cv2
import csv
import numpy as np
def load_tvsum(data_root_path, scale=False, ):
"""
:param data_root_path: tvsum data path
:param scale: scale to 0.0 ~ 1.0 or not
:return: dic
"""
anno_file = os.path.join(data_root_path, 'data/ydata-tvsum50-anno.tsv')
video_path = os.path.join(data_root_path, 'video')
video_name, fps, nframes, user_score, avg_score = [], [], [], [], []
with open(anno_file) as fd:
rd = csv.reader(fd, delimiter="\t", quotechar='"')
for us in rd: # user summary
if us[0] not in video_name:
video_name.append(us[0])
vidx = video_name.index(us[0])
capture = cv2.VideoCapture(
os.path.join(video_path, us[0] + '.mp4'))
# get fps
fps.append(int(capture.get(cv2.CAP_PROP_FPS)))
# get frames
nframes.append(int(capture.get(cv2.CAP_PROP_FRAME_COUNT)))
user_score.append([])
user_score[vidx].append(np.asarray(us[2].split(',')).astype(float))
for vidx in range(len(video_name)): # video key
avg_score.append(np.asarray(user_score[vidx]).mean(axis=0))
user_score[vidx] = np.asarray(user_score[vidx])
# scale to 0.0 ~ 1.0
if scale:
for i in range(len(avg_score)):
max_score = max(avg_score[i])
min_score = min(avg_score[i])
avg_s = (avg_score[i] - min_score) / (max_score - min_score)
avg_score[i] = avg_s
dic = {}
for i, video in enumerate(video_name):
dic[video] = {'fps': fps[i],
'frames': nframes[i],
'user_score': user_score[i],
'avg_score': avg_score[i]
}
return dic
def knapsack_dp(values, weights, n_items, capacity, return_all=False):
check_inputs(values, weights, n_items, capacity)
table = np.zeros((n_items+1, capacity+1), dtype=np.float32)
keep = np.zeros((n_items+1, capacity+1), dtype=np.float32)
for i in range(1, n_items+1):
for w in range(0, capacity+1):
wi = weights[i-1] # weight of current item
vi = values[i-1] # value of current item
if (wi <= w) and (vi + table[i-1, w-wi] > table[i-1, w]):
table[i, w] = vi + table[i-1, w-wi]
keep[i, w] = 1
else:
table[i, w] = table[i-1, w]
picks = []
K = capacity
for i in range(n_items, 0, -1):
if keep[i, K] == 1:
picks.append(i)
K -= weights[i-1]
picks.sort()
picks = [x-1 for x in picks] # change to 0-index
if return_all:
max_val = table[n_items, capacity]
return picks, max_val
return picks
def check_inputs(values, weights, n_items, capacity):
# check variable type
assert(isinstance(values, list))
assert(isinstance(weights, list))
assert(isinstance(n_items, int))
assert(isinstance(capacity, int))
# check value type
assert(all(isinstance(val, int) or isinstance(val, float) for val in
values))
assert(all(isinstance(val, int) for val in weights))
# check validity of value
assert(all(val >= 0 for val in weights))
assert(n_items > 0)
assert(capacity > 0)
def get_summary(score, sum_rate):
"""
:param score: score list
:param sum_rate: summary rate
:return: summary mask, one hot
"""
clip_scores = [x * 1000 for x in score] # up scale
clip_scores = [int(round(x)) for x in clip_scores]
n = len(clip_scores) # 总帧数
W = int(n * sum_rate) # summary帧总数
val = clip_scores #
wt = [1 for x in range(n)] # 全1
sum_ = knapsack_dp(val, wt, n, W)
summary = np.zeros((1), dtype=np.float32) # this element should be deleted
for seg_idx in range(n):
nf = wt[seg_idx]
if seg_idx in sum_:
tmp = np.ones((nf), dtype=np.float32)
else:
tmp = np.zeros((nf), dtype=np.float32)
summary = np.concatenate((summary, tmp))
summary = list(summary)
del summary[0]
return summary