robocan commited on
Commit
4345f8a
1 Parent(s): a8f08c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -85,7 +85,7 @@ def create_map_figure(predictions, cell_ids, selected_index=None):
85
 
86
  # Assign colors based on rank
87
  colors = ['rgba(0, 255, 0, 0.2)'] * 3 + ['rgba(255, 255, 0, 0.2)'] * 7
88
- zoom_level = 1
89
  center_lat = None
90
  center_lon = None
91
 
@@ -94,7 +94,8 @@ def create_map_figure(predictions, cell_ids, selected_index=None):
94
  polygon = get_s2_cell_polygon(cell_id)
95
  lats, lons = zip(*polygon)
96
  color = colors[rank]
97
-
 
98
  fig.add_trace(go.Scattermapbox(
99
  lat=lats,
100
  lon=lons,
@@ -102,15 +103,16 @@ def create_map_figure(predictions, cell_ids, selected_index=None):
102
  fill='toself',
103
  fillcolor=color,
104
  line=dict(color='blue'),
105
- name=f'Prediction {rank + 1}', # Updated label
106
  ))
107
 
108
- # Set zoom based on the selected index
109
  if selected_index is not None and rank == selected_index:
110
- zoom_level = 10 # Adjust zoom level
111
  center_lat = np.mean(lats)
112
  center_lon = np.mean(lons)
113
 
 
114
  fig.update_layout(
115
  mapbox_style="open-street-map",
116
  hovermode='closest',
@@ -121,7 +123,7 @@ def create_map_figure(predictions, cell_ids, selected_index=None):
121
  lon=center_lon if center_lon else np.mean(lons)
122
  ),
123
  pitch=0,
124
- zoom=zoom_level # Zoom in if an index is selected
125
  ),
126
  )
127
 
@@ -134,10 +136,17 @@ def create_label_output(predictions):
134
  fig = create_map_figure(results, cell_ids)
135
  return fig
136
 
137
- # Update the predict_and_plot function to handle zoom on selection
138
  def predict_and_plot(input_img, selected_prediction):
139
  predictions = predict(input_img)
140
- return create_map_figure(predictions, predictions[1], selected_index=selected_prediction)
 
 
 
 
 
 
 
 
141
 
142
 
143
 
@@ -145,7 +154,7 @@ def predict_and_plot(input_img, selected_prediction):
145
  with gr.Blocks() as gradio_app:
146
  with gr.Column():
147
  input_image = gr.Image(label="Upload an Image", type="pil")
148
- selected_prediction = gr.Dropdown(choices=[f"Prediction {i+1}" for i in range(10)], label="Select Prediction to Zoom")
149
  output_map = gr.Plot(label="Predicted Location on Map")
150
  btn_predict = gr.Button("Predict")
151
 
 
85
 
86
  # Assign colors based on rank
87
  colors = ['rgba(0, 255, 0, 0.2)'] * 3 + ['rgba(255, 255, 0, 0.2)'] * 7
88
+ zoom_level = 1 # Default zoom level
89
  center_lat = None
90
  center_lon = None
91
 
 
94
  polygon = get_s2_cell_polygon(cell_id)
95
  lats, lons = zip(*polygon)
96
  color = colors[rank]
97
+
98
+ # Draw S2 cell polygon
99
  fig.add_trace(go.Scattermapbox(
100
  lat=lats,
101
  lon=lons,
 
103
  fill='toself',
104
  fillcolor=color,
105
  line=dict(color='blue'),
106
+ name=f'Prediction {rank + 1}',
107
  ))
108
 
109
+ # Adjust zoom level if selected prediction is found
110
  if selected_index is not None and rank == selected_index:
111
+ zoom_level = 10 # Adjust the zoom level to your liking
112
  center_lat = np.mean(lats)
113
  center_lon = np.mean(lons)
114
 
115
+ # Update map layout
116
  fig.update_layout(
117
  mapbox_style="open-street-map",
118
  hovermode='closest',
 
123
  lon=center_lon if center_lon else np.mean(lons)
124
  ),
125
  pitch=0,
126
+ zoom=zoom_level # Zoom in based on selection
127
  ),
128
  )
129
 
 
136
  fig = create_map_figure(results, cell_ids)
137
  return fig
138
 
 
139
  def predict_and_plot(input_img, selected_prediction):
140
  predictions = predict(input_img)
141
+
142
+ # Convert dropdown selection into an index (Prediction 1 corresponds to index 0, etc.)
143
+ if selected_prediction is not None:
144
+ selected_index = int(selected_prediction.split()[-1]) - 1 # Extract index from "Prediction X"
145
+ else:
146
+ selected_index = None # No selection, default view
147
+
148
+ return create_map_figure(predictions, predictions[1], selected_index=selected_index)
149
+
150
 
151
 
152
 
 
154
  with gr.Blocks() as gradio_app:
155
  with gr.Column():
156
  input_image = gr.Image(label="Upload an Image", type="pil")
157
+ selected_prediction = gr.Dropdown(choices=[f"Prediction {i+1}" for i in range(10)], label="Select Prediction to Zoom", value=None)
158
  output_map = gr.Plot(label="Predicted Location on Map")
159
  btn_predict = gr.Button("Predict")
160