-
Notifications
You must be signed in to change notification settings - Fork 5
/
SpeedupVideo.m
executable file
·207 lines (176 loc) · 10.6 KB
/
SpeedupVideo.m
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This file is part of SemanticFastForward_JVCI.
%
% SemanticFastForward_JVCI is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SemanticFastForward_JVCI is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SemanticFastForward_JVCI. If not, see <http://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create a fast forward video based on the arguments
%
% video_dir -> Video Directory
% experiment -> Experiment Name
% semantic_extractor -> The extractor (face, pedestrian, coolnet,...)
%
% Other arguments (see function parser)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function SpeedupVideo(video_dir, experiment, semantic_extractor, varargin)
close all
warning ('off', 'images:initSize:adjustingMag');
addpath(genpath(pwd));
%% Parsing input parameters
p = inputParser;
validSemanticExtractor = {'none', 'face', 'pedestrian', 'coolnet'};
checkSemanticExtractor = @(x) any(validatestring(x,validSemanticExtractor));
addRequired(p,'semantic_extractor', checkSemanticExtractor);
addOptional(p, 'SaveGeneralResults', true, @islogical);
addOptional(p, 'ExportOutputVideo', true, @islogical);
addOptional(p, 'ShakinessWeights', [0, 0], @isrow);
addOptional(p, 'VelocityWeights', [0, 0], @isrow);
addOptional(p, 'AppearanceWeights', [0, 0], @isrow);
addOptional(p, 'SemanticWeights', [0, 0], @isrow);
addOptional(p, 'ForwardnessWeights', [0, 0], @isrow);
addOptional(p, 'HigherOrderWeights', [0, 0], @isrow);
addOptional(p, 'Speedup', 10, @isnumeric);
validAlgorithm = {'Naive', 'NaiveSemantic', 'EgoSampling', 'FFSE'};
checkAlgorithm = @(x) any(validatestring(x,validAlgorithm));
addOptional(p, 'Algorithm', 'FFSE', checkAlgorithm); %'Naive', 'NaiveSemantic', 'EgoSampling', 'FFSE'
parse(p, semantic_extractor, varargin{:});
%% Assignments
save_general_results_to_file = p.Results.SaveGeneralResults;
skip_video_output = ~p.Results.ExportOutputVideo;
shakiness_weights = p.Results.ShakinessWeights;
velocity_weights = p.Results.VelocityWeights;
appearance_weights = p.Results.AppearanceWeights;
semantic_weights = p.Results.SemanticWeights;
forwardness_weights = p.Results.ForwardnessWeights;
high_order_weights = p.Results.HigherOrderWeights;
required_speedup = p.Results.Speedup;
algorithm = p.Results.Algorithm;
%% Defining the optimization characteristic
optimize = strcmp(algorithm, 'FFSE') && (isempty(nonzeros(shakiness_weights)) && isempty(nonzeros(velocity_weights)) &&...
isempty(nonzeros(appearance_weights)) && isempty(nonzeros(semantic_weights)) &&...
isempty(nonzeros(forwardness_weights)) && isempty(nonzeros(high_order_weights)));
%% Getting the experiment details
fprintf('%sGetting ''%s'' details...\n', log_line_prefix, experiment);
cfg = SemanticSequenceLibrary.GetFFExperimentDetails(video_dir, experiment, algorithm, required_speedup, skip_video_output);
startInd = cfg.get('startInd');
endInd = cfg.get('endInd');
[~, fname, ~] = fileparts(cfg.get('inputVideoFileName'));
% Load the video .mat file
fpstereo = [video_dir '/' fname 'fpstereo.mat'];
if ~exist(fpstereo, 'file')
fprintf('%sPreprocess file not found...\n', log_line_prefix);
optical_flow_csv = [video_dir '/' fname '.csv'];
if exist(optical_flow_csv, 'file') == 0
fprintf('%sOptical Flow CSV file not found...\n', log_line_prefix);
error('Please run Vid2OpticalFlowCSV first!');
end
fprintf('%sPreprocessing optical flow data...\n', log_line_prefix);
Util.PrepreocessSequences(cfg.get('inputVideoFileName'), '');
fprintf('%sDone preprocessing optical flow data...\n', log_line_prefix);
end
sd = Util.LoadVidDataFromMat(cfg.get('inputVideoFileName'),'fpstereo','returnonly');
% End index treatment
if endInd > size(sd.CDC_Raw_X,1) + sd.StartFrame-1
endInd = size(sd.CDC_Raw_X,1) + sd.StartFrame-1;
end
if save_general_results_to_file
general_results = [video_dir '/out/' fname '_GeneralResults.csv'];
if ( exist(general_results, 'file') == 2 )
results_csv = fopen(general_results, 'a');
else
if ~exist([video_dir '/out/'], 'dir')
mkdir(video_dir, 'out')
end
[results_csv, msg] = fopen(general_results, 'a');
if (results_csv < 0)
error('Could not open/create file.\nReason: %s', msg);
end
fprintf(results_csv, 'Experiment ID,Algorithm,Required Speedup,Achieved Speedup,Alphas,Betas,Gammas,Etas,Deltas,Zetas,# Selected Frames,# Faces in Selected Frames,Avg. Skip Frames,Median Skip Frames,Faces Value,Jitter,Instability\n');
end
end
%% Satisfying the semantic requirements
semantic_filename = [video_dir '/' fname '_' semantic_extractor '_extracted.mat'];
fprintf('%sAttaching the semantic extraction information...\n', log_line_prefix);
if exist(semantic_filename, 'file')
load(semantic_filename);
SemanticData = Rects;
clear Rects;
clear total_values;
else
error('Please, run ExtractAndSave with the extractor ''%s'' to extract the semantic information first!', semantic_extractor);
end
if strcmp(algorithm, 'FFSE')%Only our algorithm requires speedup calculation
fprintf('%sCalculating speedup rates...\n', log_line_prefix);
[Speedups, SemanticRanges, RangesAndSpeedups, err] = CalculateSpeedupRates(semantic_filename, required_speedup,...
'MultipleThresholds', cfg.get('UseMultipleSemanticThresholds'), 'InputRange', [startInd endInd]);
[RangesAndSpeedups] = AddNonSemanticRanges(RangesAndSpeedups, Speedups, startInd, endInd, 50);
fprintf('-----------------------------------------------------------------------------------------------\n');
fprintf('%sChosen ranges and speedups:\n', log_line_prefix);
for i=1:size(RangesAndSpeedups,2)
fprintf('%sRange[%d-%d]\t@ (%dx)\t-- Length(%d)\t-- Semantic? %d\n', log_line_prefix,...
RangesAndSpeedups(1,i), RangesAndSpeedups(2,i),RangesAndSpeedups(3,i),...
RangesAndSpeedups(2,i)-RangesAndSpeedups(1,i),RangesAndSpeedups(4,i));
end
fprintf('-----------------------------------------------------------------------------------------------\n');
fprintf('\n');
cfg.set('Speedups', Speedups);
cfg.set('SemanticRanges', SemanticRanges);
cfg.set('RangesAndSpeedups', RangesAndSpeedups);
else
RangesAndSpeedups = [startInd; endInd; required_speedup; 0];
cfg.set('RangesAndSpeedups', RangesAndSpeedups);
end
%% More assignments
cfg.set('ShakinessTermWeight', [shakiness_weights(1), shakiness_weights(2)]);
cfg.set('VelocityTermWeight', [velocity_weights(1), velocity_weights(2)]);
cfg.set('AppearanceTermWeight', [appearance_weights(1), appearance_weights(2)]);
cfg.set('SemanticTermWeight', [semantic_weights(1), semantic_weights(2)]);
cfg.set('ForwardnessTermWeight', [forwardness_weights(1), forwardness_weights(2)]);
cfg.set('HighOrderTermWeight', [high_order_weights(1), high_order_weights(2)]);
se = SemanticFastForward(sd,cfg, SemanticData, strcmp(algorithm, 'FFSE'),optimize);
fprintf('%sRunning Experiment...\n', log_line_prefix);
se.run();
shakiness_weights = cfg.get('ShakinessTermWeight');
velocity_weights = cfg.get('VelocityTermWeight');
appearance_weights = cfg.get('AppearanceTermWeight');
semantic_weights = cfg.get('SemanticTermWeight');
forwardness_weights = cfg.get('ForwardnessTermWeight');
high_order_weights = cfg.get('HighOrderTermWeight');
if save_general_results_to_file
if cfg.get('UseHigherOrder')
fprintf(results_csv, '%d,%s,%d,%.3f,[%.3f %.3f],[%.3f %.3f],[%.3f %.3f],[%.3f %.3f],[%.3f %.3f],[%.3f %.3f], %d, %d, %.3f, %.3f, %.4f, %.4f, %.4f\n',...
cfg.get('ID'),algorithm,required_speedup,se.ResultsMetaData.achieved_speedup,shakiness_weights(1),shakiness_weights(2),velocity_weights(1),velocity_weights(2),appearance_weights(1),appearance_weights(2),...
semantic_weights(1),semantic_weights(2),forwardness_weights(1),forwardness_weights(2),high_order_weights(1),high_order_weights(2),...
size(se.ResultsMetaData.frames,1), se.ResultsMetaData.total_semantic_in_frames, se.ResultsMetaData.avarage_skip,...
se.ResultsMetaData.median_skip, se.ResultsMetaData.semantic_value, se.ResultsMetaData.jitter, se.ResultsMetaData.instability);
else
fprintf(results_csv, '%d,%s,%d,%.3f,[%.3f %.3f],[%.3f %.3f],[%.3f %.3f],[%.3f %.3f],[x x],[x x],%d, %d, %.3f, %.3f, %.4f, %.4f, %.4f\n',...
cfg.get('ID'),algorithm,required_speedup,se.ResultsMetaData.achieved_speedup,shakiness_weights(1),shakiness_weights(2),velocity_weights(1),velocity_weights(2),appearance_weights(1),appearance_weights(2),...
semantic_weights(1),semantic_weights(2), size(se.ResultsMetaData.frames,1), se.ResultsMetaData.total_semantic_in_frames, se.ResultsMetaData.avarage_skip,...
se.ResultsMetaData.median_skip, se.ResultsMetaData.semantic_value, se.ResultsMetaData.jitter, se.ResultsMetaData.instability);
end
end
% Creating semantic cost csv for stabilizer
video_speedup = cfg.get('FastForwardSkipRatio');
cost_path = [video_dir '/' experiment '_SemanticCosts_' num2str(video_speedup) 'x.csv'];
if exist(cost_path, 'file') == 0
csvwrite(cost_path, startInd);
dlmwrite(cost_path, se.Semantic_cost, 'delimiter' , ',' , '-append');
end
fprintf('%sDone! Experiment #%d finished\n',log_line_prefix,cfg.get('ID'));
if save_general_results_to_file
fclose(results_csv);
end
end