Unverified Commit 669d5655 authored by Lezwon Castelino's avatar Lezwon Castelino Committed by GitHub
Browse files

add warning if font is not set (#5785)



* add warning if font is not set

* added quotes

* added changes after review

* Apply suggestions from code review
Co-authored-by: default avatarPhilip Meier <github.pmeier@posteo.de>

* escaped warning string

* Apply suggestions from code review
Co-authored-by: default avatarPhilip Meier <github.pmeier@posteo.de>

* updated warning message in test

* refactored code

* Apply suggestions from code review
Co-authored-by: default avatarPhilip Meier <github.pmeier@posteo.de>
Co-authored-by: default avatarPhilip Meier <github.pmeier@posteo.de>
Co-authored-by: default avatarVasilis Vryniotis <datumbox@users.noreply.github.com>
parent e9fb2a35
import os import os
import re
import sys import sys
import tempfile import tempfile
from io import BytesIO from io import BytesIO
...@@ -168,6 +169,13 @@ def test_draw_invalid_boxes(): ...@@ -168,6 +169,13 @@ def test_draw_invalid_boxes():
utils.draw_bounding_boxes(img_correct, boxes, colors=colors_wrong) utils.draw_bounding_boxes(img_correct, boxes, colors=colors_wrong)
def test_draw_boxes_warning():
img = torch.full((3, 100, 100), 255, dtype=torch.uint8)
with pytest.warns(UserWarning, match=re.escape("Argument 'font_size' will be ignored since 'font' is not set.")):
utils.draw_bounding_boxes(img, boxes, font_size=11)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"colors", "colors",
[ [
......
...@@ -164,7 +164,7 @@ def draw_bounding_boxes( ...@@ -164,7 +164,7 @@ def draw_bounding_boxes(
fill: Optional[bool] = False, fill: Optional[bool] = False,
width: int = 1, width: int = 1,
font: Optional[str] = None, font: Optional[str] = None,
font_size: int = 10, font_size: Optional[int] = None,
) -> torch.Tensor: ) -> torch.Tensor:
""" """
...@@ -223,6 +223,13 @@ def draw_bounding_boxes( ...@@ -223,6 +223,13 @@ def draw_bounding_boxes(
colors = [(ImageColor.getrgb(color) if isinstance(color, str) else color) for color in colors] colors = [(ImageColor.getrgb(color) if isinstance(color, str) else color) for color in colors]
if font is None:
if font_size is not None:
warnings.warn("Argument 'font_size' will be ignored since 'font' is not set.")
txt_font = ImageFont.load_default()
else:
txt_font = ImageFont.truetype(font=font, size=font_size or 10)
# Handle Grayscale images # Handle Grayscale images
if image.size(0) == 1: if image.size(0) == 1:
image = torch.tile(image, (3, 1, 1)) image = torch.tile(image, (3, 1, 1))
...@@ -236,8 +243,6 @@ def draw_bounding_boxes( ...@@ -236,8 +243,6 @@ def draw_bounding_boxes(
else: 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)
for bbox, color, label in zip(img_boxes, colors, labels): # type: ignore[arg-type] for bbox, color, label in zip(img_boxes, colors, labels): # type: ignore[arg-type]
if fill: if fill:
fill_color = color + (100,) fill_color = color + (100,)
......
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