-
Notifications
You must be signed in to change notification settings - Fork 5
/
qpsnr.rb
executable file
·291 lines (258 loc) · 9.06 KB
/
qpsnr.rb
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env ruby1.9.1
# encoding: UTF-8
DATA=<<-END
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{{SOURCE}}</title>
<script language="javascript" type="text/javascript" src="./scripts/jquery.js"></script>
<script language="javascript" type="text/javascript" src="./scripts/jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="./scripts/jquery.flot.crosshair.js"></script>
<style type="text/css">
#placeholder {
width: 1700px;
height: 300px;
}
#imgcontainer {
width: 90%;
margin-left: auto;
margin-right: auto;
}
#original {
background: grey;
float: left;
margin-left: auto;
margin-right: 2px;
width: 40%;
}
#original img {
width:100%;
}
#original_name {
width: 100%;
margin-left: auto;
margin-right: auto;
}
#encoded {
background: grey;
float: left;
margin-right: auto;
margin-left: 2px;
width: 40%;
}
#encoded img {
width:100%;
}
#encoded_name {
width: 100%;
margin-left: auto;
margin-right: auto;
}
#controls {
clear: both;
width: 30%;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="imgcontainer">
<div id="original">
<img id="original_img" />
<div id="original_name">{SOURCE}</div>
</div>
<div id="encoded">
<img id="encoded_img" />
<div id="encoded_name" />
</div>
</div>
<div id="controls">
</div>
<div id="placeholder"></div>
<script type="text/javascript">
$(function() {
var current_ssim = {VALUE};
var current_frame = {FRAME};
var current_series = 0;
var max_frame = {MAX};
var min_frame = {MIN};
var series = [ {ITEMS} ];
function zeroFill(number, width) {
width -= number.toString().length;
if ( width > 0 ) {
return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
}
return number + "";
}
var plot = $.plot($("#placeholder"),
[ {DATA} ], {
series: {
lines: { show: true, fill: false },
points: { show: true, fill: false }
},
grid: { hoverable: true, clickable: true },
crosshair: { mode: "x" }
});
var data = plot.getData();
function getSSIM(frame, series, data) {
var s = data[series];
for(j = 0; j < s.data.length; j++) {
if(s.data[j][0] >= frame) {
return s.data[j][1].toFixed(6);
}
}
}
function page(frame, serie, ssim) {
$("#original_img").attr("src", '{BASENAME}/{SOURCE}' + '.' + zeroFill(frame, 7) + '.jpeg');
$("#encoded_img").attr("src", '{BASENAME}/' + series[serie] + '.' + zeroFill(frame, 7) + '.jpeg');
//ssim = data[current_item].data[current_frame - 1][1];
$("#encoded_name").text(series[serie] + ' FRAME: ' + frame.toString() + ' SSIM: ' + ssim.toString() );
plot.unhighlight();
plot.highlight(serie, [frame, ssim]);
plot.lockCrosshair({ x: frame, y: ssim });
console.log("Current : " + ' ' + series[serie] + ' FRAME: ' + frame.toString() + ' SSIM: ' + ssim );
}
$("#placeholder").bind('plotclick', function(event, pos, item) {
if(item) {
current_frame = parseInt(item.datapoint[0].toFixed(0));
//current_ssim = parseInt(item.datapoint[1].toFixed(4));
current_series = series.indexOf(item.series.label);
current_ssim = getSSIM(current_frame, current_series, data);
page(current_frame, current_series, current_ssim);
}
});
page(current_frame, current_series, current_ssim);
$("body").keydown(function(event) {
if (event.which == 37) {
if(current_frame > min_frame) {
current_frame = current_frame - 1;
} else {
current_frame = max_frame;
}
} else if(event.which == 39) {
if((current_frame + 1) <= max_frame) {
current_frame = current_frame + 1;
} else {
current_frame = min_frame;
}
} else if(event.which == 38) {
if(current_series <= 0) {
current_series = series.length - 1;
} else {
current_series = current_series - 1;
}
} else if(event.which == 40) {
current_series = (current_series + 1)%series.length;
}
//current_ssim = data[current_series]
current_ssim = getSSIM(current_frame, current_series, data);
page(current_frame, current_series, current_ssim);
});
});
</script>
</body>
</html>
END
require "optparse"
require "ostruct"
require "fileutils"
# Utility method to change working directory
def cwd dir
original_dir = Dir.pwd
begin
puts "Creating #{dir} directory"
FileUtils.mkdir_p(dir)
Dir.chdir dir
yield
ensure
Dir.chdir original_dir
end
end
options = OpenStruct.new
options.library = {}
options.library[:ana] = :ssim
options.library[:log] = 3
options.library[:color] = :rgb
options.library[:bs] = 8
options.inplace = false
options.encoding = "utf8"
options.transfer_type = :auto
options.verbose = false
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename __FILE__} [options] -r REFERENCE INPUT1 INPUT2 ..."
opts.separator ""
opts.separator "Specific options: "
opts.on("-r", "--reference REF", "Reference REF") { |lib| options.library[:ref] = lib }
opts.on("-m", "--max-frames [MAXFRAMES]", "set max frames to process before quit") { |lib| options.library[:max] = lib }
opts.on("-s", "--skip-frames [SKIPFRAMES]", "skip n initial frames") { |lib| options.library[:skip] = lib }
opts.on("-a", "--analyzer ANALYZER", [:psnr, :ssim], "psnr, ssim (default)") { |lib| options.library[:ana] = lib }
opts.on("-l", "--loglevel LEVEL", [0, 1, 2, 3, 4], "0: no log, 1: errors, 2: warnings, 3: info (default), 4: debug") { |lib| options.library[:ana] = lib }
opts.on("-c", "--colorspace [SPACE]", [:rgb, :hsi, :ycbcr, :y], "color space to use (PSNR only): rgb, hsi, ycbcr or y (default rgb)") { |lib| options.library[:color] = lib }
opts.on("-bs", "--block-size BS", "block size used in SSIM analysis (default 8)") { |lib| options.library[:bs] = lib }
end
opts.parse!(ARGV)
if options.library[:ref].nil?
puts "Missing reference clip (-r) argument"
exit -1
end
if ARGV.empty?
puts "Missing input clips"
end
files = ARGV.join(" ")
ARGV.each do |f|
if ! File.exists?(f) or ! File.readable?(f)
puts "File #{f} does not exists or is not readable"
exit
end
end
#qpsnr_cmd = "/usr/bin/qpsnr -a ssim -I -m 10 -r #{ARGV.join(" ")}"
qpsnr_cmd = "#{File.dirname(__FILE__)}/qpsnr "
qpsnr_cmd += "-a #{options.library[:ana]} -J -G "
qpsnr_cmd += "-m #{options.library[:max]} " if options.library[:max].to_i > 0
qpsnr_cmd += "-s #{options.library[:skip]} " if options.library[:skip].to_i > 0
qpsnr_cmd += "-o colorspace=#{options.library[:color]}:blocksize=#{options.library[:bs]} "
qpsnr_cmd += "-r #{options.library[:ref]} "
qpsnr_cmd += "#{ARGV.join(" ")}"
#puts "== Running qpsnr on input files"
puts qpsnr_cmd
data = []
links = ""
result = ""
dirname = File.dirname options.library[:ref]
extname = File.extname options.library[:ref]
basename = File.basename options.library[:ref], extname
cwd basename do
result = `#{qpsnr_cmd}`
result.split.each do |line|
values = line.split(",")
next if values[0] == "Sample"
ARGV.size.times do |col|
data[col] ||= []
data[col] << [values[0].to_i, values[col + 1].to_f]
end
end
end
File.open("#{basename}.dat", "w") { |fp| fp << result }
min = (options.library[:skip] || 0).to_i + 1
max = min + data[0].size - 1
ssim = data[0][0][1]
frame = data[0][0][0]
# Convert data to a string for flot
data = data.each_with_index.map do |x, i|
"{ data: #{x.to_s}, label: '#{File.basename(ARGV[i])}' }"
end.join(",")
FileUtils.mkdir_p("scripts")
FileUtils.cp(File.join(File.dirname(__FILE__), "scripts", "jquery.js"), "scripts/")
FileUtils.cp(File.join(File.dirname(__FILE__), "scripts", "jquery.flot.js"), "scripts/")
FileUtils.cp(File.join(File.dirname(__FILE__), "scripts", "jquery.flot.crosshair.js"), "scripts/")
File.open("#{basename}.html", "w") do |fp|
fp << DATA.gsub("{SOURCE}", "#{basename}#{extname}")
.gsub("{BASENAME}", basename)
.gsub("{MAX}", max.to_s )
.gsub("{MIN}", min.to_s )
.gsub("{VALUE}", ssim.to_s )
.gsub("{FRAME}", frame.to_s )
.gsub("{ITEMS}", ARGV.map { |a| "'#{File.basename(a)}'" }.join(",").to_s)
.gsub("{DATA}", data.to_s)
end