clement-bonnet commited on
Commit
1f14f97
·
1 Parent(s): 3e506b8

fix: image selection

Browse files
Files changed (3) hide show
  1. app.py +20 -30
  2. imgs/heatmap_0.png +0 -0
  3. imgs/heatmap_1.png +0 -0
app.py CHANGED
@@ -1,37 +1,26 @@
1
  import gradio as gr
2
- import numpy as np
3
  from PIL import Image
4
 
5
  from inference import generate_image
6
 
7
 
8
- # Create a square image for the coordinate selector
9
- def create_selector_image():
10
- # Create a white square with black border
11
- size = 400
12
- border = 2
13
- img = np.ones((size, size, 3), dtype=np.uint8) * 255
14
- # Add black border
15
- img[:border, :] = 0 # top
16
- img[-border:, :] = 0 # bottom
17
- img[:, :border] = 0 # left
18
- img[:, -border:] = 0 # right
19
- return Image.fromarray(img)
20
-
21
-
22
  def process_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
23
  """
24
  Process the click event on the coordinate selector
25
  """
26
- # Extract coordinates from click event
27
  x, y = evt.index[0], evt.index[1]
28
- # Normalize coordinates to [0, 1]
29
- x, y = x / 400, y / 400 # Divide by image size (400x400)
30
  print(f"Clicked at coordinates: ({x:.3f}, {y:.3f})")
31
- # Generate image using the model
32
  return generate_image(image_idx, x, y)
33
 
34
 
 
 
 
 
 
 
 
35
  with gr.Blocks() as demo:
36
  gr.Markdown(
37
  """
@@ -41,7 +30,7 @@ with gr.Blocks() as demo:
41
  )
42
 
43
  with gr.Row():
44
- # Left column: Reference images and coordinate selector
45
  with gr.Column(scale=1):
46
  # Radio buttons for image selection
47
  image_idx = gr.Radio(
@@ -54,18 +43,20 @@ with gr.Blocks() as demo:
54
  # Display reference images
55
  gallery = gr.Gallery(
56
  value=[
 
57
  "imgs/pattern_1.png",
58
- "imgs/pattern_2.png",
59
  ],
60
- columns=2,
61
  rows=2,
62
- height=300,
63
  label="Reference Images",
64
  )
65
 
66
- # Coordinate selector (now using Image component)
 
 
67
  coord_selector = gr.Image(
68
- value=create_selector_image(),
69
  label="Click to select (x, y) coordinates",
70
  show_label=True,
71
  interactive=True,
@@ -73,12 +64,11 @@ with gr.Blocks() as demo:
73
  width=400,
74
  )
75
 
76
- # Right column: Generated image
77
- with gr.Column(scale=1):
78
- output_image = gr.Image(label="Generated Image", height=308, width=328)
79
 
80
- # Handle click events
81
  coord_selector.select(process_click, inputs=[image_idx], outputs=output_image, trigger_mode="multiple")
 
82
 
83
- # Launch the app
84
  demo.launch()
 
1
  import gradio as gr
 
2
  from PIL import Image
3
 
4
  from inference import generate_image
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def process_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
8
  """
9
  Process the click event on the coordinate selector
10
  """
 
11
  x, y = evt.index[0], evt.index[1]
12
+ x, y = x / 400, y / 400
 
13
  print(f"Clicked at coordinates: ({x:.3f}, {y:.3f})")
 
14
  return generate_image(image_idx, x, y)
15
 
16
 
17
+ def update_background(image_idx: int) -> Image.Image:
18
+ """
19
+ Update the coordinate selector background based on selected image
20
+ """
21
+ return f"imgs/heatmap_{image_idx}.png"
22
+
23
+
24
  with gr.Blocks() as demo:
25
  gr.Markdown(
26
  """
 
30
  )
31
 
32
  with gr.Row():
33
+ # Left column: Reference images only
34
  with gr.Column(scale=1):
35
  # Radio buttons for image selection
36
  image_idx = gr.Radio(
 
43
  # Display reference images
44
  gallery = gr.Gallery(
45
  value=[
46
+ "imgs/pattern_0.png",
47
  "imgs/pattern_1.png",
 
48
  ],
49
+ columns=1,
50
  rows=2,
51
+ min_width=600,
52
  label="Reference Images",
53
  )
54
 
55
+ # Right column: Coordinate selector and output image
56
+ with gr.Column(scale=1):
57
+ # Coordinate selector with dynamic background
58
  coord_selector = gr.Image(
59
+ value="imgs/heatmap_0.png", # Initial background
60
  label="Click to select (x, y) coordinates",
61
  show_label=True,
62
  interactive=True,
 
64
  width=400,
65
  )
66
 
67
+ # Generated image output
68
+ output_image = gr.Image(label="Generated Image", height=400, width=400)
 
69
 
70
+ # Handle click events and background updates
71
  coord_selector.select(process_click, inputs=[image_idx], outputs=output_image, trigger_mode="multiple")
72
+ image_idx.change(update_background, inputs=[image_idx], outputs=[coord_selector])
73
 
 
74
  demo.launch()
imgs/heatmap_0.png ADDED
imgs/heatmap_1.png ADDED