-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGetMetrics.py
132 lines (99 loc) · 4.2 KB
/
GetMetrics.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
# Search values important for evaluation (runtime, bitrate, psnr, ms-ssim, vmaf)
# write values to .csv file for further processing in excel
import os
import os.path
from os import path
import re
import csv
folder = "encodingOutput/"
def getDataFromLogfile(filename):
try:
with open(folder + filename, "rt") as myfile:
data = []
bitrate = 2 # default bitrate position for HM, VTM and vvenc
runtime = 2 # default runtime position for HM
if "vvenc" in filename or "VTM" in filename:
runtime = 5
for line in myfile:
# search for bitrate
if " a " in line:
split = line.split()
print("Bitrate=" + split[bitrate])
data.append(split[bitrate])
# search for runtime
if "Total Time" in line:
split = line.split()
print("Runtime=" + split[runtime])
data.append(split[runtime])
return data
except Exception as e:
print(e)
def getMetricsFromXML(xmlFilename):
print("*** Additional metrics exist ***")
try:
with open(folder + xmlFilename, 'r') as myfile:
metrics = []
for line in myfile:
if "fyi" in line:
split = line.split("\"")
print("VMAF=" + split[3])
print("PSNR=" + split[5])
print("MS-SSIM=" + split[9])
metrics.append(split[3])
metrics.append(split[5])
metrics.append(split[9])
return metrics
except Exception as e:
print(e)
def buildFilename(filename):
filenameSplit = filename.split("_")
xmlFilename = ""
for i in range((len(filenameSplit) - 1)):
xmlFilename = xmlFilename + filenameSplit[i] + "_"
xmlFilename += "rec.yuv.xml"
return xmlFilename
def buildTableEntry(filename):
filenameSplit = filename.split("_")
tableEntry = ""
for i in range((len(filenameSplit) - 2)):
tableEntry = tableEntry + filenameSplit[i] + "_"
tableEntry += filenameSplit[len(filenameSplit) - 2]
return tableEntry
# taken from: https://stackoverflow.com/questions/2669059/how-to-sort-alpha-numeric-set-in-python
def sorted_nicely(input):
""" Sort the given iterable in the way that humans expect."""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(input, key=alphanum_key)
def main():
processedFiles = 0
try:
# create csv file with header line
with open('encodingOutput/metrics.csv', mode='w') as metrics_csv:
metrics_writer = csv.writer(metrics_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
metrics_writer.writerow(["Filename", "Bitrate", "Runtime", "VMAF", "PSNR", "MS-SSIM"])
# iterate over all files in given folder
for filename in sorted_nicely(os.listdir(folder)):
csvRow = []
# get metrics from encoding output and write to csvRow
if filename.endswith("log.txt"):
print("\n*** Current File: " + filename + " ***")
csvRow.append(buildTableEntry(filename))
for entry in getDataFromLogfile(filename):
csvRow.append(entry)
processedFiles += 1
# search for libVMAF output (xml file) with same filename
# if it exists get metrics and write to csvRow
xmlFilename = buildFilename(filename)
if path.exists(folder + xmlFilename):
for entry in getMetricsFromXML(xmlFilename):
csvRow.append(entry)
processedFiles += 1
# write row to csv file
if len(csvRow) != 0:
metrics_writer.writerow(csvRow)
print("*** Processed " + str(processedFiles) + " files in total ***")
except Exception as e:
print(e)
if __name__ == "__main__":
main()