Add new drawing methods and improve type hints

Expanded the drawing functionality by adding new utility methods 'draw_image', 'draw_line' and 'draw_rectangle' and imported them in supervision/__init__.py. Updated the documentation of draw/utils, to include 'draw_image'. Improved type hinting in draw/color.py and draw/utils.py. Also, some redundant comments and unnecessary immediate memory freeing line are removed from draw/utils.py to enhance code readability.
This commit is contained in:
SkalskiP 2023-10-18 22:31:34 +02:00
parent 78a4e73f25
commit 737cb37249
4 changed files with 17 additions and 5 deletions

View File

@ -17,3 +17,7 @@
## draw_text
:::supervision.draw.utils.draw_text
## draw_image
:::supervision.draw.utils.draw_image

View File

@ -42,7 +42,14 @@ from supervision.detection.utils import (
polygon_to_xyxy,
)
from supervision.draw.color import Color, ColorPalette
from supervision.draw.utils import draw_filled_rectangle, draw_polygon, draw_text
from supervision.draw.utils import (
draw_filled_rectangle,
draw_polygon,
draw_text,
draw_image,
draw_line,
draw_rectangle
)
from supervision.geometry.core import Point, Position, Rect
from supervision.geometry.utils import get_polygon_center
from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision

View File

@ -48,7 +48,7 @@ class Color:
b: int
@classmethod
def from_hex(cls, color_hex: str):
def from_hex(cls, color_hex: str) -> Color:
"""
Create a Color instance from a hex string.
@ -143,7 +143,7 @@ class ColorPalette:
return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE)
@classmethod
def from_hex(cls, color_hex_list: List[str]):
def from_hex(cls, color_hex_list: List[str]) -> ColorPalette:
"""
Create a ColorPalette instance from a list of hex strings.

View File

@ -190,11 +190,13 @@ def draw_image(
np.ndarray: The scene with the image drawn onto it.
Example:
```python
>>> scene = np.zeros((400, 400, 3), dtype=np.uint8)
>>> image_path = "path/to/image.jpg"
>>> opacity = 0.5
>>> rect = Rect(x=50, y=50, width=200, height=200)
>>> new_scene = draw_image(scene, image_path, opacity, rect)
```
"""
if isinstance(image, str):
assert os.path.exists(image), f'The specified path ("{image}") does not exist.'
@ -211,14 +213,13 @@ def draw_image(
image = cv2.resize(image, (rect.width, rect.height))
# watermark with transparent background
if image.shape[2] == 4:
b, g, r, a = cv2.split(image)
b = cv2.bitwise_and(b, b, mask=a)
g = cv2.bitwise_and(g, g, mask=a)
r = cv2.bitwise_and(r, r, mask=a)
image = cv2.merge([b, g, r, a])
del b, g, r, a # immediately free up memory
del b, g, r, a
if scene.shape[2] == 3:
scene = np.dstack([scene, np.ones(scene.shape[:2], dtype=np.uint8) * 255])