Visualization utilities
draw_boxes_on_image(pil_img, boxes, labels=None, scores=None, box_color=(0, 255, 0), text_color=(255, 255, 255), scaled=(640, 640))
Draws bounding boxes with optional labels and scores onto an image.
This function is vectorized and handles scaling of boxes internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pil_img
|
Image
|
The original image in PIL format. |
required |
boxes
|
ndarray | Tensor
|
An array or tensor of shape (R, 4) containing R boxes in [x1, y1, x2, y2] format. |
required |
labels
|
list[str]
|
A list of class names for each box. |
None
|
scores
|
list[float]
|
A list of confidence scores for each box. |
None
|
box_color
|
tuple[int, int, int]
|
The (B, G, R) color for the bounding boxes, by default (0, 255, 0). |
(0, 255, 0)
|
text_color
|
tuple[int, int, int]
|
The (B, G, R) color for the text labels, by default (255, 255, 255). |
(255, 255, 255)
|
scaled
|
tuple[int, int]
|
The (height, width) shape the boxes are scaled from (i.e., the model input shape), by default (640, 640). |
(640, 640)
|
Returns:
| Type | Description |
|---|---|
Image
|
A new PIL Image with the annotations drawn on it. |
Source code in hierarchical_loss/viz_utils.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 | |
rescale_boxes(pred_boxes, from_shape, to_shape)
Rescales predicted boxes from model input shape to original image shape.
This function works for both NumPy arrays and PyTorch tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred_boxes
|
ndrray | Tensor
|
An array or tensor of shape (..., 4) containing boxes in [x1, y1, x2, y2] format. |
required |
from_shape
|
tuple[int, int]
|
The original (height, width) of the model input, e.g., (640, 640). |
required |
to_shape
|
Tuple[int, int]
|
The target (height, width) of the original image. |
required |
Returns:
| Type | Description |
|---|---|
ndarray | Tensor
|
The rescaled boxes, in the same type as |
Examples:
>>> boxes_np = np.array([[10, 10, 60, 60]], dtype=np.float32)
>>> rescale_boxes(boxes_np, from_shape=(100, 100), to_shape=(200, 400))
array([[ 40., 20., 240., 120.]], dtype=float32)
>>> boxes_torch = torch.tensor([[10, 10, 60, 60]], dtype=torch.float32)
>>> rescale_boxes(boxes_torch, from_shape=(100, 100), to_shape=(200, 400))
tensor([[ 40., 20., 240., 120.]])
Source code in hierarchical_loss/viz_utils.py
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 | |