Spaces:
Sleeping
Sleeping
Commit ·
0564522
1
Parent(s): a18a9ef
Add SAM segmentation app
Browse files- app.py +38 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Initialize the pipeline for SAM
|
| 7 |
+
# Use "facebook/sam-vit-base" for better speed on Spaces
|
| 8 |
+
generator = pipeline("mask-generation", model="facebook/sam-vit-base", device=-1) # device=-1 for CPU
|
| 9 |
+
|
| 10 |
+
def segment_image(input_image):
|
| 11 |
+
# Generate masks automatically
|
| 12 |
+
outputs = generator(input_image, points_per_batch=64)
|
| 13 |
+
masks = outputs["masks"]
|
| 14 |
+
|
| 15 |
+
# Create a combined visualization of the masks
|
| 16 |
+
# Each mask is a boolean array; we can overlay them with colors
|
| 17 |
+
width, height = input_image.size
|
| 18 |
+
overlay = np.zeros((height, width, 3), dtype=np.uint8)
|
| 19 |
+
|
| 20 |
+
for mask in masks:
|
| 21 |
+
color = np.random.randint(0, 255, (3,))
|
| 22 |
+
overlay[mask] = color
|
| 23 |
+
|
| 24 |
+
# Blend the original image with the masks
|
| 25 |
+
combined = Image.blend(input_image.convert("RGB"), Image.fromarray(overlay), alpha=0.5)
|
| 26 |
+
return combined
|
| 27 |
+
|
| 28 |
+
# Define the Gradio Interface
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=segment_image,
|
| 31 |
+
inputs=gr.Image(type="pil"),
|
| 32 |
+
outputs=gr.Image(type="pil", label="Segmented Output"),
|
| 33 |
+
title="Segment Anything with SAM",
|
| 34 |
+
description="Upload an image to see automatic zero-shot semantic segmentation."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
numpy
|
| 5 |
+
pillow
|