-
Notifications
You must be signed in to change notification settings - Fork 2
/
saliency_update.py
210 lines (138 loc) · 6.7 KB
/
saliency_update.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'''
MIT License
Copyright (c) [2020] [Duin BAEK]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import json
import ast
import numpy as np
import os
def deviation_function(input_x):
return 2*input_x*np.heaviside(input_x, 1) - 2*input_x*np.heaviside(input_x - 150, 1) + 300*np.heaviside(input_x - 150, 1)
def distance_from_current_progress(json_values, current_deviation):
distance_weight_list = []
half_side = int(512/2)
max_distance = 1280
for dictionary_data in json_values:
#here 1024 is the length of one cube subframe side
if dictionary_data['name'] == 'R':
distance = abs(current_deviation - (int(dictionary_data['column']) + half_side))
elif dictionary_data['name'] == 'L':
distance = abs(current_deviation - (1024 - (int(dictionary_data['column']) + half_side)))
elif dictionary_data['name'] == 'U':
distance = 1024 - (int(dictionary_data['row']) + 512)
elif dictionary_data['name'] == 'D':
distance = int(dictionary_data['row'])
else:
distance = abs(current_deviation - (1024 + min([int(dictionary_data['column']) + half_side, 1024 - (int(dictionary_data['column']) + half_side)])))
distance_weight = np.exp(-2*(distance/max_distance))
if dictionary_data['name'] in ['U', 'D']:
distance_weight /= 2
distance_weight_list.append(distance_weight)
return np.array(distance_weight_list)
def current_deviation_progress(frame_idx, fps, duration_unit):
current_progress = frame_idx % (fps*int(duration_unit))
return deviation_function(current_progress)
def distance_weight(json_values, frame_idx, fps, duration_unit):
current_deviation = current_deviation_progress(frame_idx, fps, duration_unit)
#print(current_deviation)
distance_weight_array = distance_from_current_progress(json_values, current_deviation)#distance from the current_deviation
return distance_weight_array
def neighbors(json_values):
idx = 0
neighbor_dict = {}
updated_json_values = []
hit = 0
while(idx < len(json_values)):
if 0 < int(json_values[idx]['width']) < 256:
if json_values[idx]['name'] == 'B':
neighbor_dict[str(idx-hit)] = json_values[idx]
updated_json_values.append(json_values[(idx+1)])
else:
neighbor_dict[str(idx-hit)] = json_values[idx+1]
updated_json_values.append(json_values[(idx)])
idx += 2
hit += 1
else:
updated_json_values.append(json_values[(idx)])
idx += 1
return neighbor_dict, np.array(updated_json_values)
def saliency_score_update(json_path, frame_idx, fps, duration_unit):
with open(json_path) as json_file:
json_data = json.load(json_file)
json_data = ast.literal_eval(json_data)
json_keys = np.array(list(json_data.keys()))
json_values = np.array(list(json_data.values()))
sorted_json_idx = np.argsort([int(key) for key in json_keys])
json_keys = json_keys[sorted_json_idx]
json_values = json_values[sorted_json_idx]
neighbor_dict, json_values = neighbors(json_values)
#print(json_values)
#print(neighbor_dict)
distance_weight_array = distance_weight(json_values, frame_idx, fps, duration_unit)
saliency_score = []
#print('saliency score, name, width, column, distance')
for json_value in json_values:
#print(json_value['saliency'], json_value['name'], json_value['width'], json_value['column'], distance)
saliency_score.append(float(json_value['saliency']))
saliency_score = np.array(saliency_score)
updated_saliency_score = saliency_score*distance_weight_array
updated_idx = updated_saliency_score.argsort()[::-1]
updated_json_values = json_values[updated_idx]
#print(updated_idx)
#print('update!')
updated_json_value_list = []
neighbor_keys = np.array(list(neighbor_dict.keys()))
for idx, updated_values in zip(updated_idx, updated_json_values):
updated_json_value_list.append(updated_values)
#print(updated_values)
if str(idx) in neighbor_keys:
updated_json_value_list.append(neighbor_dict[str(idx)])
updated_json_values = np.array(updated_json_value_list)
#print(updated_json_values)
#for key, value in neighbor_dict.items():
# idx = np.where(updated_idx == int(key))[0][0]
# #print(idx)
# updated_json_values = np.insert(updated_json_values, idx+1, value)
#print('saliency score, name, width, column')
#for updated_json_value, updated_saliency in zip(updated_json_values, updated_saliency_score[updated_saliency_score.argsort()[::-1]]):
updated_json_data = {}
for json_key, updated_json_value in zip(json_keys, updated_json_values):
#print(json_key, updated_json_value['name'], updated_json_value['width'], updated_json_value['column'])
#print(frame_idx, json_key, updated_json_value)
updated_json_data[json_key] = updated_json_value
return updated_json_data
def json_sort_by_name(json_base_path):
json_file_list = np.array(os.listdir(json_base_path))
idx_list = [int(json_file.split('.')[0].split('_')[-1]) for json_file in json_file_list]
sorted_idx_list = np.argsort(idx_list)
sorted_json_file_list = json_file_list[sorted_idx_list]
return sorted_json_file_list
'''
frame_idx = 0
fps = 30
duration_unit = 5
json_base_path = 'Y0P0/'
sorted_json_file_list = json_sort_by_name(json_base_path)
for json_file in sorted_json_file_list:
json_path = os.path.join(json_base_path, json_file)
updated_json_data = saliency_score_update(json_path, frame_idx, fps, duration_unit)
print(updated_json_data.values())
frame_idx += 1
#print(updated_json_data)
'''
#end