-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdds_utils.py
893 lines (757 loc) · 32.1 KB
/
dds_utils.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
import re
import os
import csv
import shutil
import subprocess
import numpy as np
import cv2 as cv
import networkx
from networkx.algorithms.components.connected import connected_components
import time
from datetime import datetime
class Results:
def __init__(self):
self.regions = []
self.regions_dict = {}
def __len__(self):
return len(self.regions)
def results_high_len(self, threshold):
count = 0
for r in self.regions:
if r.conf > threshold:
count += 1
return count
def is_dup(self, result_to_add, threshold=0.5):
# if the regions with IOU greater than threshold
# return maximum confidence result
if result_to_add.fid not in self.regions_dict:
return None
# have not added this fid, return none,means add saightly;
#
# if havd added this fid,but all not is_same, also return none;
# if self.regions fid have had regions, return max confidence result
max_conf = -1
max_conf_result = None
for existing_result in self.regions_dict[result_to_add.fid]:
if existing_result.is_same(result_to_add, threshold):
if existing_result.conf > max_conf:
max_conf = existing_result.conf
max_conf_result = existing_result
return max_conf_result
def combine_results(self, additional_results, threshold=0.5):
for result_to_add in additional_results.regions:
self.add_single_result(result_to_add, threshold)
def add_single_result(self, region_to_add, threshold=0.5):
if threshold == 1:
self.append(region_to_add)
return
dup_region = self.is_dup(region_to_add, threshold) # dlicate: chongfu
# if dup_region==none, means no duplicated regions, so need to add region_to_add
if (not dup_region or
("tracking" in region_to_add.origin and
"tracking" in dup_region.origin)):
self.regions.append(region_to_add)
if region_to_add.fid not in self.regions_dict:
self.regions_dict[region_to_add.fid] = []
self.regions_dict[region_to_add.fid].append(region_to_add)
# if dup_region!=none:
else:
final_object = None
if dup_region.origin == region_to_add.origin:
final_object = max([region_to_add, dup_region],
key=lambda r: r.conf)
elif ("low" in dup_region.origin and
"high" in region_to_add.origin):
final_object = region_to_add
elif ("high" in dup_region.origin and
"low" in region_to_add.origin):
final_object = dup_region
dup_region.x = final_object.x
dup_region.y = final_object.y
dup_region.w = final_object.w
dup_region.h = final_object.h
dup_region.conf = final_object.conf
dup_region.origin = final_object.origin
def suppress(self, threshold=0.5):
new_regions_list = []
while len(self.regions) > 0:
max_conf_obj = max(self.regions, key=lambda e: e.conf)
new_regions_list.append(max_conf_obj)
self.remove(max_conf_obj)
objs_to_remove = []
for r in self.regions:
if r.fid != max_conf_obj.fid:
continue
if calc_iou(r, max_conf_obj) > threshold:
objs_to_remove.append(r)
for r in objs_to_remove:
self.remove(r)
new_regions_list.sort(key=lambda e: e.fid)
for r in new_regions_list:
self.append(r)
def append(self, region_to_add):
self.regions.append(region_to_add)
if region_to_add.fid not in self.regions_dict:
self.regions_dict[region_to_add.fid] = []
self.regions_dict[region_to_add.fid].append(region_to_add)
def remove(self, region_to_remove):
self.regions_dict[region_to_remove.fid].remove(region_to_remove)
self.regions.remove(region_to_remove)
self.regions_dict[region_to_remove.fid].remove(region_to_remove)
def fill_gaps(self, number_of_frames):
if len(self.regions) == 0:
return
results_to_add = Results()
max_resolution = max([e.resolution for e in self.regions])
fids_in_results = [e.fid for e in self.regions]
for i in range(number_of_frames):
if i not in fids_in_results:
results_to_add.regions.append(Region(i, 0, 0, 0, 0,
0.1, "no obj",
max_resolution))
self.combine_results(results_to_add)
self.regions.sort(key=lambda r: r.fid)
def write_results_txt(self, fname):
results_file = open(fname, "w")
for region in self.regions:
# prepare the string to write
str_to_write = (f"{region.fid},{region.x},{region.y},"
f"{region.w},{region.h},"
f"{region.label},{region.conf},"
f"{region.resolution},{region.origin}\n")
results_file.write(str_to_write)
results_file.close()
def write_results_csv(self, fname):
results_files = open(fname, "w")
csv_writer = csv.writer(results_files)
for region in self.regions:
row = [region.fid, region.x, region.y,
region.w, region.h,
region.label, region.conf,
region.resolution, region.origin]
csv_writer.writerow(row)
results_files.close()
def write(self, fname):
if re.match(r"\w+[.]csv\Z", fname):
self.write_results_csv(fname)
else:
self.write_results_txt(fname)
def to_graph(l):
G = networkx.Graph()
for part in l:
# each sublist is a bunch of nodes
G.add_nodes_from(part)
# it also imlies a number of edges:
G.add_edges_from(to_edges(part))
return G
def to_edges(l):
"""
treat `l` as a Graph and returns it's edges
to_edges(['a','b','c','d']) -> [(a,b), (b,c),(c,d)]
"""
it = iter(l)
last = next(it)
for current in it:
yield last, current
last = current
def filter_bbox_group(bb1, bb2, iou_threshold):
if calc_iou(bb1, bb2) > iou_threshold and bb1.label == bb2.label:
return True
else:
return False
def overlap(bb1, bb2):
# determine the coordinates of the intersection rectangle
x_left = max(bb1.x, bb2.x)
y_top = max(bb1.y, bb2.y)
x_right = min(bb1.x+bb1.w, bb2.x+bb2.w)
y_bottom = min(bb1.y+bb1.h, bb2.y+bb2.h)
# no overlap
if x_right < x_left or y_bottom < y_top:
return False
else:
return True
def pairwise_overlap_indexing_list(single_result_frame, iou_threshold):
pointwise = [[i] for i in range(len(single_result_frame))]
pairwise = [[i, j] for i, x in enumerate(single_result_frame)
for j, y in enumerate(single_result_frame)
if i != j if filter_bbox_group(x, y, iou_threshold)]
return pointwise + pairwise
# enumerate,同时列出数据下标和数据
def simple_merge(single_result_frame, index_to_merge):
# directly using the largest box
bbox_large = []
for i in index_to_merge:
i2np = np.array([j for j in i])
left = min(np.array(single_result_frame)[i2np], key=lambda x: x.x)
top = min(np.array(single_result_frame)[i2np], key=lambda x: x.y)
right = max(
np.array(single_result_frame)[i2np], key=lambda x: x.x + x.w)
bottom = max(
np.array(single_result_frame)[i2np], key=lambda x: x.y + x.h)
fid, x, y, w, h, conf, label, resolution, origin = (
left.fid, left.x, top.y, right.x + right.w - left.x,
bottom.y + bottom.h - top.y, left.conf, left.label,
left.resolution, left.origin)
single_merged_region = Region(fid, x, y, w, h, conf,
label, resolution, origin)
bbox_large.append(single_merged_region)
return bbox_large
def merge_boxes_in_results(results_dict, min_conf_threshold, iou_threshold):
final_results = Results()
# Clean dict to remove min_conf_threshold
for _, regions in results_dict.items():
to_remove = []
for r in regions:
if r.conf < min_conf_threshold:
to_remove.append(r)
for r in to_remove:
regions.remove(r) # remove resul.regions_dict and result.regions
for fid, regions in results_dict.items():
overlap_pairwise_list = pairwise_overlap_indexing_list(
regions, iou_threshold)
overlap_graph = to_graph(overlap_pairwise_list)
grouped_bbox_idx = [c for c in sorted(
connected_components(overlap_graph), key=len, reverse=True)]
merged_regions = simple_merge(regions, grouped_bbox_idx)
for r in merged_regions:
final_results.append(r)
return final_results
def read_results_csv_dict(fname):
"""Return a dictionary with fid mapped to an array
that contains all Regions objects"""
results_dict = {}
rows = []
with open(fname) as csvfile:
csv_reader = csv.reader(csvfile)
for row in csv_reader:
rows.append(row)
for row in rows:
fid = int(row[0])
x, y, w, h = [float(e) for e in row[1:5]]
conf = float(row[6])
label = row[5]
resolution = float(row[7])
origin = float(row[8])
region = Region(fid, x, y, w, h, conf, label, resolution, origin)
if fid not in results_dict:
results_dict[fid] = []
if label != "no obj":
results_dict[fid].append(region)
return results_dict
def read_results_txt_dict(fname):
"""Return a dictionary with fid mapped to
and array that contains all SingleResult objects
from that particular frame"""
results_dict = {}
with open(fname, "r") as f:
lines = f.readlines()
f.close()
for line in lines:
line = line.split(",")
fid = int(line[0])
x, y, w, h = [float(e) for e in line[1:5]]
label = line[5]
conf = float(line[6])
resolution = float(line[7])
origin = "generic"
if len(line) > 8:
origin = line[8].strip()
single_result = Region(fid, x, y, w, h, conf, label,
resolution, origin.rstrip())
# if single_result.label=='object':
# print(fid)
# break
if fid not in results_dict:
results_dict[fid] = []
if label != "no obj":
results_dict[fid].append(single_result)
return results_dict
def read_results_dict(fname):
# TODO: Need to implement a CSV function
if re.match(r"\w+[.]csv\Z", fname):
return read_results_csv_dict(fname)
else:
return read_results_txt_dict(fname)
def calc_intersection_area(a, b):
# y image axis,upper left 0
to = max(a.y, b.y)
le = max(a.x, b.x)
bo = min(a.y + a.h, b.y + b.h)
ri = min(a.x + a.w, b.x + b.w)
w = max(0, ri - le)
h = max(0, bo - to)
return w * h
def calc_area(a):
w = max(0, a.w)
h = max(0, a.h)
return w * h
def calc_iou(a, b):
intersection_area = calc_intersection_area(a, b)
union_area = calc_area(a) + calc_area(b) - intersection_area
return intersection_area / union_area
def get_interval_area(width, all_yes):
area = 0
for y1, y2 in all_yes:
area += (y2 - y1) * width
return area
def insert_range_y(all_yes, y1, y2):
ranges_length = len(all_yes)
idx = 0
while idx < ranges_length:
if not (y1 > all_yes[idx][1] or all_yes[idx][0] > y2):
# Overlapping
y1 = min(y1, all_yes[idx][0])
y2 = max(y2, all_yes[idx][1])
del all_yes[idx]
ranges_length = len(all_yes)
else:
idx += 1
all_yes.append((y1, y2))
def get_y_ranges(regions, j, x1, x2):
all_yes = []
while j < len(regions):
if (x1 < (regions[j].x + regions[j].w) and
x2 > regions[j].x):
y1 = regions[j].y
y2 = regions[j].y + regions[j].h
insert_range_y(all_yes, y1, y2)
j += 1
return all_yes
def compute_area_of_frame(regions):
regions.sort(key=lambda r: r.x + r.w)
all_xes = []
for r in regions:
all_xes.append(r.x)
all_xes.append(r.x + r.w)
all_xes.sort()
area = 0
j = 0
for i in range(len(all_xes) - 1):
x1 = all_xes[i]
x2 = all_xes[i + 1]
if x1 < x2:
while (regions[j].x + regions[j].w) < x1:
j += 1
all_yes = get_y_ranges(regions, j, x1, x2)
area += get_interval_area(x2 - x1, all_yes)
return area
def compute_area_of_regions(results):
if len(results.regions) == 0:
return 0
min_frame = min([r.fid for r in results.regions])
max_frame = max([r.fid for r in results.regions])
total_area = 0
for fid in range(min_frame, max_frame + 1):
regions_for_frame = [r for r in results.regions if r.fid == fid]
total_area += compute_area_of_frame(regions_for_frame)
return total_area
def compress_and_get_size(images_path, start_id, end_id, qp,
enforce_iframes=False, resolution=None):
number_of_frames = end_id - start_id
encoded_vid_path = os.path.join(images_path, "temp.mp4")
if resolution and enforce_iframes:
scale = f"scale=trunc(iw*{resolution}/2)*2:trunc(ih*{resolution}/2)*2"
if not qp:
encoding_result = subprocess.run(["ffmpeg", "-y",
"-loglevel", "error",
"-start_number", str(start_id),
'-i', f"{images_path}/%010d.png",
"-vcodec", "libx264", "-g", "15",
"-keyint_min", "15",
"-pix_fmt", "yuv420p",
"-vf", scale,
"-frames:v",
str(number_of_frames),
encoded_vid_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
else:
encoding_result = subprocess.run(["ffmpeg", "-y",
"-loglevel", "error",
"-start_number", str(start_id),
'-i', f"{images_path}/%010d.png",
"-vcodec", "libx264",
"-g", "15",
"-keyint_min", "15",
"-qp", f"{qp}",
"-pix_fmt", "yuv420p",
"-vf", scale,
"-frames:v",
str(number_of_frames),
encoded_vid_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
else:
encoding_result = subprocess.run(["ffmpeg", "-y",
"-start_number", str(start_id),
"-i", f"{images_path}/%010d.png",
"-loglevel", "error",
"-vcodec", "libx264",
"-pix_fmt", "yuv420p", "-crf", "23",
encoded_vid_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
size = 0
if encoding_result.returncode != 0:
# Encoding failed
print("ENCODING FAILED")
print(encoding_result.stdout)
print(encoding_result.stderr)
exit()
else:
size = os.path.getsize(encoded_vid_path)
return size
def extract_images_from_video(images_path, req_regions):
# server_temp-cropped
if not os.path.isdir(images_path):
return
for fname in os.listdir(images_path):
if "png" not in fname:
continue
else:
os.remove(os.path.join(images_path, fname)) # remove before batch's png
encoded_vid_path = os.path.join(images_path, "temp.mp4")
extacted_images_path = os.path.join(images_path, "%010d.png")
decoding_result = subprocess.run(["ffmpeg", "-y",
"-i", encoded_vid_path,
"-pix_fmt", "yuvj420p",
"-g", "8", "-q:v", "2",
"-vsync", "0", "-start_number", "0",
extacted_images_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
if decoding_result.returncode != 0:
print("DECODING FAILED")
print(decoding_result.stdout)
print(decoding_result.stderr)
exit()
fnames = sorted(
[os.path.join(images_path, name)
for name in os.listdir(images_path) if "png" in name])
fids = sorted(list(set([r.fid for r in req_regions.regions])))
# print('extract_images_from_video------------------------------regions.fid:',fids)
fids_mapping = zip(fids, fnames)
for fname in fnames:
# Rename temporarily
os.rename(fname, f"{fname}_temp")
for fid, fname in fids_mapping:
os.rename(os.path.join(f"{fname}_temp"),
os.path.join(images_path, f"{str(fid).zfill(10)}.png"))
def crop_images(results, vid_name, images_direc, resolution=None):
cached_image = None # ple initial
cropped_images = {}
for region in results.regions:
# if not the first time or same frame(fid), need to read raw images in ..dataset/trafficcam_1/src/...
if not (cached_image and
cached_image[0] == region.fid):
image_path = os.path.join(images_direc,
f"{str(region.fid).zfill(10)}.png")
cached_image = (region.fid, cv.imread(image_path))
# Just move the complete image
if region.x == 0 and region.y == 0 and region.w == 1 and region.h == 1:
cropped_images[region.fid] = cached_image[1]
continue
width = cached_image[1].shape[1] # column
height = cached_image[1].shape[0] # row
x0 = int(region.x * width)
y0 = int(region.y * height)
x1 = int((region.w * width) + x0 - 1)
y1 = int((region.h * height) + y0 - 1)
# make a all black(0) image array, copy the region in raw image to black image
if region.fid not in cropped_images:
cropped_images[region.fid] = np.zeros_like(cached_image[1])
# attention, cropped_image/cropped_images[]
cropped_image = cropped_images[region.fid]
cropped_image[y0:y1, x0:x1, :] = cached_image[1][y0:y1, x0:x1, :]
cropped_images[region.fid] = cropped_image
os.makedirs(vid_name, exist_ok=True)
frames_count = len(cropped_images)
# below, sort by fid cropped_images字典 帧号:image,但是用for访问字典不一定按顺序,所以用enumerate
frames = sorted(cropped_images.items(), key=lambda e: e[0])
for idx, (_, frame) in enumerate(frames):
if resolution:
w = int(frame.shape[1] * resolution) # lie
h = int(frame.shape[0] * resolution) # hang
im_to_write = cv.resize(frame, (w, h), fx=0, fy=0,
interpolation=cv.INTER_CUBIC)
# fx:width方向的缩放比例,如果它是0,那么它就会按照(double)dsize.width/src.cols来计算;
# fy:height方向的缩放比例,如果它是0,那么它就会按照(double)dsize.height/src.rows来计算
# cv.INTER_CUBIC 双线性插值 ,放缩使用cv.INTER_CUBIC(较慢)和cv.INTER_LINEAR(较快效果也不错)。
frame = im_to_write
cv.imwrite(os.path.join(vid_name, f"{str(idx).zfill(10)}.png"), frame,
[cv.IMWRITE_PNG_COMPRESSION, 0])
return frames_count
def merge_images(cropped_images_direc, low_images_direc, req_regions):
# server_temp-cropped/merged server_temp
images = {}
for fname in os.listdir(cropped_images_direc):
if "png" not in fname:
continue
fid = int(fname.split(".")[0])
# Read high resolution image
high_image = cv.imread(os.path.join(cropped_images_direc, fname))
width = high_image.shape[1]
height = high_image.shape[0]
# Read low resolution image
# print("merge_images", low_images_direc) merge_images server_temp
low_image = cv.imread(os.path.join(low_images_direc, fname))
# Enlarge low resolution image
enlarged_image = cv.resize(low_image, (width, height), fx=0, fy=0,
interpolation=cv.INTER_CUBIC)
# Put regions in place
for r in req_regions.regions:
if fid != r.fid:
continue
x0 = int(r.x * width)
y0 = int(r.y * height)
x1 = int((r.w * width) + x0 - 1)
y1 = int((r.h * height) + y0 - 1)
enlarged_image[y0:y1, x0:x1, :] = high_image[y0:y1, x0:x1, :]
cv.imwrite(os.path.join(cropped_images_direc, fname), enlarged_image,
[cv.IMWRITE_PNG_COMPRESSION, 0])
images[fid] = enlarged_image
return images
def compute_regions_size(results, vid_name, images_direc, resolution, qp,
enforce_iframes, estimate_banwidth=True):
if estimate_banwidth:
# If not simulation, compress and encode images
# and get size
vid_name = f"{vid_name}-cropped"
frames_count = crop_images(results, vid_name, images_direc,
resolution)
t1=time.time()
print('begin encode-----------------------------------------',t1)
size = compress_and_get_size(vid_name, 0, frames_count, qp=qp,
enforce_iframes=enforce_iframes,
resolution=1)
t2 = time.time()
print('end encode-----------------------------------------', t2,'--',t2-t1)
pixel_size = compute_area_of_regions(results)
return size, pixel_size
else:
size = compute_area_of_regions(results)
return size
def cleanup(vid_name, debug_mode=False, start_id=None, end_id=None):
# if debug mode , copy cropped picture to debugging wenjianjia xiamian
# 如果是debug mode,复制crop的图片到debugging文件夹
# results/trafficcam_1_dds_low res_high res_low qp_high_qp_rpn enlarge ratio_twosides_batch ze_
if not (os.path.isdir(vid_name+"-cropped") or os.path.isdir(vid_name+"-base-phase-cropped")):
return
if not debug_mode:
if os.path.isdir(vid_name + "-cropped"):
shutil.rmtree(vid_name, "-cropped")
if os.path.isdir(vid_name+"-base-phase-cropped"):
shutil.rmtree(vid_name+"-base-phase-cropped")
else:
if start_id is None or end_id is None:
print("Need start_fid and end_fid for debugging mode")
exit()
os.makedirs("debugging", exist_ok=True)
leaf_direc = vid_name.split("/")[-1] + "-cropped"
shutil.move(vid_name + "-cropped", "debugging")
shutil.move(os.path.join("debugging", leaf_direc),
os.path.join("debugging",
f"{leaf_direc}-{start_id}-{end_id}"),
copy_function=os.rename)
def get_size_from_mpeg_results(results_log_path, images_path, resolution):
with open(results_log_path, "r") as f:
lines = f.readlines()
lines = [line for line in lines if line.rstrip().lstrip() != ""]
num_frames = len([x for x in os.listdir(images_path) if "png" in x])
bandwidth = 0
for idx, line in enumerate(lines):
if f"RES {resolution}" in line:
bandwidth = float(lines[idx + 2])
break
size = bandwidth * 1024.0 * (num_frames / 10.0)
return size
def filter_results(bboxes, gt_flag, gt_confid_thresh, mpeg_confid_thresh,
max_area_thresh_gt, max_area_thresh_mpeg):
relevant_classes = ["vehicle"]
if gt_flag:
confid_thresh = gt_confid_thresh # 0.3
max_area_thresh = max_area_thresh_gt
else:
confid_thresh = mpeg_confid_thresh # 0.5
max_area_thresh = max_area_thresh_mpeg
result = []
for b in bboxes:
b = b.x, b.y, b.w, b.h, b.label, b.conf
(x, y, w, h, label, confid) = b
if (confid >= confid_thresh and w*h <= max_area_thresh and
label in relevant_classes):
result.append(b)
return result
def iou(b1, b2):
(x1, y1, w1, h1, label1, confid1) = b1
(x2, y2, w2, h2, label2, confid2) = b2
x3 = max(x1, x2)
y3 = max(y1, y2)
x4 = min(x1+w1, x2+w2)
y4 = min(y1+h1, y2+h2)
if x3 > x4 or y3 > y4:
return 0
else:
overlap = (x4-x3)*(y4-y3)
return overlap/(w1*h1+w2*h2-overlap)
def evaluate(max_fid, map_dd, map_gt,
gt_confid_thresh, mpeg_confid_thresh,
max_area_thresh_gt, max_area_thresh_mpeg, iou_thresh=0.3):
# evaluate(
# number_of_frames - 1, results.regions_dict, ground_truth_dict,
# 0.3, 0.5,
# 0.4, 0.4)
tp_list = []
fp_list = []
fn_list = []
count_list = []
for fid in range(max_fid+1):
# sun 加上的,如果不加找不到fid直接访问会报错
if fid not in map_dd.keys():
continue
bboxes_dd = map_dd[fid]
bboxes_gt = map_gt[fid]
bboxes_dd = filter_results(
bboxes_dd, gt_flag=False, gt_confid_thresh=gt_confid_thresh,
mpeg_confid_thresh=mpeg_confid_thresh,
max_area_thresh_gt=max_area_thresh_gt,
max_area_thresh_mpeg=max_area_thresh_mpeg)
bboxes_gt = filter_results(
bboxes_gt, gt_flag=True, gt_confid_thresh=gt_confid_thresh,
mpeg_confid_thresh=mpeg_confid_thresh,
max_area_thresh_gt=max_area_thresh_gt,
max_area_thresh_mpeg=max_area_thresh_mpeg)
tp = 0
fp = 0
fn = 0
count = 0
for b_dd in bboxes_dd:
found = False
for b_gt in bboxes_gt:
if iou(b_dd, b_gt) >= iou_thresh:
found = True
break
if found:
tp += 1
else:
fp += 1
for b_gt in bboxes_gt:
found = False
for b_dd in bboxes_dd:
if iou(b_dd, b_gt) >= iou_thresh:
found = True
break
if not found:
fn += 1
else:
count += 1
tp_list.append(tp)
fp_list.append(fp)
fn_list.append(fn)
count_list.append(count)
tp = sum(tp_list)
fp = sum(fp_list)
fn = sum(fn_list)
count = sum(count_list)
print(tp,fp,fn,"----------------------------------------------")
return (tp, fp, fn, count,
round(tp/(tp+fp), 3),
round(tp/(tp+fn), 3),
round((2.0*tp/(2.0*tp+fp+fn)), 3))
# fp 错检测为 是目标
# fn 错检测成为 不是目标
# 实验结果显示fn远远多,说明目标没检测出来的居多
def write_stats_txt(fname, vid_name, config, f1, stats,
bw, frames_count, mode):
header = ("video-name,low-resolution,high-resolution,low_qp,high_qp,"
"batch-size,low-threshold,high-threshold,"
"tracker-length,TP,FP,FN,F1,"
"low-size,high-size,total-size,frames,mode")
stats = (f"{vid_name},{config.low_resolution},{config.high_resolution},"
f"{config.low_qp},{config.high_qp},{config.batch_size},"
f"{config.low_threshold},{config.high_threshold},"
f"{config.tracker_length},{stats[0]},{stats[1]},{stats[2]},"
f"{f1},{bw[0]},{bw[1]},{bw[0] + bw[1]},"
f"{frames_count},{mode}")
if not os.path.isfile(fname):
str_to_write = f"{header}\n{stats}\n"
else:
str_to_write = f"{stats}\n"
with open(fname, "a") as f:
f.write(str_to_write)
def write_stats_csv(fname, vid_name, config, f1, stats, bw,
frames_count, mode):
header = ("video-name,low-resolution,high-resolution,low-qp,high-qp,"
"batch-size,low-threshold,high-threshold,"
"tracker-length,TP,FP,FN,F1,"
"low-size,high-size,total-size,frames,mode").split(",")
stats = (f"{vid_name},{config.low_resolution},{config.high_resolution},"
f"{config.low_qp},{config.high_qp},{config.batch_size},"
f"{config.low_threshold},{config.high_threshold},"
f"{config.tracker_length},{stats[0]},{stats[1]},{stats[2]},"
f"{f1},{bw[0]},{bw[1]},{bw[0] + bw[1]},"
f"{frames_count},{mode}").split(",")
results_files = open(fname, "a")
csv_writer = csv.writer(results_files)
if not os.path.isfile(fname):
# If file does not exist write the header row
csv_writer.writerow(header)
csv_writer.writerow(stats)
results_files.close()
def write_stats(fname, vid_name, config, f1, stats, bw,
frames_count, mode):
if re.match(r"\w+[.]csv\Z", fname):
write_stats_csv(fname, vid_name, config, f1, stats, bw,
frames_count, mode)
else:
write_stats_txt(fname, vid_name, config, f1, stats, bw,
frames_count, mode)
def visualize_regions(results, images_direc,
low_conf=0.0, high_conf=1.0,
label="debugging"):
idx = 0
fids = sorted(list(set([r.fid for r in results.regions])))
while idx < len(fids):
image_np = cv.imread(
os.path.join(images_direc, f"{str(fids[idx]).zfill(10)}.png"))
width = image_np.shape[1]
height = image_np.shape[0]
regions = [r for r in results.regions if r.fid == fids[idx]]
for r in regions:
if r.conf < low_conf or r.conf > high_conf:
continue
x0 = int(r.x * width)
y0 = int(r.y * height)
x1 = int(r.w * width + x0)
y1 = int(r.h * height + y0)
cv.rectangle(image_np, (x0, y0), (x1, y1), (0, 0, 255), 2)
cv.putText(image_np, f"{fids[idx]}", (10, 20),
cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv.imshow(label, image_np)
key = cv.waitKey()
if key & 0xFF == ord("q"):
break
elif key & 0xFF == ord("k"):
idx -= 2
idx += 1
cv.destroyAllWindows()
def visualize_single_regions(region, images_direc, label="debugging"):
image_path = os.path.join(images_direc, f"{str(region.fid).zfill(10)}.png")
image_np = cv.imread(image_path)
width = image_np.shape[1]
height = image_np.shape[0]
x0 = int(region.x * width)
y0 = int(region.y * height)
x1 = int((region.w * width) + x0)
y1 = int((region.h * height) + y0)
cv.rectangle(image_np, (x0, y0), (x1, y1), (0, 0, 255), 2)
cv.putText(image_np, f"{region.fid}, {region.label}, {region.conf:0.2f}, "
f"{region.w * region.h}",
(10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv.imshow(label, image_np)
cv.waitKey()
cv.destroyAllWindows()