lichih commited on
Commit
4e9b2e5
·
verified ·
1 Parent(s): a90480c

no share example image

Browse files
Files changed (1) hide show
  1. app.py +218 -215
app.py CHANGED
@@ -1,215 +1,218 @@
1
- import gradio as gr
2
- from core import Ladeco
3
- from matplotlib.figure import Figure
4
- import matplotlib.pyplot as plt
5
- import matplotlib as mpl
6
- import spaces
7
- from PIL import Image
8
-
9
-
10
- plt.rcParams['figure.facecolor'] = '#0b0f19'
11
- plt.rcParams['text.color'] = '#aab6cc'
12
- ladeco = Ladeco()
13
-
14
-
15
- @spaces.GPU
16
- def infer(img: str) -> tuple[Figure, Figure]:
17
- out = ladeco.predict(img)
18
-
19
- seg = out.visualize(level=2)[0].image
20
- colormap = out.color_map(level=2)
21
-
22
- area = out.area()[0]
23
-
24
- # match the color of segmentation image and pie chart
25
- colors = []
26
- l2_area = {}
27
- for labelname, area_ratio in area.items():
28
- if labelname.startswith("l2") and area_ratio > 0:
29
- colors.append(colormap[labelname])
30
- labelname = labelname.replace("l2_", "").capitalize()
31
- l2_area[labelname] = area_ratio
32
-
33
- pie = plot_pie(l2_area, colors=colors)
34
-
35
- seg.set_dpi(96)
36
- seg.set_size_inches(256/96, 256/96) # 256px x 256px
37
-
38
- pie.set_dpi(96)
39
- pie.set_size_inches(256/96, 256/96) # 256px x 256px
40
-
41
- return seg, pie
42
-
43
-
44
- def plot_pie(data: dict[str, float], colors=None) -> Figure:
45
- fig, ax = plt.subplots()
46
-
47
- labels = list(data.keys())
48
- sizes = list(data.values())
49
-
50
- *_, autotexts = ax.pie(sizes, labels=labels, autopct="%1.1f%%", colors=colors)
51
-
52
- for percent_text in autotexts:
53
- percent_text.set_color("k")
54
-
55
- ax.axis("equal")
56
-
57
- return fig
58
-
59
-
60
- def choose_example(imgpath: str) -> gr.Image:
61
- img = Image.open(imgpath)
62
- width, height = img.size
63
- ratio = 512 / max(width, height)
64
- img = img.resize((int(width * ratio), int(height * ratio)))
65
- return gr.Image(value=img, label="輸入影像(不支援 SVG 格式)", type="filepath")
66
-
67
-
68
- css = """
69
- .reference {
70
- text-align: center;
71
- font-size: 1.2em;
72
- color: #d1d5db;
73
- margin-bottom: 20px;
74
- }
75
- .reference a {
76
- color: #FB923C;
77
- text-decoration: none;
78
- }
79
- .reference a:hover {
80
- text-decoration: underline;
81
- color: #FB923C;
82
- }
83
- .description {
84
- text-align: center;
85
- font-size: 1.1em;
86
- color: #d1d5db;
87
- margin-bottom: 25px;
88
- }
89
- .footer {
90
- text-align: center;
91
- margin-top: 30px;
92
- padding-top: 20px;
93
- border-top: 1px solid #ddd;
94
- color: #d1d5db;
95
- font-size: 14px;
96
- }
97
- .main-title {
98
- font-size: 24px;
99
- font-weight: bold;
100
- text-align: center;
101
- margin-bottom: 20px;
102
- }
103
- .selected-image {
104
- height: 756px;
105
- }
106
- .example-image {
107
- height: 220px;
108
- padding: 25px;
109
- }
110
- """.strip()
111
- theme = gr.themes.Base(
112
- primary_hue="orange",
113
- secondary_hue="cyan",
114
- neutral_hue="gray",
115
- ).set(
116
- body_text_color='*neutral_100',
117
- body_text_color_subdued='*neutral_600',
118
- background_fill_primary='*neutral_950',
119
- background_fill_secondary='*neutral_600',
120
- border_color_accent='*secondary_800',
121
- color_accent='*primary_50',
122
- color_accent_soft='*secondary_800',
123
- code_background_fill='*neutral_700',
124
- block_background_fill_dark='*body_background_fill',
125
- block_info_text_color='#6b7280',
126
- block_label_text_color='*neutral_300',
127
- block_label_text_weight='700',
128
- block_title_text_color='*block_label_text_color',
129
- block_title_text_weight='300',
130
- panel_background_fill='*neutral_800',
131
- table_text_color_dark='*secondary_800',
132
- checkbox_background_color_selected='*primary_500',
133
- checkbox_label_background_fill='*neutral_500',
134
- checkbox_label_background_fill_hover='*neutral_700',
135
- checkbox_label_text_color='*neutral_200',
136
- input_background_fill='*neutral_700',
137
- input_background_fill_focus='*neutral_600',
138
- slider_color='*primary_500',
139
- table_even_background_fill='*neutral_700',
140
- table_odd_background_fill='*neutral_600',
141
- table_row_focus='*neutral_800'
142
- )
143
- with gr.Blocks(css=css, theme=theme) as demo:
144
- gr.HTML(
145
- """
146
- <div class="main-title">LaDeco 景觀環境影像語意分析模型</div>
147
- <div class="reference">
148
- 引用資料:
149
- <a href="https://www.sciencedirect.com/science/article/pii/S1574954123003187" target="_blank">
150
- Li-Chih Ho (2023), LaDeco: A Tool to Analyze Visual Landscape Elements, Ecological Informatics, vol. 78.
151
- </a>
152
- </div>
153
- """.strip()
154
- )
155
- with gr.Row(equal_height=True):
156
- with gr.Group():
157
- img = gr.Image(
158
- label="輸入影像(不支援 SVG 格式)",
159
- type="filepath",
160
- height="256px",
161
- #elem_classes="selected-image",
162
- )
163
- gr.Label("範例影像", show_label=False)
164
- with gr.Row():
165
- ex1 = gr.Image(
166
- value="examples/beach.jpg",
167
- show_label=False,
168
- type="filepath",
169
- elem_classes="example-image",
170
- interactive=False,
171
- show_download_button=False,
172
- show_fullscreen_button=False,
173
- )
174
- ex2 = gr.Image(
175
- value="examples/field.jpg",
176
- show_label=False,
177
- type="filepath",
178
- elem_classes="example-image",
179
- interactive=False,
180
- show_download_button=False,
181
- show_fullscreen_button=False,
182
- )
183
- ex3 = gr.Image(
184
- value="examples/sky.jpg",
185
- show_label=False,
186
- type="filepath",
187
- elem_classes="example-image",
188
- interactive=False,
189
- show_download_button=False,
190
- show_fullscreen_button=False,
191
- )
192
-
193
- with gr.Column():
194
- seg = gr.Plot(label="語意分割")
195
- pie = gr.Plot(label="元素面積比例")
196
-
197
- start = gr.Button("開始", variant="primary")
198
-
199
- gr.HTML(
200
- """
201
- <div class="footer">
202
- © 2024 LaDeco 版權所有<br>
203
- 開發者:何立智、楊哲睿
204
- </div>
205
- """.strip()
206
- )
207
-
208
- start.click(fn=infer, inputs=img, outputs=[seg, pie])
209
-
210
- ex1.select(fn=choose_example, inputs=ex1, outputs=img)
211
- ex2.select(fn=choose_example, inputs=ex2, outputs=img)
212
- ex3.select(fn=choose_example, inputs=ex3, outputs=img)
213
-
214
- if __name__ == "__main__":
215
- demo.launch()
 
 
 
 
1
+ import gradio as gr
2
+ from core import Ladeco
3
+ from matplotlib.figure import Figure
4
+ import matplotlib.pyplot as plt
5
+ import matplotlib as mpl
6
+ import spaces
7
+ from PIL import Image
8
+
9
+
10
+ plt.rcParams['figure.facecolor'] = '#0b0f19'
11
+ plt.rcParams['text.color'] = '#aab6cc'
12
+ ladeco = Ladeco()
13
+
14
+
15
+ @spaces.GPU
16
+ def infer(img: str) -> tuple[Figure, Figure]:
17
+ out = ladeco.predict(img)
18
+
19
+ seg = out.visualize(level=2)[0].image
20
+ colormap = out.color_map(level=2)
21
+
22
+ area = out.area()[0]
23
+
24
+ # match the color of segmentation image and pie chart
25
+ colors = []
26
+ l2_area = {}
27
+ for labelname, area_ratio in area.items():
28
+ if labelname.startswith("l2") and area_ratio > 0:
29
+ colors.append(colormap[labelname])
30
+ labelname = labelname.replace("l2_", "").capitalize()
31
+ l2_area[labelname] = area_ratio
32
+
33
+ pie = plot_pie(l2_area, colors=colors)
34
+
35
+ seg.set_dpi(96)
36
+ seg.set_size_inches(256/96, 256/96) # 256px x 256px
37
+
38
+ pie.set_dpi(96)
39
+ pie.set_size_inches(256/96, 256/96) # 256px x 256px
40
+
41
+ return seg, pie
42
+
43
+
44
+ def plot_pie(data: dict[str, float], colors=None) -> Figure:
45
+ fig, ax = plt.subplots()
46
+
47
+ labels = list(data.keys())
48
+ sizes = list(data.values())
49
+
50
+ *_, autotexts = ax.pie(sizes, labels=labels, autopct="%1.1f%%", colors=colors)
51
+
52
+ for percent_text in autotexts:
53
+ percent_text.set_color("k")
54
+
55
+ ax.axis("equal")
56
+
57
+ return fig
58
+
59
+
60
+ def choose_example(imgpath: str) -> gr.Image:
61
+ img = Image.open(imgpath)
62
+ width, height = img.size
63
+ ratio = 512 / max(width, height)
64
+ img = img.resize((int(width * ratio), int(height * ratio)))
65
+ return gr.Image(value=img, label="輸入影像(不支援 SVG 格式)", type="filepath")
66
+
67
+
68
+ css = """
69
+ .reference {
70
+ text-align: center;
71
+ font-size: 1.2em;
72
+ color: #d1d5db;
73
+ margin-bottom: 20px;
74
+ }
75
+ .reference a {
76
+ color: #FB923C;
77
+ text-decoration: none;
78
+ }
79
+ .reference a:hover {
80
+ text-decoration: underline;
81
+ color: #FB923C;
82
+ }
83
+ .description {
84
+ text-align: center;
85
+ font-size: 1.1em;
86
+ color: #d1d5db;
87
+ margin-bottom: 25px;
88
+ }
89
+ .footer {
90
+ text-align: center;
91
+ margin-top: 30px;
92
+ padding-top: 20px;
93
+ border-top: 1px solid #ddd;
94
+ color: #d1d5db;
95
+ font-size: 14px;
96
+ }
97
+ .main-title {
98
+ font-size: 24px;
99
+ font-weight: bold;
100
+ text-align: center;
101
+ margin-bottom: 20px;
102
+ }
103
+ .selected-image {
104
+ height: 756px;
105
+ }
106
+ .example-image {
107
+ height: 220px;
108
+ padding: 25px;
109
+ }
110
+ """.strip()
111
+ theme = gr.themes.Base(
112
+ primary_hue="orange",
113
+ secondary_hue="cyan",
114
+ neutral_hue="gray",
115
+ ).set(
116
+ body_text_color='*neutral_100',
117
+ body_text_color_subdued='*neutral_600',
118
+ background_fill_primary='*neutral_950',
119
+ background_fill_secondary='*neutral_600',
120
+ border_color_accent='*secondary_800',
121
+ color_accent='*primary_50',
122
+ color_accent_soft='*secondary_800',
123
+ code_background_fill='*neutral_700',
124
+ block_background_fill_dark='*body_background_fill',
125
+ block_info_text_color='#6b7280',
126
+ block_label_text_color='*neutral_300',
127
+ block_label_text_weight='700',
128
+ block_title_text_color='*block_label_text_color',
129
+ block_title_text_weight='300',
130
+ panel_background_fill='*neutral_800',
131
+ table_text_color_dark='*secondary_800',
132
+ checkbox_background_color_selected='*primary_500',
133
+ checkbox_label_background_fill='*neutral_500',
134
+ checkbox_label_background_fill_hover='*neutral_700',
135
+ checkbox_label_text_color='*neutral_200',
136
+ input_background_fill='*neutral_700',
137
+ input_background_fill_focus='*neutral_600',
138
+ slider_color='*primary_500',
139
+ table_even_background_fill='*neutral_700',
140
+ table_odd_background_fill='*neutral_600',
141
+ table_row_focus='*neutral_800'
142
+ )
143
+ with gr.Blocks(css=css, theme=theme) as demo:
144
+ gr.HTML(
145
+ """
146
+ <div class="main-title">LaDeco 景觀環境影像語意分析模型</div>
147
+ <div class="reference">
148
+ 引用資料:
149
+ <a href="https://www.sciencedirect.com/science/article/pii/S1574954123003187" target="_blank">
150
+ Li-Chih Ho (2023), LaDeco: A Tool to Analyze Visual Landscape Elements, Ecological Informatics, vol. 78.
151
+ </a>
152
+ </div>
153
+ """.strip()
154
+ )
155
+ with gr.Row(equal_height=True):
156
+ with gr.Group():
157
+ img = gr.Image(
158
+ label="輸入影像(不支援 SVG 格式)",
159
+ type="filepath",
160
+ height="256px",
161
+ #elem_classes="selected-image",
162
+ )
163
+ gr.Label("範例影像", show_label=False)
164
+ with gr.Row():
165
+ ex1 = gr.Image(
166
+ value="examples/beach.jpg",
167
+ show_label=False,
168
+ type="filepath",
169
+ elem_classes="example-image",
170
+ interactive=False,
171
+ show_download_button=False,
172
+ show_fullscreen_button=False,
173
+ show_share_button=False,
174
+ )
175
+ ex2 = gr.Image(
176
+ value="examples/field.jpg",
177
+ show_label=False,
178
+ type="filepath",
179
+ elem_classes="example-image",
180
+ interactive=False,
181
+ show_download_button=False,
182
+ show_fullscreen_button=False,
183
+ show_share_button=False,
184
+ )
185
+ ex3 = gr.Image(
186
+ value="examples/sky.jpg",
187
+ show_label=False,
188
+ type="filepath",
189
+ elem_classes="example-image",
190
+ interactive=False,
191
+ show_download_button=False,
192
+ show_fullscreen_button=False,
193
+ show_share_button=False,
194
+ )
195
+
196
+ with gr.Column():
197
+ seg = gr.Plot(label="語意分割")
198
+ pie = gr.Plot(label="元素面積比例")
199
+
200
+ start = gr.Button("開始", variant="primary")
201
+
202
+ gr.HTML(
203
+ """
204
+ <div class="footer">
205
+ © 2024 LaDeco 版權所有<br>
206
+ 開發者:何立智、楊哲睿
207
+ </div>
208
+ """.strip()
209
+ )
210
+
211
+ start.click(fn=infer, inputs=img, outputs=[seg, pie])
212
+
213
+ ex1.select(fn=choose_example, inputs=ex1, outputs=img)
214
+ ex2.select(fn=choose_example, inputs=ex2, outputs=img)
215
+ ex3.select(fn=choose_example, inputs=ex3, outputs=img)
216
+
217
+ if __name__ == "__main__":
218
+ demo.launch()