adityas2410 commited on
Commit
c7f78e3
1 Parent(s): 3449cc9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from segment_functions import segment_image
3
+
4
+ def load_image_from_url(url):
5
+ try:
6
+ image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
7
+ return image
8
+ except Exception as e:
9
+ return f"Error: {str(e)}"
10
+
11
+ selected_points = [] # Global list to store points
12
+
13
+ def capture_points(image, evt: gr.SelectData):
14
+ """
15
+ Capture click coordinates on the image.
16
+ """
17
+ global selected_points
18
+ x, y = evt.index[0], evt.index[1] # Extract x, y from Gradio click event
19
+ selected_points.append([x, y]) # Append as [x, y] to list
20
+ return str(selected_points) # Display points as a string
21
+
22
+ def segment_image_ui(image):
23
+ """
24
+ Run the segmentation function using selected points.
25
+ """
26
+ global selected_points
27
+ if not selected_points:
28
+ return "Error: No points selected!"
29
+
30
+ # Call your existing segment_image function
31
+ segmented_image = segment_image(image, selected_points)
32
+ selected_points = [] # Clear points after use
33
+ return segmented_image
34
+
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("# Image Segmentation")
37
+
38
+ with gr.Row():
39
+ # Image upload and URL input
40
+ with gr.Column():
41
+ image_input = gr.Image(sources=["upload"], type="pil", label="Upload Image")
42
+ image_url = gr.Textbox(label="Paste Image URL Here")
43
+ load_button = gr.Button("Load Image from URL")
44
+
45
+ image_output = gr.Image(type="pil", label="Segmented Image")
46
+
47
+ # Selected points
48
+ points_output = gr.Textbox(label="Selected Points (x, y)", interactive=False)
49
+
50
+ # Button to run segmentation
51
+ segment_button = gr.Button("Run Segmentation")
52
+
53
+ # Load image from URL
54
+ load_button.click(fn=load_image_from_url, inputs=[image_url], outputs=[image_input])
55
+
56
+ # Capture click points
57
+ image_input.select(fn=capture_points, inputs=image_input, outputs=points_output)
58
+
59
+ # Run segmentation
60
+ segment_button.click(fn=segment_image_ui, inputs=image_input, outputs=image_output)
61
+
62
+ demo.launch()