Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,144 +1,145 @@
|
|
1 |
-
import itertools
|
2 |
-
from datetime import datetime
|
3 |
-
from pathlib import Path
|
4 |
-
|
5 |
-
import folium
|
6 |
-
import streamlit as st
|
7 |
-
from loguru import logger as log
|
8 |
-
from matplotlib import colors
|
9 |
-
from streamlit_folium import st_folium
|
10 |
-
|
11 |
-
from utils.folium import (
|
12 |
-
draw_gdf,
|
13 |
-
get_clean_rendering_container,
|
14 |
-
get_map_crop,
|
15 |
-
get_parquet,
|
16 |
-
)
|
17 |
-
|
18 |
-
COLOR_MAP = {
|
19 |
-
"Grassland": "lime",
|
20 |
-
"Cereals": "saddlebrown",
|
21 |
-
"Other": "white",
|
22 |
-
"Flowers": "cyan",
|
23 |
-
"Vegetables": "purple",
|
24 |
-
"Fruit trees": "darkgreen",
|
25 |
-
}
|
26 |
-
|
27 |
-
|
28 |
-
id_class = {(i + 1): k for i, k in enumerate(COLOR_MAP.keys())}
|
29 |
-
color_map_hex = {k: colors.to_hex(v) for k, v in COLOR_MAP.items()}
|
30 |
-
id_map_hex = {(i + 1): v for i, v in enumerate(color_map_hex.values())}
|
31 |
-
color_map_hex_r = {v: k for k, v in color_map_hex.items()}
|
32 |
-
merged_list = list(itertools.chain(*list(color_map_hex_r.items())))
|
33 |
-
|
34 |
-
|
35 |
-
def style_call(feat):
|
36 |
-
di = {
|
37 |
-
"fillColor": id_map_hex[feat["properties"]["class"]],
|
38 |
-
"fillOpacity": 0.7,
|
39 |
-
"color": id_map_hex[feat["properties"]["class"]],
|
40 |
-
"border-width": "thin",
|
41 |
-
"border-color": "#ffffff",
|
42 |
-
"weight": 1.2,
|
43 |
-
}
|
44 |
-
return di
|
45 |
-
|
46 |
-
|
47 |
-
# Page configs
|
48 |
-
st.set_page_config(page_title="AIDA", page_icon="🌐", layout="wide")
|
49 |
-
|
50 |
-
# base paths
|
51 |
-
base_path = Path("data")
|
52 |
-
inference_path = base_path / "inference"
|
53 |
-
|
54 |
-
# list examples and predictions
|
55 |
-
inference_paths = sorted(list(inference_path.iterdir()))
|
56 |
-
|
57 |
-
example_names = [p.stem for p in inference_paths]
|
58 |
-
inference_dict = {p.stem: p for p in inference_paths}
|
59 |
-
|
60 |
-
|
61 |
-
def change_key():
|
62 |
-
st.session_state["key_map"] = str(datetime.now())
|
63 |
-
|
64 |
-
|
65 |
-
# Create selection menu
|
66 |
-
container_predictions = st.container(border=True)
|
67 |
-
with container_predictions:
|
68 |
-
col1, col2, col3 = st.columns([0.2, 0.1, 0.7])
|
69 |
-
with col1:
|
70 |
-
prediction_selectbox = st.selectbox(
|
71 |
-
"Select an example",
|
72 |
-
options=example_names,
|
73 |
-
index=None,
|
74 |
-
key="selectbox_pred",
|
75 |
-
)
|
76 |
-
is_prediction_selected = prediction_selectbox is not None
|
77 |
-
if is_prediction_selected:
|
78 |
-
try:
|
79 |
-
# add loading of the selected parquet prediction
|
80 |
-
chosen_tile_path = inference_dict[prediction_selectbox]
|
81 |
-
with open(chosen_tile_path, "rb") as f:
|
82 |
-
prediction_file = f.read()
|
83 |
-
except:
|
84 |
-
st.warning("File not found")
|
85 |
-
chosen_tile_path = None
|
86 |
-
prediction_file = None
|
87 |
-
else:
|
88 |
-
prediction_file = None
|
89 |
-
chosen_tile_path = None
|
90 |
-
with col2:
|
91 |
-
height_value = st.radio(
|
92 |
-
"Map height",
|
93 |
-
options=[800, 500],
|
94 |
-
horizontal=True,
|
95 |
-
key="height_map",
|
96 |
-
on_change=change_key,
|
97 |
-
)
|
98 |
-
with col3:
|
99 |
-
with st.container():
|
100 |
-
st.write("######")
|
101 |
-
with st.expander("See information about the methodology"):
|
102 |
-
st.write(
|
103 |
-
"""The model uses a modified UTAE architecture, a U-Net variant with attention,
|
104 |
-
for segmenting crop types from satellite imagery.
|
105 |
-
It was trained using Sentinel-2 time series data from several European countries,
|
106 |
-
labeled with aggregated Eurocrops data to identify 6 broad crop classes.
|
107 |
-
More informations about the methodology [HERE](https://huggingface.co/links-ads/aida-cropland-models)
|
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 |
-
|
|
|
|
1 |
+
import itertools
|
2 |
+
from datetime import datetime
|
3 |
+
from pathlib import Path
|
4 |
+
|
5 |
+
import folium
|
6 |
+
import streamlit as st
|
7 |
+
from loguru import logger as log
|
8 |
+
from matplotlib import colors
|
9 |
+
from streamlit_folium import st_folium
|
10 |
+
|
11 |
+
from utils.folium import (
|
12 |
+
draw_gdf,
|
13 |
+
get_clean_rendering_container,
|
14 |
+
get_map_crop,
|
15 |
+
get_parquet,
|
16 |
+
)
|
17 |
+
|
18 |
+
COLOR_MAP = {
|
19 |
+
"Grassland": "lime",
|
20 |
+
"Cereals": "saddlebrown",
|
21 |
+
"Other": "white",
|
22 |
+
"Flowers": "cyan",
|
23 |
+
"Vegetables": "purple",
|
24 |
+
"Fruit trees": "darkgreen",
|
25 |
+
}
|
26 |
+
|
27 |
+
|
28 |
+
id_class = {(i + 1): k for i, k in enumerate(COLOR_MAP.keys())}
|
29 |
+
color_map_hex = {k: colors.to_hex(v) for k, v in COLOR_MAP.items()}
|
30 |
+
id_map_hex = {(i + 1): v for i, v in enumerate(color_map_hex.values())}
|
31 |
+
color_map_hex_r = {v: k for k, v in color_map_hex.items()}
|
32 |
+
merged_list = list(itertools.chain(*list(color_map_hex_r.items())))
|
33 |
+
|
34 |
+
|
35 |
+
def style_call(feat):
|
36 |
+
di = {
|
37 |
+
"fillColor": id_map_hex[feat["properties"]["class"]],
|
38 |
+
"fillOpacity": 0.7,
|
39 |
+
"color": id_map_hex[feat["properties"]["class"]],
|
40 |
+
"border-width": "thin",
|
41 |
+
"border-color": "#ffffff",
|
42 |
+
"weight": 1.2,
|
43 |
+
}
|
44 |
+
return di
|
45 |
+
|
46 |
+
|
47 |
+
# Page configs
|
48 |
+
st.set_page_config(page_title="AIDA", page_icon="🌐", layout="wide")
|
49 |
+
|
50 |
+
# base paths
|
51 |
+
base_path = Path("data")
|
52 |
+
inference_path = base_path / "inference"
|
53 |
+
|
54 |
+
# list examples and predictions
|
55 |
+
inference_paths = sorted(list(inference_path.iterdir()))
|
56 |
+
|
57 |
+
example_names = [p.stem for p in inference_paths]
|
58 |
+
inference_dict = {p.stem: p for p in inference_paths}
|
59 |
+
|
60 |
+
|
61 |
+
def change_key():
|
62 |
+
st.session_state["key_map"] = str(datetime.now())
|
63 |
+
|
64 |
+
|
65 |
+
# Create selection menu
|
66 |
+
container_predictions = st.container(border=True)
|
67 |
+
with container_predictions:
|
68 |
+
col1, col2, col3 = st.columns([0.2, 0.1, 0.7])
|
69 |
+
with col1:
|
70 |
+
prediction_selectbox = st.selectbox(
|
71 |
+
"Select an example",
|
72 |
+
options=example_names,
|
73 |
+
index=None,
|
74 |
+
key="selectbox_pred",
|
75 |
+
)
|
76 |
+
is_prediction_selected = prediction_selectbox is not None
|
77 |
+
if is_prediction_selected:
|
78 |
+
try:
|
79 |
+
# add loading of the selected parquet prediction
|
80 |
+
chosen_tile_path = inference_dict[prediction_selectbox]
|
81 |
+
with open(chosen_tile_path, "rb") as f:
|
82 |
+
prediction_file = f.read()
|
83 |
+
except:
|
84 |
+
st.warning("File not found")
|
85 |
+
chosen_tile_path = None
|
86 |
+
prediction_file = None
|
87 |
+
else:
|
88 |
+
prediction_file = None
|
89 |
+
chosen_tile_path = None
|
90 |
+
with col2:
|
91 |
+
height_value = st.radio(
|
92 |
+
"Map height",
|
93 |
+
options=[800, 500],
|
94 |
+
horizontal=True,
|
95 |
+
key="height_map",
|
96 |
+
on_change=change_key,
|
97 |
+
)
|
98 |
+
with col3:
|
99 |
+
with st.container():
|
100 |
+
st.write("######")
|
101 |
+
with st.expander("See information about the methodology"):
|
102 |
+
st.write(
|
103 |
+
"""The model uses a modified UTAE architecture, a U-Net variant with attention,
|
104 |
+
for segmenting crop types from satellite imagery.
|
105 |
+
It was trained using Sentinel-2 time series data from several European countries,
|
106 |
+
labeled with aggregated Eurocrops data to identify 6 broad crop classes.
|
107 |
+
More informations about the methodology [HERE](https://huggingface.co/links-ads/aida-cropland-models).
|
108 |
+
Know more how this model was used in practice [HERE](https://business.esa.int/projects/aida)"""
|
109 |
+
)
|
110 |
+
|
111 |
+
container = get_clean_rendering_container(prediction_selectbox)
|
112 |
+
|
113 |
+
|
114 |
+
# Stange Hack to always update the map height I guess
|
115 |
+
|
116 |
+
|
117 |
+
container = get_clean_rendering_container(height_value)
|
118 |
+
|
119 |
+
# draw map
|
120 |
+
interactive_map = get_map_crop()
|
121 |
+
|
122 |
+
if prediction_selectbox is not None:
|
123 |
+
# draw prediction
|
124 |
+
draw_gdf(interactive_map, chosen_tile_path, "Prediction", style_call, id_class)
|
125 |
+
|
126 |
+
|
127 |
+
with container.form(key="form1", clear_on_submit=True):
|
128 |
+
|
129 |
+
folium.LayerControl().add_to(interactive_map)
|
130 |
+
output_map = st_folium(
|
131 |
+
interactive_map,
|
132 |
+
width=None,
|
133 |
+
height=height_value,
|
134 |
+
returned_objects=["all_drawings"],
|
135 |
+
key=st.session_state.get("key_map", "key_map"),
|
136 |
+
)
|
137 |
+
submit = st.form_submit_button("Recenter map")
|
138 |
+
|
139 |
+
# Update messages
|
140 |
+
|
141 |
+
|
142 |
+
for name, path in inference_dict.items():
|
143 |
+
if path not in st.session_state.keys():
|
144 |
+
log.info(f"Loading parquet {name}")
|
145 |
+
st.session_state[path] = get_parquet(path, id_class)
|