Add icon annotator

This commit is contained in:
David Freilich
2024-02-19 20:30:34 +02:00
parent c1c528d378
commit 88f995803f
2 changed files with 134 additions and 0 deletions
+1
View File
@@ -25,6 +25,7 @@ from supervision.annotators.core import (
RoundBoxAnnotator,
TraceAnnotator,
TriangleAnnotator,
IconAnnotator
)
from supervision.annotators.utils import ColorLookup
from supervision.classification.core import Classifications
+133
View File
@@ -1763,3 +1763,136 @@ class PercentageBarAnnotator(BaseAnnotator):
if not all(0 <= value <= 1 for value in custom_values):
raise ValueError("All values in custom_values must be between 0 and 1.")
class IconAnnotator(BaseAnnotator):
"""
A class for drawing an icon on an image, using provided detections.
"""
def __init__(
self,
icon_path: str,
position: Position = Position.TOP_CENTER,
icon_size: float = 0.2,
color: Union[Color, ColorPalette] = ColorPalette.default(),
color_lookup: ColorLookup = ColorLookup.CLASS,
):
"""
Args:
icon_path (str): path of the icon in png format.
position (Position): The position of the heatmap. Defaults to
`TOP_CENTER`.
color (Union[Color, ColorPalette]): The color or color palette to use for
annotating detections.
icon_size (float): Represents the fraction of the original icon size to
be displayed, with a default value of 0.2
(equivalent to 20% of the original size).
color (Union[Color, ColorPalette]): The color to draw the trace, can be
a single color or a color palette.
color_lookup (str): Strategy for mapping colors to annotations.
Options are `INDEX`, `CLASS`, `TRACE`.
"""
self.color: Union[Color, ColorPalette] = color
self.color_lookup: ColorLookup = color_lookup
self.position = position
self.icon = cv2.imread(icon_path, cv2.IMREAD_UNCHANGED)
if self.icon is None:
print(f"Error: Couldn't load the icon image from {icon_path}")
return
self.icon_size = icon_size
@staticmethod
def draw_icon(
icon: np.ndarray,
new_color: List,
scene: np.ndarray,
cordinates: Tuple[int, int],
) -> np.ndarray:
new_icon = np.ones_like(icon) * new_color
new_icon[:, :, 3] = icon[:, :, 3]
x, y = cordinates
icon_height, icon_width, _ = new_icon.shape
scene_height, scene_width, _ = scene.shape
w = min(icon_width, scene_width, icon_width + x, scene_width - x)
h = min(icon_height, scene_height, icon_height + y, scene_height - y)
if w < 1 or h < 1:
print("Icon size too small")
return
# clip icon and scene to the overlapping regions
icon_x = max(0, x)
icon_y = max(0, y)
scene_x = max(0, x * -1)
scene_y = max(0, y * -1)
# Create a copy of the background region where the icon will be pasted
new_icon = new_icon[scene_y: scene_y + h, scene_x: scene_x + w]
bg_region = scene[icon_y: icon_y + h, icon_x: icon_x + w].copy()
# Extract the alpha channel from the icon
alpha_channel = new_icon[:, :, 3]
# Apply alpha blending to combine the icon and the background
for c in range(3): # Iterate over RGB channels
bg_region[:, :, c] = (1 - alpha_channel / 255.0) * bg_region[:, :, c] + (
alpha_channel / 255.0
) * new_icon[:, :, c]
# Paste the blended icon region back into the background
scene[icon_y: icon_y + h, icon_x: icon_x + w] = bg_region
return scene
def annotate(
self,
scene: np.ndarray,
detections: Detections,
custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
"""
Annotates the given scene with icons based on the provided detections.
Args:
scene (np.ndarray): The image where bounding boxes will be drawn.
detections (Detections): Object detections to annotate.
custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
Allows to override the default color mapping strategy.
Returns:
np.ndarray: The annotated image.
Example:
```python
>>> import supervision as sv
>>> image = ...
>>> detections = sv.Detections(...)
>>> icon_annotator = sv.IconAnnotator(icon_path='path_of_icon')
>>> annotated_frame = icon_annotator.annotate(
... scene=image.copy(),
... detections=detections
... )
```
"""
resized_icon_h, resized_icon_w = int(self.icon.shape[0] * self.icon_size), int(
self.icon.shape[1] * self.icon_size
)
resized_icon = cv2.resize(
self.icon, (resized_icon_h, resized_icon_w), interpolation=cv2.INTER_AREA
)
xy = detections.get_anchors_coordinates(anchor=self.position)
for detection_idx in range(len(detections)):
color = resolve_color(
color=self.color,
detections=detections,
detection_idx=detection_idx,
color_lookup=self.color_lookup
if custom_color_lookup is None
else custom_color_lookup,
)
coordinates = (
int(xy[detection_idx, 0] - resized_icon_w / 2),
int(xy[detection_idx, 1] - resized_icon_h),
)
new_color = list(color.as_bgr()) + [
1
] # [1](alpha) is added to convert the color to BGRA format
scene = self.draw_icon(resized_icon, new_color, scene, coordinates)
return scene