-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_background_remover.py
More file actions
1236 lines (1090 loc) · 68.2 KB
/
dynamic_background_remover.py
File metadata and controls
1236 lines (1090 loc) · 68.2 KB
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cv2
import json
import os
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
import torch
import torchvision
from datetime import datetime
import numpy as np
from PIL import Image, ImageEnhance
from tqdm import tqdm
from animal_tracker import animal_tracker
import copy
import math
class dynamic_background_remover:
def __init__(self,
model_path: str,
thing_names_path: str,
detection_threshold: float = 0.25,
iou: float = 0.4,
sigmoid_threshold: float = 0.55,
inference_size: int = 640,
enable_mps: bool = False) -> None:
self.current_frame = 0
self.batch_size = 1
self.header_center = None
self.header_font_size = None
self.model = None
self.model_things = None
self.original_tensor = []
self.processed_image_tensor = []
self.outputs = None
self.trackers = None
self.animal_frames = {}
self.detection_threshold = detection_threshold
self.iou = iou
self.sigmoid_threshold = sigmoid_threshold
self.frame_count = 0
self.start_frame = 0
self.duration = 0
self.percent_complete = 0
self.save_original = False
self.stop = False
self.inference_size = inference_size
self.ort_sess = None
self.enable_mps = enable_mps
self._load_model(model_path)
self._load_thing_names(thing_names_path)
# Load the Model. It checks what kind of local resources you have and loads the best option for you
# to optimize speed of processing
def _load_model(self, model_path):
# Check if any hardware acceleration devices are present and if so use them. Otherwise, use the CPU.
if torch.cuda.is_available():
self.model = torch.jit.load(model_path, map_location='cuda:0')
self.device = torch.device('cuda:0')
elif self.enable_mps:
if torch.backends.mps.is_available():
self.model = torch.jit.load(model_path, map_location='cpu')
self.device = torch.device('mps')
self.model.to(self.device)
else:
self.model = torch.jit.load(model_path, map_location='cpu')
self.device = torch.device('cpu')
else:
self.model = torch.jit.load(model_path, map_location='cpu')
self.device = torch.device('cpu')
# Load in the dictionary of real world names to detected indexes that we saved while exporting the
# original model.
def _load_thing_names(self, thing_names_path):
with open(thing_names_path) as f:
imported_data = f.read()
# Reconstruct the data as a dictionary in index, name pairs {'0': 'rat', '1': 'larva', etc)
class_storage = json.loads(imported_data)
if 'training_size' in class_storage:
self.model_things = class_storage['animal_mapping']
self.inference_size = int(class_storage['training_size'])
else:
self.model_things = class_storage
# Invert the references so we can easily reverse lookup
self.model_things_lookup = {}
for key in self.model_things.keys():
self.model_things_lookup[self.model_things[key]] = key
# Takes the input from the model then filters it and resizes the outputs (masks, boxes)
# to match the original image. This is also where I'll add conversions for other model types in
# such as Yolov8 which could be used in the future.
def _normalize_model_outputs(self, model_outputs, view_classes, temp_view_class_indexes, scale):
outputs = []
for i in range(len(model_outputs)):
# Resize the boxes and masks to match the original image before storing them in the internal structure.
# shape[1] is the height of the images and shape[0] is the width.
boxes = model_outputs[i]['pred_boxes']
# Rescale the boxes to fit the original image, put scale ratio onto the correct device
new_boxes = boxes * scale[i]
# Scale the masks.
masks = model_outputs[i]["pred_masks"]
new_masks = []
# Changing dimensions with "unsqueeze" is necessary for the interpolate command.
# This is required because the animal masks are all sent back
# as 28x28 pixel images that must be scaled up to the size of their bounding box.
for boxes_index in range(len(new_boxes)):
height = int(new_boxes[boxes_index][3]) - int(new_boxes[boxes_index][1])
width = int(new_boxes[boxes_index][2]) - int(new_boxes[boxes_index][0])
mask = masks[boxes_index]
mask = torch.unsqueeze(mask, dim=0)
mask = torch.nn.functional.interpolate(mask,
size=(height, width),
mode="bicubic",
align_corners=False)
mask = (mask.sigmoid() > self.sigmoid_threshold)
mask = mask * 255
new_masks.append(torch.squeeze(mask))
# normalize the boxes to catch boundary condition at maximum width and height
if int(new_boxes[boxes_index][3]) <= self.height:
new_boxes[boxes_index][3] = int(new_boxes[boxes_index][3])
else:
new_boxes[boxes_index][3] = self.height
new_boxes[boxes_index][1] = int(new_boxes[boxes_index][3] - height)
if int(new_boxes[boxes_index][2]) <= self.width:
new_boxes[boxes_index][2] = int(new_boxes[boxes_index][2])
else:
new_boxes[boxes_index][2] = self.width
new_boxes[boxes_index][0] = int(new_boxes[boxes_index][2] - width)
# Filter the scores and predicted classes based on the NMS output
new_scores = model_outputs[i]['scores']
new_prediction_classes = model_outputs[i]["pred_classes"]
outputs.append({'boxes': new_boxes,
'scores': new_scores,
'masks': new_masks,
'pred_classes': new_prediction_classes,
'view_classes': view_classes,
'view_classes_indexes': temp_view_class_indexes,
})
return outputs
# Run inference on the provided image or image path and update internal variables accordingly.
def run_inference(self, img_path=None, view_classes=None, input_scale=1):
# If img_path a string is passed in, assume it is the path to the image.
original_tensor = []
scale = []
processed_image_tensor = []
model_input = []
if isinstance(img_path, str):
i = 0
original_tensor.append(torchvision.io.read_image(img_path))
pad = (0, 0, 0, 0)
# Create a pad around the image to make it square and divisible by 32
# then resize it to the inference size
if original_tensor[i].shape[2] > original_tensor[i].shape[1]:
bottom_pad = original_tensor[i].shape[2] % 32
right_pad = original_tensor[i].shape[2] - original_tensor[i].shape[1] + bottom_pad
pad = (0, bottom_pad, 0, right_pad)
elif original_tensor[i].shape[1] > original_tensor[i].shape[2]:
right_pad = original_tensor[i].shape[1] % 32
bottom_pad = original_tensor[i].shape[1] - original_tensor[i].shape[2] + right_pad
pad = (0, bottom_pad, 0, right_pad)
resize = torchvision.transforms.Resize(size=(self.inference_size, self.inference_size),
interpolation=torchvision.transforms.InterpolationMode.BICUBIC,
antialias=True)
processed_image_tensor.append(resize(torch.nn.functional.pad(original_tensor[i], pad)))
input_image = torch.nn.functional.pad(original_tensor[i], pad)
model_input.append({'image': processed_image_tensor[i], })
temp4 = input_image.shape[1]
temp5 = processed_image_tensor[i].shape[1]
scale.append((temp4 / temp5), )
# If img_path is not a string, assume it is a numpy array from opencv
# which has a format of BGR. Required to process video frames
# from opencv (cv2.VideoCapture)
else:
model_input = []
scale = []
for i in range(len(img_path)):
model_input.append({'image': img_path[i]}, )
scale.append(input_scale)
with torch.no_grad():
outputs = self.model(model_input)
# If no prediction classes were selected, show all animals.
if view_classes is None:
view_classes = list(self.model_things.values())
# Get the 'index' values for the text strings to map them
# to integer class values (animals) found during inferencing.
temp_view_class_indexes = []
key_list = list(self.model_things.keys())
val_list = list(self.model_things.values())
# For each animal type you are looking at, check whether to add it to self.outputs,
# the list of animals you want to see.
for name in view_classes:
# This confirms that there is a valid mapping for the animal you entered within the list of things to view.
if name in val_list:
temp_view_class_indexes.append(int(key_list[val_list.index(name)]))
outputs = self._normalize_model_outputs(outputs, view_classes, temp_view_class_indexes, scale)
return outputs
def get_original_with_masks(self,
image_path=None,
view_classes: list = None,
frame_number: int = 0,
model_output: dict = {},
draw_bbox: bool = True,
draw_text: bool = True,
draw_masks: bool = True):
# If an image path was passed in, then run inference on the image before proceeding.
if isinstance(image_path, str):
self.original_tensor = torchvision.io.read_image(image_path)
# Assume that inference must be re-run and get the first dictionary in the return list
width = self.original_tensor.shape[2]
height = self.original_tensor.shape[1]
self.height = height
self.width = width
font_scale, font_width = self.get_font_size(image_width=width, image_height=height)
self.header_font_scale = font_scale
self.header_font_width = int(font_width * .4)
self.header_center = width / 2 - (width * .15)
model_output = self.run_inference(image_path, view_classes)[0]
self.update_animal_data(tracking=False,
frame_number=0,
model_output=model_output)
if 'original_tensor' not in model_output.keys():
model_output['original_tensor'] = self.original_tensor
if view_classes is None:
view_classes = list(self.model_things.values())
# Get the original image array
original_image = np.array(model_output['original_tensor'])
# Fixed the dimensions of the numpy coming from a tensor, move first dimension to end.
original_image = np.transpose(original_image, (1, 2, 0)).copy()
pil_comp = Image.fromarray(original_image)
# Create a holder from the masks and then load its pixel map into a variable
mask_holder = Image.new('RGB', Image.fromarray(original_image,
mode='RGB').size)
holder_pixels = mask_holder.load() # Create the pixel map
if len(model_output['boxes'] > 0):
if draw_masks:
# For each type of animal that we are trying to track, process their tracking info.
for animal_class in self.trackers:
# If there aren't any tracked animals, stop processing this animal set and move on.
if len(animal_class.active_tracklets['animals']) == 0:
continue
# For each key in the animal tracker,
# make the masked images, individual animal image, and contour images
for z in animal_class.active_tracklets['animals'].keys():
# Check to be sure an animal that is being tracked is visible in this frame before processing
if animal_class.active_tracklets['animals'][z]['visible']:
# 'Boxes' get the coordinates out of the boxes variable
x_min = int(animal_class.active_tracklets['animals'][z]['boxes'][-1][0])
y_min = int(animal_class.active_tracklets['animals'][z]['boxes'][-1][1])
# Image prep completed
temp_mask = animal_class.active_tracklets['animals'][z]['masks'][-1]
temp_mask = torch.stack([temp_mask, temp_mask, temp_mask], dim=2)
temp_mask2 = temp_mask.detach().cpu().numpy()
data = temp_mask2.astype(np.uint8)
red, green, blue = data.T # Separate the channels of the image
# Create a filter to find non-back color and then replaces black with the chosen color
non_black_areas = (red > 0) | (blue > 0) | (green > 0)
data[..., :][non_black_areas.T] = animal_class.active_tracklets['animals'][z]['color']
mask = Image.fromarray(data)
pixels = mask.load()
for i in range(mask.size[0]):
for j in range(mask.size[1]):
if holder_pixels[i + x_min, j + y_min] == (0, 0, 0):
# Change if white, change to color
holder_pixels[i + x_min, j + y_min] = pixels[i, j]
brightness = ImageEnhance.Brightness(pil_comp)
pil_comp = brightness.enhance(1.2)
pil_comp = Image.blend(pil_comp.convert('RGB'), mask_holder, 0.3)
# Now that the mask has been blended, revert back to numpy array.
return_image = np.array(pil_comp)
for animal_class in self.trackers:
if len(animal_class.active_tracklets['animals']) == 0:
continue
for z in animal_class.active_tracklets['animals'].keys():
if animal_class.active_tracklets['animals'][z]['visible']:
# 'Boxes' is a tensor with a gradient, so you have to detach it and
# convert it to a numpy to get access to the values
tempbox = animal_class.active_tracklets['animals'][z]['boxes'][-1] # .numpy()
x_min = int(tempbox[0])
x_max = int(tempbox[2])
y_min = int(tempbox[1])
y_max = int(tempbox[3])
height = y_max - y_min
width = x_max - x_min
if draw_bbox or draw_text:
outline_color = animal_class.active_tracklets['animals'][z]['color']
if draw_text:
score = np.round(animal_class.active_tracklets['animals'][z]['scores'][-1].detach().cpu(),
3) * 100
score = '%s' % float('%.3g' % score)
text = f" {self.model_things[str(animal_class.class_index)]}{str(z)}: {str(score)}% "
font_scale, font_width = self.get_font_size(image_width=int(width),
image_height=int(height))
return_image = cv2.putText(img=return_image,
text=text,
org=(int(tempbox[0]), int(tempbox[1])),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale,
color=outline_color,
thickness=font_width
)
if draw_bbox:
cv2.rectangle(img=return_image,
pt1=(int(tempbox[0]), int(tempbox[1])),
pt2=(int(tempbox[2]), int(tempbox[3])),
color=outline_color,
thickness=1
)
else:
return_image = original_image
if draw_text:
text = f"Frame Number: {frame_number}"
return_image = cv2.putText(img=return_image,
text=text,
org=(int(self.header_center), int(30 * self.header_font_scale)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=self.header_font_scale,
color=(255, 0, 0), # Put the frame number in red.
thickness=self.header_font_width
)
return_image = cv2.cvtColor(return_image, cv2.COLOR_RGB2BGR)
return return_image
def get_masked_image(self,
image_path=None,
view_classes: list = None,
frame_number: int = 0,
draw_text: bool = True,
model_output: dict = {}):
if isinstance(image_path, str):
self.original_tensor = torchvision.io.read_image(image_path)
# Assume that inference must be re-run and get the first dictionary in the return list
width = self.original_tensor.shape[2]
height = self.original_tensor.shape[1]
self.height = height
self.width = width
font_scale, font_width = self.get_font_size(image_width=width, image_height=height)
self.header_font_scale = font_scale
self.header_font_width = int(font_width * .4)
self.header_center = width / 2 - (width * .15)
# Assume that inference must be re-run.
model_output = self.run_inference(image_path, view_classes)[0]
self.update_animal_data(tracking=False,
frame_number=0,
model_output=model_output)
if 'original_tensor' not in model_output.keys():
model_output['original_tensor'] = self.original_tensor
if view_classes is None:
view_classes = list(self.model_things.values())
# Get the original image array
original_image = np.array(model_output['original_tensor'])
# Fixed the dimensions of the numpy coming from a tensor, move first dimension to end.
original_image = np.transpose(original_image, (1, 2, 0)).copy()
mask_holder = np.zeros_like(original_image)
# Look through the boxes that were found
for animal_name in view_classes:
animal_id = self.model_things_lookup[animal_name]
value_mask = torch.logical_and(model_output['pred_classes'] ==
torch.tensor(int(animal_id), dtype=torch.int8),
model_output['scores'] > self.detection_threshold)
for z in range(len(model_output['boxes'])):
# If the box met the criteria for valid animal type and score
if value_mask[z]:
# Get the coordinates of the bounding box for each animal
box = model_output['boxes'][z]
temp_mask = model_output['masks'][z].detach().cpu().numpy().astype(np.uint8)
# Check a pixel in the mask_holder and if it is zero (black), copy the corresponding
# value from the mask into it. This code also catch the boundary
# condition at the extremes of the frame
top = int(math.floor(box[1]))
bottom = top + temp_mask.shape[0]
left = int(math.floor(box[0]))
right = left + temp_mask.shape[1]
mask_holder[top:bottom, left:right] += temp_mask.clip(max=1)[..., None]
# Make a true/false array out of the mask_holder numpy. True means not white
mask_holder = mask_holder[..., :] == 0 # 255
# Everywhere that mask_holder is True (not white), set the original equals to zero
original_image[mask_holder] = 0
if draw_text:
text = f"Frame Number: {frame_number}"
original_image = cv2.putText(img=original_image,
text=text,
org=(int(self.header_center), int(30 * self.header_font_scale)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=self.header_font_scale,
color=(255, 0, 0), # Put the frame number in red.
thickness=self.header_font_width
)
# Turn the color channels back to BRG from RGB for opencv
composite = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)
# Return the image
return composite
def get_original_with_contours(self,
image_path=None,
view_classes: list = None,
frame_number: int = 0,
model_output: dict = {},
draw_bbox: bool = True,
draw_text: bool = True,
draw_contour: bool = True):
# If an image path was passed in, then run inference on the image before proceeding.
if isinstance(image_path, str):
self.original_tensor = torchvision.io.read_image(image_path)
# Assume that inference must be re-run. and get the first dictionary in the return list
width = self.original_tensor.shape[2]
height = self.original_tensor.shape[1]
self.height = height
self.width = width
font_scale, font_width = self.get_font_size(image_width=width, image_height=height)
self.header_font_scale = font_scale
self.header_font_width = int(font_width * .4)
self.header_center = width / 2 - (width * .15)
model_output = self.run_inference(image_path, view_classes)[0]
self.update_animal_data(tracking=False,
frame_number=0,
model_output=model_output)
if 'original_tensor' not in model_output.keys():
model_output['original_tensor'] = self.original_tensor
if view_classes is None:
view_classes = list(self.model_things.values())
# Get the original image array
original_image = np.array(model_output['original_tensor'])
# Fixed the dimensions of the numpy coming from a tensor, move first dimension to end.
original_image = np.transpose(original_image, (1, 2, 0)).copy()
if len(model_output['boxes'] > 0):
for animal_class in self.trackers:
if len(animal_class.active_tracklets['animals']) == 0:
continue
for z in animal_class.active_tracklets['animals'].keys():
if animal_class.active_tracklets['animals'][z]['visible']:
# 'Boxes' is a tensor with a gradient, so you have to detach it and
# convert it to a numpy to get access to the values
tempbox = animal_class.active_tracklets['animals'][z]['boxes'][-1] # .numpy()
x_min = int(tempbox[0])
x_max = int(tempbox[2])
y_min = int(tempbox[1])
y_max = int(tempbox[3])
height = y_max - y_min
width = x_max - x_min
outline_color = animal_class.active_tracklets['animals'][z]['color']
temp_mask = animal_class.active_tracklets['animals'][z]['masks'][-1]
contours, _ = cv2.findContours(temp_mask.detach().cpu().numpy(), cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_NONE)
if draw_contour:
cv2.drawContours(image=original_image,
contours=contours,
contourIdx=-1,
color=outline_color,
thickness=1,
offset=(int(x_min), int(y_min))
)
if draw_bbox:
cv2.rectangle(img=original_image,
pt1=(int(tempbox[0]), int(tempbox[1])),
pt2=(int(tempbox[2]), int(tempbox[3])),
color=outline_color,
thickness=1
)
if draw_text:
score = np.round(animal_class.active_tracklets['animals'][z]['scores'][-1].detach().cpu(),
3) * 100
score = '%s' % float('%.3g' % score)
text = f" {self.model_things[str(animal_class.class_index)]}{str(z)}: {str(score)}% "
font_scale, font_width = self.get_font_size(image_width=int(width),
image_height=int(height))
original_image = cv2.putText(img=original_image,
text=text,
org=(int(tempbox[0]), int(tempbox[1])),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale,
color=outline_color,
thickness=font_width
)
if draw_text:
text = f"Frame Number: {frame_number}"
original_image = cv2.putText(img=original_image,
text=text,
org=(int(self.header_center), int(30 * self.header_font_scale)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=self.header_font_scale,
color=(255, 0, 0), # Put the frame number in red.
thickness=self.header_font_width
)
return_image = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)
return return_image
def update_animal_data(self,
tracking: bool = False,
frame_number: int = 0,
model_output: dict = {},
keep_history: bool = False):
# Get the original image array
original_image = np.array(model_output['original_tensor']) if keep_history else []
# If you aren't using this library's tracking algorithm, it will just create a dummy tracking item
# to store the info about the animals.
if not tracking:
self.trackers = []
for animal_class in model_output['view_classes_indexes']:
self.trackers.append(animal_tracker(score_threshold=self.detection_threshold,
lost_threshold=1,
class_index=animal_class,
max_distance_moved=5,
save_trackers=False))
for tracker in self.trackers:
tracker.track(model_output['boxes'], model_output['pred_classes'],
model_output['scores'], model_output['masks'], 0)
# For each type of animal that we are trying to track, process their tracking info.
for animal_class in self.trackers:
# If there aren't any tracked animals, stop processing this animal set and move on
if len(animal_class.active_tracklets['animals']) == 0:
continue
# For each key in the animal tracker, make the masked images, individual animal image, and contour images
for z in animal_class.active_tracklets['animals'].keys():
# Check to be sure an animal that is being tracked is visible in this frame before processing
if animal_class.active_tracklets['animals'][z]['visible']:
# 'Boxes' get the coordinates out of the boxes variable
tempbox = animal_class.active_tracklets['animals'][z]['boxes'][-1]
# Image prep completed
temp_mask = animal_class.active_tracklets['animals'][z]['masks'][-1].detach().cpu().numpy()
animal_id = animal_class.active_tracklets['animals'][z]['animal_id']
if len(self.animal_frames) == 0:
for class_id in self.model_things.keys():
self.animal_frames[int(class_id)] = {"Frames": {}}
else:
for class_id in self.model_things.keys():
if len(self.animal_frames[int(class_id)]) == 0:
self.animal_frames[int(class_id)] = {"Frames": {}}
# If the animal is not being tracked then set its first element, otherwise append to the
# element.
if (animal_id not in self.animal_frames[animal_class.class_index].keys()) or \
(keep_history is False):
if keep_history:
self.animal_frames[animal_class.class_index]['Frames'][f"{frame_number}"] = original_image
self.animal_frames[animal_class.class_index][animal_id] = {"masks": [temp_mask, ],
"boxes": [tempbox, ],
"frame_number": [frame_number, ]}
else:
self.animal_frames[animal_class.class_index]['Frames']["0"] = original_image
self.animal_frames[animal_class.class_index][animal_id] = {"masks": [temp_mask, ],
"boxes": [tempbox, ],
"frame_number": [frame_number, ]}
else:
self.animal_frames[animal_class.class_index]['Frames'][f"{frame_number}"] = original_image
self.animal_frames[animal_class.class_index][animal_id]["masks"].append(temp_mask)
self.animal_frames[animal_class.class_index][animal_id]["boxes"].append(tempbox)
self.animal_frames[animal_class.class_index][animal_id]["frame_number"].append(frame_number)
return
def resize_frame(self, frame, inference_size=None):
if not inference_size:
inference_size = self.inference_size
img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img_to_tensor = torchvision.transforms.ToTensor()(img_rgb)
# Convert the image to a tensor and scale it
img_to_tensor = img_to_tensor * 255
# Make the tensor an uint8 type
frame = img_to_tensor.type(torch.uint8)
pad = (0, 0, 0, 0)
# Resize the image to feed into the model by padding it to make it square and divisible by 32.
# Then resize to the inference size.
if frame.shape[2] > frame.shape[1]:
bottom_pad = frame.shape[2] % 32
right_pad = frame.shape[2] - frame.shape[1] + bottom_pad
pad = (0, bottom_pad, 0, right_pad)
elif frame.shape[1] > frame.shape[2]:
right_pad = frame.shape[1] % 32
bottom_pad = frame.shape[1] - frame.shape[2] + right_pad
pad = (0, bottom_pad, 0, right_pad)
resize = torchvision.transforms.Resize(size=(inference_size, inference_size),
interpolation=torchvision.transforms.InterpolationMode.BICUBIC,
antialias=True)
processed_frame = resize(torch.nn.functional.pad(frame, pad))
return processed_frame
def find_scale(self, width, height, inference_size):
if height >= width:
bottom_pad = height % 32
else:
right_pad = width % 32
bottom_pad = width - height + right_pad
new_height = height + bottom_pad
scale = (new_height / inference_size)
return scale
def get_tracked_video(self,
duration: int,
view_classes: list,
input_video_name: str,
video_output_path: str = os.getcwd(),
batch_size: int = 1,
progress: bool = False,
original_with_masks: bool = False,
masked_original: bool = False,
original_with_contours: bool = False,
save_progress_videos: bool = False,
decorate_videos: bool = True,
save_tracker_info: bool = False,
mini_clip_length: int = 0,
lost_threshold: int = 10,
max_distance_moved: int = 5,
video_start: int = 0,
debug_masks: bool = False,
tracking_iou: float = 0.5) -> None:
# If the requested length of individual animal clips is > 0, set the flag to make individual videos and
# contours.
self.stop = False
if mini_clip_length == 0:
individual_videos = False
else:
individual_videos = True
if len(view_classes) == 0:
view_classes = list(self.model_things.values())
self.batch_size = batch_size
# Get the 'index' values for the text strings that were sent in so that we can map those
# to animals found during inferencing.
temp_view_class_indexes = []
key_list = list(self.model_things.keys())
val_list = list(self.model_things.values())
for name in view_classes:
if name in val_list:
temp_view_class_indexes.append(int(key_list[val_list.index(name)]))
# Set up the storage for individual animal frames if we will make individual videos/contours
if individual_videos:
self.animal_frames = {}
cap = cv2.VideoCapture(input_video_name)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.width = width
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.height = height
frames_per_second = int(cap.get(cv2.CAP_PROP_FPS))
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
max_distance_moved = math.sqrt(width ** 2 + height ** 2) * max_distance_moved / 100
# Set up trackers
self.trackers = []
for animal_class in temp_view_class_indexes:
self.trackers.append(animal_tracker(score_threshold=self.detection_threshold,
lost_threshold=lost_threshold,
class_index=animal_class,
max_distance_moved=max_distance_moved,
tracking_iou_threshold=tracking_iou,
save_trackers=save_tracker_info))
# Get the Date-Time string and replace characters we don't want with underscores.
date_time = datetime.now()
date_time = f"date_{date_time.year}_{date_time.month}_{date_time.day}_time_" \
f"{date_time.hour}_{date_time.minute}_{date_time.second}"
project_path = os.path.join(video_output_path, f"animals_{os.path.split(input_video_name)[1][:-4]}_{date_time}")
os.makedirs(project_path,
exist_ok=True)
# Set the video output format
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
if original_with_masks and save_progress_videos:
original_with_masks_result = os.path.join(project_path,
"original_with_masks_" +
os.path.split(input_video_name)[1][:-4] + '_' +
date_time + "_result.avi")
output_writer_original_with_masks = cv2.VideoWriter(original_with_masks_result, fourcc,
frames_per_second, (width, height))
if original_with_contours and save_progress_videos:
original_with_contours_result = os.path.join(project_path,
"original_with_contours_" +
os.path.split(input_video_name)[1][:-4] + '_' +
date_time + "_result.avi")
output_writer_original_with_contours = cv2.VideoWriter(original_with_contours_result, fourcc,
frames_per_second, (width, height))
if masked_original and save_progress_videos:
masked_original_result = os.path.join(project_path,
"masked_original_" +
os.path.split(input_video_name)[1][:-4] + '_' +
date_time + "_result.avi")
output_writer_masked_original = cv2.VideoWriter(masked_original_result, fourcc,
frames_per_second, (width, height))
if num_frames == 0:
cap.release()
assert num_frames == 0, 'video not found or empty!'
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
assert int(video_start * frames_per_second) <= video_length, "Check the start time and ensure it is within " \
"the duration of the video"
# If a specific duration is not sent in for video output, use the entire video.
if duration is None or duration <= 0:
if video_length - video_start * frames_per_second > 0:
duration = video_length - video_start * frames_per_second
else:
duration = 0
else:
if (duration * frames_per_second) > video_length:
duration = int(video_length - video_start * frames_per_second)
else:
duration = int(duration * frames_per_second)
start_frame = int(video_start * frames_per_second)
self.start_frame = start_frame
self.duration = duration
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
if decorate_videos:
# Find the right size for the Frame Number text
font_scale, font_width = self.get_font_size(image_width=width, image_height=height)
self.header_font_scale = font_scale
self.header_font_width = int(font_width * .4)
self.header_center = width / 2 - (width * .15)
# Loop through the video and create the appropriate output files.
scale = width / self.inference_size if width > height else height / self.inference_size
frame_count = 0
with tqdm(total=duration, position=0, leave=True,
desc=f"Processing video: {os.path.basename(input_video_name)}") as pbar:
while frame_count < duration:
if self.stop:
break
self.current_frame = frame_count + start_frame
batch = []
for i in range(self.batch_size):
_, frame = cap.read()
if frame is None:
break
if batch_size == 1:
original_frame = copy.deepcopy(frame)
frame = self.resize_frame(frame)
batch.append(frame, )
if frame is None and len(batch) == 0:
if frame_count < duration:
print(
f'Video file metadata or content is corrupted. Skipping any remaining content after corruption.')
print('Exiting cleanly now. Please check the results.')
break
# if no animals were found in the frame, move on to the next frame
outputs = self.run_inference(batch, view_classes, input_scale=scale)
# This is an "expensive" operation so only do it if you are batching images. Then the cost
# would be worth it.
if (original_with_contours or original_with_masks or individual_videos or masked_original) and \
batch_size > 1:
cap.set(cv2.CAP_PROP_POS_FRAMES, self.current_frame)
for batch_idx, output in enumerate(outputs):
if original_with_contours or original_with_masks or individual_videos or masked_original:
if (original_with_contours or original_with_masks or individual_videos or masked_original) and \
batch_size > 1:
_, temp_frame = cap.read()
else:
temp_frame = original_frame
img_rgb = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB)
img_to_tensor = torchvision.transforms.ToTensor()(img_rgb)
img_to_tensor = img_to_tensor * 255
temp_frame = img_to_tensor.type(torch.uint8)
output['original_tensor'] = temp_frame
if len(output['boxes']) <= 0:
if original_with_masks:
foreground = self.get_original_with_masks(frame_number=self.current_frame,
view_classes=view_classes,
model_output=output,
draw_bbox=decorate_videos,
draw_text=decorate_videos,
draw_masks=True)
if save_progress_videos:
output_writer_original_with_masks.write(foreground)
if progress:
cv2.imshow('video progress: showing original with masks', foreground)
cv2.waitKey(1)
if original_with_contours:
foreground = self.get_original_with_contours(frame_number=self.current_frame,
view_classes=view_classes,
model_output=output,
draw_bbox=decorate_videos,
draw_text=decorate_videos,
draw_contour=True)
if save_progress_videos:
output_writer_original_with_contours.write(foreground)
if progress:
cv2.imshow('video progress: showing original with contours', foreground)
cv2.waitKey(1)
if masked_original:
foreground = self.get_masked_image(frame_number=self.current_frame,
view_classes=view_classes,
draw_text=decorate_videos,
model_output=output)
if save_progress_videos:
output_writer_masked_original.write(foreground)
if progress:
cv2.imshow('video progress: masked original', foreground)
cv2.waitKey(1)
for animal_class_id in self.animal_frames.keys():
self.animal_frames[animal_class_id] = {}
self.current_frame += 1
continue
for tracker in self.trackers:
tracker.track(output['boxes'], output['pred_classes'],
output['scores'], output['masks'], self.current_frame, (height, width))
self.update_animal_data(tracking=True,
frame_number=self.current_frame,
model_output=output,
keep_history=individual_videos)
if original_with_masks:
foreground = self.get_original_with_masks(frame_number=self.current_frame,
view_classes=view_classes,
model_output=output,
draw_bbox=decorate_videos,
draw_text=decorate_videos,
draw_masks=True)
if save_progress_videos:
output_writer_original_with_masks.write(foreground)
if progress:
cv2.imshow('video progress: showing original with masks', foreground)
cv2.waitKey(1)
if original_with_contours:
foreground = self.get_original_with_contours(frame_number=self.current_frame,
view_classes=view_classes,
model_output=output,
draw_bbox=decorate_videos,
draw_text=decorate_videos,
draw_contour=True)
if save_progress_videos:
output_writer_original_with_contours.write(foreground)
if progress:
cv2.imshow('video progress: showing original with contours', foreground)
cv2.waitKey(1)
if masked_original:
foreground = self.get_masked_image(frame_number=self.current_frame,
view_classes=view_classes,
draw_text=decorate_videos,
model_output=output)
if save_progress_videos:
output_writer_masked_original.write(foreground)
if progress:
cv2.imshow('video progress: masked original', foreground)
cv2.waitKey(1)
if individual_videos:
if (self.current_frame + 1) % mini_clip_length == 0:
for animal_class_id in self.animal_frames.keys():
animal_name = self.model_things[str(animal_class_id)]
for idx1, animal in enumerate(self.animal_frames[animal_class_id].keys()):
if animal != 'Frames' and len(
self.animal_frames[animal_class_id][animal]['boxes']) == mini_clip_length:
xmin = np.inf
xmax = 0
ymin = np.inf
ymax = 0
for box in self.animal_frames[animal_class_id][animal]['boxes']:
if box[0] < xmin:
xmin = int(box[0])
if box[1] < ymin:
ymin = int(box[1])
if box[2] > xmax:
xmax = int(box[2])
if box[3] > ymax:
ymax = int(box[3])
# Make the output images squares
height = ymax - ymin
width = xmax - xmin
if height > width:
side_length = height
offset_x = int((side_length - width) / 2)
offset_y = 0
else:
side_length = width
offset_x = 0
offset_y = int((side_length - height) / 2)
temp_mask_mini = np.zeros([side_length, side_length, 3], dtype=np.uint8)
temp_contours = np.zeros_like(temp_mask_mini)
frame_reference = self.current_frame - mini_clip_length + 1
for idx, animal_frame in enumerate(
self.animal_frames[animal_class_id][animal]['masks']):
os.makedirs(os.path.join(project_path,
f"{animal_name}",
f"{animal}"),
exist_ok=True)
if debug_masks:
os.makedirs(os.path.join(project_path,
f"{animal_name}",
f"{animal}",
f"debug"),
exist_ok=True)
if idx == 0:
filename = os.path.join(project_path,
f"{animal_name}",
f"{animal}",
f"{animal}_{self.current_frame}.avi")
mini_animal_writer = cv2.VideoWriter(filename,
fourcc,
frames_per_second,
(temp_contours.shape[1],
temp_contours.shape[
0]))
box = self.animal_frames[animal_class_id][animal]['boxes'][idx]
temp_frame = copy.deepcopy(
self.animal_frames[animal_class_id]['Frames'][f'{frame_reference}'])
# Important: need to make a deepcopy
# so that we don't modify the original array
temp_frame2 = copy.deepcopy(temp_frame)
temp_frame2 = temp_frame2[:, int(box[1]):int(box[3]),
int(box[0]):int(box[2])]
debug_keeper = copy.deepcopy(
np.transpose(temp_frame2, (1, 2, 0))) if debug_masks else None
temp_animal_mask = self.animal_frames[animal_class_id][animal]['masks'][
idx].astype(np.uint8)
temp_frame2 = np.transpose(temp_frame2, (1, 2, 0))
# Multiply the image and the mask, but set the max value in the mask image
# to 1. So all pixels in the image are multiplied by 0 or 1.
temp_frame2 *= temp_animal_mask.clip(max=1)[..., None]
temp_mask_mini[int(box[1]) - ymin + offset_y:int(box[3]) - ymin + offset_y,
int(box[0]) - xmin + offset_x:int(box[2]) - xmin + offset_x] = temp_frame2
# Write the masked mini frame after turning the image back to BGR for opencv
mini_animal_writer.write(temp_mask_mini)
temp_mask_mini.fill(0)
frame_reference += 1
# Saves Mask to a "debug" folder to see the mask vs original framed box
if debug_masks:
filename_mask = os.path.join(project_path,
f"{animal_name}",
f"{animal}",