-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Add label background #9018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AntoineSimoulin
merged 7 commits into
pytorch:main
from
AntoineSimoulin:add-label-background
Apr 16, 2025
Merged
Add label background #9018
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15c613e
Add label background
AntoineSimoulin 69a320e
Apply suggestions from code review
AntoineSimoulin 2e72bbb
Let user specify `label_color`
AntoineSimoulin eabb41a
Add tests for label background drawing
AntoineSimoulin ae6dafb
Update torchvision/utils.py
AntoineSimoulin 697a358
Update torchvision/utils.py
AntoineSimoulin 670ffa5
Merge branch 'main' into add-label-background
AntoineSimoulin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,6 +162,7 @@ def draw_bounding_boxes( | |
| font: Optional[str] = None, | ||
| font_size: Optional[int] = None, | ||
| label_colors: Optional[Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]] = None, | ||
| fill_labels: Optional[bool] = False, | ||
| ) -> torch.Tensor: | ||
|
|
||
| """ | ||
|
|
@@ -187,6 +188,7 @@ def draw_bounding_boxes( | |
| font_size (int): The requested font size in points. | ||
| label_colors (color or list of colors, optional): Colors for the label text. See the description of the | ||
| `colors` argument for details. Defaults to the same colors used for the boxes. | ||
AntoineSimoulin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fill_labels (bool): If `True` fills the label background with specified color. | ||
AntoineSimoulin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Returns: | ||
| img (Tensor[C, H, W]): Image Tensor of dtype uint8 with bounding boxes plotted. | ||
|
|
@@ -259,7 +261,14 @@ def draw_bounding_boxes( | |
| draw.rectangle(bbox, width=width, outline=color) | ||
|
|
||
| if label is not None: | ||
| margin = width + 1 | ||
| box_margin = 1 | ||
| margin = width + box_margin | ||
| if fill_labels: | ||
| left, top, right, bottom = draw.textbbox((bbox[0] + margin, bbox[1] + margin), label, font=txt_font) | ||
| draw.rectangle( | ||
| (left - box_margin, top - box_margin, right + box_margin, bottom + box_margin), fill=color | ||
| ) | ||
| label_color = "black" | ||
|
||
| draw.text((bbox[0] + margin, bbox[1] + margin), label, fill=label_color, font=txt_font) # type: ignore[arg-type] | ||
|
|
||
| out = F.pil_to_tensor(img_to_draw) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.