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|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
|
4 |
+
class_names = [
|
5 |
+
"person",
|
6 |
+
"bicycle",
|
7 |
+
"car",
|
8 |
+
"motorcycle",
|
9 |
+
"airplane",
|
10 |
+
"bus",
|
11 |
+
"train",
|
12 |
+
"truck",
|
13 |
+
"boat",
|
14 |
+
"traffic light",
|
15 |
+
"fire hydrant",
|
16 |
+
"stop sign",
|
17 |
+
"parking meter",
|
18 |
+
"bench",
|
19 |
+
"bird",
|
20 |
+
"cat",
|
21 |
+
"dog",
|
22 |
+
"horse",
|
23 |
+
"sheep",
|
24 |
+
"cow",
|
25 |
+
"elephant",
|
26 |
+
"bear",
|
27 |
+
"zebra",
|
28 |
+
"giraffe",
|
29 |
+
"backpack",
|
30 |
+
"umbrella",
|
31 |
+
"handbag",
|
32 |
+
"tie",
|
33 |
+
"suitcase",
|
34 |
+
"frisbee",
|
35 |
+
"skis",
|
36 |
+
"snowboard",
|
37 |
+
"sports ball",
|
38 |
+
"kite",
|
39 |
+
"baseball bat",
|
40 |
+
"baseball glove",
|
41 |
+
"skateboard",
|
42 |
+
"surfboard",
|
43 |
+
"tennis racket",
|
44 |
+
"bottle",
|
45 |
+
"wine glass",
|
46 |
+
"cup",
|
47 |
+
"fork",
|
48 |
+
"knife",
|
49 |
+
"spoon",
|
50 |
+
"bowl",
|
51 |
+
"banana",
|
52 |
+
"apple",
|
53 |
+
"sandwich",
|
54 |
+
"orange",
|
55 |
+
"broccoli",
|
56 |
+
"carrot",
|
57 |
+
"hot dog",
|
58 |
+
"pizza",
|
59 |
+
"donut",
|
60 |
+
"cake",
|
61 |
+
"chair",
|
62 |
+
"couch",
|
63 |
+
"potted plant",
|
64 |
+
"bed",
|
65 |
+
"dining table",
|
66 |
+
"toilet",
|
67 |
+
"tv",
|
68 |
+
"laptop",
|
69 |
+
"mouse",
|
70 |
+
"remote",
|
71 |
+
"keyboard",
|
72 |
+
"cell phone",
|
73 |
+
"microwave",
|
74 |
+
"oven",
|
75 |
+
"toaster",
|
76 |
+
"sink",
|
77 |
+
"refrigerator",
|
78 |
+
"book",
|
79 |
+
"clock",
|
80 |
+
"vase",
|
81 |
+
"scissors",
|
82 |
+
"teddy bear",
|
83 |
+
"hair drier",
|
84 |
+
"toothbrush",
|
85 |
+
]
|
86 |
+
|
87 |
+
# Create a list of colors for each class where each color is a tuple of 3 integer values
|
88 |
+
rng = np.random.default_rng(3)
|
89 |
+
colors = rng.uniform(0, 255, size=(len(class_names), 3))
|
90 |
+
|
91 |
+
|
92 |
+
def nms(boxes, scores, iou_threshold):
|
93 |
+
# Sort by score
|
94 |
+
sorted_indices = np.argsort(scores)[::-1]
|
95 |
+
|
96 |
+
keep_boxes = []
|
97 |
+
while sorted_indices.size > 0:
|
98 |
+
# Pick the last box
|
99 |
+
box_id = sorted_indices[0]
|
100 |
+
keep_boxes.append(box_id)
|
101 |
+
|
102 |
+
# Compute IoU of the picked box with the rest
|
103 |
+
ious = compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :])
|
104 |
+
|
105 |
+
# Remove boxes with IoU over the threshold
|
106 |
+
keep_indices = np.where(ious < iou_threshold)[0]
|
107 |
+
|
108 |
+
# print(keep_indices.shape, sorted_indices.shape)
|
109 |
+
sorted_indices = sorted_indices[keep_indices + 1]
|
110 |
+
|
111 |
+
return keep_boxes
|
112 |
+
|
113 |
+
|
114 |
+
def multiclass_nms(boxes, scores, class_ids, iou_threshold):
|
115 |
+
unique_class_ids = np.unique(class_ids)
|
116 |
+
|
117 |
+
keep_boxes = []
|
118 |
+
for class_id in unique_class_ids:
|
119 |
+
class_indices = np.where(class_ids == class_id)[0]
|
120 |
+
class_boxes = boxes[class_indices, :]
|
121 |
+
class_scores = scores[class_indices]
|
122 |
+
|
123 |
+
class_keep_boxes = nms(class_boxes, class_scores, iou_threshold)
|
124 |
+
keep_boxes.extend(class_indices[class_keep_boxes])
|
125 |
+
|
126 |
+
return keep_boxes
|
127 |
+
|
128 |
+
|
129 |
+
def compute_iou(box, boxes):
|
130 |
+
# Compute xmin, ymin, xmax, ymax for both boxes
|
131 |
+
xmin = np.maximum(box[0], boxes[:, 0])
|
132 |
+
ymin = np.maximum(box[1], boxes[:, 1])
|
133 |
+
xmax = np.minimum(box[2], boxes[:, 2])
|
134 |
+
ymax = np.minimum(box[3], boxes[:, 3])
|
135 |
+
|
136 |
+
# Compute intersection area
|
137 |
+
intersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin)
|
138 |
+
|
139 |
+
# Compute union area
|
140 |
+
box_area = (box[2] - box[0]) * (box[3] - box[1])
|
141 |
+
boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
|
142 |
+
union_area = box_area + boxes_area - intersection_area
|
143 |
+
|
144 |
+
# Compute IoU
|
145 |
+
iou = intersection_area / union_area
|
146 |
+
|
147 |
+
return iou
|
148 |
+
|
149 |
+
|
150 |
+
def xywh2xyxy(x):
|
151 |
+
# Convert bounding box (x, y, w, h) to bounding box (x1, y1, x2, y2)
|
152 |
+
y = np.copy(x)
|
153 |
+
y[..., 0] = x[..., 0] - x[..., 2] / 2
|
154 |
+
y[..., 1] = x[..., 1] - x[..., 3] / 2
|
155 |
+
y[..., 2] = x[..., 0] + x[..., 2] / 2
|
156 |
+
y[..., 3] = x[..., 1] + x[..., 3] / 2
|
157 |
+
return y
|
158 |
+
|
159 |
+
|
160 |
+
def draw_detections(image, boxes, scores, class_ids, mask_alpha=0.3):
|
161 |
+
det_img = image.copy()
|
162 |
+
|
163 |
+
img_height, img_width = image.shape[:2]
|
164 |
+
font_size = min([img_height, img_width]) * 0.0006
|
165 |
+
text_thickness = int(min([img_height, img_width]) * 0.001)
|
166 |
+
|
167 |
+
#det_img = draw_masks(det_img, boxes, class_ids, mask_alpha)
|
168 |
+
|
169 |
+
# Draw bounding boxes and labels of detections
|
170 |
+
for class_id, box, score in zip(class_ids, boxes, scores):
|
171 |
+
color = colors[class_id]
|
172 |
+
|
173 |
+
draw_box(det_img, box, color)
|
174 |
+
|
175 |
+
label = class_names[class_id]
|
176 |
+
caption = f"{label} {int(score * 100)}%"
|
177 |
+
draw_text(det_img, caption, box, color, font_size, text_thickness)
|
178 |
+
|
179 |
+
return det_img
|
180 |
+
|
181 |
+
|
182 |
+
def draw_box(
|
183 |
+
image: np.ndarray,
|
184 |
+
box: np.ndarray,
|
185 |
+
color: tuple[int, int, int] = (0, 0, 255),
|
186 |
+
thickness: int = 2,
|
187 |
+
) -> np.ndarray:
|
188 |
+
x1, y1, x2, y2 = box.astype(int)
|
189 |
+
return cv2.rectangle(image, (x1, y1), (x2, y2), color, thickness)
|
190 |
+
|
191 |
+
|
192 |
+
def draw_text(
|
193 |
+
image: np.ndarray,
|
194 |
+
text: str,
|
195 |
+
box: np.ndarray,
|
196 |
+
color: tuple[int, int, int] = (0, 0, 255),
|
197 |
+
font_size: float = 0.001,
|
198 |
+
text_thickness: int = 2,
|
199 |
+
) -> np.ndarray:
|
200 |
+
x1, y1, x2, y2 = box.astype(int)
|
201 |
+
(tw, th), _ = cv2.getTextSize(
|
202 |
+
text=text,
|
203 |
+
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
|
204 |
+
fontScale=font_size,
|
205 |
+
thickness=text_thickness,
|
206 |
+
)
|
207 |
+
th = int(th * 1.2)
|
208 |
+
|
209 |
+
cv2.rectangle(image, (x1, y1), (x1 + tw, y1 - th), color, -1)
|
210 |
+
|
211 |
+
return cv2.putText(
|
212 |
+
image,
|
213 |
+
text,
|
214 |
+
(x1, y1),
|
215 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
216 |
+
font_size,
|
217 |
+
(255, 255, 255),
|
218 |
+
text_thickness,
|
219 |
+
cv2.LINE_AA,
|
220 |
+
)
|
221 |
+
|
222 |
+
|
223 |
+
def draw_masks(
|
224 |
+
image: np.ndarray, boxes: np.ndarray, classes: np.ndarray, mask_alpha: float = 0.3
|
225 |
+
) -> np.ndarray:
|
226 |
+
mask_img = image.copy()
|
227 |
+
|
228 |
+
# Draw bounding boxes and labels of detections
|
229 |
+
for box, class_id in zip(boxes, classes):
|
230 |
+
color = colors[class_id]
|
231 |
+
|
232 |
+
x1, y1, x2, y2 = box.astype(int)
|
233 |
+
|
234 |
+
# Draw fill rectangle in mask image
|
235 |
+
cv2.rectangle(mask_img, (x1, y1), (x2, y2), color, -1)
|
236 |
+
|
237 |
+
return cv2.addWeighted(mask_img, mask_alpha, image, 1 - mask_alpha, 0)
|