forked from Netflix/codec_compare
-
Notifications
You must be signed in to change notification settings - Fork 3
/
compute_xlmetrics.py
executable file
·623 lines (546 loc) · 29.6 KB
/
compute_xlmetrics.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/usr/bin/env python
import errno
import os
import sys
import subprocess
import json
import argparse
def mkdir_p(path):
""" mkdir -p
"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def listdir_full_path(directory):
""" like os.listdir(), but returns full paths
"""
for f in os.listdir(directory):
if not os.path.isdir(f):
yield os.path.abspath(os.path.join(directory, f))
def get_dimensions(image, classname):
""" given a source image, return dimensions
"""
start, ext = os.path.splitext(image)
if ext == '.yuv':
bitdepth = "8"
res_split = start.split('x')
width_split = res_split[0].split('_')
width = width_split[-1]
height_split = res_split[-1].split('_')
m = res_split[-1].find("bit")
if res_split[-1][m - 2] == "_":
depth = res_split[-1][m - 1]
else:
depth = res_split[-1][m - 2:m]
height = height_split[0]
elif classname == "classE_exr":
size = os.path.basename(image).split('_')[2]
try:
dimension_cmd = ["identify", '-size', size, '-format', '%w,%h,%z', image]
width, height, depth = subprocess.check_output(dimension_cmd).split(",")
except subprocess.CalledProcessError as e:
print dimension_cmd, e.output
else:
try:
dimension_cmd = ["identify", '-format', '%w,%h,%z', image]
width, height, depth = subprocess.check_output(dimension_cmd).split(",")
except subprocess.CalledProcessError as e:
print dimension_cmd, e.output
return width, height, depth
def compute_vmaf(ref_image, dist_image, width, height, pix_fmt):
""" given a pair of reference and distored images:
use the ffmpeg libvmaf filter to compute vmaf, vif, ssim, and ms_ssim.
"""
log_path = '/tmp/stats.json'
cmd = ['ffmpeg', '-s:v', '%s,%s' % (width, height), '-i', dist_image,
'-s:v', '%s,%s' % (width, height), '-i', ref_image,
'-lavfi', 'libvmaf=ssim=true:ms_ssim=true:log_fmt=json:log_path=' + log_path,
'-f', 'null', '-'
]
try:
print "\033[92m[VMAF]\033[0m " + dist_image
subprocess.check_output(" ".join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print "\033[91m[ERROR]\033[0m " + " ".join(cmd) + "\n" + e.output
vmaf_log = json.load(open(log_path))
vmaf_dict = dict()
vmaf_dict["vmaf"] = vmaf_log["frames"][0]["metrics"]["vmaf"]
vmaf_dict["vif"] = vmaf_log["frames"][0]["metrics"]["vif_scale0"]
vmaf_dict["ssim"] = vmaf_log["frames"][0]["metrics"]["ssim"]
vmaf_dict["ms_ssim"] = vmaf_log["frames"][0]["metrics"]["ms_ssim"]
return vmaf_dict
def compute_psnr(ref_image, dist_image, width, height):
""" given a pair of reference and distorted images:
use the ffmpeg psnr filter to compute psnr and mse for each channel.
"""
log_path = '/tmp/stats.log'
cmd = ['ffmpeg', '-s:v', '%s,%s' % (width, height), '-i', dist_image,
'-s:v', '%s,%s' % (width, height), '-i', ref_image,
'-lavfi', 'psnr=stats_file=' + log_path,
'-f', 'null', '-'
]
try:
print "\033[92m[PSNR]\033[0m " + dist_image
subprocess.check_output(" ".join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print "\033[91m[ERROR]\033[0m " + e.output
psnr_dict = dict()
psnr_log = open(log_path).read()
for stat in psnr_log.rstrip().split(" "):
key, value = stat.split(":")
if key is not "n" and not 'mse' in key:
psnr_dict[key] = float(value)
return psnr_dict
def compute_metrics(ref_image, dist_image, encoded_image, bpp_target, codec, width, height, pix_fmt):
""" given a pair of reference and distorted images:
call vmaf and psnr functions, dump results to a json file.
"""
vmaf = compute_vmaf(ref_image, dist_image, width, height, pix_fmt)
psnr = compute_psnr(ref_image, dist_image, width, height)
stats = vmaf.copy()
stats.update(psnr)
return stats
def compute_metrics_SDR(ref_image, dist_image, encoded_image, bpp_target, codec, width, height, pix_fmt, depth):
""" given a pair of reference and distorted images:
call vmaf and psnr functions, dump results to a json file.
"""
refname, ref_pix_fmt = os.path.basename(ref_image).split(".")
dist_pix_fmt = os.path.basename(dist_image).split(".")[-1]
logfile = '/tmp/stats.log'
HDRConvert_dir = '/tools/HDRTools-0.18-dev/bin/HDRConvert'
ppm_to_yuv_cfg = 'convert_configs/HDRConvertPPMToYCbCr444fr.cfg'
chroma_fmt = 3
HDRMetrics_dir = '/tools/HDRTools-0.18-dev/bin/HDRMetrics'
HDRMetrics_config = 'convert_configs/HDRMetrics.cfg'
try:
cmd = [HDRMetrics_dir, '-f', HDRMetrics_config, '-p', 'Input0File=%s' % ref_image, '-p',
'Input0Width=%s' % width,
'-p', 'Input0Height=%s' % height, '-p', 'Input0ChromaFormat=%d' % chroma_fmt, '-p',
'Input0BitDepthCmp0=%s'
% depth, '-p', 'Input0BitDepthCmp1=%s' % depth, '-p', 'Input0BitDepthCmp2=%s' % depth, '-p',
'Input1File=%s' % dist_image, '-p', 'Input1Width=%s' % width, '-p', 'Input1Height=%s' % height, '-p',
'Input1ChromaFormat=%d' % chroma_fmt, '-p', 'Input1BitDepthCmp0=%s' % depth, '-p',
'Input1BitDepthCmp1=%s' % depth, '-p', 'Input1BitDepthCmp2=%s' % depth, '-p', 'LogFile=%s' % logfile,
'-p', 'TFPSNRDistortion=0', '-p', 'EnablePSNR=1', '-p', 'EnableSSIM=1', '-p', 'EnableMSSSIM=1',
'-p', 'Input1ColorPrimaries=4', '-p', 'Input0ColorPrimaries=4', '-p', 'Input0ColorSpace=0', '-p',
'Input1ColorSpace=0', '>', '/tmp/statsHDRTools_SDRmetrics.json']
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
objective_dict = dict()
with open('/tmp/statsHDRTools_SDRmetrics.json', 'r') as f:
for line in f:
if '000000' in line:
metriclist = line.split()
objective_dict["psnr-y"] = metriclist[1]
if 'classB' not in ref_image:
objective_dict["psnr-avg"] = (6 * float(metriclist[1]) + float(metriclist[2]) + float(
metriclist[3])) / 8.0
objective_dict["ms_ssim"] = metriclist[4]
objective_dict["ssim"] = metriclist[7]
if depth == '8':
log_path = '/tmp/stats.json'
cmd = ['ffmpeg', '-s:v', '%s,%s' % (width, height), '-i', dist_image,
'-s:v', '%s,%s' % (width, height), '-i', ref_image,
'-lavfi', 'libvmaf=log_fmt=json:log_path=' + log_path,
'-f', 'null', '-'
]
try:
print "\033[92m[VMAF]\033[0m " + dist_image
subprocess.check_output(" ".join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print "\033[91m[ERROR]\033[0m " + " ".join(cmd) + "\n" + e.output
vmaf_log = json.load(open(log_path))
vmaf_dict = dict()
vmaf_dict["vmaf"] = vmaf_log["frames"][0]["metrics"]["vmaf"]
vmaf_dict["vif"] = vmaf_log["frames"][0]["metrics"]["vif_scale0"]
stats = vmaf_dict.copy()
stats.update(objective_dict)
else:
stats = objective_dict
return stats
def compute_metrics_HDR(ref_image, dist_image, encoded_image, bpp_target, codec, width, height, pix_fmt, depth):
""" given a pair of reference and distorted images:
call vmaf and psnr functions, dump results to a json file.
"""
ref_pix_fmt = os.path.basename(ref_image).split(".")[-1]
dist_pix_fmt = os.path.basename(dist_image).split(".")[-1]
HDRConvert_dir = '/tools/HDRTools-0.18-dev/bin/HDRConvert'
ppm_to_exr_cfg = 'convert_configs/HDRConvertPPMToEXR.cfg'
yuv_to_exr_cfg = 'convert_configs/HDRConvertYCbCrToBT2020EXR.cfg'
logfile = '/tmp/stats.log'
primary = '1'
if dist_pix_fmt == 'ppm':
exr_dir = os.path.join('objective_images', 'PPM_EXR')
exr_dest = os.path.join(exr_dir, os.path.basename(dist_image) + '.exr')
if not os.path.isfile(exr_dest):
print "\033[92m[EXR]\033[0m " + exr_dest
mkdir_p(exr_dir)
try:
cmd = [HDRConvert_dir, '-f', ppm_to_exr_cfg, '-p', 'SourceFile=%s' % dist_image,
'-p',
'SourceWidth=%s' % width,
'-p', 'SourceHeight=%s' % height, '-p', 'SourceBitDepthCmp0=%s' % depth, '-p',
'SourceBitDepthCmp1=%s'
% depth, '-p', 'SourceBitDepthCmp2=%s' % depth, '-p', 'SourceColorPrimaries=%s' % primary, '-p',
'OutputFile=%s' % exr_dest, '-p', 'OutputWidth=%s' % width, '-p', 'OutputHeight=%s' % height,
'-p',
'OutputBitDepthCmp0=%s' % depth, '-p', 'OutputBitDepthCmp1=%s' % depth, '-p',
'OutputBitDepthCmp2=%s'
% depth, '-p', 'OutputColorPrimaries=%s' % primary]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
else:
print "\033[92m[EXR OK]\033[0m " + exr_dest
dist_image = exr_dest
chroma_fmt = 3
if ref_pix_fmt == 'ppm':
exr_dir = os.path.join('objective_images', 'PPM_EXR')
exr_dest = os.path.join(exr_dir, os.path.basename(ref_image) + '.exr')
if not os.path.isfile(exr_dest):
print "\033[92m[EXR]\033[0m " + exr_dest
mkdir_p(exr_dir)
try:
cmd = [HDRConvert_dir, '-f', ppm_to_exr_cfg, '-p', 'SourceFile=%s' % ref_image,
'-p',
'SourceWidth=%s' % width,
'-p', 'SourceHeight=%s' % height, '-p', 'SourceBitDepthCmp0=%s' % depth, '-p',
'SourceBitDepthCmp1=%s'
% depth, '-p', 'SourceBitDepthCmp2=%s' % depth, '-p', 'SourceColorPrimaries=%s' % primary, '-p',
'OutputFile=%s' % exr_dest, '-p', 'OutputWidth=%s' % width, '-p', 'OutputHeight=%s' % height,
'-p',
'OutputBitDepthCmp0=%s' % depth, '-p', 'OutputBitDepthCmp1=%s' % depth, '-p',
'OutputBitDepthCmp2=%s'
% depth, '-p', 'OutputColorPrimaries=%s' % primary]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
else:
print "\033[92m[EXR OK]\033[0m " + exr_dest
ref_image = exr_dest
chroma_fmt = 3
if dist_pix_fmt == 'yuv':
exr_dir = os.path.join('objective_images', 'YUV_EXR')
exr_dest = os.path.join(exr_dir, os.path.basename(dist_image) + '.exr')
if not os.path.isfile(exr_dest):
print "\033[92m[EXR]\033[0m " + exr_dest
mkdir_p(exr_dir)
try:
cmd = [HDRConvert_dir, '-f', yuv_to_exr_cfg, '-p', 'SourceFile=%s' % dist_image,
'-p',
'SourceWidth=%s' % width,
'-p', 'SourceHeight=%s' % height, '-p', 'SourceBitDepthCmp0=%s' % depth, '-p',
'SourceBitDepthCmp1=%s'
% depth, '-p', 'SourceBitDepthCmp2=%s' % depth, '-p', 'SourceColorPrimaries=%s' % primary, '-p',
'OutputFile=%s' % exr_dest, '-p', 'OutputWidth=%s' % width, '-p', 'OutputHeight=%s' % height,
'-p',
'OutputBitDepthCmp0=%s' % depth, '-p', 'OutputBitDepthCmp1=%s' % depth, '-p',
'OutputBitDepthCmp2=%s'
% depth, '-p', 'OutputColorPrimaries=%s' % primary]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
else:
print "\033[92m[EXR OK]\033[0m " + exr_dest
dist_image = exr_dest
chroma_fmt = 3
if dist_pix_fmt == 'yuv':
exr_dir = os.path.join('objective_images', 'YUV_EXR')
exr_dest = os.path.join(exr_dir, os.path.basename(ref_image) + '.exr')
if not os.path.isfile(exr_dest):
print "\033[92m[EXR]\033[0m " + exr_dest
mkdir_p(exr_dir)
try:
cmd = [HDRConvert_dir, '-f', yuv_to_exr_cfg, '-p', 'SourceFile=%s' % ref_image,
'-p',
'SourceWidth=%s' % width,
'-p', 'SourceHeight=%s' % height, '-p', 'SourceBitDepthCmp0=%s' % depth, '-p',
'SourceBitDepthCmp1=%s'
% depth, '-p', 'SourceBitDepthCmp2=%s' % depth, '-p', 'SourceColorPrimaries=%s' % primary, '-p',
'OutputFile=%s' % exr_dest, '-p', 'OutputWidth=%s' % width, '-p', 'OutputHeight=%s' % height,
'-p',
'OutputBitDepthCmp0=%s' % depth, '-p', 'OutputBitDepthCmp1=%s' % depth, '-p',
'OutputBitDepthCmp2=%s'
% depth, '-p', 'OutputColorPrimaries=%s' % primary]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
else:
print "\033[92m[EXR OK]\033[0m " + exr_dest
ref_image = exr_dest
chroma_fmt = 3
HDRMetrics_dir = '/tools/HDRTools-0.18-dev/bin/HDRMetrics'
HDRMetrics_config = HDRMetrics_dir + '/HDRMetrics_config'
try:
cmd = [HDRMetrics_dir, '-f', HDRMetrics_config, '-p', 'Input0File=%s' % ref_image, '-p',
'Input0Width=%s' % width,
'-p', 'Input0Height=%s' % height, '-p', 'Input0ChromaFormat=%d' % chroma_fmt, '-p', 'Input0ColorSpace=1',
'-p',
'Input0BitDepthCmp0=%s'
% depth, '-p', 'Input0BitDepthCmp1=%s' % depth, '-p', 'Input0BitDepthCmp2=%s' % depth, '-p',
'Input1ColorSpace=1', '-p',
'Input1File=%s' % dist_image, '-p', 'Input1Width=%s' % width, '-p', 'Input1Height=%s' % height, '-p',
'Input1ChromaFormat=%d' % chroma_fmt, '-p', 'Input1BitDepthCmp0=%s' % depth, '-p',
'Input1BitDepthCmp1=%s' % depth, '-p', 'Input1BitDepthCmp2=%s' % depth, '-p', 'LogFile=%s' % logfile,
'-p', 'Input0ColorPrimaries=1', '-p', 'Input1ColorPrimaries=1', '-p', '-p', 'TFPSNRDistortion=1', '-p',
'EnableTFPSNR=1', '-p', 'EnableTFMSSSIM=1',
'>', '/tmp/statsHDRTools.json']
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
print(' '.join(cmd))
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
objective_dict = dict()
with open('/tmp/statsHDRTools.json', 'r') as f:
for line in f:
if '000000' in line:
metriclist = line.split()
objective_dict["psnr-y"] = metriclist[5]
objective_dict["ms_ssim"] = metriclist[9]
return objective_dict
def create_derivatives(image, classname):
""" given a test image, create ppm and yuv derivatives
"""
name = os.path.basename(image).split(".")[0]
extension = os.path.splitext(image)[1]
derivative_images = []
yuv_dir = os.path.join('derivative_images', 'yuv420p')
yuv_dest = os.path.join(yuv_dir, name + '.yuv')
ppm_dir = os.path.join('derivative_images', 'ppm')
if 'tif' in extension and 'XRAY' not in name:
ppm_dest = os.path.join(ppm_dir, name + '.tif')
else:
ppm_dest = os.path.join(ppm_dir, name + '.ppm')
width, height, depth = get_dimensions(image, classname)
HDRTools_dir = '/tools/HDRTools-0.18-dev/bin/HDRConvert'
ppm_to_yuv_cfg = 'convert_configs/HDRConvertPPMToYCbCr420fr.cfg'
if 'classB' in classname:
if not os.path.isfile(ppm_dest):
try:
print "\033[92m[PPM]\033[0m " + ppm_dest
mkdir_p(ppm_dir)
cmd = ["/tools/difftest_ng-master/difftest_ng", "--convert", ppm_dest, os.path.join('images', image),
"-"]
subprocess.check_output(" ".join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
exit(1)
else:
print "\033[92m[PPM OK]\033[0m " + ppm_dest
derivative_images.append((ppm_dest, 'ppm'))
return derivative_images
if 'WALTHAM' in name:
if not os.path.isfile(ppm_dest):
try:
print "\033[92m[PPM]\033[0m " + ppm_dest
mkdir_p(ppm_dir)
cmd = ["/tools/difftest_ng-master/difftest_ng", "--convert", ppm_dest, os.path.join('images', image),
"-"]
subprocess.check_output(" ".join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
exit(1)
else:
print "\033[92m[PPM OK]\033[0m " + ppm_dest
derivative_images.append((ppm_dest, 'ppm'))
if not os.path.isfile(yuv_dest):
try:
print "\033[92m[YUV420]\033[0m " + yuv_dest
mkdir_p(yuv_dir)
cmd = [HDRTools_dir, '-f', ppm_to_yuv_cfg, '-p', 'SourceFile=%s' % image, '-p', 'SourceWidth=%s' % width,
'-p', 'SourceHeight=%s' % height, '-p', 'SourceBitDepthCmp0=%s' % depth, '-p',
'SourceBitDepthCmp1=%s'
% depth, '-p', 'SourceBitDepthCmp2=%s' % depth, '-p', 'SourceColorPrimaries=%s' % primary, '-p',
'OutputFile=%s' % yuv_dest, '-p', 'OutputWidth=%s' % width, '-p', 'OutputHeight=%s' % height, '-p',
'OutputBitDepthCmp0=%s' % depth, '-p', 'OutputBitDepthCmp1=%s' % depth, '-p', 'OutputBitDepthCmp2=%s'
% depth, '-p', 'OutputColorPrimaries=%s' % primary]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
else:
print "\033[92m[YUV420 OK]\033[0m " + yuv_dest
derivative_images.append((yuv_dest, 'yuv420p'))
if not os.path.isfile(ppm_dest):
try:
mkdir_p(ppm_dir)
cmd = ['cp', image, ppm_dest]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
print cmd, e.output
raise e
return derivative_images
def convert_decoded(image, width, height, depth, codecname):
name, extension = os.path.splitext(os.path.basename(image))
HDRTools_dir = '/tools/HDRTools-0.18-dev/bin/HDRConvert'
primary = '0'
if 'tat' in codecname or 'webp' in codecname: # decoded image is YCbCr4:2:0
yuv444_dir = os.path.join('objective_images', 'YUV420_YUV444')
yuv444_dest = os.path.join(yuv444_dir, name + '.yuv')
config = 'convert_configs/HDRConvertYCbCr420ToYCbCr444.cfg'
else:
yuv444_dir = os.path.join('objective_images', 'PPM444_YUV444')
yuv444_dest = os.path.join(yuv444_dir, name + '.yuv')
config = 'convert_configs/HDRConvertPPMToYCbCr444fr.cfg'
if not os.path.isfile(yuv444_dest):
try:
print "\033[92m[YUV444]\033[0m " + yuv444_dest
mkdir_p(yuv444_dir)
cmd = [HDRTools_dir, '-f', config, '-p', 'SourceFile=%s' % image, '-p',
'SourceWidth=%s' % width,
'-p', 'SourceHeight=%s' % height, '-p', 'SourceBitDepthCmp0=%s' % depth, '-p',
'SourceBitDepthCmp1=%s'
% depth, '-p', 'SourceBitDepthCmp2=%s' % depth, '-p', 'SourceColorPrimaries=%s' % primary, '-p',
'OutputFile=%s' % yuv444_dest, '-p', 'OutputWidth=%s' % width, '-p', 'OutputHeight=%s' % height,
'-p',
'OutputBitDepthCmp0=%s' % depth, '-p', 'OutputBitDepthCmp1=%s' % depth, '-p',
'OutputBitDepthCmp2=%s'
% depth, '-p', 'OutputColorPrimaries=%s' % primary]
subprocess.check_output(' '.join(cmd), stderr=subprocess.STDOUT, shell=True)
# print(' '.join(cmd))
except subprocess.CalledProcessError as e:
print "\033[91m[ERROR]\033[0m"
print cmd, e.output
#raise e
else:
print "\033[92m[YUV420 OK]\033[0m " + yuv444_dest
return yuv444_dest
def main():
""" check for Docker, check for complementary encoding and decoding scripts, check for test images.
fire off encoding and decoding scripts, followed by metrics computations.
"""
parser = argparse.ArgumentParser(description='codec_compare')
parser.add_argument('path', metavar='DIR',
help='path to images folder')
args = parser.parse_args()
classpath = args.path
classname = classpath.split('/')[1]
images = set(listdir_full_path(classpath))
if len(images) <= 0:
print "\033[91m[ERROR]\033[0m" + " no source files in ./images."
sys.exit(1)
codeclist_full = set(['aom', 'deepcoder', 'deepcoder-lite', 'fuif', 'fvdo', 'hevc', 'kakadu', 'jpeg',
'pik', 'tat', 'xavs', 'xavs-fast', 'xavs-median', 'webp'])
bpp_targets = set([0.06, 0.12, 0.25, 0.50, 0.75, 1.00, 1.50, 2.00])
for image in images:
width, height, depth = get_dimensions(image, classname)
name, imgfmt = os.path.splitext(image)
imgfmt = os.path.basename(image).split(".")[-1]
derivative_images = []
if classname[:6] == 'classB':
derivative_images = create_derivatives(image, classname)
else:
derivative_images.append((image, imgfmt))
for derivative_image, pix_fmt in derivative_images:
json_dir = 'metrics'
mkdir_p(json_dir)
json_file = os.path.join(json_dir,
os.path.splitext(os.path.basename(derivative_image))[0] + "." + pix_fmt + ".json")
# if os.path.isfile(json_file):
# print "\033[92m[JSON OK]\033[0m " + json_file
# continue
main_dict = dict()
derivative_image_metrics = dict()
for codecname in codeclist_full:
convertflag = 1
caseflag = pix_fmt
if (codecname == 'webp' or codecname == 'tat' or 'deepcoder' in codecname) and depth != '8':
continue
if 'xavs' in codecname and depth != '8' and depth != '10':
continue
if 'classE' in classname and ('tat' in codecname or 'xavs' in codecname or 'deepcoder' in codecname):
continue
if codecname == 'kakadu' and classname[:6] == 'classB':
convertflag = 0
caseflag = imgfmt
bpp_target_metrics = dict()
for bpp_target in bpp_targets:
print(codecname)
if codecname == 'aom' and classname[:6] == 'classB':
# ('AERIAL2' in image or 'CATS' in image or 'XRAY' in image or 'GOLD' in image or 'TEXTURE1' in image):
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '_' + str(bpp_target) + '_' + imgfmt + '.' + 'av1'
encoded_image = os.path.join('outputs', codecname, encoded_image_name)
decoded_image = os.path.join('outputs', codecname, 'decoded', encoded_image_name + '.' + imgfmt)
original_image = image
elif codecname == 'kakadu' and classname[:6] == 'classB':
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '_' + str(bpp_target) + '_' + imgfmt + '.' + codecname
encoded_image = os.path.join('outputs', codecname, encoded_image_name)
decoded_image = os.path.join('outputs', codecname, 'decoded', encoded_image_name + '.' + imgfmt)
original_image = image
elif 'xavs' in codecname and classname[:6] == 'classB':
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '_' + str(bpp_target) + '_' + imgfmt + '.' + codecname
encoded_image = os.path.join('outputs', codecname, encoded_image_name)
decoded_image = os.path.join('outputs', codecname, 'decoded', encoded_image_name + '.' + imgfmt)
original_image = image
elif codecname == 'fvdo' and classname[:6] == 'classB':
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '_' + str(bpp_target) + '_pgm' + '.' + codecname
encoded_image = os.path.join('outputs', codecname, encoded_image_name)
decoded_image = os.path.join('outputs', codecname, 'decoded', encoded_image_name + '.pgm')
original_image = image
else:
if codecname == 'fuif' and 'tif' in imgfmt:
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '.tif_' + str(bpp_target) + '_' + pix_fmt + '.' + codecname
elif codecname == 'webp' or codecname == 'tat':
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '_' + str(bpp_target) + '_yuv420p.' + codecname
else:
encoded_image_name = os.path.splitext(os.path.basename(derivative_image))[
0] + '_' + str(bpp_target) + '_' + pix_fmt + '.' + codecname
encoded_image = os.path.join('outputs', codecname, encoded_image_name)
decoded_image_path = os.path.join('outputs', codecname, 'decoded')
decoded_image = ''
for decodedfile in os.listdir(decoded_image_path):
encoderoot = '_'.join(os.path.splitext(os.path.basename(encoded_image_name))[0].split('_')[:-1])
if encoderoot in decodedfile:
if ('tat' in codecname or 'webp' in codecname) and os.path.splitext(os.path.basename(decodedfile))[1] == '.yuv':
decoded_image = os.path.join('outputs', codecname, 'decoded', decodedfile)
print(decoded_image)
if ('tat' not in codecname or 'webp' not in codecname) and os.path.splitext(os.path.basename(decodedfile))[1] != '.yuv':
decoded_image = os.path.join('outputs', codecname, 'decoded', decodedfile)
if 'classE' not in classname and 'classB' not in classname and os.path.isfile(decoded_image):
decoded_image = convert_decoded(decoded_image, width, height, depth, codecname)
original_image = convert_decoded(derivative_image, width, height, depth, 'reference')
else:
original_image = derivative_image
print('Reference:' + original_image)
print('Encoded:' + encoded_image)
print('Decoded:' + decoded_image)
if (os.path.isfile(original_image) and os.path.isfile(decoded_image) and os.path.isfile(encoded_image)):
if 'classE' in classname:
metrics = compute_metrics_HDR(original_image, decoded_image, encoded_image, bpp_target,
codecname, width, height, pix_fmt, depth)
elif 'classB' in classname:
metrics = compute_metrics(original_image, decoded_image, encoded_image, bpp_target, codecname,
width, height, pix_fmt)
else:
metrics = compute_metrics_SDR(original_image, decoded_image, encoded_image, bpp_target,
codecname, width,
height, imgfmt, depth)
measured_bpp = (os.path.getsize(encoded_image) * 1.024 * 8) / (float((int(width) * int(height))))
bpp_target_metrics[measured_bpp] = metrics
else:
continue
derivative_image_metrics[codecname] = bpp_target_metrics
main_dict[derivative_image] = derivative_image_metrics
mkdir_p(json_dir)
with open(json_file, 'w') as f:
f.write(json.dumps(main_dict, indent=2))
if __name__ == "__main__":
main()