Unverified Commit b2cf6045 authored by Aditya Oke's avatar Aditya Oke Committed by GitHub
Browse files

Adds fill paramter to utils.draw_bounding_boxes (#3296)



* adds fill paramter

* small doc edit

* Change fill to bool

* add filled

* fix the bugs

* Fixes bugs

* adds test with fill param

* fix tuple bug
Co-authored-by: default avatarVasilis Vryniotis <datumbox@users.noreply.github.com>
parent 1e3a183e
...@@ -25,3 +25,4 @@ gen.yml ...@@ -25,3 +25,4 @@ gen.yml
.idea/ .idea/
*.orig *.orig
*-checkpoint.ipynb *-checkpoint.ipynb
*.venv
test/assets/fakedata/draw_boxes_util.png

490 Bytes | W: | H:

test/assets/fakedata/draw_boxes_util.png

547 Bytes | W: | H:

test/assets/fakedata/draw_boxes_util.png
test/assets/fakedata/draw_boxes_util.png
test/assets/fakedata/draw_boxes_util.png
test/assets/fakedata/draw_boxes_util.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -86,7 +86,7 @@ class Tester(unittest.TestCase): ...@@ -86,7 +86,7 @@ class Tester(unittest.TestCase):
[10, 15, 30, 35], [23, 35, 93, 95]], dtype=torch.float) [10, 15, 30, 35], [23, 35, 93, 95]], dtype=torch.float)
labels = ["a", "b", "c", "d"] labels = ["a", "b", "c", "d"]
colors = ["green", "#FF00FF", (0, 255, 0), "red"] colors = ["green", "#FF00FF", (0, 255, 0), "red"]
result = utils.draw_bounding_boxes(img, boxes, labels=labels, colors=colors) result = utils.draw_bounding_boxes(img, boxes, labels=labels, colors=colors, fill=True)
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "fakedata", "draw_boxes_util.png") path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "fakedata", "draw_boxes_util.png")
if not os.path.exists(path): if not os.path.exists(path):
......
...@@ -4,8 +4,7 @@ import torch ...@@ -4,8 +4,7 @@ import torch
import math import math
import warnings import warnings
import numpy as np import numpy as np
from PIL import Image, ImageDraw from PIL import Image, ImageDraw, ImageFont, ImageColor
from PIL import ImageFont
__all__ = ["make_grid", "save_image", "draw_bounding_boxes"] __all__ = ["make_grid", "save_image", "draw_bounding_boxes"]
...@@ -142,6 +141,7 @@ def draw_bounding_boxes( ...@@ -142,6 +141,7 @@ def draw_bounding_boxes(
boxes: torch.Tensor, boxes: torch.Tensor,
labels: Optional[List[str]] = None, labels: Optional[List[str]] = None,
colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None, colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None,
fill: Optional[bool] = False,
width: int = 1, width: int = 1,
font: Optional[str] = None, font: Optional[str] = None,
font_size: int = 10 font_size: int = 10
...@@ -150,6 +150,7 @@ def draw_bounding_boxes( ...@@ -150,6 +150,7 @@ def draw_bounding_boxes(
""" """
Draws bounding boxes on given image. Draws bounding boxes on given image.
The values of the input image should be uint8 between 0 and 255. The values of the input image should be uint8 between 0 and 255.
If filled, Resulting Tensor should be saved as PNG image.
Args: Args:
image (Tensor): Tensor of shape (C x H x W) image (Tensor): Tensor of shape (C x H x W)
...@@ -159,6 +160,7 @@ def draw_bounding_boxes( ...@@ -159,6 +160,7 @@ def draw_bounding_boxes(
labels (List[str]): List containing the labels of bounding boxes. labels (List[str]): List containing the labels of bounding boxes.
colors (List[Union[str, Tuple[int, int, int]]]): List containing the colors of bounding boxes. The colors can colors (List[Union[str, Tuple[int, int, int]]]): List containing the colors of bounding boxes. The colors can
be represented as `str` or `Tuple[int, int, int]`. be represented as `str` or `Tuple[int, int, int]`.
fill (bool): If `True` fills the bounding box with specified color.
width (int): Width of bounding box. width (int): Width of bounding box.
font (str): A filename containing a TrueType font. If the file is not found in this filename, the loader may font (str): A filename containing a TrueType font. If the file is not found in this filename, the loader may
also search in other directories, such as the `fonts/` directory on Windows or `/Library/Fonts/`, also search in other directories, such as the `fonts/` directory on Windows or `/Library/Fonts/`,
...@@ -178,11 +180,30 @@ def draw_bounding_boxes( ...@@ -178,11 +180,30 @@ def draw_bounding_boxes(
img_boxes = boxes.to(torch.int64).tolist() img_boxes = boxes.to(torch.int64).tolist()
if fill:
draw = ImageDraw.Draw(img_to_draw, "RGBA")
else:
draw = ImageDraw.Draw(img_to_draw) draw = ImageDraw.Draw(img_to_draw)
txt_font = ImageFont.load_default() if font is None else ImageFont.truetype(font=font, size=font_size) txt_font = ImageFont.load_default() if font is None else ImageFont.truetype(font=font, size=font_size)
for i, bbox in enumerate(img_boxes): for i, bbox in enumerate(img_boxes):
color = None if colors is None else colors[i] if colors is None:
color = None
else:
color = colors[i]
if fill:
if color is None:
fill_color = (255, 255, 255, 100)
elif isinstance(color, str):
# This will automatically raise Error if rgb cannot be parsed.
fill_color = ImageColor.getrgb(color) + (100,)
elif isinstance(color, tuple):
fill_color = color + (100,)
draw.rectangle(bbox, width=width, outline=color, fill=fill_color)
else:
draw.rectangle(bbox, width=width, outline=color) draw.rectangle(bbox, width=width, outline=color)
if labels is not None: if labels is not None:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment