clement-bonnet commited on
Commit
4cac868
·
1 Parent(s): 0276a9b

feat: add marker on the heatmap

Browse files
Files changed (1) hide show
  1. app.py +41 -7
app.py CHANGED
@@ -1,12 +1,33 @@
1
  import gradio as gr
2
- from PIL import Image
3
 
4
  from inference import generate_image
5
 
6
-
7
  TASK_TO_INDEX = {"Task 1": 0, "Task 2": 1, "Task 3": 2, "Task 4": 3}
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def update_reference_image(choice: int) -> tuple[str, int, str]:
11
  """
12
  Update the reference image display based on radio button selection
@@ -17,13 +38,23 @@ def update_reference_image(choice: int) -> tuple[str, int, str]:
17
  return image_path, choice, heatmap_path
18
 
19
 
20
- def process_coord_click(image_idx: int, evt: gr.SelectData) -> Image.Image:
21
  """
22
  Process the click event on the coordinate selector
 
23
  """
24
  x, y = evt.index[0], evt.index[1]
25
- x, y = x / 1155, y / 1155 # Normalize the coordinates
26
- return generate_image(image_idx, x, y)
 
 
 
 
 
 
 
 
 
27
 
28
 
29
  with gr.Blocks(
@@ -109,9 +140,12 @@ with gr.Blocks(
109
  outputs=[reference_image, selected_idx, coord_selector],
110
  )
111
 
112
- # Handle coordinate selection
113
  coord_selector.select(
114
- process_coord_click, inputs=[selected_idx], outputs=output_image, trigger_mode="multiple"
 
 
 
115
  )
116
 
117
  demo.launch()
 
1
  import gradio as gr
2
+ from PIL import Image, ImageDraw
3
 
4
  from inference import generate_image
5
 
 
6
  TASK_TO_INDEX = {"Task 1": 0, "Task 2": 1, "Task 3": 2, "Task 4": 3}
7
 
8
 
9
+ def create_marker_overlay(image_path: str, x: int, y: int) -> Image.Image:
10
+ """
11
+ Creates an image with a marker at the specified coordinates
12
+ """
13
+ # Load the base image
14
+ base_image = Image.open(image_path)
15
+
16
+ # Create a copy to draw on
17
+ marked_image = base_image.copy()
18
+ draw = ImageDraw.Draw(marked_image)
19
+
20
+ # Define marker properties
21
+ marker_size = 10
22
+ marker_color = "red"
23
+
24
+ # Draw marker
25
+ draw.line([x - marker_size, y, x + marker_size, y], fill=marker_color, width=2)
26
+ draw.line([x, y - marker_size, x, y + marker_size], fill=marker_color, width=2)
27
+
28
+ return marked_image
29
+
30
+
31
  def update_reference_image(choice: int) -> tuple[str, int, str]:
32
  """
33
  Update the reference image display based on radio button selection
 
38
  return image_path, choice, heatmap_path
39
 
40
 
41
+ def process_coord_click(image_idx: int, evt: gr.SelectData) -> tuple[Image.Image, Image.Image]:
42
  """
43
  Process the click event on the coordinate selector
44
+ Returns both the generated image and the marked coordinate selector
45
  """
46
  x, y = evt.index[0], evt.index[1]
47
+ # Create normalized coordinates for generation
48
+ x_norm, y_norm = x / 1155, y / 1155
49
+
50
+ # Generate the output image
51
+ generated_image = generate_image(image_idx, x_norm, y_norm)
52
+
53
+ # Create marked coordinate selector
54
+ heatmap_path = f"imgs/heatmap_{image_idx}.png"
55
+ marked_selector = create_marker_overlay(heatmap_path, x, y)
56
+
57
+ return generated_image, marked_selector
58
 
59
 
60
  with gr.Blocks(
 
140
  outputs=[reference_image, selected_idx, coord_selector],
141
  )
142
 
143
+ # Handle coordinate selection - now updates both output image and coord_selector
144
  coord_selector.select(
145
+ process_coord_click,
146
+ inputs=[selected_idx],
147
+ outputs=[output_image, coord_selector],
148
+ trigger_mode="multiple",
149
  )
150
 
151
  demo.launch()