tdurbor commited on
Commit
bae2dd5
1 Parent(s): d54a5af

Fix zooming issue

Browse files
Files changed (1) hide show
  1. app.py +59 -64
app.py CHANGED
@@ -179,41 +179,49 @@ def gradio_interface():
179
  inputs=username_input,
180
  outputs=feedback_output
181
  )
182
-
183
- filename, input_image, segmented_a, segmented_b, model_a_name, model_b_name = select_new_image()
184
- state_segmented_a = gr.State(segmented_a)
185
- state_segmented_b = gr.State(segmented_b)
186
- state_model_a_name = gr.State(model_a_name)
187
- state_model_b_name = gr.State(model_b_name)
188
- state_filename = gr.State(filename)
189
-
190
- zoomed_state_a = gr.State(False)
191
- zoomed_state_b = gr.State(False)
192
-
193
- # Compute the absolute difference between the masks
194
- mask_difference = compute_mask_difference(segmented_a, segmented_b)
195
-
196
-
197
- with gr.Row():
198
- image_a_display = gr.Image(
199
  value=segmented_a,
200
  label="Image",
201
  width=image_width,
202
  height=image_height
203
  )
 
204
  input_image_display = gr.AnnotatedImage(
205
  value=(input_image, [(mask_difference > 0, button_name)]),
206
  label="Input Image",
207
  width=image_width,
208
  height=image_height
209
  )
210
- image_b_display = gr.Image(
 
211
  value=segmented_b,
212
  label="Image",
213
  width=image_width,
214
  height=image_height
215
  )
216
- state_tie = gr.State("Tie")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  with gr.Row():
218
  vote_a_button = gr.Button("👈 A is better")
219
  vote_tie_button = gr.Button("🤝 Tie")
@@ -237,80 +245,68 @@ def gradio_interface():
237
  except Exception as e:
238
  logging.error("Error recording vote: %s", str(e))
239
 
240
- new_filename, new_input_image, new_segmented_a, new_segmented_b, new_model_a_name, new_model_b_name = select_new_image()
241
- model_a_name.value = new_model_a_name
242
- model_b_name.value = new_model_b_name
243
- original_filename.value = new_filename
244
- state_segmented_a.value = new_segmented_a
245
- state_segmented_b.value = new_segmented_b
246
-
247
- mask_difference = compute_mask_difference(new_segmented_a, new_segmented_b)
248
-
249
- # Update the notice markdown with the new vote count
250
  new_notice_markdown = get_notice_markdown()
251
 
252
- # Reinitialize zoom states to False
253
- zoomed_state_a.value = False
254
- zoomed_state_b.value = False
255
-
256
- return (
257
- original_filename.value,
258
- (new_input_image, [(mask_difference, button_name)]),
259
- new_segmented_a,
260
- new_segmented_b,
261
- model_a_name.value,
262
- model_b_name.value,
263
- new_notice_markdown,
264
- state_segmented_a.value,
265
- state_segmented_b.value,
266
- zoomed_state_a.value,
267
- zoomed_state_b.value
268
- )
269
 
270
  vote_a_button.click(
271
  fn=lambda username: vote_for_model("model_a", state_filename, state_model_a_name, state_model_b_name, username),
272
- inputs=username_input,
273
  outputs=[
274
- state_filename, input_image_display, image_a_display, image_b_display, state_model_a_name, state_model_b_name, notice_markdown, state_segmented_a, state_segmented_b, zoomed_state_a, zoomed_state_b
 
 
275
  ]
276
  )
277
  vote_b_button.click(
278
  fn=lambda username: vote_for_model("model_b", state_filename, state_model_a_name, state_model_b_name, username),
279
- inputs=username_input,
280
  outputs=[
281
- state_filename, input_image_display, image_a_display, image_b_display, state_model_a_name, state_model_b_name, notice_markdown, state_segmented_a, state_segmented_b, zoomed_state_a, zoomed_state_b
 
 
282
  ]
283
  )
284
  vote_tie_button.click(
285
  fn=lambda username: vote_for_model("tie", state_filename, state_model_a_name, state_model_b_name, username),
286
- inputs=username_input,
287
  outputs=[
288
- state_filename, input_image_display, image_a_display, image_b_display, state_model_a_name, state_model_b_name, notice_markdown, state_segmented_a, state_segmented_b, zoomed_state_a, zoomed_state_b
 
 
289
  ]
290
  )
291
 
292
 
293
- def handle_zoom(image, event: gr.SelectData, zoomed_state, segmented_image):
294
  """Toggle between zoomed and original image based on click events."""
 
 
 
 
 
 
 
295
  if zoomed_state:
296
  return gr.Image(
297
- value=segmented_image,
298
  label="Image",
299
  width=image_width,
300
  height=image_height
301
  ), False
302
 
303
  start_row, start_col = event.index[1], event.index[0]
304
- zoom_size = max(10, min(image.shape[:2]) // 10)
305
 
306
- row_start, row_end = max(start_row - zoom_size, 0), min(start_row + zoom_size, image.shape[0])
307
- col_start, col_end = max(start_col - zoom_size, 0), min(start_col + zoom_size, image.shape[1])
308
 
309
- grey_image = np.mean(image, axis=-1, keepdims=True).astype(image.dtype)
310
- grey_image = np.repeat(grey_image, image.shape[-1], axis=-1)
311
  output_image = grey_image.copy()
312
 
313
- zoomed_area = image[row_start:row_end, col_start:col_end]
314
  upscale_factor = 6
315
  zoomed_area_upscaled = np.kron(zoomed_area, np.ones((upscale_factor, upscale_factor, 1)))
316
 
@@ -333,10 +329,9 @@ def gradio_interface():
333
 
334
  return output_image, True
335
 
336
-
337
- image_a_display.select(handle_zoom, [image_a_display, zoomed_state_a, state_segmented_a], [image_a_display, zoomed_state_a])
338
- image_b_display.select(handle_zoom, [image_b_display, zoomed_state_b, state_segmented_b], [image_b_display, zoomed_state_b])
339
-
340
  with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
341
  rankings_table = gr.Dataframe(
342
  headers=["Model", "Ranking"],
 
179
  inputs=username_input,
180
  outputs=feedback_output
181
  )
182
+
183
+ def refresh_states():
184
+ filename, input_image, segmented_a, segmented_b, model_a_name, model_b_name = select_new_image()
185
+ mask_difference = compute_mask_difference(segmented_a, segmented_b)
186
+ image_a = gr.Image(
 
 
 
 
 
 
 
 
 
 
 
 
187
  value=segmented_a,
188
  label="Image",
189
  width=image_width,
190
  height=image_height
191
  )
192
+
193
  input_image_display = gr.AnnotatedImage(
194
  value=(input_image, [(mask_difference > 0, button_name)]),
195
  label="Input Image",
196
  width=image_width,
197
  height=image_height
198
  )
199
+
200
+ image_b = gr.Image(
201
  value=segmented_b,
202
  label="Image",
203
  width=image_width,
204
  height=image_height
205
  )
206
+
207
+ zoomed_state_a = gr.State(False)
208
+ zoomed_state_b = gr.State(False)
209
+
210
+ state_model_a_name = gr.State(model_a_name)
211
+ state_model_b_name = gr.State(model_b_name)
212
+ state_filename = gr.State(filename)
213
+ state_segmented_a = gr.Image(value=segmented_a, visible=False)
214
+ state_segmented_b = gr.Image(value=segmented_b, visible=False)
215
+
216
+ outputs = [
217
+ state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
218
+ input_image_display, zoomed_state_a, zoomed_state_b, state_segmented_a, state_segmented_b
219
+ ]
220
+ return outputs
221
+
222
+ with gr.Row():
223
+ state_filename, image_a, image_b, state_model_a_name, state_model_b_name, input_image_display, zoomed_state_a, zoomed_state_b, state_segmented_a, state_segmented_b = refresh_states()
224
+
225
  with gr.Row():
226
  vote_a_button = gr.Button("👈 A is better")
227
  vote_tie_button = gr.Button("🤝 Tie")
 
245
  except Exception as e:
246
  logging.error("Error recording vote: %s", str(e))
247
 
248
+ outputs = refresh_states()
 
 
 
 
 
 
 
 
 
249
  new_notice_markdown = get_notice_markdown()
250
 
251
+ return outputs + [new_notice_markdown]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  vote_a_button.click(
254
  fn=lambda username: vote_for_model("model_a", state_filename, state_model_a_name, state_model_b_name, username),
255
+ inputs=[username_input],
256
  outputs=[
257
+ state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
258
+ input_image_display, zoomed_state_a, zoomed_state_b,
259
+ state_segmented_a, state_segmented_b, notice_markdown
260
  ]
261
  )
262
  vote_b_button.click(
263
  fn=lambda username: vote_for_model("model_b", state_filename, state_model_a_name, state_model_b_name, username),
264
+ inputs=[username_input],
265
  outputs=[
266
+ state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
267
+ input_image_display, zoomed_state_a, zoomed_state_b,
268
+ state_segmented_a, state_segmented_b, notice_markdown
269
  ]
270
  )
271
  vote_tie_button.click(
272
  fn=lambda username: vote_for_model("tie", state_filename, state_model_a_name, state_model_b_name, username),
273
+ inputs=[username_input],
274
  outputs=[
275
+ state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
276
+ input_image_display, zoomed_state_a, zoomed_state_b,
277
+ state_segmented_a, state_segmented_b, notice_markdown
278
  ]
279
  )
280
 
281
 
282
+ def handle_zoom(current_image, zoomed_state_input, original_image, event: gr.SelectData):
283
  """Toggle between zoomed and original image based on click events."""
284
+
285
+ # Check if zoomed_state is a gr.State and get its value
286
+ if isinstance(zoomed_state_input, gr.State):
287
+ zoomed_state = zoomed_state_input.value
288
+ else:
289
+ zoomed_state = zoomed_state_input
290
+
291
  if zoomed_state:
292
  return gr.Image(
293
+ value=original_image,
294
  label="Image",
295
  width=image_width,
296
  height=image_height
297
  ), False
298
 
299
  start_row, start_col = event.index[1], event.index[0]
300
+ zoom_size = max(10, min(current_image.shape[:2]) // 10)
301
 
302
+ row_start, row_end = max(start_row - zoom_size, 0), min(start_row + zoom_size, current_image.shape[0])
303
+ col_start, col_end = max(start_col - zoom_size, 0), min(start_col + zoom_size, current_image.shape[1])
304
 
305
+ grey_image = np.mean(current_image, axis=-1, keepdims=True).astype(current_image.dtype)
306
+ grey_image = np.repeat(grey_image, current_image.shape[-1], axis=-1)
307
  output_image = grey_image.copy()
308
 
309
+ zoomed_area = current_image[row_start:row_end, col_start:col_end]
310
  upscale_factor = 6
311
  zoomed_area_upscaled = np.kron(zoomed_area, np.ones((upscale_factor, upscale_factor, 1)))
312
 
 
329
 
330
  return output_image, True
331
 
332
+ image_a.select(handle_zoom, [image_a, zoomed_state_a, state_segmented_a], [image_a, zoomed_state_a])
333
+ image_b.select(handle_zoom, [image_b, zoomed_state_b, state_segmented_b], [image_b, zoomed_state_b])
334
+
 
335
  with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
336
  rankings_table = gr.Dataframe(
337
  headers=["Model", "Ranking"],