From 9612fe78498bee835d44eac01405302520ff327c Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 5 Dec 2023 12:03:59 +0100 Subject: [PATCH] Refactor PixelateAnnotator and update docs --- docs/annotators.md | 25 +++++++++++++++++++++++++ supervision/annotators/core.py | 33 ++++++++++++++------------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/docs/annotators.md b/docs/annotators.md index 7dd8124d..3ea66bed 100644 --- a/docs/annotators.md +++ b/docs/annotators.md @@ -229,6 +229,27 @@ +=== "Pixelate" + + ```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){ align=center width="800" } + +
+ === "Trace" ```python @@ -337,6 +358,10 @@ :::supervision.annotators.core.BlurAnnotator +## PixelateAnnotator + +:::supervision.annotators.core.PixelateAnnotator + ## TraceAnnotator :::supervision.annotators.core.TraceAnnotator diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 6de2213c..10db7920 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1201,8 +1201,7 @@ class HeatMapAnnotator: class PixelateAnnotator(BaseAnnotator): """ - A class for replacing regions in an image with a pixelated effect - using provided detections. + A class for pixelating regions in an image using provided detections. """ def __init__(self, pixel_size: int = 20): @@ -1218,11 +1217,11 @@ class PixelateAnnotator(BaseAnnotator): detections: Detections, ) -> np.ndarray: """ - Annotates the given scene by replacing regions with a - pixelated effect based on the provided detections. + Annotates the given scene by pixelating regions based on the provided + detections. Args: - scene (np.ndarray): The image where regions will be pixelated. + scene (np.ndarray): The image where pixelating will be applied. detections (Detections): Object detections to annotate. Returns: @@ -1235,15 +1234,15 @@ class PixelateAnnotator(BaseAnnotator): >>> image = ... >>> detections = sv.Detections(...) - >>> blur_annotator = sv.PixelateAnnotator() - >>> annotated_frame = color_annotator.annotate( + >>> 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.png) + supervision-annotator-examples/pixelate-annotator-example-10.png) """ image_height, image_width = scene.shape[:2] clipped_xyxy = clip_boxes( @@ -1252,17 +1251,13 @@ class PixelateAnnotator(BaseAnnotator): 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) - # downscale image - small = cv2.resize( - roi, None, fx=1 / self.pixel_size, fy=1 / self.pixel_size - ) - - # upscale image - result = cv2.resize( - small, (roi.shape[1], roi.shape[0]), interpolation=cv2.INTER_NEAREST - ) - - scene[y1:y2, x1:x2] = result + scene[y1:y2, x1:x2] = scaled_down_roi return scene