-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix annotation of draw_segmentation_masks #4527
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
Changes from 8 commits
6cb46e4
663fa4c
2c34a52
b9da8f3
0482b7a
be63a15
dec7270
de943a9
9f49b23
bda9132
456bc92
7723858
2489fea
10b4e7e
36683cb
8295044
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,9 +159,9 @@ def draw_bounding_boxes( | |
the boxes are absolute coordinates with respect to the image. In other words: `0 <= xmin < xmax < W` and | ||
`0 <= ymin < ymax < H`. | ||
labels (List[str]): List containing the labels of bounding boxes. | ||
colors (Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]): List containing the colors | ||
or a single color for all of the bounding boxes. The colors can be represented as `str` or | ||
`Tuple[int, int, int]`. | ||
colors: (color or list of colors, optional): List containing the colors | ||
of the boxes or single color for all boxes. The color can be represented as | ||
PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``. | ||
fill (bool): If `True` fills the bounding box with specified color. | ||
width (int): Width of bounding box. | ||
font (str): A filename containing a TrueType font. If the file is not found in this filename, the loader may | ||
|
@@ -230,7 +230,7 @@ def draw_segmentation_masks( | |
image: torch.Tensor, | ||
masks: torch.Tensor, | ||
alpha: float = 0.8, | ||
colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None, | ||
colors: Optional[Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]] = None, | ||
) -> torch.Tensor: | ||
|
||
""" | ||
|
@@ -242,10 +242,10 @@ def draw_segmentation_masks( | |
masks (Tensor): Tensor of shape (num_masks, H, W) or (H, W) and dtype bool. | ||
alpha (float): Float number between 0 and 1 denoting the transparency of the masks. | ||
0 means full transparency, 1 means no transparency. | ||
colors (list or None): List containing the colors of the masks. The colors can | ||
be represented as PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``. | ||
When ``masks`` has a single entry of shape (H, W), you can pass a single color instead of a list | ||
with one element. By default, random colors are generated for each mask. | ||
colors: (color or list of colors, optional): List containing the colors | ||
oke-aditya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
of the masks or single color for all masks. The color can be represented as | ||
PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``. | ||
By default, random colors are generated for each mask. | ||
|
||
Returns: | ||
img (Tensor[C, H, W]): Image Tensor, with segmentation masks drawn on top. | ||
|
@@ -288,18 +288,17 @@ def draw_segmentation_masks( | |
for color in colors: | ||
if isinstance(color, str): | ||
color = ImageColor.getrgb(color) | ||
color = torch.tensor(color, dtype=out_dtype) | ||
colors_.append(color) | ||
colors_.append(torch.tensor(color, dtype=out_dtype)) | ||
|
||
img_to_draw = image.detach().clone() | ||
# TODO: There might be a way to vectorize this | ||
for mask, color in zip(masks, colors_): | ||
img_to_draw[:, mask] = color[:, None] | ||
for mask, color_tensor in zip(masks, colors_): | ||
img_to_draw[:, mask] = color_tensor[:, None] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to preserve git blame I would suggest to revert these changes, as I don't think they significantly improve readability either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Problem is mypy. To satisfy mypy I had to change the name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is mypy complaining about? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mypy complains that
The easiest fix was to rename, rather than ignoring mypy errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let's leave it as-is then. @pmeier what is your take on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this is one of the most frustrating things when working with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done #4531 |
||
|
||
out = image * (1 - alpha) + img_to_draw * alpha | ||
return out.to(out_dtype) | ||
|
||
|
||
def _generate_color_palette(num_masks): | ||
def _generate_color_palette(num_masks: int): | ||
palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1]) | ||
return [tuple((i * palette) % 255) for i in range(num_masks)] |
Uh oh!
There was an error while loading. Please reload this page.