File size: 1,265 Bytes
37de32d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
This utils script contains PORTAGE of wai-core semantics methods for MapAnything.
"""

import numpy as np
from PIL import Image

INVALID_ID = 0
INVALID_COLOR = (0, 0, 0)


def load_semantic_color_mapping(filename: str = "colors_fps_5k.npz") -> np.ndarray:
    """Loads a precomputed colormap."""
    from mapanything.utils.wai.core import WAI_COLORMAP_PATH

    return np.load(WAI_COLORMAP_PATH / filename).get("arr_0")


def apply_id_to_color_mapping(
    data_id: np.ndarray | Image.Image,
    semantic_color_mapping: np.ndarray,
) -> tuple[np.ndarray, dict[int, tuple[int, int, int]]]:
    """Maps semantic class/instance IDs to RGB colors."""
    if isinstance(data_id, Image.Image):
        data_id = np.array(data_id)

    max_color_id = semantic_color_mapping.shape[0] - 1
    max_data_id = data_id.max()
    if max_data_id > max_color_id:
        raise ValueError("The provided color palette does not have enough colors!")

    # Create palette containing the id->color mappings of the input data IDs
    unique_indices = np.unique(data_id).tolist()
    color_palette = {
        index: semantic_color_mapping[index, :].tolist() for index in unique_indices
    }

    data_colors = semantic_color_mapping[data_id]

    return data_colors, color_palette