| from typing import Optional
|
|
|
| import numpy as np
|
| from PIL import Image
|
|
|
|
|
| def show_masks(
|
| image: np.ndarray,
|
| masks: np.ndarray,
|
| scores: Optional[np.ndarray],
|
| alpha: Optional[float] = 0.5,
|
| display_image: Optional[bool] = False,
|
| only_best: Optional[bool] = True,
|
| autogenerated_mask: Optional[bool] = False,
|
| ) -> Image.Image:
|
| if scores is not None:
|
|
|
| sorted_ind = np.argsort(scores)[::-1]
|
| masks = masks[sorted_ind]
|
|
|
| if autogenerated_mask:
|
| masks = sorted(masks, key=(lambda x: x["area"]), reverse=True)
|
| else:
|
|
|
| h, w = masks.shape[-2:]
|
|
|
| if display_image:
|
| output_image = Image.fromarray(image)
|
| else:
|
|
|
| if autogenerated_mask:
|
| output_image = Image.new(
|
| mode="RGBA",
|
| size=(
|
| masks[0]["segmentation"].shape[1],
|
| masks[0]["segmentation"].shape[0],
|
| ),
|
| color=(0, 0, 0),
|
| )
|
| else:
|
| output_image = Image.new(mode="RGBA", size=(w, h), color=(0, 0, 0))
|
|
|
| for i, mask in enumerate(masks):
|
| if not autogenerated_mask:
|
| if mask.ndim > 2:
|
| mask = mask.squeeze()
|
| else:
|
| mask = mask["segmentation"]
|
|
|
| color = np.concatenate(
|
| (np.random.randint(0, 256, size=3), [int(alpha * 255)]), axis=0
|
| )
|
|
|
|
|
| mask_image = Image.fromarray((mask * 255).astype(np.uint8)).convert("L")
|
| mask_colored = Image.new("RGBA", mask_image.size, tuple(color))
|
| mask_image = Image.composite(
|
| mask_colored, Image.new("RGBA", mask_image.size), mask_image
|
| )
|
|
|
|
|
| output_image = Image.alpha_composite(output_image, mask_image)
|
|
|
|
|
| if only_best:
|
| break
|
|
|
| return output_image
|
|
|