Skip to content

Commit

Permalink
Documentation for PolygonZone and PolygonZoneAnnotator
Browse files Browse the repository at this point in the history
  • Loading branch information
SkalskiP committed Apr 10, 2023
1 parent d02bac7 commit c1ccff4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/detection/tools/polygon_zone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## PolygonZone

:::supervision.detection.tools.polygon_zone.PolygonZone

## PolygonZoneAnnotator

:::supervision.detection.tools.polygon_zone.PolygonZoneAnnotator
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ nav:
- Core: detection/core.md
- Annotate: detection/annotate.md
- Utils: detection/utils.md
- Tools:
- Polygon Zone: detection/tools/polygon_zone.md
- Draw:
- Utils: draw/utils.md
- Annotations:
Expand Down
2 changes: 1 addition & 1 deletion supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from supervision.detection.annotate import BoxAnnotator, MaskAnnotator
from supervision.detection.core import Detections
from supervision.detection.line_counter import LineZone, LineZoneAnnotator
from supervision.detection.polygon_zone import PolygonZone, PolygonZoneAnnotator
from supervision.detection.tools.polygon_zone import PolygonZone, PolygonZoneAnnotator
from supervision.detection.utils import generate_2d_mask, mask_to_xyxy
from supervision.draw.color import Color, ColorPalette
from supervision.draw.utils import draw_filled_rectangle, draw_polygon, draw_text
Expand Down
12 changes: 6 additions & 6 deletions supervision/detection/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ def annotate(


class MaskAnnotator:
"""
A class for overlaying masks on an image using detections provided.
Attributes:
color (Union[Color, ColorPalette]): The color to fill the mask, can be a single color or a color palette
"""
def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.default(),
):
"""
A class for overlaying masks on an image using detections provided.
Attributes:
color (Union[Color, ColorPalette]): The color to fill the mask, can be a single color or a color palette
"""
self.color: Union[Color, ColorPalette] = color

def annotate(
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@


class PolygonZone:
"""
A class for defining a polygon-shaped zone within a frame for detecting objects.
Attributes:
polygon (np.ndarray): A numpy array defining the polygon vertices
frame_resolution_wh (Tuple[int, int]): The frame resolution (width, height)
triggering_position (Position): The position within the bounding box that triggers the zone (default: Position.BOTTOM_CENTER)
current_count (int): The current count of detected objects within the zone
mask (np.ndarray): The 2D bool mask for the polygon zone
"""
def __init__(
self,
polygon: np.ndarray,
Expand All @@ -30,6 +40,16 @@ def __init__(
)

def trigger(self, detections: Detections) -> np.ndarray:
"""
Determines if the detections are within the polygon zone.
Parameters:
detections (Detections): The detections to be checked against the polygon zone
Returns:
np.ndarray: A boolean numpy array indicating if each detection is within the polygon zone
"""

clipped_xyxy = clip_boxes(
boxes_xyxy=detections.xyxy, frame_resolution_wh=self.frame_resolution_wh
)
Expand All @@ -43,6 +63,20 @@ def trigger(self, detections: Detections) -> np.ndarray:


class PolygonZoneAnnotator:
"""
A class for annotating a polygon-shaped zone within a frame with a count of detected objects.
Attributes:
zone (PolygonZone): The polygon zone to be annotated
color (Color): The color to draw the polygon lines
thickness (int): The thickness of the polygon lines, default is 2
text_color (Color): The color of the text on the polygon, default is black
text_scale (float): The scale of the text on the polygon, default is 0.5
text_thickness (int): The thickness of the text on the polygon, default is 1
text_padding (int): The padding around the text on the polygon, default is 10
font (int): The font type for the text on the polygon, default is cv2.FONT_HERSHEY_SIMPLEX
center (Tuple[int, int]): The center of the polygon for text placement
"""
def __init__(
self,
zone: PolygonZone,
Expand All @@ -64,6 +98,16 @@ def __init__(
self.center = get_polygon_center(polygon=zone.polygon)

def annotate(self, scene: np.ndarray, label: Optional[str] = None) -> np.ndarray:
"""
Annotates the polygon zone within a frame with a count of detected objects.
Parameters:
scene (np.ndarray): The image on which the polygon zone will be annotated
label (Optional[str]): An optional label for the count of detected objects within the polygon zone (default: None)
Returns:
np.ndarray: The image with the polygon zone and count of detected objects
"""
annotated_frame = draw_polygon(
scene=scene,
polygon=self.zone.polygon,
Expand Down

0 comments on commit c1ccff4

Please sign in to comment.