tdurbor commited on
Commit
078c9a8
1 Parent(s): 60d2a4b

Remove python zoom and fill width

Browse files
Files changed (1) hide show
  1. app.py +10 -79
app.py CHANGED
@@ -194,14 +194,13 @@ function load_zoom() {
194
 
195
  def gradio_interface():
196
  """Create and return the Gradio interface."""
197
- with gr.Blocks(js=js) as demo:
198
  gr.Markdown("# Background Removal Arena")
199
  button_name = "Difference between masks"
200
 
201
  with gr.Tabs() as tabs:
202
  with gr.Tab("⚔️ Arena (battle)", id=0):
203
- image_width = 800
204
- image_height = 800
205
  notice_markdown = gr.Markdown(get_notice_markdown(), elem_id="notice_markdown")
206
  with gr.Row(equal_height=True):
207
  def on_enter_contest(username):
@@ -237,41 +236,33 @@ def gradio_interface():
237
  image_a = gr.Image(
238
  value=segmented_a,
239
  label="Image",
240
- width=image_width,
241
- height=image_height
242
  )
243
 
244
  input_image_display = gr.AnnotatedImage(
245
  value=(input_image, [(mask_difference > 0, button_name)]),
246
  label="Input Image",
247
- width=image_width,
248
- height=image_height
249
  )
250
 
251
  image_b = gr.Image(
252
  value=segmented_b,
253
  label="Image",
254
- width=image_width,
255
- height=image_height
256
  )
257
-
258
- zoomed_state_a = gr.State(False)
259
- zoomed_state_b = gr.State(False)
260
 
261
  state_model_a_name = gr.State(model_a_name)
262
  state_model_b_name = gr.State(model_b_name)
263
  state_filename = gr.State(filename)
264
- state_segmented_a = gr.Image(value=segmented_a, visible=False)
265
- state_segmented_b = gr.Image(value=segmented_b, visible=False)
266
 
267
  outputs = [
268
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
269
- input_image_display, zoomed_state_a, zoomed_state_b, state_segmented_a, state_segmented_b
270
  ]
271
  return outputs
272
 
273
  with gr.Row():
274
- 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()
275
 
276
  with gr.Row():
277
  vote_a_button = gr.Button("👈 A is better")
@@ -306,8 +297,7 @@ def gradio_interface():
306
  inputs=[username_input],
307
  outputs=[
308
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
309
- input_image_display, zoomed_state_a, zoomed_state_b,
310
- state_segmented_a, state_segmented_b, notice_markdown
311
  ]
312
  )
313
  vote_b_button.click(
@@ -315,8 +305,7 @@ def gradio_interface():
315
  inputs=[username_input],
316
  outputs=[
317
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
318
- input_image_display, zoomed_state_a, zoomed_state_b,
319
- state_segmented_a, state_segmented_b, notice_markdown
320
  ]
321
  )
322
  vote_tie_button.click(
@@ -324,69 +313,11 @@ def gradio_interface():
324
  inputs=[username_input],
325
  outputs=[
326
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
327
- input_image_display, zoomed_state_a, zoomed_state_b,
328
- state_segmented_a, state_segmented_b, notice_markdown
329
  ]
330
  )
331
 
332
 
333
- def handle_zoom(current_image, zoomed_state_input, original_image, other_image, event: gr.SelectData):
334
- """Toggle between zoomed and original image based on click events."""
335
-
336
- # Determine the current zoom state
337
- zoomed_state = zoomed_state_input.value if isinstance(zoomed_state_input, gr.State) else zoomed_state_input
338
-
339
- if zoomed_state:
340
- return (
341
- gr.Image(value=original_image, label="Image", width=image_width, height=image_height),
342
- False,
343
- gr.Image(value=other_image, label="Image", width=image_width, height=image_height),
344
- False
345
- )
346
-
347
- start_row, start_col = event.index[1], event.index[0]
348
- zoom_size = max(10, min(current_image.shape[:2]) // 10)
349
- row_start, row_end = max(start_row - zoom_size, 0), min(start_row + zoom_size, current_image.shape[0])
350
- col_start, col_end = max(start_col - zoom_size, 0), min(start_col + zoom_size, current_image.shape[1])
351
-
352
- grey_image = np.mean(current_image, axis=-1, keepdims=True).astype(current_image.dtype)
353
- grey_image = np.repeat(grey_image, current_image.shape[-1], axis=-1)
354
- output_image = grey_image.copy()
355
-
356
- zoomed_area = current_image[row_start:row_end, col_start:col_end]
357
- upscale_factor = 6
358
- zoomed_area_upscaled = np.kron(zoomed_area, np.ones((upscale_factor, upscale_factor, 1)))
359
-
360
- center_row, center_col = start_row, start_col
361
- row_start_upscaled = max(center_row - zoomed_area_upscaled.shape[0] // 2, 0)
362
- row_end_upscaled = min(center_row + zoomed_area_upscaled.shape[0] // 2, output_image.shape[0])
363
- col_start_upscaled = max(center_col - zoomed_area_upscaled.shape[1] // 2, 0)
364
- col_end_upscaled = min(center_col + zoomed_area_upscaled.shape[1] // 2, output_image.shape[1])
365
-
366
- row_start_zoomed = max(0, -row_start_upscaled)
367
- row_end_zoomed = row_start_zoomed + (row_end_upscaled - row_start_upscaled)
368
- col_start_zoomed = max(0, -col_start_upscaled)
369
- col_end_zoomed = col_start_zoomed + (col_end_upscaled - col_start_upscaled)
370
-
371
- row_end_zoomed = min(row_end_zoomed, zoomed_area_upscaled.shape[0])
372
- col_end_zoomed = min(col_end_zoomed, zoomed_area_upscaled.shape[1])
373
-
374
- output_image[row_start_upscaled:row_end_upscaled, col_start_upscaled:col_end_upscaled] = \
375
- zoomed_area_upscaled[row_start_zoomed:row_end_zoomed, col_start_zoomed:col_end_zoomed]
376
-
377
- # Apply the same zoom to the other image
378
- other_output_image = grey_image.copy()
379
- other_zoomed_area = other_image[row_start:row_end, col_start:col_end]
380
- other_zoomed_area_upscaled = np.kron(other_zoomed_area, np.ones((upscale_factor, upscale_factor, 1)))
381
-
382
- other_output_image[row_start_upscaled:row_end_upscaled, col_start_upscaled:col_end_upscaled] = \
383
- other_zoomed_area_upscaled[row_start_zoomed:row_end_zoomed, col_start_zoomed:col_end_zoomed]
384
-
385
- return output_image, True, other_output_image, True
386
-
387
- image_a.select(handle_zoom, [image_a, zoomed_state_a, state_segmented_a, state_segmented_b], [image_a, zoomed_state_a, image_b, zoomed_state_b])
388
- image_b.select(handle_zoom, [image_b, zoomed_state_b, state_segmented_b, state_segmented_a], [image_b, zoomed_state_b, image_a, zoomed_state_a])
389
-
390
  with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
391
  rankings_table = gr.Dataframe(
392
  headers=["Model", "Ranking"],
 
194
 
195
  def gradio_interface():
196
  """Create and return the Gradio interface."""
197
+ with gr.Blocks(js=js, fill_width=True) as demo:
198
  gr.Markdown("# Background Removal Arena")
199
  button_name = "Difference between masks"
200
 
201
  with gr.Tabs() as tabs:
202
  with gr.Tab("⚔️ Arena (battle)", id=0):
203
+ image_width = None
 
204
  notice_markdown = gr.Markdown(get_notice_markdown(), elem_id="notice_markdown")
205
  with gr.Row(equal_height=True):
206
  def on_enter_contest(username):
 
236
  image_a = gr.Image(
237
  value=segmented_a,
238
  label="Image",
239
+ width=image_width
 
240
  )
241
 
242
  input_image_display = gr.AnnotatedImage(
243
  value=(input_image, [(mask_difference > 0, button_name)]),
244
  label="Input Image",
245
+ width=image_width
 
246
  )
247
 
248
  image_b = gr.Image(
249
  value=segmented_b,
250
  label="Image",
251
+ width=image_width
 
252
  )
 
 
 
253
 
254
  state_model_a_name = gr.State(model_a_name)
255
  state_model_b_name = gr.State(model_b_name)
256
  state_filename = gr.State(filename)
 
 
257
 
258
  outputs = [
259
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
260
+ input_image_display
261
  ]
262
  return outputs
263
 
264
  with gr.Row():
265
+ state_filename, image_a, image_b, state_model_a_name, state_model_b_name, input_image_display = refresh_states()
266
 
267
  with gr.Row():
268
  vote_a_button = gr.Button("👈 A is better")
 
297
  inputs=[username_input],
298
  outputs=[
299
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
300
+ input_image_display, notice_markdown
 
301
  ]
302
  )
303
  vote_b_button.click(
 
305
  inputs=[username_input],
306
  outputs=[
307
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
308
+ input_image_display, notice_markdown
 
309
  ]
310
  )
311
  vote_tie_button.click(
 
313
  inputs=[username_input],
314
  outputs=[
315
  state_filename, image_a, image_b, state_model_a_name, state_model_b_name,
316
+ input_image_display, notice_markdown
 
317
  ]
318
  )
319
 
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  with gr.Tab("🏆 Leaderboard", id=1) as leaderboard_tab:
322
  rankings_table = gr.Dataframe(
323
  headers=["Model", "Ranking"],