initial version of refactored CropAnnotator

This commit is contained in:
SkalskiP
2024-03-14 13:32:52 +01:00
parent 2a9d4257ba
commit c7a6b38275
7 changed files with 132 additions and 217 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
comments: true
---
# File
# File Utils
<div class="md-typeset">
<h2>list_files_with_extensions</h2>
+18 -1
View File
@@ -1,8 +1,13 @@
---
comments: true
status: new
---
# ImageSink
# Image Utils
<div class="md-typeset">
<h2>ImageSink</h2>
</div>
:::supervision.utils.image.ImageSink
@@ -11,3 +16,15 @@ comments: true
</div>
:::supervision.utils.image.crop_image
<div class="md-typeset">
<h2>resize_image</h2>
</div>
:::supervision.utils.image.resize_image
<div class="md-typeset">
<h2>place_image</h2>
</div>
:::supervision.utils.image.place_image
+1 -1
View File
@@ -2,7 +2,7 @@
comments: true
---
# Notebooks
# Notebooks Utils
<div class="md-typeset">
<h2>plot_image</h2>
+1 -1
View File
@@ -2,7 +2,7 @@
comments: true
---
# Video
# Video Utils
<div class="md-typeset">
<h2>VideoInfo</h2>
+1 -1
View File
@@ -71,7 +71,7 @@ from supervision.geometry.utils import get_polygon_center
from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision
from supervision.tracker.byte_tracker.core import ByteTrack
from supervision.utils.file import list_files_with_extensions
from supervision.utils.image import ImageSink, crop_image
from supervision.utils.image import ImageSink, crop_image, resize_image, place_image
from supervision.utils.notebook import plot_image, plot_images_grid
from supervision.utils.video import (
FPSMonitor,
+37 -209
View File
@@ -12,6 +12,7 @@ from supervision.detection.utils import clip_boxes, mask_to_polygons
from supervision.draw.color import Color, ColorPalette
from supervision.draw.utils import draw_polygon
from supervision.geometry.core import Position
from supervision.utils.image import crop_image, resize_image, place_image
class BoundingBoxAnnotator(BaseAnnotator):
@@ -1756,14 +1757,14 @@ class CropAnnotator(BaseAnnotator):
A class for viewing a cropped part of detections with a selected zoom scale.
"""
def __init__(self, position: Position = Position.TOP_CENTER, zoom_factor: int = 2):
def __init__(self, position: Position = Position.TOP_CENTER, scale_factor: int = 2):
"""
Args:
position (Position): the anchor position to place cropped detections
zoom_factor (int): the percentage factor to zoom cropped image part
scale_factor (int): the percentage factor to zoom cropped image part
"""
self.position: Position = position
self.zoom_factor: int = zoom_factor
self.scale_factor: int = scale_factor
def annotate(
self,
@@ -1793,228 +1794,55 @@ class CropAnnotator(BaseAnnotator):
detections=detections
)
```
![crop-annotator-example](https://media.roboflow.com/ #TODO update link
supervision-annotator-examples/______.png)
"""
base_scene = scene.copy()
crops = [
crop_image(image=scene, xyxy=xyxy)
for xyxy
in detections.xyxy.astype(int)
]
resized_crops = [
resize_image(image=crop, scale_factor=self.scale_factor)
for crop
in crops
]
anchors = detections.get_anchors_coordinates(anchor=self.position)
for detection_idx in range(len(detections)):
x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
anchor = anchors[detection_idx].astype(int)
cropped_scene = base_scene[y1:y2, x1:x2]
resized_scene = self.apply_zoom(
cropped_scene=cropped_scene, zoom_factor=self.zoom_factor
)
coordinates_and_images = self.calculate_place_coordinates_and_crop(
scene=base_scene,
cropped_scene=resized_scene,
position=self.position,
for resized_crop, anchor in zip(resized_crops, anchors):
crop_wh = resized_crop.shape[1], resized_crop.shape[0]
xy, _ = self.calculate_border_coordinates(
anchor_xy=anchor,
border_wh=crop_wh,
position=self.position
)
scene = self.place_cropped_scene(
scene=base_scene, coordinates_and_image=coordinates_and_images
)
scene = place_image(scene=scene, image=resized_crop, anchor=xy)
return scene
@staticmethod
def apply_zoom(cropped_scene: np.ndarray, zoom_factor: int) -> np.ndarray:
"""
Applies resize (zoom) to the cropped part of detections
Args:
cropped_scene (np.ndarray): the cropped part of detections
Returns:
resized_scene (np.ndarray): the resized cropped scene
"""
resized_scene = cv2.resize(
cropped_scene,
(
zoom_factor * cropped_scene.shape[1],
zoom_factor * cropped_scene.shape[0],
),
)
return resized_scene
@staticmethod
def calculate_place_coordinates_and_crop(
scene: np.ndarray,
cropped_scene: np.ndarray,
position: Position,
anchor_xy: np.ndarray,
) -> Tuple[Tuple[int, int], np.ndarray]:
"""
This method is responsible to calculate the x,y coordinates that the cropped
part of the detections will be placed and crop any exceeding part.
The returned x,y in all position cases is the final top left pixel.
-Args:
scene (np.ndarray): The image where cropped detections will be placed.
cropped_scene (np.ndarray): The cropped part of the detections.
-Returns:
x (int): position in horizontal axis to use for placement of cropped part.
y (int): position in vertical axis to use for placement of cropped part.
cropped_scene (np.ndarray): The cropped part after any applied trim.
"""
h, w = scene.shape[:2]
crop_h, crop_w = cropped_scene.shape[:2]
def calculate_border_coordinates(
anchor_xy: Tuple[int, int], border_wh: Tuple[int, int], position: Position
) -> Tuple[Tuple[int, int], Tuple[int, int]]:
cx, cy = anchor_xy
width, height = border_wh
if position == Position.TOP_LEFT:
x = max(0, (cx - crop_w))
y = max(0, (cy - crop_h))
if x == 0 and (cx - crop_w < 0):
cropped_scene = cropped_scene[:, abs(cx - crop_w) :]
if y == 0 and (cy - crop_h < 0):
cropped_scene = cropped_scene[abs(cy - crop_h) :, :]
return x, y, cropped_scene
return (cx - width, cy - height), (cx, cy)
elif position == Position.TOP_CENTER:
if (cx - crop_w // 2) < 0:
x = 0
cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :]
else:
x = cx - crop_w // 2
if (cx + crop_w // 2) >= w:
cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)]
y = max(0, (cy - crop_h))
if y == 0 and (cy - crop_h < 0):
cropped_scene = cropped_scene[abs(cy - crop_h) :, :]
return x, y, cropped_scene
return (cx - width // 2, cy), (cx + width // 2, cy - height)
elif position == Position.TOP_RIGHT:
x = cx
x_max = min(w, (cx + crop_w))
y = max(0, (cy - crop_h))
if x_max == w and (cx + crop_w > w):
cropped_scene = cropped_scene[:, : (w - cx)]
if y == 0 and (cy - crop_h < 0):
cropped_scene = cropped_scene[abs(cy - crop_h) :, :]
return x, y, cropped_scene
return (cx, cy), (cx + width, cy - height)
elif position == Position.CENTER_LEFT:
x = max(0, (cx - crop_w))
if x == 0 and (cx - crop_w < 0):
cropped_scene = cropped_scene[:, abs(cx - crop_w) :]
if (cy - crop_h // 2) < 0:
y = 0
cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :]
else:
y = cy - crop_h // 2
if (cy + crop_h // 2) >= h:
cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :]
return x, y, cropped_scene
elif position == (Position.CENTER or Position.CENTER_OF_MASS):
if (cx - crop_w // 2) < 0:
x = 0
cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :]
else:
x = cx - crop_w // 2
if (cx + crop_w // 2) >= w:
cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)]
if (cy - crop_h // 2) < 0:
y = 0
cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :]
else:
y = cy - crop_h // 2
if (cy + crop_h // 2) >= h:
cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :]
return x, y, cropped_scene
return (cx - width, cy - height // 2), (cx, cy + height // 2)
elif position == Position.CENTER or position == Position.CENTER_OF_MASS:
return (
(cx - width // 2, cy - height // 2),
(cx + width // 2, cy + height // 2),
)
elif position == Position.CENTER_RIGHT:
x = cx
x_max = min(w, (cx + crop_w))
if x_max == w and (cx + crop_w > w):
cropped_scene = cropped_scene[:, : (w - cx)]
if (cy - crop_h // 2) < 0:
y = 0
cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :]
else:
y = cy - crop_h // 2
if (cy + crop_h // 2) >= h:
cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :]
return x, y, cropped_scene
return (cx, cy - height // 2), (cx + width, cy + height // 2)
elif position == Position.BOTTOM_LEFT:
x = max(0, (cx - crop_w))
y = cy
y_max = min(h, (cy + crop_h))
if x == 0 and (cx - crop_w < 0):
cropped_scene = cropped_scene[:, abs(cx - crop_w) :]
if y_max == h and (cy + crop_h > h):
cropped_scene = cropped_scene[: h - cy, :]
return x, y, cropped_scene
return (cx - width, cy), (cx, cy + height)
elif position == Position.BOTTOM_CENTER:
if (cx - crop_w // 2) < 0:
x = 0
cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :]
else:
x = cx - crop_w // 2
if (cx + crop_w // 2) >= w:
cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)]
y = cy
y_max = min(h, (cy + crop_h))
if y_max == h and (cy + crop_h > h):
cropped_scene = cropped_scene[: h - cy, :]
return x, y, cropped_scene
return (cx - width // 2, cy), (cx + width // 2, cy + height)
elif position == Position.BOTTOM_RIGHT:
x = cx
y = cy
x_max = min(w, (cx + crop_w))
y_max = min(h, (cy + crop_h))
if x_max == w and (cx + crop_w > w):
cropped_scene = cropped_scene[:, : (w - cx)]
if y_max == h and (cy + crop_h > h):
cropped_scene = cropped_scene[: h - cy, :]
return x, y, cropped_scene
@staticmethod
def place_cropped_scene(
scene: np.ndarray, coordinates_and_image: Tuple[Tuple[int, int], np.ndarray]
) -> np.ndarray:
x, y = coordinates_and_image[0:2]
cropped_scene = coordinates_and_image[2]
scene[
y : y + cropped_scene.shape[0], x : x + cropped_scene.shape[1]
] = cropped_scene
return scene
return (cx, cy), (cx + width, cy + height)
+73 -3
View File
@@ -1,6 +1,6 @@
import os
import shutil
from typing import Optional
from typing import Optional, Tuple
import cv2
import numpy as np
@@ -32,8 +32,78 @@ def crop_image(image: np.ndarray, xyxy: np.ndarray) -> np.ndarray:
xyxy = np.round(xyxy).astype(int)
x1, y1, x2, y2 = xyxy
cropped_img = image[y1:y2, x1:x2]
return cropped_img
return image[y1:y2, x1:x2]
def resize_image(image: np.ndarray, scale_factor: float) -> np.ndarray:
"""
Resizes an image by a given scale factor using cv2.INTER_LINEAR interpolation.
Args:
image (np.ndarray): The input image to be resized.
scale_factor (float): The factor by which the image will be scaled. Scale factor
> 1.0 zooms in, < 1.0 zooms out.
Returns:
np.ndarray: The resized image.
Raises:
ValueError: If the scale factor is non-positive.
"""
if scale_factor <= 0:
raise ValueError("Scale factor must be positive.")
old_width, old_height = image.shape[1], image.shape[0]
nwe_width = int(old_width * scale_factor)
new_height = int(old_height * scale_factor)
return cv2.resize(image, (nwe_width, new_height), interpolation=cv2.INTER_LINEAR)
def place_image(
scene: np.ndarray,
image: np.ndarray,
anchor: Tuple[int, int]
) -> np.ndarray:
"""
Places an image onto a scene at a given anchor point, handling cases where
the image's position is partially or completely outside the scene's bounds.
Args:
scene (np.ndarray): The background scene onto which the image is placed.
image (np.ndarray): The image to be placed onto the scene.
anchor (Tuple[int, int]): The (x, y) coordinates in the scene where the
top-left corner of the image will be placed.
Returns:
np.ndarray: The modified scene with the image placed at the anchor point,
or unchanged if the image placement is completely outside the scene.
"""
scene_height, scene_width = scene.shape[:2]
image_height, image_width = image.shape[:2]
anchor_x, anchor_y = anchor
is_out_horizontally = anchor_x + image_width <= 0 or anchor_x >= scene_width
is_out_vertically = anchor_y + image_height <= 0 or anchor_y >= scene_height
if is_out_horizontally or is_out_vertically:
return scene
start_y = max(anchor_y, 0)
start_x = max(anchor_x, 0)
end_y = min(scene_height, anchor_y + image_height)
end_x = min(scene_width, anchor_x + image_width)
crop_start_y = max(-anchor_y, 0)
crop_start_x = max(-anchor_x, 0)
crop_end_y = image_height - max((anchor_y + image_height) - scene_height, 0)
crop_end_x = image_width - max((anchor_x + image_width) - scene_width, 0)
scene[start_y:end_y, start_x:end_x] = image[
crop_start_y:crop_end_y, crop_start_x:crop_end_x
]
return scene
class ImageSink: