github-actions[bot] commited on
Commit
550071e
·
1 Parent(s): 01551ef

Sync with https://github.com/mozilla-ai/osm-ai-helper

Browse files
Files changed (1) hide show
  1. app.py +56 -18
app.py CHANGED
@@ -4,6 +4,8 @@ from shutil import move
4
 
5
  import folium
6
  import streamlit as st
 
 
7
  from loguru import logger
8
  from huggingface_hub import hf_hub_download
9
  from PIL import Image
@@ -24,6 +26,34 @@ class StreamlitHandler:
24
  return
25
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  @st.fragment
28
  def inference(lat_lon, margin):
29
  with st.spinner("Downloading model..."):
@@ -35,7 +65,7 @@ def inference(lat_lon, margin):
35
  )
36
  with st.spinner("Running inference..."):
37
  output_path, existing, new, missed = run_inference(
38
- model_file="models/model.pt",
39
  output_dir="results",
40
  lat_lon=lat_lon,
41
  margin=margin,
@@ -97,31 +127,39 @@ def upload_results(output_path):
97
  )
98
 
99
 
100
- st.title("Open Street Map AI Helper")
101
 
102
  st.divider()
103
 
104
  st.subheader("Click on the map to select a latitude and longitude")
105
 
106
- m = folium.Map(location=[42.2489, -8.5117], zoom_start=11, tiles="OpenStreetMap")
107
 
108
- st_data = st_folium(m, width=725)
109
 
110
- if st_data.get("last_clicked"):
111
- lat = st_data["last_clicked"]["lat"]
112
- lon = st_data["last_clicked"]["lng"]
113
- st.write(f"Last Clicked: {lat}, {lon}")
114
 
115
- if st.button("Run Inference"):
116
- streamlit_handler = StreamlitHandler()
117
- logger.add(streamlit_handler, format="<level>{message}</level>")
 
118
 
119
- output_path, new = inference(lat_lon=(lat, lon), margin=3)
 
 
 
 
 
 
 
 
 
 
 
120
 
121
- if new:
122
- st.divider()
123
- st.header("Review `new` polygons")
124
- for new in Path(output_path).glob("*.json"):
125
- handle_polygon(new)
126
 
127
- upload_results(output_path)
 
 
 
4
 
5
  import folium
6
  import streamlit as st
7
+ from branca.element import MacroElement
8
+ from jinja2 import Template
9
  from loguru import logger
10
  from huggingface_hub import hf_hub_download
11
  from PIL import Image
 
26
  return
27
 
28
 
29
+ @st.fragment
30
+ def show_map():
31
+ class LatLngPopup(MacroElement):
32
+ _template = Template(
33
+ """
34
+ {% macro script(this, kwargs) %}
35
+ var {{this.get_name()}} = L.popup();
36
+ function latLngPop(e) {
37
+ {{this.get_name()}}
38
+ .setLatLng(e.latlng)
39
+ .setContent(e.latlng.lat.toFixed(4) + ", " + e.latlng.lng.toFixed(4))
40
+ .openOn({{this._parent.get_name()}});
41
+ }
42
+ {{this._parent.get_name()}}.on('click', latLngPop);
43
+ {% endmacro %}
44
+ """
45
+ )
46
+
47
+ def __init__(self):
48
+ super().__init__()
49
+ self._name = "LatLngPopup"
50
+
51
+ m = folium.Map(location=[42.2489, -8.5117], zoom_start=11, tiles="OpenStreetMap")
52
+ m.add_child(LatLngPopup())
53
+
54
+ st_folium(m, height=400, width=800)
55
+
56
+
57
  @st.fragment
58
  def inference(lat_lon, margin):
59
  with st.spinner("Downloading model..."):
 
65
  )
66
  with st.spinner("Running inference..."):
67
  output_path, existing, new, missed = run_inference(
68
+ yolo_model_file="models/model.pt",
69
  output_dir="results",
70
  lat_lon=lat_lon,
71
  margin=margin,
 
127
  )
128
 
129
 
130
+ st.title("OpenStreetMap AI Helper")
131
 
132
  st.divider()
133
 
134
  st.subheader("Click on the map to select a latitude and longitude")
135
 
136
+ show_map()
137
 
138
+ lat_lon = st.text_input("Paste the copied (latitude, longitude)")
139
 
140
+ if st.button("Run Inference") and lat_lon:
141
+ logger.add(StreamlitHandler(), format="<level>{message}</level>")
 
 
142
 
143
+ lat, lon = lat_lon.split(",")
144
+ output_path, new = inference(
145
+ lat_lon=(float(lat.strip()), float(lon.strip())), margin=3
146
+ )
147
 
148
+ if new:
149
+ st.divider()
150
+ st.header("Review `new` polygons")
151
+ st.markdown(
152
+ "Every `new` polygon will be displayed at the center of the image in `yellow`."
153
+ )
154
+ st.markdown(
155
+ "Polygons in other colors are those already existing in OpenStreetMap and they just "
156
+ "indicate whether the model has found them (`green`) or missed them (`red`)."
157
+ )
158
+ for new in Path(output_path).glob("*.json"):
159
+ handle_polygon(new)
160
 
161
+ logger.add(StreamlitHandler(), format="<level>{message}</level>")
 
 
 
 
162
 
163
+ upload_results(output_path)
164
+ else:
165
+ st.warning("No `new` polygons were found. Try a different location.")