Core
Detections¶
Data class containing information about the detections in a video frame.
Attributes:
xyxy (np.ndarray): An array of shape (n, 4)
containing the bounding boxes coordinates in format [x1, y1, x2, y2]
mask: (Optional[np.ndarray]): An array of shape (n, W, H)
containing the segmentation masks.
confidence (Optional[np.ndarray]): An array of shape (n,)
containing the confidence scores of the detections.
class_id (Optional[np.ndarray]): An array of shape (n,)
containing the class ids of the detections.
tracker_id (Optional[np.ndarray]): An array of shape (n,)
containing the tracker ids of the detections.
Source code in supervision/detection/core.py
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 |
|
area: np.ndarray
property
¶
Calculate the area of each detection in the set of object detections. If masks field is defined property returns are of each mask. If only box is given property return area of each box.
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: An array of floats containing the area of each detection in the format of |
box_area: np.ndarray
property
¶
Calculate the area of each bounding box in the set of object detections.
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: An array of floats containing the area of each bounding box in the format of |
__iter__()
¶
Iterates over the Detections object and yield a tuple of (xyxy, mask, confidence, class_id, tracker_id)
for each detection.
Source code in supervision/detection/core.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
__len__()
¶
Returns the number of detections in the Detections object.
Source code in supervision/detection/core.py
76 77 78 79 80 |
|
from_sam(sam_result)
classmethod
¶
Creates a Detections instance from Segment Anything Model (SAM) by Meta AI.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sam_result |
List[dict]
|
The output Results instance from SAM |
required |
Returns:
Name | Type | Description |
---|---|---|
Detections |
Detections
|
A new Detections object. |
Example
>>> from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
>>> import supervision as sv
>>> sam = sam_model_registry[MODEL_TYPE](checkpoint=CHECKPOINT_PATH).to(device=DEVICE)
>>> mask_generator = SamAutomaticMaskGenerator(sam)
>>> sam_result = mask_generator.generate(IMAGE)
>>> detections = sv.Detections.from_sam(sam_result=sam_result)
Source code in supervision/detection/core.py
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 |
|
from_transformers(transformers_results)
classmethod
¶
Creates a Detections instance from Object Detection Transformer output Results
Returns:
Name | Type | Description |
---|---|---|
Detections |
Detections
|
A new Detections object. |
Source code in supervision/detection/core.py
191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
from_yolov5(yolov5_results)
classmethod
¶
Creates a Detections instance from a YOLOv5 output Detections
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yolov5_results |
Detections
|
The output Detections instance from YOLOv5 |
required |
Returns:
Name | Type | Description |
---|---|---|
Detections |
Detections
|
A new Detections object. |
Example
>>> import torch
>>> from supervision import Detections
>>> model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
>>> results = model(IMAGE)
>>> detections = Detections.from_yolov5(results)
Source code in supervision/detection/core.py
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 |
|
from_yolov8(yolov8_results)
classmethod
¶
Creates a Detections instance from a YOLOv8 output Results
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yolov8_results |
Results
|
The output Results instance from YOLOv8 |
required |
Returns:
Name | Type | Description |
---|---|---|
Detections |
Detections
|
A new Detections object. |
Example
>>> from ultralytics import YOLO
>>> from supervision import Detections
>>> model = YOLO('yolov8s.pt')
>>> yolov8_results = model(IMAGE)[0]
>>> detections = Detections.from_yolov8(yolov8_results)
Source code in supervision/detection/core.py
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 |
|
get_anchor_coordinates(anchor)
¶
Returns the bounding box coordinates for a specific anchor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
anchor |
Position
|
Position of bounding box anchor for which to return the coordinates. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: An array of shape |
Source code in supervision/detection/core.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
with_nms(threshold=0.5, class_agnostic=False)
¶
Perform non-maximum suppression on the current set of object detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
float
|
The intersection-over-union threshold to use for non-maximum suppression. Defaults to 0.5. |
0.5
|
class_agnostic |
bool
|
Whether to perform class-agnostic non-maximum suppression. If True, the class_id of each detection will be ignored. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Detections |
Detections
|
A new Detections object containing the subset of detections after non-maximum suppression. |
Raises:
Type | Description |
---|---|
AssertionError
|
If |
Source code in supervision/detection/core.py
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 |
|