File size: 3,758 Bytes
6c2a15f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dc2bc3
6c2a15f
 
 
 
 
 
 
 
 
 
 
4dc2bc3
 
 
 
6c2a15f
4dc2bc3
 
6c2a15f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dc2bc3
6c2a15f
 
 
 
 
4dc2bc3
6c2a15f
 
 
 
 
 
 
 
 
 
 
 
 
 
4dc2bc3
 
 
 
6c2a15f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
gradio app.py

for semantic segmentation
"""
import os

import cv2
import gradio as gr
import numpy as np

from otfgt import mask2sbd


def gen_sbd(image, mask):
    h, w = image.shape[:2]
    if w > 1280 or h > 720:
        resize_factor = max(w / 1280, h / 720)
        h = int(h / resize_factor)
        w = int(w / resize_factor)
    image = cv2.resize(image, (w, h))
    mask = cv2.resize(mask, (w, h), interpolation=cv2.INTER_NEAREST)

    binary_labels = np.zeros((19, mask.shape[0], mask.shape[1]), dtype=np.uint8)
    unique_labels = np.unique(mask)
    for label in unique_labels:
        binary_labels[label] = mask == label
    sbd = mask2sbd(binary_labels, ignore_indices=[])
    # remove the first channel (background)
    sbd = sbd[1:]
    unique_boundary_labels = np.unique(np.where(sbd == 1)[0])
    value = [(sbd[x], ID2LABEL[x]) for x in unique_boundary_labels]

    # change 0 to 255
    mask[mask == 0] = 255
    # reduce the entries by 1
    mask -= 1
    unique_labels = np.unique(mask)
    # remove 254 (background) from unique_labels
    unique_labels = unique_labels[unique_labels != 254]
    value_segmentation = [(mask == x, ID2LABEL[x]) for x in unique_labels]

    return (image, value), (image, value_segmentation)


HF_TOKEN = os.environ.get("HF_TOKEN", None)

ID2LABEL = {  # id: label
    0: "road",
    1: "dirt",
    2: "gravel",
    3: "rock",
    4: "grass",
    5: "vegetation",
    6: "tree",
    7: "obstacle",
    8: "animals",
    9: "person",
    10: "bicycle",
    11: "vehicle",
    12: "water",
    13: "boat",
    14: "building",
    15: "roof",
    16: "sky",
    17: "drone",
}


input_1 = gr.Image(
    image_mode="RGB",
    type="numpy",
    label="Image (RGB)",
)

input_2 = gr.Image(
    image_mode="L",
    type="numpy",
    label="Segmentation Mask (Greyscale)",
)

INPUTS = [input_1, input_2]

output_1 = gr.AnnotatedImage(
    label="Boundary Mask",
)

output_2 = gr.AnnotatedImage(
    label="Segmentation Mask",
)

OUTPUTS = [output_1, output_2]

TITLE = "Semantic Boundary Generation"

DESCRIPTION = "Semantic Boundary Generation based on [paper](https://arxiv.org/pdf/2304.09427.pdf)."

theme = gr.themes.Monochrome(
    primary_hue="indigo",
    secondary_hue="blue",
    neutral_hue="slate",
    radius_size=gr.themes.sizes.radius_sm,
    font=[
        gr.themes.GoogleFont("Open Sans"),
        "ui-sans-serif",
        "system-ui",
        "sans-serif",
    ],
)

cur_dir = os.path.dirname(os.path.abspath(__file__))
EXAMPLES = [
    [
        f"{cur_dir}/examples/aeroscape_1.jpg",
        f"{cur_dir}/examples/aeroscape_1_mask.png",
    ],
    [
        f"{cur_dir}/examples/aeroscape_2.jpg",
        f"{cur_dir}/examples/aeroscape_2_mask.png",
    ],
    [
        f"{cur_dir}/examples/floodnet_1.jpg",
        f"{cur_dir}/examples/floodnet_1_mask.png",
    ],
    [
        f"{cur_dir}/examples/floodnet_2.jpg",
        f"{cur_dir}/examples/floodnet_2_mask.png",
    ],
    [
        f"{cur_dir}/examples/floodnet_3.jpg",
        f"{cur_dir}/examples/floodnet_3_mask.png",
    ],
    [
        f"{cur_dir}/examples/floodnet_4.jpg",
        f"{cur_dir}/examples/floodnet_4_mask.png",
    ],
    [
        f"{cur_dir}/examples/floodnet_5.jpg",
        f"{cur_dir}/examples/floodnet_5_mask.png",
    ],
    [
        f"{cur_dir}/examples/udd_1.jpg",
        f"{cur_dir}/examples/udd_1_mask.png",
    ],
    [
        f"{cur_dir}/examples/udd_2.jpg",
        f"{cur_dir}/examples/udd_2_mask.png",
    ],
]

demo = gr.Interface(
    fn=gen_sbd,
    inputs=INPUTS,
    outputs=OUTPUTS,
    title=TITLE,
    description=DESCRIPTION,
    live=False,
    theme=theme,
    allow_flagging="never",
    cache_examples=True,
    examples=EXAMPLES,
)

if __name__ == "__main__":
    demo.launch()