utils/yolo_utils
module¶
yolo_utils
¶
YOLOv8 utility functions for object detection tasks.
This module provides utility functions for processing YOLOv8 detections, including: - Non-maximum suppression (NMS) - Bounding box conversions - Drawing functions for visualization
Functions¶
nms
¶
Apply Non-Maximum Suppression (NMS) to filter overlapping bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
np.ndarray
|
Array of bounding boxes with shape (N, 4) in (x1, y1, x2, y2) format. |
required |
scores
|
np.ndarray
|
Confidence scores for each bounding box. |
required |
iou_threshold
|
float
|
Threshold for Intersection over Union (IoU) to filter boxes. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
List of indices for boxes to keep. |
Source code in color_correction/utils/yolo_utils.py
multiclass_nms
¶
multiclass_nms(boxes: np.ndarray, scores: np.ndarray, class_ids: np.ndarray, iou_threshold: float) -> list[int]
Perform Non-Maximum Suppression (NMS) on boxes across multiple classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
np.ndarray
|
Array of bounding boxes in (x1, y1, x2, y2) format. |
required |
scores
|
np.ndarray
|
Confidence scores corresponding to each box. |
required |
class_ids
|
np.ndarray
|
Class identifier for each bounding box. |
required |
iou_threshold
|
float
|
IoU threshold to determine overlapping boxes. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
List of indices for boxes to keep. |
Source code in color_correction/utils/yolo_utils.py
compute_iou
¶
Compute the Intersection over Union (IoU) between a box and an array of boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box
|
np.ndarray
|
Single bounding box in (x1, y1, x2, y2) format. |
required |
boxes
|
np.ndarray
|
Array of boxes to compare against. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
Array containing the IoU of the input box with each box in 'boxes'. |
Source code in color_correction/utils/yolo_utils.py
xywh2xyxy
¶
Convert bounding boxes from (x, y, w, h) to (x1, y1, x2, y2) format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
np.ndarray
|
Array of bounding boxes in (x, y, w, h) format. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
Array of bounding boxes in (x1, y1, x2, y2) format. |
Source code in color_correction/utils/yolo_utils.py
draw_detections
¶
draw_detections(image: np.ndarray, boxes: list[list[int]], scores: list[float], class_ids: list[int], mask_alpha: float = DEFAULT_MASK_ALPHA) -> np.ndarray
Draw bounding boxes, labels, and semi-transparent masks on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
np.ndarray
|
The input image on which detections will be drawn. |
required |
boxes
|
list[list[int]]
|
List of bounding boxes represented as (x1, y1, x2, y2). |
required |
scores
|
list[float]
|
Confidence scores for every box. |
required |
class_ids
|
list[int]
|
Class IDs corresponding to each detection. |
required |
mask_alpha
|
float
|
Transparency of the drawn mask, default is DEFAULT_MASK_ALPHA. |
DEFAULT_MASK_ALPHA
|
Returns:
Type | Description |
---|---|
np.ndarray
|
The annotated image with drawn detections. |
Source code in color_correction/utils/yolo_utils.py
draw_box
¶
draw_box(image: np.ndarray, box: list[int], color: tuple[int, int, int] = DEFAULT_COLOR, thickness: int = DEFAULT_THICKNESS) -> np.ndarray
Draw a single bounding box on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
np.ndarray
|
The original image. |
required |
box
|
list[int]
|
Bounding box coordinates in (x1, y1, x2, y2) format. |
required |
color
|
tuple[int, int, int]
|
Color of the box in RGB format; default is DEFAULT_COLOR. |
DEFAULT_COLOR
|
thickness
|
int
|
Line thickness; default is DEFAULT_THICKNESS. |
DEFAULT_THICKNESS
|
Returns:
Type | Description |
---|---|
np.ndarray
|
The image with the box drawn on it. |
Source code in color_correction/utils/yolo_utils.py
draw_text
¶
draw_text(image: np.ndarray, text: str, box: list[int], color: tuple[int, int, int] = DEFAULT_COLOR, font_size: float = 0.001, text_thickness: int = 2) -> np.ndarray
Draw text with a background rectangle near a bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
np.ndarray
|
The image on which text is drawn. |
required |
text
|
str
|
Text string to be displayed. |
required |
box
|
list[int]
|
Bounding box coordinates (x1, y1, x2, y2) where the text will be placed. |
required |
color
|
tuple[int, int, int]
|
Background color for the text; default is DEFAULT_COLOR. |
DEFAULT_COLOR
|
font_size
|
float
|
Font scaling factor; default is 0.001. |
0.001
|
text_thickness
|
int
|
Thickness of the text stroke; default is 2. |
2
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image with the annotated text. |
Source code in color_correction/utils/yolo_utils.py
draw_masks
¶
draw_masks(image: np.ndarray, boxes: list[list[int]], classes: list[int], mask_alpha: float = DEFAULT_MASK_ALPHA) -> np.ndarray
Overlay semi-transparent masks on the image for detected objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
np.ndarray
|
The original image. |
required |
boxes
|
list[list[int]]
|
List of bounding boxes in (x1, y1, x2, y2) format. |
required |
classes
|
list[int]
|
Class IDs corresponding to each bounding box. |
required |
mask_alpha
|
float
|
Alpha value for mask transparency; default is DEFAULT_MASK_ALPHA. |
DEFAULT_MASK_ALPHA
|
Returns:
Type | Description |
---|---|
np.ndarray
|
The image with masks applied. |