Skip to content

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
merged 7 commits into from
Apr 16, 2025

Conversation

AntoineSimoulin
Copy link
Member

@AntoineSimoulin AntoineSimoulin commented Apr 14, 2025

Context

This PR add the ability to add background color for the label of bounding boxes. In this case, the background label will be the same as the one used for the box frame. The text itself will automatically be switched to black. This should allow better visibility in cases where brightness and darkness prevent from distinguishing regular labels, as mentioned in #8317

How to use

We add a fill_labels parameter in the draw_bounding_boxes function. As illustrated below, this will draw a colored rectangle with the same size of the text rectangle for the text label. By default, the option is deactivated.

boxes = torch.tensor(
    [[100, 100, 150, 150], [50, 50, 100, 100], [300, 100, 400, 300]],
    dtype=torch.float32,
)

image = torch.ones((3, 500, 500), dtype=torch.uint8) * 255

colors = ["blue", "yellow", "red"]
image = draw_bounding_boxes(
    image, boxes, colors=colors, width=1, fill=False, labels=colors, fill_labels=True
)
plt.imshow(image.numpy().transpose((1, 2, 0)))
plt.axis("off")
plt.show()

image

Copy link

pytorch-bot bot commented Apr 14, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9018

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 670ffa5 with merge base ac7ad5f (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @AntoineSimoulin , this looks good! I made some minor comments below, it'd be also great to have some small test to check the expected behavior, similar to the existing tests we have for draw_bounding_boxes

draw.rectangle(
(left - box_margin, top - box_margin, right + box_margin, bottom + box_margin), fill=color
)
label_color = "black"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any reason to force label_color to black? Could we just make black the default, but let the user specify it if they wanted to?

AntoineSimoulin and others added 2 commits April 15, 2025 08:56
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
@AntoineSimoulin
Copy link
Member Author

@NicolasHug, thanks for the comments. I did the following modifications:

  • Let the user choose the background color and default to black otherwise in 2e72bbb;
  • Added some tests in 9f9e320, similar to the existing tests we have for draw_bounding_boxes. Please run with pytest test/test_utils.py -vvv -k test_draw_boxes_with_coloured_label_backgrounds.

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

AntoineSimoulin and others added 3 commits April 16, 2025 08:03
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
@AntoineSimoulin AntoineSimoulin merged commit f799a53 into pytorch:main Apr 16, 2025
58 checks passed
Copy link

Hey @AntoineSimoulin!

You merged this PR, but no labels were added.
The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants