Skip to content

Commit 374ace8

Browse files
YosuaMichaelpmeierdatumbox
authored andcommitted
[fbsync] add warning if font is not set (#5785)
Summary: * add warning if font is not set * added quotes * added changes after review * Apply suggestions from code review * escaped warning string * Apply suggestions from code review * updated warning message in test * refactored code * Apply suggestions from code review Reviewed By: jdsgomes, NicolasHug Differential Revision: D36095644 fbshipit-source-id: efc171d4f4a5567b48b9365dc35e7d4ac45922a5 Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Vasilis Vryniotis <[email protected]>
1 parent 64730c4 commit 374ace8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

test/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
import tempfile
45
from io import BytesIO
@@ -168,6 +169,13 @@ def test_draw_invalid_boxes():
168169
utils.draw_bounding_boxes(img_correct, boxes, colors=colors_wrong)
169170

170171

172+
def test_draw_boxes_warning():
173+
img = torch.full((3, 100, 100), 255, dtype=torch.uint8)
174+
175+
with pytest.warns(UserWarning, match=re.escape("Argument 'font_size' will be ignored since 'font' is not set.")):
176+
utils.draw_bounding_boxes(img, boxes, font_size=11)
177+
178+
171179
@pytest.mark.parametrize(
172180
"colors",
173181
[

torchvision/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def draw_bounding_boxes(
164164
fill: Optional[bool] = False,
165165
width: int = 1,
166166
font: Optional[str] = None,
167-
font_size: int = 10,
167+
font_size: Optional[int] = None,
168168
) -> torch.Tensor:
169169

170170
"""
@@ -223,6 +223,13 @@ def draw_bounding_boxes(
223223

224224
colors = [(ImageColor.getrgb(color) if isinstance(color, str) else color) for color in colors]
225225

226+
if font is None:
227+
if font_size is not None:
228+
warnings.warn("Argument 'font_size' will be ignored since 'font' is not set.")
229+
txt_font = ImageFont.load_default()
230+
else:
231+
txt_font = ImageFont.truetype(font=font, size=font_size or 10)
232+
226233
# Handle Grayscale images
227234
if image.size(0) == 1:
228235
image = torch.tile(image, (3, 1, 1))
@@ -236,8 +243,6 @@ def draw_bounding_boxes(
236243
else:
237244
draw = ImageDraw.Draw(img_to_draw)
238245

239-
txt_font = ImageFont.load_default() if font is None else ImageFont.truetype(font=font, size=font_size)
240-
241246
for bbox, color, label in zip(img_boxes, colors, labels): # type: ignore[arg-type]
242247
if fill:
243248
fill_color = color + (100,)

0 commit comments

Comments
 (0)