forked from uwdb/visualroad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
verifier.py
executable file
·309 lines (230 loc) · 9.98 KB
/
verifier.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
#!/usr/bin/python3
import subprocess
import cv2
import tempfile
import logging
import argparse
import numpy as np
from sklearn.metrics import jaccard_similarity_score
from itertools import zip_longest
from common import *
ALL_QUERIES = "1,2a,2b,2c,2d,3,4,5,6a,6b".split(',')
def load_yaml(filename):
with open(filename, 'r') as stream:
return yaml.safe_load(stream)
def load_queries(filename):
return load_yaml(filename)
def load_results(filename):
return load_yaml(filename)
def get_queries(queries, query_id):
queries = next(batch for batch in queries['batches'] if batch['query'] == str(query_id))['batch']
return [q['query'] for q in queries]
def get_results(results, query_id):
return next(result for result in results if result['query'] == str(query_id))['result']
def get_writer(filename, reader, width=None, height=None):
fps = reader.get(cv2.CAP_PROP_FPS)
width = width or int(reader.get(cv2.CAP_PROP_FRAME_WIDTH))
height = height or int(reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
return cv2.VideoWriter(filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height), True)
def assert_psnr(filename, reference_filename, threshold):
subprocess.run(['./assert-psnr.sh', filename, reference_filename, str(threshold)])
def validate_query(query_id, queries, dataset, results, validator):
logging.info('Validating Q%s', query_id)
for i, (query, result_filename) in enumerate(zip_longest(get_queries(queries, query_id), get_results(results, query_id))):
logging.info('Instance %d', i)
validator(dataset, query, result_filename)
def validate_q1(dataset, query, result_filename):
with tempfile.NamedTemporaryFile(suffix='.mp4') as output:
index = 0
result = True
reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
writer = get_writer(output.name, reader,
width=query['x'][1] - query['x'][0] + 1,
height=query['y'][1] - query['y'][0] + 1)
while result:
index += 1
result, frame = reader.read()
if result and index in range(*query['t']):
cropped = frame[query['y'][0]:query['y'][1] + 1, query['x'][0]:query['x'][1] + 1, :]
writer.write(cropped)
writer.release()
assert_psnr(result_filename, output.name, LOSSLESS_PSNR_THRESHOLD)
def validate_q2a(dataset, query, result_filename):
with tempfile.NamedTemporaryFile(suffix='.mp4') as output:
reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
writer = get_writer(output.name, reader)
result = True
while result:
result, frame = reader.read()
if result:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
writer.write(gray)
writer.release()
assert_psnr(result_filename, output.name, LOSSLESS_PSNR_THRESHOLD)
def validate_q2b(dataset, query, result_filename):
with tempfile.NamedTemporaryFile(suffix='.mp4') as output:
reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
writer = get_writer(output.name, reader)
kernel = query['d']
result = True
while result:
result, frame = reader.read()
if result:
blurred = cv2.blur(frame, (kernel, kernel))
writer.write(blurred)
writer.release()
assert_psnr(result_filename, output.name, LOSSLESS_PSNR_THRESHOLD)
def validate_q2c(dataset, query, result_filename):
segment_colors = {'pedestrian': (60, 20, 220), 'vehicle': (142, 0, 0)} # BGR
objects = query['objects'] if 'objects' in query else ['pedestrian', 'vehicle']
threshold = 50
truth_reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
result_reader = cv2.VideoCapture(result_filename)
segmented_result = True
index = 0
iou_total = 0
while segmented_result:
segmented_result, segmented_frame = truth_reader.read()
result_result, result_frame = result_reader.read()
truth_frame= np.full_like(segmented_result, 0)
index += 1
if not result_result and segmented_result:
raise RuntimeError("Unexpected EOF in result video.")
elif not segmented_result and result_result:
raise RuntimeError("Too many frames in result video.")
elif segmented_result:
# Generate truth frame
for object in objects:
color = segment_colors[object]
thresholded = cv2.inRange(segmented_frame, tuple(t - threshold for t in color),
tuple(t + threshold for t in color))
_, contours, hierarchy = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(truth_frame, (x, y), (x + w, y + h), color, cv2.FILLED)
# For each class, compare to result frame
for object in objects:
color = segment_colors[object]
thresholded_result = cv2.inRange(result_frame, color, color)
thresholded_truth = cv2.inRange(truth_frame, color, color)
iou = jaccard_similarity_score(thresholded_result, thresholded_truth)
iou_total += iou
if iou < JACCARD_THRESHOLD:
print('FAIL: calculated Jaccard {} below limit of {} on frame {}'.format(
iou, JACCARD_THRESHOLD, index))
return
print('PASS: Mean Jaccard {}'.format(iou_total / index))
def validate_q2d(dataset, query, result_filename):
with tempfile.NamedTemporaryFile(suffix='.mp4') as output:
reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
writer = get_writer(output.name, reader)
window_size = query['m']
epsilon = query['epsilon']
omega = 0
result = True
queue = []
def write_frame():
mean = np.average(queue, axis=0)
current = queue.pop(0)
current[np.abs(current - mean) < epsilon] = omega
writer.write(current)
while result:
result, frame = reader.read()
if result:
queue.append(frame)
if len(queue) >= window_size:
write_frame()
while queue:
write_frame()
writer.release()
assert_psnr(result_filename, output.name, LOSSLESS_PSNR_THRESHOLD)
def validate_q3(dataset, query, result_filename):
raise RuntimeError("Unimplemented")
def validate_q4(dataset, query, result_filename):
with tempfile.NamedTemporaryFile(suffix='.mp4') as output:
reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
writer = get_writer(output.name, reader)
alpha = query['alpha']
beta = query['beta']
width = int(reader.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
result = True
while result:
result, frame = reader.read()
if result:
resized = cv2.resize(frame, (height*beta, width*alpha), cv2.INTER_LINEAR)
writer.write(resized)
writer.release()
assert_psnr(result_filename, output.name, LOSSLESS_PSNR_THRESHOLD)
def validate_q5(dataset, query, result_filename):
with tempfile.NamedTemporaryFile(suffix='.mp4') as output:
reader = cv2.VideoCapture(os.path.join(dataset['path'], query['path']))
writer = get_writer(output.name, reader)
alpha = query['alpha']
beta = query['beta']
width = int(reader.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
result = True
while result:
result, frame = reader.read()
if result:
resized = cv2.resize(frame, (int(height / beta), int(width / alpha)), cv2.INTER_LINEAR)
writer.write(resized)
writer.release()
assert_psnr(result_filename, output.name, LOSSLESS_PSNR_THRESHOLD)
def validate_q6a(dataset, query, result_filename):
raise RuntimeError("Unimplemented")
def validate_q6b(dataset, query, result_filename):
raise RuntimeError("Unimplemented")
VERIFIERS = {
'1': validate_q1,
'2a': validate_q2a,
'2b': validate_q2b,
'2c': validate_q2c,
'2d': validate_q2d,
'3': validate_q3,
'4': validate_q4,
'5': validate_q5,
'6a': validate_q6a,
'6b': validate_q6b
}
def validate(validate_set, queries_filename, dataset_path, results_filename):
queries = load_queries(queries_filename)
dataset = load_configuration(dataset_path or queries['source'])
results = load_results(results_filename)
for q in validate_set:
if q in VERIFIERS:
validate_query(q, queries, dataset, results, VERIFIERS[q])
else:
print('Unsupported query {} in verifier'.format(q))
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument(
'-v', '--validate',
metavar='V',
default='all',
type=str,
help='Comma-separated list of queries to verify (e.g., "1,2a") or "all"')
parser.add_argument(
'-q', '--queries',
metavar='Q',
required=True,
type=str,
help='Query metadata YAML filename')
parser.add_argument(
'-d', '--dataset',
metavar='D',
required=False,
default=None,
type=str,
help='Video dataset path')
parser.add_argument(
'-r', '--results',
metavar='R',
required=True,
type=str,
help='Query result YAML filename')
args = parser.parse_args()
validate(map(str.strip, args.validate.split(',')) if args.validate != 'all' else ALL_QUERIES,
args.queries, args.dataset, args.results)