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

Modified triangle and dot in annotators-core #1294

Merged
merged 15 commits into from
Jun 20, 2024
Merged
Changes from 4 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
100 changes: 79 additions & 21 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,10 @@ class DotAnnotator(BaseAnnotator):
def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.DEFAULT,
radius: int = 4,
radius: int = 5,
onuralpszr marked this conversation as resolved.
Show resolved Hide resolved
position: Position = Position.CENTER,
color_lookup: ColorLookup = ColorLookup.CLASS,
outer_thickness: int = 0,
):
"""
Args:
Expand All @@ -847,6 +848,7 @@ def __init__(
self.radius: int = radius
self.position: Position = position
self.color_lookup: ColorLookup = color_lookup
self.outer_thickness = outer_thickness
onuralpszr marked this conversation as resolved.
Show resolved Hide resolved

@convert_for_annotation_method
def annotate(
Expand Down Expand Up @@ -899,6 +901,7 @@ def annotate(
)
center = (int(xy[detection_idx, 0]), int(xy[detection_idx, 1]))
cv2.circle(scene, center, self.radius, color.as_bgr(), -1)
cv2.circle(scene, center, self.radius, (0, 0, 0), self.outer_thickness)
onuralpszr marked this conversation as resolved.
Show resolved Hide resolved
return scene


Expand Down Expand Up @@ -1613,6 +1616,7 @@ def __init__(
height: int = 10,
position: Position = Position.TOP_CENTER,
color_lookup: ColorLookup = ColorLookup.CLASS,
outline_thickness: int = 0,
):
"""
Args:
Expand All @@ -1629,6 +1633,7 @@ def __init__(
self.height: int = height
self.position: Position = position
self.color_lookup: ColorLookup = color_lookup
self.outline_thickness: int = outline_thickness

@convert_for_annotation_method
def annotate(
Expand Down Expand Up @@ -1669,27 +1674,80 @@ def annotate(
![triangle-annotator-example](https://media.roboflow.com/
supervision-annotator-examples/triangle-annotator-example.png)
"""
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,
)
tip_x, tip_y = int(xy[detection_idx, 0]), int(xy[detection_idx, 1])
vertices = np.array(
[
[tip_x - self.base // 2, tip_y - self.height],
[tip_x + self.base // 2, tip_y - self.height],
[tip_x, tip_y],
],
np.int32,
)
if self.outline_thickness:
onuralpszr marked this conversation as resolved.
Show resolved Hide resolved
for detection_idx in range(len(detections)):
x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
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,
)

midpoint_top = (
(x1 + x2) // 2,
(y1 + y1) // 2,
) # midpoint((x1, y1), (x2, y1))

# shifted origin/centroid
cen2 = (midpoint_top[0], midpoint_top[1] - 18)
tri_vertices = np.array(
[
[cen2[0], cen2[1] + 12],
[cen2[0] - 10, cen2[1] - 10],
[cen2[0] + 10, cen2[1] - 10],
]
)

# upperhead pointer
cv2.drawContours(
scene, [tri_vertices], -1, color.as_bgr(), thickness=-1
) # BGR -->(0,255,232)
cv2.line(
scene,
([cen2[0], cen2[1] + 12]),
([cen2[0] - 10, cen2[1] - 10]),
(0, 0, 0),
self.outline_thickness,
)
cv2.line(
scene,
[cen2[0] - 10, cen2[1] - 10],
[cen2[0] + 10, cen2[1] - 10],
(0, 0, 0),
self.outline_thickness,
)
cv2.line(
scene,
[cen2[0] + 10, cen2[1] - 10],
[cen2[0], cen2[1] + 12],
(0, 0, 0),
self.outline_thickness,
)
else:
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,
)
tip_x, tip_y = int(xy[detection_idx, 0]), int(xy[detection_idx, 1])
vertices = np.array(
[
[tip_x - self.base // 2, tip_y - self.height],
[tip_x + self.base // 2, tip_y - self.height],
[tip_x, tip_y],
],
np.int32,
)

cv2.fillPoly(scene, [vertices], color.as_bgr())
cv2.fillPoly(scene, [vertices], color.as_bgr())

return scene

Expand Down