Spaces:
Build error
Build error
123LETSPLAY
commited on
Commit
•
e1cc11d
1
Parent(s):
0e9a7e8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
5 |
+
|
6 |
+
# Load the SAM2 model
|
7 |
+
predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2.1-hiera-large")
|
8 |
+
|
9 |
+
# Function to predict masks from the image and prompts
|
10 |
+
def generate_mask(image, prompt):
|
11 |
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
12 |
+
predictor.set_image(image)
|
13 |
+
masks, _, _ = predictor.predict(prompt)
|
14 |
+
return masks[0] # Returning the first mask for simplicity
|
15 |
+
|
16 |
+
# Set up the Gradio interface
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
gr.Markdown("# Image Segmentation using SAM2")
|
19 |
+
|
20 |
+
# Input: Upload an image
|
21 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
22 |
+
|
23 |
+
# Input: Text prompt for image segmentation
|
24 |
+
prompt_input = gr.Textbox(label="Enter segmentation prompt", placeholder="Describe what you want to segment")
|
25 |
+
|
26 |
+
# Output: Display the mask generated by the SAM2 model
|
27 |
+
output_mask = gr.Image(label="Generated Mask")
|
28 |
+
|
29 |
+
# Button to trigger mask generation
|
30 |
+
generate_button = gr.Button("Generate Mask")
|
31 |
+
|
32 |
+
# Link button click with the segmentation function
|
33 |
+
generate_button.click(fn=generate_mask, inputs=[image_input, prompt_input], outputs=output_mask)
|
34 |
+
|
35 |
+
# Launch the Gradio app
|
36 |
+
demo.launch()
|