Skip to content

Commit eb2e156

Browse files
NicolasHugdatumbox
authored andcommitted
Adds fill paramter to utils.draw_bounding_boxes (#3296)
Summary: * 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 Reviewed By: datumbox Differential Revision: D26226612 fbshipit-source-id: 485e9aec56c4f92da64c37a080ebcb6a2182a76f Co-authored-by: Vasilis Vryniotis <[email protected]>
1 parent 432aad6 commit eb2e156

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ gen.yml
2525
.idea/
2626
*.orig
2727
*-checkpoint.ipynb
28+
*.venv
57 Bytes
Loading

test/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_draw_boxes(self):
8686
[10, 15, 30, 35], [23, 35, 93, 95]], dtype=torch.float)
8787
labels = ["a", "b", "c", "d"]
8888
colors = ["green", "#FF00FF", (0, 255, 0), "red"]
89-
result = utils.draw_bounding_boxes(img, boxes, labels=labels, colors=colors)
89+
result = utils.draw_bounding_boxes(img, boxes, labels=labels, colors=colors, fill=True)
9090

9191
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "fakedata", "draw_boxes_util.png")
9292
if not os.path.exists(path):

torchvision/utils.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import math
55
import warnings
66
import numpy as np
7-
from PIL import Image, ImageDraw
8-
from PIL import ImageFont
7+
from PIL import Image, ImageDraw, ImageFont, ImageColor
98

109
__all__ = ["make_grid", "save_image", "draw_bounding_boxes"]
1110

@@ -142,6 +141,7 @@ def draw_bounding_boxes(
142141
boxes: torch.Tensor,
143142
labels: Optional[List[str]] = None,
144143
colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None,
144+
fill: Optional[bool] = False,
145145
width: int = 1,
146146
font: Optional[str] = None,
147147
font_size: int = 10
@@ -150,6 +150,7 @@ def draw_bounding_boxes(
150150
"""
151151
Draws bounding boxes on given image.
152152
The values of the input image should be uint8 between 0 and 255.
153+
If filled, Resulting Tensor should be saved as PNG image.
153154
154155
Args:
155156
image (Tensor): Tensor of shape (C x H x W)
@@ -159,6 +160,7 @@ def draw_bounding_boxes(
159160
labels (List[str]): List containing the labels of bounding boxes.
160161
colors (List[Union[str, Tuple[int, int, int]]]): List containing the colors of bounding boxes. The colors can
161162
be represented as `str` or `Tuple[int, int, int]`.
163+
fill (bool): If `True` fills the bounding box with specified color.
162164
width (int): Width of bounding box.
163165
font (str): A filename containing a TrueType font. If the file is not found in this filename, the loader may
164166
also search in other directories, such as the `fonts/` directory on Windows or `/Library/Fonts/`,
@@ -178,12 +180,31 @@ def draw_bounding_boxes(
178180

179181
img_boxes = boxes.to(torch.int64).tolist()
180182

181-
draw = ImageDraw.Draw(img_to_draw)
183+
if fill:
184+
draw = ImageDraw.Draw(img_to_draw, "RGBA")
185+
186+
else:
187+
draw = ImageDraw.Draw(img_to_draw)
188+
182189
txt_font = ImageFont.load_default() if font is None else ImageFont.truetype(font=font, size=font_size)
183190

184191
for i, bbox in enumerate(img_boxes):
185-
color = None if colors is None else colors[i]
186-
draw.rectangle(bbox, width=width, outline=color)
192+
if colors is None:
193+
color = None
194+
else:
195+
color = colors[i]
196+
197+
if fill:
198+
if color is None:
199+
fill_color = (255, 255, 255, 100)
200+
elif isinstance(color, str):
201+
# This will automatically raise Error if rgb cannot be parsed.
202+
fill_color = ImageColor.getrgb(color) + (100,)
203+
elif isinstance(color, tuple):
204+
fill_color = color + (100,)
205+
draw.rectangle(bbox, width=width, outline=color, fill=fill_color)
206+
else:
207+
draw.rectangle(bbox, width=width, outline=color)
187208

188209
if labels is not None:
189210
draw.text((bbox[0], bbox[1]), labels[i], fill=color, font=txt_font)

0 commit comments

Comments
 (0)