File size: 2,046 Bytes
c7f78e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 = []  # Global list to store 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]  # Extract x, y from Gradio click event
    selected_points.append([x, y])     # Append as [x, y] to list
    return str(selected_points)        # Display points as a string

def segment_image_ui(image):
    """
    Run the segmentation function using selected points.
    """
    global selected_points
    if not selected_points:
        return "Error: No points selected!"
    
    # Call your existing segment_image function
    segmented_image = segment_image(image, selected_points)
    selected_points = []  # Clear points after use
    return segmented_image

with gr.Blocks() as demo:
    gr.Markdown("# Image Segmentation")

    with gr.Row():
        # Image upload and URL input
        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")

    # Selected points
    points_output = gr.Textbox(label="Selected Points (x, y)", interactive=False)

    # Button to run segmentation
    segment_button = gr.Button("Run Segmentation")

    # Load image from URL
    load_button.click(fn=load_image_from_url, inputs=[image_url], outputs=[image_input])

    # Capture click points
    image_input.select(fn=capture_points, inputs=image_input, outputs=points_output)

    # Run segmentation
    segment_button.click(fn=segment_image_ui, inputs=image_input, outputs=image_output)

demo.launch()