File size: 2,154 Bytes
bd9da36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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:
        # sort masks by their scores
        sorted_ind = np.argsort(scores)[::-1]
        masks = masks[sorted_ind]

    if autogenerated_mask:
        masks = sorted(masks, key=(lambda x: x["area"]), reverse=True)
    else:
        # get mask dimensions
        h, w = masks.shape[-2:]

    if display_image:
        output_image = Image.fromarray(image)
    else:
        # create a new blank image to superimpose masks
        if autogenerated_mask:
            output_image = Image.new(
                mode="RGBA",
                size=(
                    masks[0]["segmentation"].shape[0],
                    masks[0]["segmentation"].shape[1],
                ),
                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:  # type: ignore
                mask = mask.squeeze()  # type: ignore
        else:
            mask = mask["segmentation"]
        # Generate a random color with specified alpha value
        color = np.concatenate(
            (np.random.randint(0, 256, size=3), [int(alpha * 255)]), axis=0
        )

        # Create an RGBA image for the mask
        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
        )

        # Overlay mask on the output image
        output_image = Image.alpha_composite(output_image, mask_image)

        # Exit if specified to only display the best mask
        if only_best:
            break

    return output_image