Skip to content

Commit

Permalink
Merge branch 'develop' into rename-boxmask-annotator
Browse files Browse the repository at this point in the history
  • Loading branch information
SkalskiP committed Dec 5, 2023
2 parents 7f2b62a + 2e7ecdd commit 4774ace
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
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
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "supervision"
version = "0.17.0rc6"
version = "0.17.0rc7"
description = "A set of easy-to-use utils that will come in handy in any Computer Vision project"
authors = ["Piotr Skalski <[email protected]>"]
maintainers = ["Piotr Skalski <[email protected]>"]
Expand Down Expand Up @@ -36,10 +36,10 @@ classifiers=[

[tool.poetry.dependencies]
python = ">=3.8,<3.12.0"
numpy = ">=1.24.0,>=1.26.1"
matplotlib = ">=3.5.0,>=3.8.1"
numpy = ">=1.20.0"
matplotlib = ">=3.5.0"
pyyaml = ">=5.3,<=6.0.1"
pillow = ">=9.4,<=10.1.0"
pillow = ">=9.4"
opencv-python = { version = ">=4.5.5.64,<=4.8.1.78", optional = true }
opencv-python-headless = ">=4.5.5.64,<=4.8.1.78"
scipy = ">=1.9.0,<=2.0.0"
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

0 comments on commit 4774ace

Please sign in to comment.