-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Adds fill paramter to utils.draw_bounding_boxes #3296
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
Conversation
There was a problem hiding this 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! I left a couple of comments for your consideration.
Also could you also change the unit-test to support the new param once you finalize the implementation? You will need to produce a new expected file but I think the code already supports this if you delete the old file.
torchvision/utils.py
Outdated
txt_font = ImageFont.load_default() if font is None else ImageFont.truetype(font=font, size=font_size) | ||
|
||
for i, bbox in enumerate(img_boxes): | ||
color = None if colors is None else colors[i] | ||
draw.rectangle(bbox, width=width, outline=color) | ||
if fill: | ||
fill_color = color + [100] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we would need to set alpha channel to some hardcoded value. Since that gives the semi transparent look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that this causes issues on the type checks. Also this is not going to work for all values of color
. There are various corner cases:
- The
color
could be None - It could be a string
- Or a tuple.
It might be worth confirming that all work as expected by adding a test, what you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If fill is true
If color is None
lets fill with default color.
If color is Tuple[int, int, int]
I think we just add alpha of 100 as above and do.
If color is str
I think we need to get RGB value of the str add default alpha of 100 and draw ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to handle the fill_color = color + [100]
especially in each corner case:
>>> None + [100]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'
>>> (0, 0, 0) + [100]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple
>>> "red" + [100]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Sorry, I'm bit low on bandwidth this week but will complete by next week.
I will add tests too to check these corner cases. It's really simple to handle and I should have thought before 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry mate. :) Take your time! Ping me when ready to make sure I review it again. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be able to update the following test to use your new feature?
Lines 83 to 97 in a0e5bbe
def test_draw_boxes(self): | |
img = torch.full((3, 100, 100), 255, dtype=torch.uint8) | |
boxes = torch.tensor([[0, 0, 20, 20], [0, 0, 0, 0], | |
[10, 15, 30, 35], [23, 35, 93, 95]], dtype=torch.float) | |
labels = ["a", "b", "c", "d"] | |
colors = ["green", "#FF00FF", (0, 255, 0), "red"] | |
result = utils.draw_bounding_boxes(img, boxes, labels=labels, colors=colors) | |
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "fakedata", "draw_boxes_util.png") | |
if not os.path.exists(path): | |
res = Image.fromarray(result.permute(1, 2, 0).contiguous().numpy()) | |
res.save(path) | |
expected = torch.as_tensor(np.array(Image.open(path))).permute(2, 0, 1) | |
self.assertTrue(torch.equal(result, expected)) |
Feel free to update the expected file as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oke-aditya I added a couple of more comments, please let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
Done ! @datumbox It works well now, semi transparent images look good. And sorry for the delay, it was really simple to fix, but I was on vacation 😅 |
Codecov Report
@@ Coverage Diff @@
## master #3296 +/- ##
==========================================
- Coverage 73.96% 73.90% -0.07%
==========================================
Files 104 104
Lines 9607 9618 +11
Branches 1537 1543 +6
==========================================
+ Hits 7106 7108 +2
- Misses 2024 2028 +4
- Partials 477 482 +5
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks a lot for the PR @oke-aditya!
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]>
Closes #3280 .
Currently, I have kept fill to
Tuple[int, int, int, int]
orstr
.Let me know if changes are needed.
Also, let me know how we can test this.