[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge branch 'develop' into feat/inference-slicer-segmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Linas Kondrackis committed May 13, 2024
2 parents 85e4c0e + f41adca commit a9044e4
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 28 deletions.
12 changes: 12 additions & 0 deletions docs/detection/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ status: new
</div>

:::supervision.detection.utils.scale_boxes

<div class="md-typeset">
<h2><a href="#supervision.detection.utils.clip_boxes">clip_boxes</a></h2>
</div>

:::supervision.detection.utils.clip_boxes

<div class="md-typeset">
<h2><a href="#supervision.detection.utils.pad_boxes">pad_boxes</a></h2>
</div>

:::supervision.detection.utils.pad_boxes
2 changes: 1 addition & 1 deletion docs/how_to/detect_small_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ objects within each, and aggregating the results.

def callback(image_slice: np.ndarray) -> sv.Detections:
results = model.infer(image_slice)[0]
detections = sv.Detections.from_inference(results)
return sv.Detections.from_inference(results)

slicer = sv.InferenceSlicer(callback = callback)
detections = slicer(image)
Expand Down
41 changes: 39 additions & 2 deletions docs/keypoint/annotators.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ status: new
image = ...
key_points = sv.KeyPoints(...)

vertex_annotator = sv.VertexAnnotator(color=sv.Color.GREEN, radius=10)
vertex_annotator = sv.VertexAnnotator(
color=sv.Color.GREEN,
radius=10
)
annotated_frame = vertex_annotator.annotate(
scene=image.copy(),
key_points=key_points
Expand All @@ -34,7 +37,10 @@ status: new
image = ...
key_points = sv.KeyPoints(...)

edge_annotator = sv.EdgeAnnotator(color=sv.Color.GREEN, thickness=5)
edge_annotator = sv.EdgeAnnotator(
color=sv.Color.GREEN,
thickness=5
)
annotated_frame = edge_annotator.annotate(
scene=image.copy(),
key_points=key_points
Expand All @@ -47,6 +53,31 @@ status: new

</div>

=== "VertexLabelAnnotator"

```python
import supervision as sv

image = ...
key_points = sv.KeyPoints(...)

vertex_label_annotator = sv.VertexLabelAnnotator(
color=sv.Color.GREEN,
text_color=sv.Color.BLACK,
border_radius=5
)
annotated_frame = vertex_label_annotator.annotate(
scene=image.copy(),
key_points=key_points
)
```

<div class="result" markdown>

![vertex-label-annotator-example](https://media.roboflow.com/supervision-annotator-examples/vertex-label-annotator-example.png){ align=center width="800" }

</div>

<div class="md-typeset">
<h2><a href="#supervision.keypoint.annotators.VertexAnnotator">VertexAnnotator</a></h2>
</div>
Expand All @@ -58,3 +89,9 @@ status: new
</div>

:::supervision.keypoint.annotators.EdgeAnnotator

<div class="md-typeset">
<h2><a href="#supervision.keypoint.annotators.VertexLabelAnnotator">VertexLabelAnnotator</a></h2>
</div>

:::supervision.keypoint.annotators.VertexLabelAnnotator
36 changes: 18 additions & 18 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
box_iou_batch,
box_non_max_suppression,
calculate_masks_centroids,
clip_boxes,
filter_polygons_by_area,
mask_iou_batch,
mask_non_max_suppression,
mask_to_polygons,
mask_to_xyxy,
move_boxes,
move_masks,
pad_boxes,
polygon_to_mask,
polygon_to_xyxy,
scale_boxes,
Expand All @@ -70,7 +72,11 @@
)
from supervision.geometry.core import Point, Position, Rect
from supervision.geometry.utils import get_polygon_center
from supervision.keypoint.annotators import EdgeAnnotator, VertexAnnotator
from supervision.keypoint.annotators import (
EdgeAnnotator,
VertexAnnotator,
VertexLabelAnnotator,
)
from supervision.keypoint.core import KeyPoints
from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision
from supervision.tracker.byte_tracker.core import ByteTrack
Expand Down
29 changes: 29 additions & 0 deletions supervision/detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,35 @@ def clip_boxes(xyxy: np.ndarray, resolution_wh: Tuple[int, int]) -> np.ndarray:
return result


def pad_boxes(xyxy: np.ndarray, px: int, py: Optional[int] = None) -> np.ndarray:
"""
Pads bounding boxes coordinates with a constant padding.
Args:
xyxy (np.ndarray): A numpy array of shape `(N, 4)` where each
row corresponds to a bounding box in the format
`(x_min, y_min, x_max, y_max)`.
px (int): The padding value to be added to both the left and right sides of
each bounding box.
py (Optional[int]): The padding value to be added to both the top and bottom
sides of each bounding box. If not provided, `px` will be used for both
dimensions.
Returns:
np.ndarray: A numpy array of shape `(N, 4)` where each row corresponds to a
bounding box with coordinates padded according to the provided padding
values.
"""
if py is None:
py = px

result = xyxy.copy()
result[:, [0, 1]] -= [px, py]
result[:, [2, 3]] += [px, py]

return result


def xywh_to_xyxy(boxes_xywh: np.ndarray) -> np.ndarray:
xyxy = boxes_xywh.copy()
xyxy[:, 2] = boxes_xywh[:, 0] + boxes_xywh[:, 2]
Expand Down
52 changes: 52 additions & 0 deletions supervision/draw/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,58 @@ def draw_filled_rectangle(scene: np.ndarray, rect: Rect, color: Color) -> np.nda
return scene


def draw_rounded_rectangle(
scene: np.ndarray,
rect: Rect,
color: Color,
border_radius: int,
) -> np.ndarray:
"""
Draws a rounded rectangle on an image.
Parameters:
scene (np.ndarray): The image on which the rounded rectangle will be drawn.
rect (Rect): The rectangle to be drawn.
color (Color): The color of the rounded rectangle.
border_radius (int): The radius of the corner rounding.
Returns:
np.ndarray: The image with the rounded rectangle drawn on it.
"""
x1, y1, x2, y2 = rect.as_xyxy_int_tuple()
width, height = x2 - x1, y2 - y1
border_radius = min(border_radius, min(width, height) // 2)

rectangle_coordinates = [
((x1 + border_radius, y1), (x2 - border_radius, y2)),
((x1, y1 + border_radius), (x2, y2 - border_radius)),
]
circle_centers = [
(x1 + border_radius, y1 + border_radius),
(x2 - border_radius, y1 + border_radius),
(x1 + border_radius, y2 - border_radius),
(x2 - border_radius, y2 - border_radius),
]

for coordinates in rectangle_coordinates:
cv2.rectangle(
img=scene,
pt1=coordinates[0],
pt2=coordinates[1],
color=color.as_bgr(),
thickness=-1,
)
for center in circle_centers:
cv2.circle(
img=scene,
center=center,
radius=border_radius,
color=color.as_bgr(),
thickness=-1,
)
return scene


def draw_polygon(
scene: np.ndarray, polygon: np.ndarray, color: Color, thickness: int = 2
) -> np.ndarray:
Expand Down
13 changes: 13 additions & 0 deletions supervision/geometry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ class Rect:
width: float
height: float

@classmethod
def from_xyxy(cls, xyxy: Tuple[float, float, float, float]) -> Rect:
x1, y1, x2, y2 = xyxy
return cls(x=x1, y=y1, width=x2 - x1, height=y2 - y1)

@property
def top_left(self) -> Point:
return Point(x=self.x, y=self.y)
Expand All @@ -113,3 +118,11 @@ def pad(self, padding) -> Rect:
width=self.width + 2 * padding,
height=self.height + 2 * padding,
)

def as_xyxy_int_tuple(self) -> Tuple[int, int, int, int]:
return (
int(self.x),
int(self.y),
int(self.x + self.width),
int(self.y + self.height),
)
Loading

0 comments on commit a9044e4

Please sign in to comment.