Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(annotator): 🚀 introduce circle annotator #386

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/detection/annotate.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

:::supervision.annotators.core.BoxCornerAnnotator

## CircleAnnotator

:::supervision.annotators.core.CircleAnnotator

## LabelAnnotator

:::supervision.annotators.core.LabelAnnotator
1 change: 1 addition & 0 deletions supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from supervision.annotators.core import (
BoundingBoxAnnotator,
BoxCornerAnnotator,
CircleAnnotator,
EllipseAnnotator,
LabelAnnotator,
MaskAnnotator,
Expand Down
84 changes: 84 additions & 0 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import sqrt
from typing import List, Tuple, Union

import cv2
Expand Down Expand Up @@ -315,6 +316,89 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
return scene


class CircleAnnotator(BaseAnnotator):
"""
A class for drawing circle on an image using provided detections.
"""

def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.default(),
thickness: int = 4,
color_map: str = "class",
):
"""
Args:
color (Union[Color, ColorPalette]): The color or color palette to use for
annotating detections.
thickness (int): Thickness of the circle line.
color_map (str): Strategy for mapping colors to annotations.
Options are `index`, `class`, or `track`.
"""

self.color: Union[Color, ColorPalette] = color
self.thickness: int = thickness
self.color_map: ColorMap = ColorMap(color_map)

def annotate(
self,
scene: np.ndarray,
detections: Detections,
) -> np.ndarray:
"""
Draws circle on the frame using the detections provided.
Args:
scene (np.ndarray): The image on which the circle will be drawn
detections (Detections): The detections for which the
circle will be drawn
Returns:
np.ndarray: The image with the circle drawn on it
Example:
```python
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> circle_annotator = sv.CircleAnnotator()
>>> annotated_frame = circle_annotator.annotate(
... scene=image.copy(),
... detections=detections
... )
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this link to an example annotated frame in docs? https://media.roboflow.com/supervision-annotator-examples/circle-annotator-example.png In a similar way, we have done it for other annotators.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SkalskiP I was going to ask that, awesome thank you !!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries ;) let me know once you update it. ;)



![circle-annotator-example](https://media.roboflow.com/
supervision-annotator-examples/circle-annotator-example.png)
"""
for detection_idx in range(len(detections)):
x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
center = ((x1 + x2) // 2, (y1 + y2) // 2)
distance = sqrt((x1 - center[0]) ** 2 + (y1 - center[1]) ** 2)

idx = resolve_color_idx(
detections=detections,
detection_idx=detection_idx,
color_map=self.color_map,
)

color = (
self.color.by_idx(idx)
if isinstance(self.color, ColorPalette)
else self.color
)

cv2.circle(
img=scene,
center=center,
radius=int(distance),
color=color.as_bgr(),
thickness=self.thickness,
)

return scene


class LabelAnnotator:
"""
A class for annotating labels on an image using provided detections.
Expand Down