-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoloSahi.py
127 lines (91 loc) · 5.39 KB
/
YoloSahi.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
# Developed By Sihab Sahariar
import cv2
from sahi import AutoDetectionModel
from sahi.predict import get_prediction, get_sliced_prediction, predict
from sahi.utils.yolov8 import download_yolov8n_model, download_yolov8m_model, download_yolov8l_model, download_yolov8x_model
from sort import *
from helper import draw_bbox
def video_infer_sahi(cap, frame_skip, detection_model, tracker, slice_width=512, slice_height=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2, scale_percent=50):
while True:
c = 0
ret, frame = cap.read()
if not ret:
break
if c % frame_skip != 0:
continue
c += 1
w = int(frame.shape[1] * scale_percent / 100)
h = int(frame.shape[0] * scale_percent / 100)
dim = (w, h)
# resize image
frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
# SAHI Prediction
results = get_sliced_prediction(frame,
detection_model,
slice_width=512,
slice_height=512, # slice height and width which means the image will be divided into 512x512 slices
overlap_height_ratio=0.2, # overlap ratio for height which means 20% of the height will be overlapped
overlap_width_ratio=0.2) # overlap ratio for width which means 20% of the width will be overlapped
# Sahi Prediction
object_prediction_list = results.object_prediction_list
# Initialize detections array
detections = np.empty((0, 5))
confidences = []
for ind, _ in enumerate(object_prediction_list):
box = int(object_prediction_list[ind].bbox.minx),int(object_prediction_list[ind].bbox.miny),int(object_prediction_list[ind].bbox.maxx), int(object_prediction_list[ind].bbox.maxy)
clss = object_prediction_list[ind].category.name
if clss == 'person': # Only consider 'person' class
detections = np.vstack((detections, [box[0], box[1], box[2], box[3], object_prediction_list[ind].score.value]))
confidences.append(object_prediction_list[ind].score.value*100)
# Update the tracker with the new detections
resultsTracker = tracker.update(detections)
# Draw bounding boxes and labels on the frame
for i,obj in enumerate(resultsTracker):
x1, y1, x2, y2, id = obj # Get coordinates and ID
w, h = x2 - x1, y2 - y1 # Calculate width and height
label = f"Person #{id}" # Label for the bounding box
draw_bbox(frame, int(x1), int(y1), int(x2), int(y2), label, confidences[i])
# Display the frame
cv2.imshow("Yolov8+SAHI Inference", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Release the video and close all windows
cap.release()
cv2.destroyAllWindows()
def image_infer(image_path, detection_model, tracker, slice_width=512, slice_height=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2, scale_percent=50):
frame = cv2.imread(image_path)
w = int(frame.shape[1] * scale_percent / 100)
h = int(frame.shape[0] * scale_percent / 100)
dim = (w, h)
# resize image
frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
# SAHI Prediction
results = get_sliced_prediction(frame,
detection_model,
slice_width=512,
slice_height=512, # slice height and width which means the image will be divided into 512x512 slices
overlap_height_ratio=0.2, # overlap ratio for height which means 20% of the height will be overlapped
overlap_width_ratio=0.2) # overlap ratio for width which means 20% of the width will be overlapped
# Sahi Prediction
object_prediction_list = results.object_prediction_list
# Initialize detections array
detections = np.empty((0, 5))
confidences = []
for ind, _ in enumerate(object_prediction_list):
box = int(object_prediction_list[ind].bbox.minx),int(object_prediction_list[ind].bbox.miny),int(object_prediction_list[ind].bbox.maxx), int(object_prediction_list[ind].bbox.maxy)
clss = object_prediction_list[ind].category.name
if clss == 'person': # Only consider 'person' class
detections = np.vstack((detections, [box[0], box[1], box[2], box[3], object_prediction_list[ind].score.value]))
confidences.append(object_prediction_list[ind].score.value)
# Update the tracker with the new detections
resultsTracker = tracker.update(detections)
# Draw bounding boxes and labels on the frame
for i,obj in enumerate(resultsTracker):
x1, y1, x2, y2, id = obj # Get coordinates and ID
w, h = x2 - x1, y2 - y1 # Calculate width and height
label = f"Person #{id}" # Label for the bounding box
draw_bbox(frame, int(x1), int(y1), int(x2), int(y2), label, confidences[i])
# Display the frame
cv2.imshow("Yolov8+SAHI Inference", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()