Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
import torch
|
| 6 |
+
from segment_anything import sam_model_registry, SamPredictor
|
| 7 |
+
|
| 8 |
+
# --- CONFIG ---
|
| 9 |
+
SAM_CHECKPOINT = "sam_vit_h_4b8939.pth"
|
| 10 |
+
SAM_MODEL_TYPE = "vit_h"
|
| 11 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
BLUR_RADIUS = 10
|
| 13 |
+
DEMO_IMAGE = "demo.png" # put a demo image in the repo
|
| 14 |
+
# --------------
|
| 15 |
+
|
| 16 |
+
sam = sam_model_registry[SAM_MODEL_TYPE](checkpoint=SAM_CHECKPOINT)
|
| 17 |
+
sam.to(device=DEVICE)
|
| 18 |
+
predictor = SamPredictor(sam)
|
| 19 |
+
|
| 20 |
+
def soft_alpha(mask_uint8, blur_radius=10):
|
| 21 |
+
blurred = cv2.GaussianBlur(mask_uint8, (0,0), sigmaX=blur_radius, sigmaY=blur_radius)
|
| 22 |
+
return (blurred.astype(np.float32) / 255.0).clip(0.0, 1.0)
|
| 23 |
+
|
| 24 |
+
def isolate_with_click(image: Image.Image, evt: gr.SelectData):
|
| 25 |
+
img_rgb = np.array(image.convert("RGB"))
|
| 26 |
+
predictor.set_image(img_rgb)
|
| 27 |
+
|
| 28 |
+
input_point = np.array([[evt.index[0], evt.index[1]]])
|
| 29 |
+
input_label = np.array([1])
|
| 30 |
+
|
| 31 |
+
masks, scores, _ = predictor.predict(
|
| 32 |
+
point_coords=input_point,
|
| 33 |
+
point_labels=input_label,
|
| 34 |
+
multimask_output=True
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
best_mask = masks[np.argmax(scores)].astype(np.uint8) * 255
|
| 38 |
+
alpha = soft_alpha(best_mask, blur_radius=BLUR_RADIUS)
|
| 39 |
+
|
| 40 |
+
ys, xs = np.where(best_mask == 255)
|
| 41 |
+
if len(xs) == 0 or len(ys) == 0:
|
| 42 |
+
return None
|
| 43 |
+
x0, x1 = xs.min(), xs.max()
|
| 44 |
+
y0, y1 = ys.min(), ys.max()
|
| 45 |
+
pad = int(max(img_rgb.shape[:2]) * 0.02)
|
| 46 |
+
x0 = max(0, x0 - pad); x1 = min(img_rgb.shape[1]-1, x1 + pad)
|
| 47 |
+
y0 = max(0, y0 - pad); y1 = min(img_rgb.shape[0]-1, y1 + pad)
|
| 48 |
+
|
| 49 |
+
fg_rgb = img_rgb[y0:y1+1, x0:x1+1]
|
| 50 |
+
fg_alpha = alpha[y0:y1+1, x0:x1+1]
|
| 51 |
+
|
| 52 |
+
rgba = np.dstack((fg_rgb, (fg_alpha * 255).astype(np.uint8)))
|
| 53 |
+
return Image.fromarray(rgba)
|
| 54 |
+
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("### SAM Object Isolation\nUpload an image or use the demo below, then click on the object to isolate it.")
|
| 57 |
+
|
| 58 |
+
inp = gr.Image(type="pil", label="Upload image", interactive=True)
|
| 59 |
+
demo_img = gr.Image(value=DEMO_IMAGE, type="pil", label="Demo image (click to try)", interactive=True)
|
| 60 |
+
out = gr.Image(type="pil", label="Isolated cutout (RGBA)")
|
| 61 |
+
|
| 62 |
+
inp.select(isolate_with_click, inputs=[inp], outputs=out)
|
| 63 |
+
demo_img.select(isolate_with_click, inputs=[demo_img], outputs=out)
|
| 64 |
+
|
| 65 |
+
demo.launch()
|