-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1189 lines (1085 loc) · 65.5 KB
/
main.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
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 os
import cv2
import mss
import time
import torch
import requests
import datetime
import platform
import pyautogui
import screeninfo
import numpy as np
import tkinter as tk
import customtkinter as ctk
import pygetwindow as gw
from tkinter import ttk, messagebox
from io import BytesIO
import matplotlib.pyplot as plt
from PIL import ImageGrab, Image, ImageDraw, ImageFont, ImageTk
from PIL.Image import Resampling
from ultralytics import YOLO
from ultralytics.engine.results import Results
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
from torchvision.models import resnet18, ResNet18_Weights, resnet50, ResNet50_Weights, resnet101, ResNet101_Weights
from torchvision import transforms
from multiprocessing import Process, Queue
def screen_capture(frames_per_second = 16):
# Set up the display window name
cv2.namedWindow("Screen Capture", cv2.WINDOW_NORMAL)
while True:
# Capture the screen
screen = ImageGrab.grab()
# Convert the image to an array
screen_np = np.array(screen)
# Convert the color space from BGR (OpenCV default) to RGB
screen_np = cv2.cvtColor(screen_np, cv2.COLOR_BGR2RGB)
# Display the captured screen
cv2.imshow('Screen Capture', screen_np)
# Wait for 100 milliseconds (1 second)
if cv2.waitKey(1000//frames_per_second) & 0xFF == ord('q'):
break
# Check if the window is closed
if cv2.getWindowProperty('Screen Capture', cv2.WND_PROP_VISIBLE) < 1:
break
# Close all OpenCV windows
cv2.destroyAllWindows()
def screen_detection_segmentation(yolo_v8_size = "NANO", # [OPTIONS] "NANO", "SMALL", "MEDIUM", "LARGE", "EXTRA-LARGE"
max_frames_per_second = 16,
input_frame_dimension = (640, 480),
# Filtering Conditions ------------------------------------------------------------------------------------- Filtering Conditions
object_confidence_threshold = 0.08,
remove_oversize_objects = True,
person_detection_only = False,
# Additional Functionalties --------------------------------------------------------------------------- Additional Functionalties
segmentation_on_person_option = "NONE", # [OPTIONS] "NONE", "SEGFORMER-B5", "SEGFORMER-B5-MAX-SIZE/X", "SEGFORMER-B5-MAX-CONF/X"
segmentation_on_officers_option = "NONE", # [OPTIONS] "NONE", "MARK", "SEGMENT", "MARK_WITH_CONF_N/X", "SEGMENT_WITH_CONF_N/X"
collecting_person_patches = "NONE", # [OPTIONS] "NONE", "SAVE_EVERY_N_SECONDS/X", "SAVE_OFFICERS_ONLY_EVERY_N_SECONDS/X", "SAVE_CIVILIANS_ONLY_EVERY_N_SECONDS/X"
# Verbose --------------------------------------------------------------------------------------------------------------- Verbose
verbose = False):
#FUNCTIONS -------------------------------------------------------------------------------------------------------------------FUNCTIONS
def loading_label_fonts():
font_path_mac = "./FONTs/STHeiti Light.ttc"
font_path_win = "./FONTs/Quicksand-VariableFont_wght.ttf"
if platform.system() == "Darwin": # macOS
try:
font = ImageFont.truetype(font_path_mac, 16)
except IOError as e:
print(f"[ERROR] -------- [Failed to load macOS font from {font_path_mac} due to {e}]")
elif platform.system() == "Windows": # Windows
try:
font = ImageFont.truetype(os.path.abspath(font_path_win), 16)
except IOError as e:
print(f"[ERROR] -------- [Failed to load Windows font from {os.path.abspath(font_path_win)} due to {e}]")
else:
print(f"[WARNING] ------ [Unsupported OS. Loading default font.]")
font = ImageFont.load_default()
return font
def loading_yolo_v8_models(yolo_v8_size, device):
if yolo_v8_size == "NANO":
model = YOLO("./MODELs/yolov8n").to(device)
elif yolo_v8_size == "SMALL":
model = YOLO("./MODELs/yolov8s").to(device)
elif yolo_v8_size == "MEDIUM":
model = YOLO("./MODELs/yolov8m").to(device)
elif yolo_v8_size == "LARGE":
model = YOLO("./MODELs/yolov8l").to(device)
elif yolo_v8_size == "EXTRA-LARGE":
model = YOLO("./MODELs/yolov8x").to(device)
else:
model = YOLO("./MODELs/yolov8n").to(device)
print(f"[PROCESS] ------ [Yolo v8 {yolo_v8_size} is loaded into {device}]")
return model
def loading_segformer_b5_models(device):
model_dir = "./Models/segformer-b5-finetuned-human-parsing"
print(f"[PROCESS] ------ [Segformer b5 is loaded into {device}]")
model = SegformerForSemanticSegmentation.from_pretrained(model_dir).to(device)
image_processor = SegformerImageProcessor.from_pretrained(model_dir)
return model, image_processor
def loading_binary_classification_resnet_model(model_size = 'resnet18', fine_tune = False):
os.environ['TORCH_HOME'] = './MODELs'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if model_size == 'resnet18':
weights = ResNet18_Weights.DEFAULT
model = resnet18(weights=weights)
elif model_size == 'resnet50':
weights = ResNet50_Weights.DEFAULT
model = resnet50(weights=weights)
elif model_size == 'resnet101':
weights = ResNet101_Weights.DEFAULT
model = resnet101(weights=weights)
else:
weights = ResNet18_Weights.DEFAULT
model = resnet18(weights=weights)
if fine_tune:
for param in model.parameters():
param.requires_grad = False
num_features = model.fc.in_features
model.fc = torch.nn.Sequential(
torch.nn.Linear(num_features, 1),
torch.nn.Sigmoid()
)
model.load_state_dict(torch.load('./MODELs/officer_classificaiton_resnet18_weights.pth'))
print(f"[PROCESS] ------ [Classification {model_size} is loaded into {device}]")
return model.to(device)
def convert_input_dimension_to_GPU_dimension(input_size, stride=32):
# Adjust size so it's divisible by the model's stride
new_width = (input_size[0] // stride) * stride
new_height = (input_size[1] // stride) * stride
return (new_width, new_height)
def filter_overlapping_detectations(boxes, classes, confidences):
# Convert to a PyTorch tensor if not already one
if not isinstance(boxes, torch.Tensor):
boxes = torch.tensor(boxes)
if not isinstance(classes, torch.Tensor):
classes = torch.tensor(classes)
if not isinstance(confidences, torch.Tensor):
confidences = torch.tensor(confidences)
# Initialize a list to mark rectangles for removal
to_remove = [False] * len(boxes)
# Loop over all pairs of boxes to check for containment and same class
for i in range(len(boxes)):
for j in range(i + 1, len(boxes)):
if classes[i] == classes[j]: # Check if they belong to the same class
# Check if box i is inside box j
if (boxes[i, 0] >= boxes[j, 0] and boxes[i, 1] >= boxes[j, 1] and
boxes[i, 2] <= boxes[j, 2] and boxes[i, 3] <= boxes[j, 3]):
# Choose to remove the one with lower confidence
if confidences[i] > confidences[j]:
to_remove[j] = True
else:
to_remove[i] = True
# Check if box j is inside box i
elif (boxes[j, 0] >= boxes[i, 0] and boxes[j, 1] >= boxes[i, 1] and
boxes[j, 2] <= boxes[i, 2] and boxes[j, 3] <= boxes[i, 3]):
# Choose to remove the one with lower confidence
if confidences[j] > confidences[i]:
to_remove[i] = True
else:
to_remove[j] = True
# Filter out the boxes and classes marked for removal
filtered_boxes = boxes[torch.tensor(to_remove) == False]
filtered_classes = classes[torch.tensor(to_remove) == False]
filtered_confidences = confidences[torch.tensor(to_remove) == False]
return filtered_boxes, filtered_classes.tolist(), filtered_confidences.tolist(), len(boxes) - len(filtered_boxes)
def filter_segmentation_await_box_index(segmentation_on_person_option, boxes, classes, confidences):
if segmentation_on_person_option == "NONE":
return None
elif segmentation_on_person_option == "SEGFORMER-B5":
return [index for index, cls in enumerate(classes) if cls == 0]
elif "SEGFORMER-B5-MAX-SIZE" in segmentation_on_person_option:
x = int(segmentation_on_person_option.split("/")[1])
if isinstance(boxes, torch.Tensor):
boxes = boxes.cpu().numpy()
cls = np.array(classes)
indices_where_cls_is_zero = np.where(cls == 0)[0]
filtered_boxes = boxes[indices_where_cls_is_zero]
try:
areas = (filtered_boxes[:, 2] - filtered_boxes[:, 0]) * (filtered_boxes[:, 3] - filtered_boxes[:, 1])
except IndexError:
print(f"[ERROR] -------- [Failed to Sort Boxes by Area due to IndexError]")
return []
sorted_indices_by_area = np.argsort(-areas)
top_x_indices = indices_where_cls_is_zero[sorted_indices_by_area][:x]
return top_x_indices
elif "SEGFORMER-B5-MAX-CONF" in segmentation_on_person_option:
x = int(segmentation_on_person_option.split("/")[1])
if isinstance(boxes, torch.Tensor):
boxes = boxes.cpu().numpy()
cls = np.array(classes)
conf = np.array(confidences)
indices_where_cls_is_zero = np.where(cls == 0)[0]
filtered_conf = conf[indices_where_cls_is_zero]
try:
sorted_indices = np.argsort(-filtered_conf)
original_indices_sorted_by_conf = indices_where_cls_is_zero[sorted_indices]
except IndexError:
print(f"[ERROR] -------- [Failed to Sort Boxes by Conf due to IndexError]")
return []
return original_indices_sorted_by_conf[:x]
else:
return None
def decode_segmentation_mask(mask, labels):
label_colors = np.array([
[0, 0, 0], # Background ----- Black
[255, 255, 255], # Hat --------------------------------------------------- White
[133, 27, 27], # Hair ----------- Brown#1
[255, 0, 0], # Sunglasses
[255, 61, 0], # Upper-clothes ---------------------- Red#2
[42, 21, 171], # Skirt ----------- Blue#2
[255, 255, 255], # Pants ------------------------------------------------- White
[98, 28, 15], # Dress ---------- Brown#2
[128, 0, 128], # Belt
[154, 23, 156], # Left-shoe ------------------------ Purple
[154, 23, 156], # Right-shoe ----------------------- Purple
[236, 150, 135], # Face ------------------ Orange
[236, 150, 135], # Left-leg -------------- Orange
[236, 150, 135], # Right-leg ------------- Orange
[236, 150, 135], # Left-arm -------------- Orange
[236, 150, 135], # Right-arm ------------- Orange
[75, 0, 130], # Bag
[0, 128, 128] # Scarf
])
# Ensure label_colors covers all the labels present in the mask
color_mask = label_colors[mask]
return Image.fromarray(color_mask.astype(np.uint8))
def apply_segmentation_mask_on_picture(original_image, mask, alpha=0.96):
image_array = np.array(original_image)
mask_resized = mask.resize(original_image.size, resample=Image.BILINEAR)
mask_array = np.array(mask_resized)
blended_image = (1 - alpha) * image_array + alpha * mask_array
blended_image = blended_image.astype(np.uint8)
return Image.fromarray(blended_image)
def segmentation_picture_with_segformer_b5(person_patch, model, processor, SEGFORMER_B5_CLOTHING_LABELS):
raw_model_inputs = processor(images=person_patch, return_tensors='pt')
inputs = raw_model_inputs.to(model.device)
with torch.no_grad():
outputs = model(**inputs)
segmentation_mask = outputs.logits.argmax(dim=1).squeeze().cpu().numpy()
original_picture = raw_model_inputs
decoded_mask = decode_segmentation_mask(segmentation_mask, SEGFORMER_B5_CLOTHING_LABELS)
blended_picture = apply_segmentation_mask_on_picture(person_patch, decoded_mask, alpha=0.5)
return blended_picture
def extract_and_save_person_patches(collecting_person_patches, person_patch, conf, CURRENT_FRAME_TIME, PREVIOUS_FRAME_TIME, COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS, officer_classification_prediction):
if "SAVE_OFFICERS_ONLY_EVERY_N_SECONDS" in collecting_person_patches and (CURRENT_FRAME_TIME - PREVIOUS_FRAME_TIME > COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS or CURRENT_FRAME_TIME == PREVIOUS_FRAME_TIME) and officer_classification_prediction == 1:
folder_path = "./DATA"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"person_patch_{timestamp}_{round(conf,2)}.png"
person_patch.save(os.path.join(folder_path, filename))
return True
if "SAVE_CIVILIANS_ONLY_EVERY_N_SECONDS" in collecting_person_patches and (CURRENT_FRAME_TIME - PREVIOUS_FRAME_TIME > COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS or CURRENT_FRAME_TIME == PREVIOUS_FRAME_TIME) and officer_classification_prediction == 0:
folder_path = "./DATA"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"person_patch_{timestamp}_{round(conf,2)}.png"
person_patch.save(os.path.join(folder_path, filename))
return True
if "SAVE_EVERY_N_SECONDS" in collecting_person_patches and CURRENT_FRAME_TIME - PREVIOUS_FRAME_TIME > COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS or CURRENT_FRAME_TIME == PREVIOUS_FRAME_TIME and officer_classification_prediction == -1:
folder_path = "./DATA"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"person_patch_{timestamp}_{round(conf,2)}.png"
person_patch.save(os.path.join(folder_path, filename))
return True
return False
def classify_officers_with_classification_resnet(model, person_patch, segmentation_on_officers_option):
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
if "WITH_CONF_N" in segmentation_on_officers_option:
threshold = int(segmentation_on_officers_option.split("/")[1])/100
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
image = person_patch.convert("RGB")
image = transform(image).unsqueeze(0).to(device)
model.eval()
with torch.no_grad():
output = model(image)
if output.item() > threshold:
prediction = 1
else:
prediction = 0
else:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
image = person_patch.convert("RGB")
image = transform(image).unsqueeze(0).to(device)
model.eval()
with torch.no_grad():
output = model(image)
prediction = output.round().item()
return prediction
#FUNCTIONS ----------------------------------------------------------------------------------------------------------------------------
#CONSTs -------------------------------------------------------------------------------------------------------------------------CONSTs
YOLOV8_SKIPPING_CLASSES = [4, 6, 62, 63, 72]
YOLOV8_LABEL_BACKGROUND_COLORS = {"#FF0000_#181818": [0,1], "#FF9900_#181818": [1,14], "#341A36_#FFFFFF": [14,24], "#00C036_#181818": [24,80]}
ADJUSTED_DIMENSION = convert_input_dimension_to_GPU_dimension(input_frame_dimension)
SEGFORMER_B5_CLOTHING_LABELS = ["Background", "Hat", "Hair", "Sunglasses", " Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Face", "Left-leg", "Right-leg", "Left-arm", "Right-arm", "Bag", "Scarf"]
FONT = loading_label_fonts()
LABEL_BACKGROUND_COLOR = []
COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS = 0
if collecting_person_patches != "NONE":
COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS = int(collecting_person_patches.split("/")[1])
for i in range(0,80):
for color in YOLOV8_LABEL_BACKGROUND_COLORS:
if i >= YOLOV8_LABEL_BACKGROUND_COLORS[color][0] and i < YOLOV8_LABEL_BACKGROUND_COLORS[color][1]:
LABEL_BACKGROUND_COLOR.append(color)
break
#CONSTs -------------------------------------------------------------------------------------------------------------------------------
#OUTER STATIC VARIABLES -----------------------------------------------------------------------------------------OUTER STATIC VARIABLES
TOTAL_OBJECTS_REMOVED = 0
TOTAL_OBJECTS_REMOVED_BY_OVERLAPPING = 0
TOTAL_OBJECTS_REMOVED_BY_CONFIDENCE = 0
TOTAL_OVERSIZE_OBJECTS_REMOVED = 0
PREVIOUS_FRAME_TIME = time.time()
#OUTER STATIC VARIABLES ---------------------------------------------------------------------------------------------------------------
#RECORDING VARIABLES ===============================================================================================RECORDING VARIABLES
is_recording = False
video_writer = None
video_file_path = "output.avi"
#RECORDING VARIABLES ==================================================================================================================
#LOAD REQUIRED MODELS ---------------------------------------------------------------------------------------------LOAD REQUIRED MODELS
device = 'cuda' if torch.cuda.is_available() else 'cpu'
yolo_v8_model = loading_yolo_v8_models(yolo_v8_size, device)
if segmentation_on_person_option != "NONE":
segformer_b5_model, segformer_b5_image_processor = loading_segformer_b5_models(device)
if segmentation_on_officers_option != "NONE":
classification_officers_resnet_model = loading_binary_classification_resnet_model()
#LOAD REQUIRED MODELS -----------------------------------------------------------------------------------------------------------------
cv2.namedWindow("Screen Capture", cv2.WINDOW_NORMAL)
while True:
#INTER STATIC VARIABLES ------------------------------------------------------------------------------------------------INTER STATIC VARIABLES
OBJECT_REMOVED_FOR_THIS_FRAME = 0
CURRENT_FRAME_TIME = time.time()
#INTER STATIC VARIABLES ----------------------------------------------------------------------------------------------------------------------
original_picture = ImageGrab.grab()
reshape_picture = original_picture.resize(ADJUSTED_DIMENSION, Resampling.LANCZOS)
drawing_picture = reshape_picture.copy()
tensor_picture = torch.from_numpy(np.array(drawing_picture)).permute(2, 0, 1).float().div(255).unsqueeze(0).to(device)
raw_model_output = yolo_v8_model(tensor_picture, verbose=False)
# EXTRACT RESULTS -------------------------------------------------------------------------------------------------------------------
model_output = raw_model_output[0]
boxes = model_output.boxes.xyxy
cls = model_output.boxes.cls.tolist()
conf = model_output.boxes.conf.tolist()
names = model_output.names
boxes, cls, conf, obejct_removed_by_overlapping = filter_overlapping_detectations(boxes, cls, conf)
if segmentation_on_person_option != "NONE":
segmentation_await_box_index = filter_segmentation_await_box_index(segmentation_on_person_option, boxes, cls, conf)
# EXTRACT RESULTS -------------------------------------------------------------------------------------------------------------------
draw = ImageDraw.Draw(drawing_picture)
# DRAWING =============================================================================================================DRAWING
for index in range(len(boxes)):
box_data = boxes[index].tolist()
# INTER LOOP FILTERING CONDITIONS ===============================================================================================
if len(box_data) != 4:
continue
else:
x1, y1, x2, y2 = box_data
if round(conf[index], 2) < object_confidence_threshold:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
continue
if cls[index] in YOLOV8_SKIPPING_CLASSES:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
TOTAL_OBJECTS_REMOVED_BY_CONFIDENCE += 1
continue
if person_detection_only and cls[index] != 0:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
continue
if remove_oversize_objects and abs(x2-x1) * abs(y2-y1) > 1/3 * (input_frame_dimension[0] * input_frame_dimension[1]) or abs(x2-x1) > 4/6 * input_frame_dimension[0]:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
TOTAL_OVERSIZE_OBJECTS_REMOVED += 1
continue
# INTER LOOP FILTERING CONDITIONS ===============================================================================================
cls_label = names[cls[index]]
conf_label = int(round(conf[index], 2)*100)
label = f"{cls_label} {conf_label}%"
filling_color = LABEL_BACKGROUND_COLOR[int(cls[index])].split("_")
# EXTRACT PERSON PATCH ===================================================================================EXTRACT PERSON PATCH
officer_classification_prediction = -1
if segmentation_on_person_option != "NONE" and segmentation_await_box_index is not None and index in segmentation_await_box_index:
person_patch = reshape_picture.crop((int(x1), int(y1), int(x2), int(y2)))
blended_patch = segmentation_picture_with_segformer_b5(person_patch, segformer_b5_model, segformer_b5_image_processor, SEGFORMER_B5_CLOTHING_LABELS)
drawing_picture.paste(blended_patch, (int(x1), int(y1)))
if segmentation_on_officers_option != "NONE" and cls[index] == 0:
person_patch = reshape_picture.crop((int(x1), int(y1), int(x2), int(y2)))
officer_classification_prediction = classify_officers_with_classification_resnet(classification_officers_resnet_model, person_patch, segmentation_on_officers_option)
if officer_classification_prediction == 1:
label = f"OFFICER"
filling_color = ["#FF0000", "#FFFFFF"]
elif officer_classification_prediction == 0:
if "SEGMENT" in segmentation_on_officers_option:
continue
label = f"CIVILIAN"
filling_color = ["#00FF00", "#000000"]
else:
label = f"{cls_label} {conf_label}%"
if collecting_person_patches != "NONE":
person_patch = reshape_picture.crop((int(x1), int(y1), int(x2), int(y2)))
if extract_and_save_person_patches(collecting_person_patches, person_patch, conf[index], CURRENT_FRAME_TIME, PREVIOUS_FRAME_TIME, COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS, officer_classification_prediction):
PREVIOUS_FRAME_TIME = CURRENT_FRAME_TIME
# EXTRACT PERSON PATCH =======================================================================================================
# DRAWING =============================================================================================================DRAWING
draw.rectangle([x1, y1, x2, y2], outline=filling_color[0], width=3)
text_bg = [x1, max(y1 - 16,0), x1 + (len(cls_label)+5) * 9, max(16,y1)]
draw.rectangle(text_bg, fill=filling_color[0])
draw.text((x1+2, max(y1 - 16,0)), label, fill=filling_color[1], font=FONT)
# DRAWING ====================================================================================================================
# DRAWING ====================================================================================================================
if is_recording:
draw.rectangle([0, 0, input_frame_dimension[0], 4], fill="red", width=3)
screen_np = np.array(drawing_picture)
screen_np = cv2.cvtColor(screen_np, cv2.COLOR_BGR2RGB)
if is_recording:
if video_writer is None: # Start new video writer
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter(video_file_path, fourcc, max_frames_per_second, input_frame_dimension)
video_writer.write(screen_np) # Write frame to video file
cv2.imshow('Screen Capture', screen_np)
if verbose:
# UPDATE STATISTICS ============================================================================================================
OBJECT_REMOVED_FOR_THIS_FRAME += obejct_removed_by_overlapping
TOTAL_OBJECTS_REMOVED_BY_OVERLAPPING += obejct_removed_by_overlapping
# UPDATE STATISTICS ============================================================================================================
if OBJECT_REMOVED_FOR_THIS_FRAME > 0:
TOTAL_OBJECTS_REMOVED += OBJECT_REMOVED_FOR_THIS_FRAME
print(f"TOTAL OBJECT REMOVED: {TOTAL_OBJECTS_REMOVED}" +
f"\n\tOBJECT REMOVED FOR THIS FRAME: {OBJECT_REMOVED_FOR_THIS_FRAME}" +
f"\n\tOBJECT REMOVED BY OVERLAPPING: {TOTAL_OBJECTS_REMOVED_BY_OVERLAPPING}" +
f"\n\tOBJECT REMOVED BY CONFIDENCE: {TOTAL_OBJECTS_REMOVED_BY_CONFIDENCE}\n" +
f"\tOVERSIZE OBJECTS REMOVED: {TOTAL_OVERSIZE_OBJECTS_REMOVED}\n")
# WINDOW CONTROL ==============================================================================================WINDOW CONTROL
key = cv2.waitKey(1000//max_frames_per_second) & 0xFF
if key == ord('q'):
break
elif key == ord('r'):
if is_recording:
is_recording = False
video_writer.release()
video_writer = None
print("Recording stopped and saved.")
else:
is_recording = True
print("Recording started.")
if cv2.getWindowProperty('Screen Capture', cv2.WND_PROP_VISIBLE) < 1:
break
# WINDOW CONTROL ============================================================================================================
cv2.destroyAllWindows()
def screen_capture_and_detect_segformer_b5(frames_per_second = 16, resize_dimension = (640, 480)):
def load_model_and_extractor(model_dir, model_id):
# Check if the directory exists and is not empty
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"[SYSTEM IS NOW RUNNING ON {device}]")
required_files = ['config.json', 'pytorch_model.bin']
files_present = os.listdir(model_dir) if os.path.exists(model_dir) else []
# Check if all required files are in the directory
if all(file in files_present for file in required_files):
print("Loading model and feature extractor from local directory.")
model = SegformerForSemanticSegmentation.from_pretrained(model_dir).to(device)
feature_extractor = SegformerImageProcessor.from_pretrained(model_dir)
else:
print("Local directory is empty or missing files. Downloading model and feature extractor.")
model = SegformerForSemanticSegmentation.from_pretrained(model_id).to(device)
feature_extractor = SegformerImageProcessor.from_pretrained(model_id)
return model, feature_extractor
def decode_segmentation_mask(mask, labels):
# Define a color for each label (in RGB)
label_colors = np.array([
[0, 0, 0], # Background - Black
[255, 192, 203], # Skin - Pink
[0, 0, 255], # Hair - Blue
[255, 0, 0], # Upper-body clothing - Red
[0, 255, 0], # Lower-body clothing - Green
[0, 255, 255], # Shoes - Cyan
# Add additional colors for any other categories
[255, 255, 0], # Additional Category 1 - Yellow
[255, 165, 0], # Additional Category 2 - Orange
[128, 0, 128], # Additional Category 3 - Purple
[255, 20, 147], # Additional Category 4 - Deep Pink
[75, 0, 130], # Additional Category 5 - Indigo
[0, 128, 128], # Additional Category 6 - Teal
[255, 255, 0], # Additional Category 1 - Yellow
[255, 165, 0], # Additional Category 2 - Orange
[128, 0, 128], # Additional Category 3 - Purple
[255, 20, 147], # Additional Category 4 - Deep Pink
[75, 0, 130], # Additional Category 5 - Indigo
[0, 128, 128] # Additional Category 6 - Teal
])
# Ensure label_colors covers all the labels present in the mask
color_mask = label_colors[mask]
return Image.fromarray(color_mask.astype(np.uint8))
def apply_mask_on_image(original_image, mask, alpha=0.5):
# Convert PIL image to array
image_array = np.array(original_image)
# Resize mask to match image size
mask_resized = mask.resize(original_image.size, resample=Image.BILINEAR)
mask_array = np.array(mask_resized)
# Blend original image and color mask
blended_image = (1 - alpha) * image_array + alpha * mask_array
blended_image = blended_image.astype(np.uint8)
return Image.fromarray(blended_image)
# Initialize the YOLO model
model_directory = '../Models/segformer-b5-finetuned-human-parsing'
model_id = 'matei-dorian/segformer-b5-finetuned-human-parsing'
model, processor = load_model_and_extractor(model_directory, model_id)
# Set up the display window name
cv2.namedWindow("Screen Capture", cv2.WINDOW_NORMAL)
while True:
screen = ImageGrab.grab()
screen_resized = screen.resize(resize_dimension, Resampling.LANCZOS)
inputs = processor(images=screen_resized, return_tensors='pt')
inputs = inputs.to(model.device)
with torch.no_grad():
outputs = model(**inputs)
segmentation_mask = outputs.logits.argmax(dim=1).squeeze().cpu().numpy()
draw = ImageDraw.Draw(screen_resized)
original_image = screen # Load your original image
labels = ["Background", "Hat", "Hair", "Sunglasses", " Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Face", "Left-leg", "Right-leg", "Left-arm", "Right-arm", "Bag", "Scarf"]
decoded_mask = decode_segmentation_mask(segmentation_mask, labels)
blended_image = apply_mask_on_image(original_image, decoded_mask, alpha=0.5)
screen_np = np.array(blended_image)
# Convert the color space from BGR (OpenCV default) to RGB
screen_np = cv2.cvtColor(screen_np, cv2.COLOR_BGR2RGB)
# Display the captured screen
cv2.imshow('Screen Capture', screen_np)
# Wait for 1000 milliseconds (1 second)
key = cv2.waitKey(1000//frames_per_second) & 0xFF
if key == ord('q'):
break
# Check if the window is closed
if cv2.getWindowProperty('Screen Capture', cv2.WND_PROP_VISIBLE) < 1:
break
# Close all OpenCV windows
cv2.destroyAllWindows()
def capture_window(window_title):
windows = gw.getWindowsWithTitle(window_title)
if not windows:
raise ValueError(f"Window '{window_title}' not found")
window = windows[0]
return window
def draw_overlay(window):
while True:
# Capture screenshot of window region
screenshot = pyautogui.screenshot(region=(window.left, window.top, window.width, window.height))
screenshot_np = np.array(screenshot)
screenshot_np = cv2.cvtColor(screenshot_np, cv2.COLOR_RGB2BGR)
# Draw a rectangle on the screenshot
cv2.rectangle(screenshot_np, (50, 50), (200, 200), (0, 255, 0), 2)
# Display the image with overlay
cv2.imshow("Overlay", screenshot_np)
# Exit on pressing 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
def capture_and_process(queue,
display_number=None,
selected_window=None,
yolo_v8_size = "NANO", # [OPTIONS] "NANO", "SMALL", "MEDIUM", "LARGE", "EXTRA-LARGE"
max_frames_per_second = 16,
input_frame_dimension = (640, 480),
# Filtering Conditions ------------------------------------------------------------------------------------- Filtering Conditions
object_confidence_threshold = 0.08,
remove_oversize_objects = True,
person_detection_only = True,
# Additional Functionalties --------------------------------------------------------------------------- Additional Functionalties
segmentation_on_person_option = "NONE", # [OPTIONS] "NONE", "SEGFORMER-B5", "SEGFORMER-B5-MAX-SIZE/X", "SEGFORMER-B5-MAX-CONF/X"
segmentation_on_officers_option = "MARK", # [OPTIONS] "NONE", "MARK", "SEGMENT", "MARK_WITH_CONF_N/X", "SEGMENT_WITH_CONF_N/X"
collecting_person_patches = "NONE", # [OPTIONS] "NONE", "SAVE_EVERY_N_SECONDS/X", "SAVE_OFFICERS_ONLY_EVERY_N_SECONDS/X", "SAVE_CIVILIANS_ONLY_EVERY_N_SECONDS/X"
# Verbose --------------------------------------------------------------------------------------------------------------- Verbose
verbose = False):
#FUNCTIONS -------------------------------------------------------------------------------------------------------------------FUNCTIONS
def loading_label_fonts():
font_path_mac = "./FONTs/STHeiti Light.ttc"
font_path_win = "./FONTs/Quicksand-VariableFont_wght.ttf"
if platform.system() == "Darwin": # macOS
try:
font = ImageFont.truetype(font_path_mac, 16)
except IOError as e:
print(f"[ERROR] -------- [Failed to load macOS font from {font_path_mac} due to {e}]")
elif platform.system() == "Windows": # Windows
try:
font = ImageFont.truetype(os.path.abspath(font_path_win), 16)
except IOError as e:
print(f"[ERROR] -------- [Failed to load Windows font from {os.path.abspath(font_path_win)} due to {e}]")
else:
print(f"[WARNING] ------ [Unsupported OS. Loading default font.]")
font = ImageFont.load_default()
return font
def loading_yolo_v8_models(yolo_v8_size, device):
if yolo_v8_size == "NANO":
model = YOLO("./MODELs/yolov8n").to(device)
elif yolo_v8_size == "SMALL":
model = YOLO("./MODELs/yolov8s").to(device)
elif yolo_v8_size == "MEDIUM":
model = YOLO("./MODELs/yolov8m").to(device)
elif yolo_v8_size == "LARGE":
model = YOLO("./MODELs/yolov8l").to(device)
elif yolo_v8_size == "EXTRA-LARGE":
model = YOLO("./MODELs/yolov8x").to(device)
else:
model = YOLO("./MODELs/yolov8n").to(device)
print(f"[PROCESS] ------ [Yolo v8 {yolo_v8_size} is loaded into {device}]")
return model
def loading_segformer_b5_models(device):
model_dir = "./Models/segformer-b5-finetuned-human-parsing"
print(f"[PROCESS] ------ [Segformer b5 is loaded into {device}]")
model = SegformerForSemanticSegmentation.from_pretrained(model_dir).to(device)
image_processor = SegformerImageProcessor.from_pretrained(model_dir)
return model, image_processor
def loading_binary_classification_resnet_model(model_size = 'resnet18', fine_tune = False):
os.environ['TORCH_HOME'] = './MODELs'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if model_size == 'resnet18':
weights = ResNet18_Weights.DEFAULT
model = resnet18(weights=weights)
elif model_size == 'resnet50':
weights = ResNet50_Weights.DEFAULT
model = resnet50(weights=weights)
elif model_size == 'resnet101':
weights = ResNet101_Weights.DEFAULT
model = resnet101(weights=weights)
else:
weights = ResNet18_Weights.DEFAULT
model = resnet18(weights=weights)
if fine_tune:
for param in model.parameters():
param.requires_grad = False
num_features = model.fc.in_features
model.fc = torch.nn.Sequential(
torch.nn.Linear(num_features, 1),
torch.nn.Sigmoid()
)
model.load_state_dict(torch.load('./MODELs/officer_classificaiton_resnet18_weights.pth'))
print(f"[PROCESS] ------ [Classification {model_size} is loaded into {device}]")
return model.to(device)
def convert_input_dimension_to_GPU_dimension(input_size, stride=32):
# Adjust size so it's divisible by the model's stride
new_width = (input_size[0] // stride) * stride
new_height = (input_size[1] // stride) * stride
return (new_width, new_height)
def filter_overlapping_detectations(boxes, classes, confidences):
# Convert to a PyTorch tensor if not already one
if not isinstance(boxes, torch.Tensor):
boxes = torch.tensor(boxes)
if not isinstance(classes, torch.Tensor):
classes = torch.tensor(classes)
if not isinstance(confidences, torch.Tensor):
confidences = torch.tensor(confidences)
# Initialize a list to mark rectangles for removal
to_remove = [False] * len(boxes)
# Loop over all pairs of boxes to check for containment and same class
for i in range(len(boxes)):
for j in range(i + 1, len(boxes)):
if classes[i] == classes[j]: # Check if they belong to the same class
# Check if box i is inside box j
if (boxes[i, 0] >= boxes[j, 0] and boxes[i, 1] >= boxes[j, 1] and
boxes[i, 2] <= boxes[j, 2] and boxes[i, 3] <= boxes[j, 3]):
# Choose to remove the one with lower confidence
if confidences[i] > confidences[j]:
to_remove[j] = True
else:
to_remove[i] = True
# Check if box j is inside box i
elif (boxes[j, 0] >= boxes[i, 0] and boxes[j, 1] >= boxes[i, 1] and
boxes[j, 2] <= boxes[i, 2] and boxes[j, 3] <= boxes[i, 3]):
# Choose to remove the one with lower confidence
if confidences[j] > confidences[i]:
to_remove[i] = True
else:
to_remove[j] = True
# Filter out the boxes and classes marked for removal
filtered_boxes = boxes[torch.tensor(to_remove) == False]
filtered_classes = classes[torch.tensor(to_remove) == False]
filtered_confidences = confidences[torch.tensor(to_remove) == False]
return filtered_boxes, filtered_classes.tolist(), filtered_confidences.tolist(), len(boxes) - len(filtered_boxes)
def filter_segmentation_await_box_index(segmentation_on_person_option, boxes, classes, confidences):
if segmentation_on_person_option == "NONE":
return None
elif segmentation_on_person_option == "SEGFORMER-B5":
return [index for index, cls in enumerate(classes) if cls == 0]
elif "SEGFORMER-B5-MAX-SIZE" in segmentation_on_person_option:
x = int(segmentation_on_person_option.split("/")[1])
if isinstance(boxes, torch.Tensor):
boxes = boxes.cpu().numpy()
cls = np.array(classes)
indices_where_cls_is_zero = np.where(cls == 0)[0]
filtered_boxes = boxes[indices_where_cls_is_zero]
try:
areas = (filtered_boxes[:, 2] - filtered_boxes[:, 0]) * (filtered_boxes[:, 3] - filtered_boxes[:, 1])
except IndexError:
print(f"[ERROR] -------- [Failed to Sort Boxes by Area due to IndexError]")
return []
sorted_indices_by_area = np.argsort(-areas)
top_x_indices = indices_where_cls_is_zero[sorted_indices_by_area][:x]
return top_x_indices
elif "SEGFORMER-B5-MAX-CONF" in segmentation_on_person_option:
x = int(segmentation_on_person_option.split("/")[1])
if isinstance(boxes, torch.Tensor):
boxes = boxes.cpu().numpy()
cls = np.array(classes)
conf = np.array(confidences)
indices_where_cls_is_zero = np.where(cls == 0)[0]
filtered_conf = conf[indices_where_cls_is_zero]
try:
sorted_indices = np.argsort(-filtered_conf)
original_indices_sorted_by_conf = indices_where_cls_is_zero[sorted_indices]
except IndexError:
print(f"[ERROR] -------- [Failed to Sort Boxes by Conf due to IndexError]")
return []
return original_indices_sorted_by_conf[:x]
else:
return None
def decode_segmentation_mask(mask, labels):
label_colors = np.array([
[0, 0, 0], # Background ----- Black
[255, 255, 255], # Hat --------------------------------------------------- White
[133, 27, 27], # Hair ----------- Brown#1
[255, 0, 0], # Sunglasses
[255, 61, 0], # Upper-clothes ---------------------- Red#2
[42, 21, 171], # Skirt ----------- Blue#2
[255, 255, 255], # Pants ------------------------------------------------- White
[98, 28, 15], # Dress ---------- Brown#2
[128, 0, 128], # Belt
[154, 23, 156], # Left-shoe ------------------------ Purple
[154, 23, 156], # Right-shoe ----------------------- Purple
[236, 150, 135], # Face ------------------ Orange
[236, 150, 135], # Left-leg -------------- Orange
[236, 150, 135], # Right-leg ------------- Orange
[236, 150, 135], # Left-arm -------------- Orange
[236, 150, 135], # Right-arm ------------- Orange
[75, 0, 130], # Bag
[0, 128, 128] # Scarf
])
# Ensure label_colors covers all the labels present in the mask
color_mask = label_colors[mask]
return Image.fromarray(color_mask.astype(np.uint8))
def apply_segmentation_mask_on_picture(original_image, mask, alpha=0.96):
image_array = np.array(original_image)
mask_resized = mask.resize(original_image.size, resample=Image.BILINEAR)
mask_array = np.array(mask_resized)
blended_image = (1 - alpha) * image_array + alpha * mask_array
blended_image = blended_image.astype(np.uint8)
return Image.fromarray(blended_image)
def segmentation_picture_with_segformer_b5(person_patch, model, processor, SEGFORMER_B5_CLOTHING_LABELS):
raw_model_inputs = processor(images=person_patch, return_tensors='pt')
inputs = raw_model_inputs.to(model.device)
with torch.no_grad():
outputs = model(**inputs)
segmentation_mask = outputs.logits.argmax(dim=1).squeeze().cpu().numpy()
original_picture = raw_model_inputs
decoded_mask = decode_segmentation_mask(segmentation_mask, SEGFORMER_B5_CLOTHING_LABELS)
blended_picture = apply_segmentation_mask_on_picture(person_patch, decoded_mask, alpha=0.5)
return blended_picture
def extract_and_save_person_patches(collecting_person_patches, person_patch, conf, CURRENT_FRAME_TIME, PREVIOUS_FRAME_TIME, COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS, officer_classification_prediction):
if "SAVE_OFFICERS_ONLY_EVERY_N_SECONDS" in collecting_person_patches and (CURRENT_FRAME_TIME - PREVIOUS_FRAME_TIME > COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS or CURRENT_FRAME_TIME == PREVIOUS_FRAME_TIME) and officer_classification_prediction == 1:
folder_path = "./DATA"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"person_patch_{timestamp}_{round(conf,2)}.png"
person_patch.save(os.path.join(folder_path, filename))
return True
if "SAVE_CIVILIANS_ONLY_EVERY_N_SECONDS" in collecting_person_patches and (CURRENT_FRAME_TIME - PREVIOUS_FRAME_TIME > COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS or CURRENT_FRAME_TIME == PREVIOUS_FRAME_TIME) and officer_classification_prediction == 0:
folder_path = "./DATA"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"person_patch_{timestamp}_{round(conf,2)}.png"
person_patch.save(os.path.join(folder_path, filename))
return True
if "SAVE_EVERY_N_SECONDS" in collecting_person_patches and CURRENT_FRAME_TIME - PREVIOUS_FRAME_TIME > COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS or CURRENT_FRAME_TIME == PREVIOUS_FRAME_TIME and officer_classification_prediction == -1:
folder_path = "./DATA"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"person_patch_{timestamp}_{round(conf,2)}.png"
person_patch.save(os.path.join(folder_path, filename))
return True
return False
def classify_officers_with_classification_resnet(model, person_patch, segmentation_on_officers_option):
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
if "WITH_CONF_N" in segmentation_on_officers_option:
threshold = int(segmentation_on_officers_option.split("/")[1])/100
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
image = person_patch.convert("RGB")
image = transform(image).unsqueeze(0).to(device)
model.eval()
with torch.no_grad():
output = model(image)
if output.item() > threshold:
prediction = 1
else:
prediction = 0
else:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
image = person_patch.convert("RGB")
image = transform(image).unsqueeze(0).to(device)
model.eval()
with torch.no_grad():
output = model(image)
prediction = output.round().item()
return prediction
#FUNCTIONS ----------------------------------------------------------------------------------------------------------------------------
#CONSTs -------------------------------------------------------------------------------------------------------------------------CONSTs
INTERVAL = 1 / max_frames_per_second
YOLOV8_SKIPPING_CLASSES = [4, 6, 62, 63, 72]
YOLOV8_LABEL_BACKGROUND_COLORS = {"#FF0000_#181818": [0,1], "#FF9900_#181818": [1,14], "#341A36_#FFFFFF": [14,24], "#00C036_#181818": [24,80]}
ADJUSTED_DIMENSION = convert_input_dimension_to_GPU_dimension(input_frame_dimension)
SEGFORMER_B5_CLOTHING_LABELS = ["Background", "Hat", "Hair", "Sunglasses", " Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Face", "Left-leg", "Right-leg", "Left-arm", "Right-arm", "Bag", "Scarf"]
FONT = loading_label_fonts()
LABEL_BACKGROUND_COLOR = []
COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS = 0
if collecting_person_patches != "NONE":
COLLECTING_PERSON_PATCHES_EVERY_N_SECONDS = int(collecting_person_patches.split("/")[1])
for i in range(0,80):
for color in YOLOV8_LABEL_BACKGROUND_COLORS:
if i >= YOLOV8_LABEL_BACKGROUND_COLORS[color][0] and i < YOLOV8_LABEL_BACKGROUND_COLORS[color][1]:
LABEL_BACKGROUND_COLOR.append(color)
break
#CONSTs -------------------------------------------------------------------------------------------------------------------------------
#OUTER STATIC VARIABLES -----------------------------------------------------------------------------------------OUTER STATIC VARIABLES
TOTAL_OBJECTS_REMOVED = 0
TOTAL_OBJECTS_REMOVED_BY_OVERLAPPING = 0
TOTAL_OBJECTS_REMOVED_BY_CONFIDENCE = 0
TOTAL_OVERSIZE_OBJECTS_REMOVED = 0
PREVIOUS_FRAME_TIME = time.time()
#OUTER STATIC VARIABLES ---------------------------------------------------------------------------------------------------------------
#RECORDING VARIABLES ===============================================================================================RECORDING VARIABLES
is_recording = False
video_writer = None
video_file_path = "output.avi"
#RECORDING VARIABLES ==================================================================================================================
#LOAD REQUIRED MODELS ---------------------------------------------------------------------------------------------LOAD REQUIRED MODELS
device = 'cuda' if torch.cuda.is_available() else 'cpu'
yolo_v8_model = loading_yolo_v8_models(yolo_v8_size, device)
if segmentation_on_person_option != "NONE":
segformer_b5_model, segformer_b5_image_processor = loading_segformer_b5_models(device)
if segmentation_on_officers_option != "NONE":
classification_officers_resnet_model = loading_binary_classification_resnet_model()
#LOAD REQUIRED MODELS -----------------------------------------------------------------------------------------------------------------
#CAPTURE SCREEN ---------------------------------------------------------------------------------------------------CAPTURE SCREEN
CAPTURE_DIMENSION = (-999, -999, -999, -999)
if display_number:
if verbose:
print(f"[PROCESS] ------ [Capturing Screen: {display_number}]")
elif selected_window:
windows = gw.getWindowsWithTitle(selected_window)
if windows:
target_window = windows[0]
if verbose:
print(f"[PROCESS] ------ [Capturing Window: {target_window}]")
left, top, width, height = target_window.left, target_window.top, target_window.width, target_window.height
CAPTURE_DIMENSION = (left, top, left + width, top + height)
else:
if verbose:
print(f"[ERROR] -------- [Window not found: {selected_window}]")
original_picture = ImageGrab.grab()
else:
print(f"[ERROR] -------- [No screen or window selected]")
#CAPTURE SCREEN -----------------------------------------------------------------------------------------------------------------
while True:
#INTER STATIC VARIABLES ------------------------------------------------------------------------------------------------INTER STATIC VARIABLES
OBJECT_REMOVED_FOR_THIS_FRAME = 0
CURRENT_FRAME_TIME = time.time()
#INTER STATIC VARIABLES ----------------------------------------------------------------------------------------------------------------------
if (CAPTURE_DIMENSION == (-999, -999, -999, -999)):
if display_number:
with mss.mss() as sct:
monitor = sct.monitors[display_number]
screenshot = sct.grab(monitor)
original_picture = Image.frombytes("RGB", (screenshot.width, screenshot.height), screenshot.rgb)
else:
original_picture = ImageGrab.grab()
else:
original_picture = ImageGrab.grab(bbox=CAPTURE_DIMENSION)
reshape_picture = original_picture.resize(ADJUSTED_DIMENSION, Resampling.LANCZOS)
drawing_picture = reshape_picture.copy()
tensor_picture = torch.from_numpy(np.array(drawing_picture)).permute(2, 0, 1).float().div(255).unsqueeze(0).to(device)
raw_model_output = yolo_v8_model(tensor_picture, verbose=False)
# EXTRACT RESULTS -------------------------------------------------------------------------------------------------------------------
model_output = raw_model_output[0]
boxes = model_output.boxes.xyxy
cls = model_output.boxes.cls.tolist()
conf = model_output.boxes.conf.tolist()
names = model_output.names
boxes, cls, conf, obejct_removed_by_overlapping = filter_overlapping_detectations(boxes, cls, conf)
if segmentation_on_person_option != "NONE":
segmentation_await_box_index = filter_segmentation_await_box_index(segmentation_on_person_option, boxes, cls, conf)
# EXTRACT RESULTS -------------------------------------------------------------------------------------------------------------------
draw = ImageDraw.Draw(drawing_picture)
# DRAWING =============================================================================================================DRAWING
for index in range(len(boxes)):
box_data = boxes[index].tolist()
# INTER LOOP FILTERING CONDITIONS ===============================================================================================
if len(box_data) != 4:
continue
else:
x1, y1, x2, y2 = box_data
if round(conf[index], 2) < object_confidence_threshold:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
continue
if cls[index] in YOLOV8_SKIPPING_CLASSES:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
TOTAL_OBJECTS_REMOVED_BY_CONFIDENCE += 1
continue
if person_detection_only and cls[index] != 0:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
continue
if remove_oversize_objects and abs(x2-x1) * abs(y2-y1) > 1/3 * (input_frame_dimension[0] * input_frame_dimension[1]) or abs(x2-x1) > 4/6 * input_frame_dimension[0]:
OBJECT_REMOVED_FOR_THIS_FRAME += 1
TOTAL_OVERSIZE_OBJECTS_REMOVED += 1
continue
# INTER LOOP FILTERING CONDITIONS ===============================================================================================