|
import gradio as gr |
|
from segment_functions import segment_image |
|
|
|
def load_image_from_url(url): |
|
try: |
|
image = Image.open(requests.get(url, stream=True).raw).convert("RGB") |
|
return image |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
selected_points = [] |
|
|
|
def capture_points(image, evt: gr.SelectData): |
|
""" |
|
Capture click coordinates on the image. |
|
""" |
|
global selected_points |
|
x, y = evt.index[0], evt.index[1] |
|
selected_points.append([x, y]) |
|
return str(selected_points) |
|
|
|
def segment_image_ui(image): |
|
""" |
|
Run the segmentation function using selected points. |
|
""" |
|
global selected_points |
|
if not selected_points: |
|
return "Error: No points selected!" |
|
|
|
|
|
segmented_image = segment_image(image, selected_points) |
|
selected_points = [] |
|
return segmented_image |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Image Segmentation") |
|
|
|
with gr.Row(): |
|
|
|
with gr.Column(): |
|
image_input = gr.Image(sources=["upload"], type="pil", label="Upload Image") |
|
image_url = gr.Textbox(label="Paste Image URL Here") |
|
load_button = gr.Button("Load Image from URL") |
|
|
|
image_output = gr.Image(type="pil", label="Segmented Image") |
|
|
|
|
|
points_output = gr.Textbox(label="Selected Points (x, y)", interactive=False) |
|
|
|
|
|
segment_button = gr.Button("Run Segmentation") |
|
|
|
|
|
load_button.click(fn=load_image_from_url, inputs=[image_url], outputs=[image_input]) |
|
|
|
|
|
image_input.select(fn=capture_points, inputs=image_input, outputs=points_output) |
|
|
|
|
|
segment_button.click(fn=segment_image_ui, inputs=image_input, outputs=image_output) |
|
|
|
demo.launch() |
|
|