Spaces:
Running
Running
Commit
·
a006f1b
1
Parent(s):
fd1210f
feat: draw marker before generating image
Browse files
app.py
CHANGED
@@ -30,17 +30,23 @@ def update_reference_image(choice: int) -> tuple[str, int, str]:
|
|
30 |
return image_path, choice, heatmap_path
|
31 |
|
32 |
|
33 |
-
def
|
34 |
"""
|
35 |
-
|
36 |
-
Returns
|
37 |
"""
|
38 |
x, y = evt.index[0], evt.index[1]
|
39 |
-
x_norm, y_norm = x / 1155, y / 1155
|
40 |
-
generated_image = generate_image(image_idx, x_norm, y_norm)
|
41 |
heatmap_path = f"imgs/heatmap_{image_idx}.png"
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
|
46 |
with gr.Blocks(
|
@@ -78,14 +84,13 @@ with gr.Blocks(
|
|
78 |
# Left column
|
79 |
with gr.Column(scale=1):
|
80 |
selected_idx = gr.State(value=0)
|
|
|
81 |
|
82 |
-
# gr.Markdown("### Select Task")
|
83 |
with gr.Column(elem_classes="radio-container"):
|
84 |
task_select = gr.Radio(
|
85 |
choices=["Task 1", "Task 2", "Task 3", "Task 4"],
|
86 |
value="Task 1",
|
87 |
label="Select Task",
|
88 |
-
# show_label=False,
|
89 |
interactive=True,
|
90 |
)
|
91 |
|
@@ -158,11 +163,16 @@ with gr.Blocks(
|
|
158 |
outputs=[reference_image, selected_idx, coord_selector],
|
159 |
)
|
160 |
|
|
|
161 |
coord_selector.select(
|
162 |
-
|
163 |
inputs=[selected_idx],
|
164 |
-
outputs=[
|
165 |
trigger_mode="multiple",
|
|
|
|
|
|
|
|
|
166 |
)
|
167 |
|
168 |
demo.launch()
|
|
|
30 |
return image_path, choice, heatmap_path
|
31 |
|
32 |
|
33 |
+
def update_marker(image_idx: int, evt: gr.SelectData) -> tuple[Image.Image, tuple[int, int]]:
|
34 |
"""
|
35 |
+
Update the coordinate selector with the marker
|
36 |
+
Returns the marked image and the coordinates for the next function
|
37 |
"""
|
38 |
x, y = evt.index[0], evt.index[1]
|
|
|
|
|
39 |
heatmap_path = f"imgs/heatmap_{image_idx}.png"
|
40 |
+
return create_marker_overlay(heatmap_path, x, y), (x, y)
|
41 |
+
|
42 |
+
|
43 |
+
def generate_output_image(image_idx: int, coords: tuple[int, int]) -> Image.Image:
|
44 |
+
"""
|
45 |
+
Generate the output image based on the selected coordinates
|
46 |
+
"""
|
47 |
+
x, y = coords
|
48 |
+
x_norm, y_norm = x / 1155, y / 1155
|
49 |
+
return generate_image(image_idx, x_norm, y_norm)
|
50 |
|
51 |
|
52 |
with gr.Blocks(
|
|
|
84 |
# Left column
|
85 |
with gr.Column(scale=1):
|
86 |
selected_idx = gr.State(value=0)
|
87 |
+
coords = gr.State() # Add state for coordinates
|
88 |
|
|
|
89 |
with gr.Column(elem_classes="radio-container"):
|
90 |
task_select = gr.Radio(
|
91 |
choices=["Task 1", "Task 2", "Task 3", "Task 4"],
|
92 |
value="Task 1",
|
93 |
label="Select Task",
|
|
|
94 |
interactive=True,
|
95 |
)
|
96 |
|
|
|
163 |
outputs=[reference_image, selected_idx, coord_selector],
|
164 |
)
|
165 |
|
166 |
+
# Split the coordinate selection into two events with state passing
|
167 |
coord_selector.select(
|
168 |
+
fn=update_marker,
|
169 |
inputs=[selected_idx],
|
170 |
+
outputs=[coord_selector, coords],
|
171 |
trigger_mode="multiple",
|
172 |
+
).then(
|
173 |
+
fn=generate_output_image,
|
174 |
+
inputs=[selected_idx, coords],
|
175 |
+
outputs=output_image,
|
176 |
)
|
177 |
|
178 |
demo.launch()
|