Skip to content

Commit

Permalink
utils refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SkalskiP committed Mar 20, 2024
1 parent 9f5c63b commit 8a2abe7
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 40 deletions.
8 changes: 4 additions & 4 deletions docs/draw/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ comments: true
:::supervision.draw.utils.draw_image

<div class="md-typeset">
<h2>calculate_dynamic_font_scale</h2>
<h2>calculate_optimal_font_scale</h2>
</div>

:::supervision.draw.utils.calculate_dynamic_text_scale
:::supervision.draw.utils.calculate_optimal_text_scale

<div class="md-typeset">
<h2>calculate_dynamic_line_thickness</h2>
<h2>calculate_optimal_line_thickness</h2>
</div>

:::supervision.draw.utils.calculate_dynamic_line_thickness
:::supervision.draw.utils.calculate_optimal_line_thickness
2 changes: 1 addition & 1 deletion examples/count_people_in_zone/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gdown
inference
supervision
supervision==0.19.0
tqdm
ultralytics
2 changes: 1 addition & 1 deletion examples/heatmap_and_track/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
supervision[assets]
supervision[assets]==0.19.0
ultralytics
2 changes: 1 addition & 1 deletion examples/speed_estimation/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
supervision==0.18.0rc1
supervision==0.19.0
tqdm==4.66.1
requests
ultralytics==8.0.237
Expand Down
2 changes: 1 addition & 1 deletion examples/tracking/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
inference
supervision
supervision==0.19.0
tqdm
ultralytics
2 changes: 1 addition & 1 deletion examples/traffic_analysis/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gdown
inference
supervision>=0.19.0rc5
supervision>=0.19.0
tqdm
ultralytics
4 changes: 2 additions & 2 deletions supervision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
)
from supervision.draw.color import Color, ColorPalette
from supervision.draw.utils import (
calculate_dynamic_line_thickness,
calculate_dynamic_text_scale,
calculate_optimal_line_thickness,
calculate_optimal_text_scale,
draw_filled_rectangle,
draw_image,
draw_line,
Expand Down
31 changes: 18 additions & 13 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,9 +1851,9 @@ def __init__(
self,
position: Position = Position.TOP_CENTER,
scale_factor: int = 2,
color: Union[Color, ColorPalette] = ColorPalette.DEFAULT,
thickness: int = 2,
color_lookup: ColorLookup = ColorLookup.CLASS,
border_color: Union[Color, ColorPalette] = ColorPalette.DEFAULT,
border_thickness: int = 2,
border_color_lookup: ColorLookup = ColorLookup.CLASS,
):
"""
Args:
Expand All @@ -1862,12 +1862,17 @@ def __init__(
scale_factor (int): The factor by which to scale the cropped image part. A
factor of 2, for example, would double the size of the cropped area,
allowing for a closer view of the detection.
border_color (Union[Color, ColorPalette]): The color or color palette to
use for annotating border around the cropped area.
border_thickness (int): The thickness of the border around the cropped area.
border_color_lookup (ColorLookup): Strategy for mapping colors to
annotations. Options are `INDEX`, `CLASS`, `TRACK`.
"""
self.position: Position = position
self.scale_factor: int = scale_factor
self.color: Union[Color, ColorPalette] = color
self.thickness: int = thickness
self.color_lookup: ColorLookup = color_lookup
self.border_color: Union[Color, ColorPalette] = border_color
self.border_thickness: int = border_thickness
self.border_color_lookup: ColorLookup = border_color_lookup

@scene_to_annotator_img_type
def annotate(
Expand Down Expand Up @@ -1923,10 +1928,10 @@ def annotate(
)
scene = place_image(scene=scene, image=resized_crop, anchor=(x1, y1))
color = resolve_color(
color=self.color,
color=self.border_color,
detections=detections,
detection_idx=idx,
color_lookup=self.color_lookup
color_lookup=self.border_color_lookup
if custom_color_lookup is None
else custom_color_lookup,
)
Expand All @@ -1935,7 +1940,7 @@ def annotate(
pt1=(x1, y1),
pt2=(x2, y2),
color=color.as_bgr(),
thickness=self.thickness,
thickness=self.border_thickness,
)

return scene
Expand All @@ -1948,14 +1953,14 @@ def calculate_crop_coordinates(
width, height = crop_wh

if position == Position.TOP_LEFT:
return ((anchor_x - width, anchor_y - height), (anchor_x, anchor_y))
return (anchor_x - width, anchor_y - height), (anchor_x, anchor_y)
elif position == Position.TOP_CENTER:
return (
(anchor_x - width // 2, anchor_y - height),
(anchor_x + width // 2, anchor_y),
)
elif position == Position.TOP_RIGHT:
return ((anchor_x, anchor_y - height), (anchor_x + width, anchor_y))
return (anchor_x, anchor_y - height), (anchor_x + width, anchor_y)
elif position == Position.CENTER_LEFT:
return (
(anchor_x - width, anchor_y - height // 2),
Expand All @@ -1972,11 +1977,11 @@ def calculate_crop_coordinates(
(anchor_x + width, anchor_y + height // 2),
)
elif position == Position.BOTTOM_LEFT:
return ((anchor_x - width, anchor_y), (anchor_x, anchor_y + height))
return (anchor_x - width, anchor_y), (anchor_x, anchor_y + height)
elif position == Position.BOTTOM_CENTER:
return (
(anchor_x - width // 2, anchor_y),
(anchor_x + width // 2, anchor_y + height),
)
elif position == Position.BOTTOM_RIGHT:
return ((anchor_x, anchor_y), (anchor_x + width, anchor_y + height))
return (anchor_x, anchor_y), (anchor_x + width, anchor_y + height)
24 changes: 8 additions & 16 deletions supervision/draw/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,39 +238,31 @@ def draw_image(
return scene


def calculate_dynamic_text_scale(resolution_wh: Tuple[int, int]) -> float:
def calculate_optimal_text_scale(resolution_wh: Tuple[int, int]) -> float:
"""
Calculate a dynamic font scale based on the resolution of an image.
Calculate font scale based on the resolution of an image.
Parameters:
resolution_wh (Tuple[int, int]): A tuple representing the width and height
of the image.
of the image.
Returns:
float: The calculated font scale factor.
"""
return min(resolution_wh) * 1e-3


def calculate_dynamic_line_thickness(resolution_wh: Tuple[int, int]) -> int:
def calculate_optimal_line_thickness(resolution_wh: Tuple[int, int]) -> int:
"""
Calculate a dynamic line thickness based on the resolution of an image.
Calculate line thickness based on the resolution of an image.
Parameters:
resolution_wh (Tuple[int, int]): A tuple representing the width and height
of the image.
of the image.
Returns:
int: The calculated line thickness in pixels.
"""
min_dimension = min(resolution_wh)
if min_dimension < 480:
if min(resolution_wh) < 1080:
return 2
if min_dimension < 720:
return 2
if min_dimension < 1080:
return 2
if min_dimension < 2160:
return 4
else:
return 4
return 4

0 comments on commit 8a2abe7

Please sign in to comment.