From d314bf0364bf18cecef01fab69d81284550a99eb Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 14:58:06 +0100 Subject: [PATCH 01/11] initial tests --- supervision/draw/color.py | 124 ++++++++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 37 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index fe55b25c..ebcc3296 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -6,23 +6,36 @@ from typing import List, Tuple from supervision.utils.internal import deprecated DEFAULT_COLOR_PALETTE = [ - "#a351fb", - "#e6194b", - "#3cb44b", - "#ffe119", - "#0082c8", - "#f58231", - "#911eb4", - "#46f0f0", - "#f032e6", - "#d2f53c", - "#fabebe", - "#008080", - "#e6beff", - "#aa6e28", - "#fffac8", - "#800000", - "#aaffc3", + "A351FB", + "FF4040", + "FFA1A0", + "FF7633", + "FFB633", + "D1D435", + "4CFB12", + "94CF1A", + "40DE8A", + "1B9640", + "00D6C1", + "2E9CAA", + "00C4FF", + "364797", + "6675FF", + "0019EF", + "863AFF", + "530087", + "CD3AFF", + "FF97CA", + "FF39C9" +] + +ROBOFLOW_COLOR_PALETTE = [ + "C28DFC", + "A351FB", + "8315F9", + "6706CE", + "5905B3", + "4D049A" ] @@ -119,24 +132,32 @@ class Color: return self.b, self.g, self.r @property - def WHITE(cls): - return cls.from_hex("#ffffff") + def WHITE(self): + return Color.from_hex("#FFFFFF") @property - def BLACK(cls): - return cls.from_hex("#000000") + def BLACK(self): + return Color.from_hex("#000000") @property - def RED(cls): - return cls.from_hex("#ff0000") + def RED(self): + return Color.from_hex("#FF0000") @property - def GREEN(cls): - return cls.from_hex("#00ff00") + def GREEN(self): + return Color.from_hex("#00FF00") @property - def BLUE(cls): - return cls.from_hex("#0000ff") + def BLUE(self): + return Color.from_hex("#0000FF") + + @property + def YELLOW(self): + return Color.from_hex("#FFFF00") + + @property + def ROBOFLOW(self): + return Color.from_hex("#A351FB") @classmethod @deprecated( @@ -183,7 +204,43 @@ class Color: class ColorPalette: colors: List[Color] + @property + def DEFAULT(self): + """ + Returns a default color palette. + + Returns: + ColorPalette: A ColorPalette instance with default colors. + + Example: + ``` + ColorPalette.DEFAULT + # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...]) + ``` + """ # noqa: E501 // docs + return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE) + + @property + def ROBOFLOW(self): + """ + Returns a Roboflow color palette. + + Returns: + ColorPalette: A ColorPalette instance with Roboflow colors. + + Example: + ``` + ColorPalette.ROBOFLOW + # ColorPalette(colors=[Color(r=194, g=141, b=252), Color(r=163, g=81, b=251), ...]) + ``` + """ # noqa: E501 // docs + return ColorPalette.from_hex(color_hex_list=ROBOFLOW_COLOR_PALETTE) + @classmethod + @deprecated( + "`ColorPalette.default()` is deprecated and will be removed in " + "`supervision-0.20.0`. Use `Color.DEFAULT` instead." + ) def default(cls) -> ColorPalette: """ Returns a default color palette. @@ -193,10 +250,10 @@ class ColorPalette: Example: ``` - >>> ColorPalette.default() - ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...]) + ColorPalette.default() + # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...]) ``` - """ + """ # noqa: E501 // docs return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE) @classmethod @@ -239,10 +296,3 @@ class ColorPalette: raise ValueError("idx argument should not be negative") idx = idx % len(self.colors) return self.colors[idx] - - -Color.WHITE = Color.from_hex("#ffffff") -Color.BLACK = Color.from_hex("#000000") -Color.RED = Color.from_hex("#ff0000") -Color.GREEN = Color.from_hex("#00ff00") -Color.BLUE = Color.from_hex("#0000ff") From 7b2c09c0ab4d8a065379aa258f7c0b3ae8adcb49 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 14:19:08 +0000 Subject: [PATCH 02/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/draw/color.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index ebcc3296..d652f76a 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -26,17 +26,10 @@ DEFAULT_COLOR_PALETTE = [ "530087", "CD3AFF", "FF97CA", - "FF39C9" + "FF39C9", ] -ROBOFLOW_COLOR_PALETTE = [ - "C28DFC", - "A351FB", - "8315F9", - "6706CE", - "5905B3", - "4D049A" -] +ROBOFLOW_COLOR_PALETTE = ["C28DFC", "A351FB", "8315F9", "6706CE", "5905B3", "4D049A"] def _validate_color_hex(color_hex: str): From 2d0a3f9d79141e623c9fb523e74835b6eadd2f5d Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 15:42:27 +0100 Subject: [PATCH 03/11] working on optimal syntax --- supervision/draw/color.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index d652f76a..8e361b75 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -124,32 +124,39 @@ class Color: """ return self.b, self.g, self.r + @classmethod @property - def WHITE(self): + def WHITE(cls): return Color.from_hex("#FFFFFF") + @classmethod @property - def BLACK(self): + def BLACK(cls): return Color.from_hex("#000000") + @classmethod @property - def RED(self): + def RED(cls): return Color.from_hex("#FF0000") + @classmethod @property - def GREEN(self): + def GREEN(cls): return Color.from_hex("#00FF00") + @classmethod @property - def BLUE(self): + def BLUE(cls): return Color.from_hex("#0000FF") + @classmethod @property - def YELLOW(self): + def YELLOW(cls): return Color.from_hex("#FFFF00") + @classmethod @property - def ROBOFLOW(self): + def ROBOFLOW(cls): return Color.from_hex("#A351FB") @classmethod @@ -197,8 +204,9 @@ class Color: class ColorPalette: colors: List[Color] + @classmethod @property - def DEFAULT(self): + def DEFAULT(cls): """ Returns a default color palette. @@ -213,8 +221,9 @@ class ColorPalette: """ # noqa: E501 // docs return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE) + @classmethod @property - def ROBOFLOW(self): + def ROBOFLOW(cls): """ Returns a Roboflow color palette. From 66245dd0dff481dc49de800c2e6ee2c144cc1a60 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 16:24:15 +0100 Subject: [PATCH 04/11] added documentation and examples --- supervision/draw/color.py | 127 +++++++++++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 22 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 8e361b75..8b043aa5 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -29,6 +29,26 @@ DEFAULT_COLOR_PALETTE = [ "FF39C9", ] +LEGACY_COLOR_PALETTE = [ + "#A351FB", + "#E6194B", + "#3CB44B", + "#FFE119", + "#0082C8", + "#F58231", + "#911EB4", + "#46F0F0", + "#F032E6", + "#D2F53C", + "#FABEBE", + "#008080", + "#E6BEFF", + "#AA6E28", + "#FFFAC8", + "#800000", + "#AAFFC3", +] + ROBOFLOW_COLOR_PALETTE = ["C28DFC", "A351FB", "8315F9", "6706CE", "5905B3", "4D049A"] @@ -45,10 +65,31 @@ class Color: """ Represents a color in RGB format. + This class provides methods to work with colors, including creating colors from hex + codes, converting colors to hex strings, RGB tuples, and BGR tuples. + Attributes: - r (int): Red channel. - g (int): Green channel. - b (int): Blue channel. + r (int): Red channel value (0-255). + g (int): Green channel value (0-255). + b (int): Blue channel value (0-255). + + | Constant | Hex Code | RGB | + |------------|------------|------------------| + | `WHITE` | `#FFFFFF` | `(255, 255, 255)`| + | `BLACK` | `#000000` | `(0, 0, 0)` | + | `RED` | `#FF0000` | `(255, 0, 0)` | + | `GREEN` | `#00FF00` | `(0, 255, 0)` | + | `BLUE` | `#0000FF` | `(0, 0, 255)` | + | `YELLOW` | `#FFFF00` | `(255, 255, 0)` | + | `ROBOFLOW` | `#A351FB` | `(163, 81, 251)` | + + Example: + ```python + import supervision as sv + + sv.Color.WHITE + # Color(r=255, g=255, b=255) + ``` """ r: int @@ -67,9 +108,11 @@ class Color: Color: Instance representing the color. Example: - ``` - >>> Color.from_hex('#ff00ff') - Color(r=255, g=0, b=255) + ```python + import supervision as sv + + sv.Color.from_hex('#ff00ff') + # Color(r=255, g=0, b=255) ``` """ _validate_color_hex(color_hex) @@ -87,9 +130,11 @@ class Color: str: The hexadecimal color string. Example: - ``` - >>> Color(r=255, g=0, b=255).as_hex() - '#ff00ff' + ```python + import supervision as sv + + sv.Color(r=255, g=255, b=0).as_hex() + # '#ffff00' ``` """ return f"#{self.r:02x}{self.g:02x}{self.b:02x}" @@ -102,9 +147,11 @@ class Color: Tuple[int, int, int]: RGB tuple. Example: - ``` - >>> color.as_rgb() - (255, 0, 255) + ```python + import supervision as sv + + sv.Color(r=255, g=255, b=0).as_rgb() + # (255, 255, 0) ``` """ return self.r, self.g, self.b @@ -117,9 +164,11 @@ class Color: Tuple[int, int, int]: BGR tuple. Example: - ``` - >>> color.as_bgr() - (255, 0, 255) + ```python + import supervision as sv + + sv.Color(r=255, g=255, b=0).as_bgr() + # (0, 255, 255) ``` """ return self.b, self.g, self.r @@ -206,7 +255,7 @@ class ColorPalette: @classmethod @property - def DEFAULT(cls): + def DEFAULT(cls) ->ColorPalette: """ Returns a default color palette. @@ -214,16 +263,21 @@ class ColorPalette: ColorPalette: A ColorPalette instance with default colors. Example: - ``` - ColorPalette.DEFAULT + ```python + import supervision as sv + + sv.ColorPalette.DEFAULT # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...]) ``` + + ![default-color-palette](https://media.roboflow.com/ + supervision-annotator-examples/default-color-palette.png) """ # noqa: E501 // docs return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE) @classmethod @property - def ROBOFLOW(cls): + def ROBOFLOW(cls) -> ColorPalette: """ Returns a Roboflow color palette. @@ -231,13 +285,40 @@ class ColorPalette: ColorPalette: A ColorPalette instance with Roboflow colors. Example: - ``` - ColorPalette.ROBOFLOW + ```python + import supervision as sv + + sv.ColorPalette.ROBOFLOW # ColorPalette(colors=[Color(r=194, g=141, b=252), Color(r=163, g=81, b=251), ...]) ``` + + ![roboflow-color-palette](https://media.roboflow.com/ + supervision-annotator-examples/roboflow-color-palette.png) """ # noqa: E501 // docs return ColorPalette.from_hex(color_hex_list=ROBOFLOW_COLOR_PALETTE) + @classmethod + @property + def LEGACY(cls) -> ColorPalette: + """ + Returns a legacy color palette. + + Returns: + ColorPalette: A ColorPalette instance with legacy colors. + + Example: + ```python + import supervision as sv + + sv.ColorPalette.LEGACY + # ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...]) + ``` + + ![legacy-color-palette](https://media.roboflow.com/ + supervision-annotator-examples/legacy-color-palette.png) + """ + return ColorPalette.from_hex(color_hex_list=LEGACY_COLOR_PALETTE) + @classmethod @deprecated( "`ColorPalette.default()` is deprecated and will be removed in " @@ -252,7 +333,9 @@ class ColorPalette: Example: ``` - ColorPalette.default() + import supervision as sv + + sv.ColorPalette.default() # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...]) ``` """ # noqa: E501 // docs From fa8a9cb5acaa6f4331f0d49d043ab899baaf7dbd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:25:02 +0000 Subject: [PATCH 05/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/draw/color.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 8b043aa5..2d0fc6d6 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -255,7 +255,7 @@ class ColorPalette: @classmethod @property - def DEFAULT(cls) ->ColorPalette: + def DEFAULT(cls) -> ColorPalette: """ Returns a default color palette. @@ -269,7 +269,7 @@ class ColorPalette: sv.ColorPalette.DEFAULT # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...]) ``` - + ![default-color-palette](https://media.roboflow.com/ supervision-annotator-examples/default-color-palette.png) """ # noqa: E501 // docs @@ -291,7 +291,7 @@ class ColorPalette: sv.ColorPalette.ROBOFLOW # ColorPalette(colors=[Color(r=194, g=141, b=252), Color(r=163, g=81, b=251), ...]) ``` - + ![roboflow-color-palette](https://media.roboflow.com/ supervision-annotator-examples/roboflow-color-palette.png) """ # noqa: E501 // docs From 295e8b725b57d22ff1170c61d205cf2c8fe69e70 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 16:34:22 +0100 Subject: [PATCH 06/11] added `from_matplotlib` method --- supervision/draw/color.py | 64 ++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 2d0fc6d6..1ae86a97 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -3,6 +3,8 @@ from __future__ import annotations from dataclasses import dataclass from typing import List, Tuple +import matplotlib.pyplot as plt + from supervision.utils.internal import deprecated DEFAULT_COLOR_PALETTE = [ @@ -73,6 +75,14 @@ class Color: g (int): Green channel value (0-255). b (int): Blue channel value (0-255). + Example: + ```python + import supervision as sv + + sv.Color.WHITE + # Color(r=255, g=255, b=255) + ``` + | Constant | Hex Code | RGB | |------------|------------|------------------| | `WHITE` | `#FFFFFF` | `(255, 255, 255)`| @@ -82,14 +92,6 @@ class Color: | `BLUE` | `#0000FF` | `(0, 0, 255)` | | `YELLOW` | `#FFFF00` | `(255, 255, 0)` | | `ROBOFLOW` | `#A351FB` | `(163, 81, 251)` | - - Example: - ```python - import supervision as sv - - sv.Color.WHITE - # Color(r=255, g=255, b=255) - ``` """ r: int @@ -332,7 +334,7 @@ class ColorPalette: ColorPalette: A ColorPalette instance with default colors. Example: - ``` + ```python import supervision as sv sv.ColorPalette.default() @@ -353,14 +355,43 @@ class ColorPalette: ColorPalette: A ColorPalette instance. Example: - ``` - >>> ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff']) - ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...]) + ```python + import supervision as sv + + sv.ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff']) + # ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...]) ``` """ colors = [Color.from_hex(color_hex) for color_hex in color_hex_list] return cls(colors) + @classmethod + def from_matplotlib(cls, palette_name: str, color_count: int) -> ColorPalette: + """ + Create a ColorPalette instance from a Matplotlib color palette. + + Args: + palette_name (str): Name of the Matplotlib palette. + color_count (int): Number of colors to sample from the palette. + + Returns: + ColorPalette: A ColorPalette instance. + + Example: + ```python + import supervision as sv + + sv.ColorPalette.from_matplotlib('viridis', 5) + # ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...]) + ``` + """ + mpl_palette = plt.get_cmap(palette_name, color_count) + colors = [ + Color(int(r * 255), int(g * 255), int(b * 255)) + for r, g, b, _ in mpl_palette.colors + ] + return cls(colors) + def by_idx(self, idx: int) -> Color: """ Return the color at a given index in the palette. @@ -372,9 +403,12 @@ class ColorPalette: Color: Color at the given index. Example: - ``` - >>> color_palette.by_idx(1) - Color(r=0, g=255, b=0) + ```python + import supervision as sv + + color_palette = sv.ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff']) + color_palette.by_idx(1) + # Color(r=0, g=255, b=0) ``` """ if idx < 0: From b14a035ede5d55cf668544bda84c4698377a4519 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 16:45:15 +0100 Subject: [PATCH 07/11] final changes --- supervision/annotators/core.py | 28 ++++++++++++++-------------- supervision/detection/annotate.py | 2 +- supervision/draw/color.py | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 1c4a2527..822ef9f9 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -21,7 +21,7 @@ class BoundingBoxAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, color_lookup: ColorLookup = ColorLookup.CLASS, ): @@ -103,7 +103,7 @@ class MaskAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, opacity: float = 0.5, color_lookup: ColorLookup = ColorLookup.CLASS, ): @@ -186,7 +186,7 @@ class PolygonAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, color_lookup: ColorLookup = ColorLookup.CLASS, ): @@ -268,7 +268,7 @@ class ColorAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, opacity: float = 0.5, color_lookup: ColorLookup = ColorLookup.CLASS, ): @@ -354,7 +354,7 @@ class HaloAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, opacity: float = 0.8, kernel_size: int = 40, color_lookup: ColorLookup = ColorLookup.CLASS, @@ -446,7 +446,7 @@ class EllipseAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, start_angle: int = -45, end_angle: int = 235, @@ -536,7 +536,7 @@ class BoxCornerAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 4, corner_length: int = 15, color_lookup: ColorLookup = ColorLookup.CLASS, @@ -622,7 +622,7 @@ class CircleAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, color_lookup: ColorLookup = ColorLookup.CLASS, ): @@ -706,7 +706,7 @@ class DotAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, radius: int = 4, position: Position = Position.CENTER, color_lookup: ColorLookup = ColorLookup.CLASS, @@ -782,7 +782,7 @@ class LabelAnnotator: def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, text_color: Color = Color.BLACK, text_scale: float = 0.5, text_thickness: int = 1, @@ -1027,7 +1027,7 @@ class TraceAnnotator: def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, position: Position = Position.CENTER, trace_length: int = 30, thickness: int = 2, @@ -1288,7 +1288,7 @@ class TriangleAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, base: int = 10, height: int = 10, position: Position = Position.TOP_CENTER, @@ -1378,7 +1378,7 @@ class RoundBoxAnnotator(BaseAnnotator): def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, color_lookup: ColorLookup = ColorLookup.CLASS, roundness: float = 0.6, @@ -1507,7 +1507,7 @@ class PercentageBarAnnotator(BaseAnnotator): self, height: int = 16, width: int = 80, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, border_color: Color = Color.black(), position: Position = Position.TOP_CENTER, color_lookup: ColorLookup = ColorLookup.CLASS, diff --git a/supervision/detection/annotate.py b/supervision/detection/annotate.py index 1ab1bdc2..f3c80653 100644 --- a/supervision/detection/annotate.py +++ b/supervision/detection/annotate.py @@ -26,7 +26,7 @@ class BoxAnnotator: def __init__( self, - color: Union[Color, ColorPalette] = ColorPalette.default(), + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, text_color: Color = Color.BLACK, text_scale: float = 0.5, diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 1ae86a97..ae7ea6c9 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -384,7 +384,7 @@ class ColorPalette: sv.ColorPalette.from_matplotlib('viridis', 5) # ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...]) ``` - """ + """ # noqa: E501 // docs mpl_palette = plt.get_cmap(palette_name, color_count) colors = [ Color(int(r * 255), int(g * 255), int(b * 255)) From a9987dbc8358849ba09f6d95b09c0efa434a8452 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 16:57:10 +0100 Subject: [PATCH 08/11] test @classproperty decorator --- supervision/draw/color.py | 32 +++++++++++++++++--------------- supervision/utils/internal.py | 5 +++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index ae7ea6c9..07e704bc 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -5,7 +5,7 @@ from typing import List, Tuple import matplotlib.pyplot as plt -from supervision.utils.internal import deprecated +from supervision.utils.internal import deprecated, classproperty DEFAULT_COLOR_PALETTE = [ "A351FB", @@ -175,38 +175,31 @@ class Color: """ return self.b, self.g, self.r - @classmethod - @property + @classproperty def WHITE(cls): return Color.from_hex("#FFFFFF") - @classmethod - @property + @classproperty def BLACK(cls): return Color.from_hex("#000000") - @classmethod - @property + @classproperty def RED(cls): return Color.from_hex("#FF0000") - @classmethod - @property + @classproperty def GREEN(cls): return Color.from_hex("#00FF00") - @classmethod - @property + @classproperty def BLUE(cls): return Color.from_hex("#0000FF") - @classmethod - @property + @classproperty def YELLOW(cls): return Color.from_hex("#FFFF00") - @classmethod - @property + @classproperty def ROBOFLOW(cls): return Color.from_hex("#A351FB") @@ -415,3 +408,12 @@ class ColorPalette: raise ValueError("idx argument should not be negative") idx = idx % len(self.colors) return self.colors[idx] + + +# Color.WHITE = Color.from_hex("#FFFFFF") +# Color.BLACK = Color.from_hex("#000000") +# Color.RED = Color.from_hex("#FF0000") +# Color.GREEN = Color.from_hex("#00FF00") +# Color.BLUE = Color.from_hex("#0000FF") +# Color.YELLOW = Color.from_hex("#FFFF00") +# Color.ROBOFLOW = Color.from_hex("#A351FB") diff --git a/supervision/utils/internal.py b/supervision/utils/internal.py index eb84588e..452e46ec 100644 --- a/supervision/utils/internal.py +++ b/supervision/utils/internal.py @@ -16,3 +16,8 @@ def deprecated(reason: str): return wrapper return decorator + + +class classproperty(property): + def __get__(self, owner_self, owner_cls): + return self.fget(owner_cls) From 0f3359c15f47818dde28bd14b407250d3249950c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:57:56 +0000 Subject: [PATCH 09/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/draw/color.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 07e704bc..04807040 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -5,7 +5,7 @@ from typing import List, Tuple import matplotlib.pyplot as plt -from supervision.utils.internal import deprecated, classproperty +from supervision.utils.internal import classproperty, deprecated DEFAULT_COLOR_PALETTE = [ "A351FB", From c064c1fc98cccd2911ba1cd6e2655bdc0df5567d Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Tue, 23 Jan 2024 17:13:51 +0100 Subject: [PATCH 10/11] final tests --- supervision/draw/color.py | 38 ++++++----------------------------- supervision/utils/internal.py | 23 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 04807040..8aaf7af6 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -248,8 +248,7 @@ class Color: class ColorPalette: colors: List[Color] - @classmethod - @property + @classproperty def DEFAULT(cls) -> ColorPalette: """ Returns a default color palette. @@ -270,8 +269,7 @@ class ColorPalette: """ # noqa: E501 // docs return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE) - @classmethod - @property + @classproperty def ROBOFLOW(cls) -> ColorPalette: """ Returns a Roboflow color palette. @@ -292,26 +290,8 @@ class ColorPalette: """ # noqa: E501 // docs return ColorPalette.from_hex(color_hex_list=ROBOFLOW_COLOR_PALETTE) - @classmethod - @property + @classproperty def LEGACY(cls) -> ColorPalette: - """ - Returns a legacy color palette. - - Returns: - ColorPalette: A ColorPalette instance with legacy colors. - - Example: - ```python - import supervision as sv - - sv.ColorPalette.LEGACY - # ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...]) - ``` - - ![legacy-color-palette](https://media.roboflow.com/ - supervision-annotator-examples/legacy-color-palette.png) - """ return ColorPalette.from_hex(color_hex_list=LEGACY_COLOR_PALETTE) @classmethod @@ -377,6 +357,9 @@ class ColorPalette: sv.ColorPalette.from_matplotlib('viridis', 5) # ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...]) ``` + + ![visualized_color_palette](https://media.roboflow.com/ + supervision-annotator-examples/visualized_color_palette.png) """ # noqa: E501 // docs mpl_palette = plt.get_cmap(palette_name, color_count) colors = [ @@ -408,12 +391,3 @@ class ColorPalette: raise ValueError("idx argument should not be negative") idx = idx % len(self.colors) return self.colors[idx] - - -# Color.WHITE = Color.from_hex("#FFFFFF") -# Color.BLACK = Color.from_hex("#000000") -# Color.RED = Color.from_hex("#FF0000") -# Color.GREEN = Color.from_hex("#00FF00") -# Color.BLUE = Color.from_hex("#0000FF") -# Color.YELLOW = Color.from_hex("#FFFF00") -# Color.ROBOFLOW = Color.from_hex("#A351FB") diff --git a/supervision/utils/internal.py b/supervision/utils/internal.py index 452e46ec..031f5be1 100644 --- a/supervision/utils/internal.py +++ b/supervision/utils/internal.py @@ -19,5 +19,26 @@ def deprecated(reason: str): class classproperty(property): - def __get__(self, owner_self, owner_cls): + """ + A decorator that combines @classmethod and @property. + It allows a method to be accessed as a property of the class, + rather than an instance, similar to a classmethod. + + Usage: + @classproperty + def my_method(cls): + ... + """ + + def __get__(self, owner_self: object, owner_cls: type) -> object: + """ + Override the __get__ method to return the result of the function call. + + Args: + owner_self: The instance through which the attribute was accessed, or None. + owner_cls: The class through which the attribute was accessed. + + Returns: + The result of calling the function stored in 'fget' with 'owner_cls'. + """ return self.fget(owner_cls) From a1b2297e9f790e622a5ee2d73ec96a42d6d4995e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 16:14:03 +0000 Subject: [PATCH 11/11] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/draw/color.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/draw/color.py b/supervision/draw/color.py index 8aaf7af6..b592e660 100644 --- a/supervision/draw/color.py +++ b/supervision/draw/color.py @@ -357,7 +357,7 @@ class ColorPalette: sv.ColorPalette.from_matplotlib('viridis', 5) # ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...]) ``` - + ![visualized_color_palette](https://media.roboflow.com/ supervision-annotator-examples/visualized_color_palette.png) """ # noqa: E501 // docs