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 util to create image tiles #1028

Merged
merged 12 commits into from
Apr 5, 2024
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
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ repos:
- id: mixed-line-ending



- repo: https://github.com/PyCQA/bandit
rev: '1.7.8'
hooks:
Expand Down
6 changes: 6 additions & 0 deletions docs/utils/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ status: new

:::supervision.utils.image.crop_image

<div class="md-typeset">
<h2>letterbox_image</h2>
</div>

:::supervision.utils.image.letterbox_image

<div class="md-typeset">
<h2>resize_image</h2>
</div>
Expand Down
18 changes: 18 additions & 0 deletions docs/utils/iterables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
comments: true
status: new
---

# Iterables Utils

<div class="md-typeset">
<h2>create_batches</h2>
</div>

:::supervision.utils.iterables.create_batches

<div class="md-typeset">
<h2>fill</h2>
</div>

:::supervision.utils.iterables.fill
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ nav:
- Utils:
- Video: utils/video.md
- Image: utils/image.md
- Iterables: utils/iterables.md
- Notebook: utils/notebook.md
- File: utils/file.md
- Assets: assets.md
Expand Down
10 changes: 9 additions & 1 deletion supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@
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, place_image, resize_image
from supervision.utils.image import (
ImageSink,
create_tiles,
crop_image,
letterbox_image,
place_image,
resize_image,
resize_image_keeping_aspect_ratio,
)
from supervision.utils.notebook import plot_image, plot_images_grid
from supervision.utils.video import (
FPSMonitor,
Expand Down
46 changes: 21 additions & 25 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
import numpy as np

from supervision.annotators.base import BaseAnnotator, ImageType
from supervision.annotators.utils import (
ColorLookup,
Trace,
resolve_color,
scene_to_annotator_img_type,
)
from supervision.annotators.utils import ColorLookup, Trace, resolve_color
from supervision.config import CLASS_NAME_DATA_FIELD, ORIENTED_BOX_COORDINATES
from supervision.detection.core import Detections
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.conversion import convert_for_annotation_method
from supervision.utils.image import crop_image, place_image, resize_image


Expand All @@ -43,7 +39,7 @@ def __init__(
self.thickness: int = thickness
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -124,7 +120,7 @@ def __init__(
self.thickness: int = thickness
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -212,7 +208,7 @@ def __init__(
self.opacity = opacity
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -299,7 +295,7 @@ def __init__(
self.thickness: int = thickness
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -385,7 +381,7 @@ def __init__(
self.color_lookup: ColorLookup = color_lookup
self.opacity = opacity

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -479,7 +475,7 @@ def __init__(
self.color_lookup: ColorLookup = color_lookup
self.kernel_size: int = kernel_size

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -577,7 +573,7 @@ def __init__(
self.end_angle: int = end_angle
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -668,7 +664,7 @@ def __init__(
self.corner_length: int = corner_length
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -756,7 +752,7 @@ def __init__(
self.thickness: int = thickness
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -846,7 +842,7 @@ def __init__(
self.position: Position = position
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -989,7 +985,7 @@ def resolve_text_background_xyxy(
center_y + text_h // 2,
)

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1112,7 +1108,7 @@ def __init__(self, kernel_size: int = 15):
"""
self.kernel_size: int = kernel_size

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1197,7 +1193,7 @@ def __init__(
self.thickness = thickness
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1304,7 +1300,7 @@ def __init__(
self.top_hue = top_hue
self.low_hue = low_hue

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(self, scene: ImageType, detections: Detections) -> ImageType:
"""
Annotates the scene with a heatmap based on the provided detections.
Expand Down Expand Up @@ -1380,7 +1376,7 @@ def __init__(self, pixel_size: int = 20):
"""
self.pixel_size: int = pixel_size

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1468,7 +1464,7 @@ def __init__(
self.position: Position = position
self.color_lookup: ColorLookup = color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1564,7 +1560,7 @@ def __init__(
raise ValueError("roundness attribute must be float between (0, 1.0]")
self.roundness: float = roundness

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1701,7 +1697,7 @@ def __init__(
if border_thickness is None:
self.border_thickness = int(0.15 * self.height)

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down Expand Up @@ -1874,7 +1870,7 @@ def __init__(
self.border_thickness: int = border_thickness
self.border_color_lookup: ColorLookup = border_color_lookup

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down
34 changes: 0 additions & 34 deletions supervision/annotators/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from enum import Enum
from functools import wraps
from typing import Optional, Union

import cv2
import numpy as np
from PIL import Image

from supervision.annotators.base import ImageType
from supervision.detection.core import Detections
from supervision.draw.color import Color, ColorPalette
from supervision.geometry.core import Position
Expand Down Expand Up @@ -123,33 +119,3 @@ def put(self, detections: Detections) -> None:

def get(self, tracker_id: int) -> np.ndarray:
return self.xy[self.tracker_id == tracker_id]


def pillow_to_cv2(image: Image.Image) -> np.ndarray:
scene = np.array(image)
scene = cv2.cvtColor(scene, cv2.COLOR_RGB2BGR)
return scene


def scene_to_annotator_img_type(annotate_func):
"""
Decorates `BaseAnnotator.annotate` implementations, converts scene to
an image type used internally by the annotators, converts back when annotation
is complete.
"""

@wraps(annotate_func)
def wrapper(self, scene: ImageType, *args, **kwargs):
if isinstance(scene, np.ndarray):
return annotate_func(self, scene, *args, **kwargs)

if isinstance(scene, Image.Image):
scene = pillow_to_cv2(scene)
annotated = annotate_func(self, scene, *args, **kwargs)
annotated = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
annotated = Image.fromarray(annotated)
return annotated

raise ValueError(f"Unsupported image type: {type(scene)}")

return wrapper
4 changes: 2 additions & 2 deletions supervision/detection/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import cv2

from supervision.annotators.base import ImageType
from supervision.annotators.utils import scene_to_annotator_img_type
from supervision.detection.core import Detections
from supervision.draw.color import Color, ColorPalette
from supervision.utils.conversion import convert_for_annotation_method
from supervision.utils.internal import deprecated


Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
self.text_thickness: int = text_thickness
self.text_padding: int = text_padding

@scene_to_annotator_img_type
@convert_for_annotation_method
def annotate(
self,
scene: ImageType,
Expand Down
Loading