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

Add PixelateAnnotator #633

Merged
merged 8 commits into from
Dec 5, 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
25 changes: 25 additions & 0 deletions docs/annotators.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@

</div>

=== "Pixelate"

```python
>>> import supervision as sv

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

>>> pixelate_annotator = sv.PixelateAnnotator()
>>> annotated_frame = pixelate_annotator.annotate(
... scene=image.copy(),
... detections=detections
... )
```

<div class="result" markdown>

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

</div>

=== "Trace"

```python
Expand Down Expand Up @@ -337,6 +358,10 @@

:::supervision.annotators.core.BlurAnnotator

## PixelateAnnotator

:::supervision.annotators.core.PixelateAnnotator

## TraceAnnotator

:::supervision.annotators.core.TraceAnnotator
Expand Down
1 change: 1 addition & 0 deletions supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
HeatMapAnnotator,
LabelAnnotator,
MaskAnnotator,
PixelateAnnotator,
PolygonAnnotator,
TraceAnnotator,
)
Expand Down
66 changes: 66 additions & 0 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,3 +1197,69 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
mask
]
return scene


class PixelateAnnotator(BaseAnnotator):
"""
A class for pixelating regions in an image using provided detections.
"""

def __init__(self, pixel_size: int = 20):
"""
Args:
pixel_size (int): The size of the pixelation.
"""
self.pixel_size: int = pixel_size

def annotate(
self,
scene: np.ndarray,
detections: Detections,
) -> np.ndarray:
"""
Annotates the given scene by pixelating regions based on the provided
detections.

Args:
scene (np.ndarray): The image where pixelating will be applied.
detections (Detections): Object detections to annotate.

Returns:
The annotated image.

Example:
```python
>>> import supervision as sv

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

>>> pixelate_annotator = sv.PixelateAnnotator()
>>> annotated_frame = pixelate_annotator.annotate(
... scene=image.copy(),
... detections=detections
... )
```

![pixelate-annotator-example](https://media.roboflow.com/
supervision-annotator-examples/pixelate-annotator-example-10.png)
"""
image_height, image_width = scene.shape[:2]
clipped_xyxy = clip_boxes(
xyxy=detections.xyxy, resolution_wh=(image_width, image_height)
).astype(int)

for x1, y1, x2, y2 in clipped_xyxy:
roi = scene[y1:y2, x1:x2]
scaled_up_roi = cv2.resize(
src=roi, dsize=None, fx=1 / self.pixel_size, fy=1 / self.pixel_size
)
scaled_down_roi = cv2.resize(
src=scaled_up_roi,
dsize=(roi.shape[1], roi.shape[0]),
interpolation=cv2.INTER_NEAREST,
)

scene[y1:y2, x1:x2] = scaled_down_roi

return scene