Spaces:
Runtime error
Runtime error
File size: 1,865 Bytes
5bd8d5b fe42dd8 5bd8d5b 61d1727 5bd8d5b 61d1727 5bd8d5b e4d42b3 61d1727 5bd8d5b 61d1727 5bd8d5b |
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 |
from typing import Optional
import gradio as gr
import numpy as np
import spaces
import supervision as sv
import torch
from PIL import Image
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
from sam2.build_sam import build_sam2
MARKDOWN = """
# Segment Anything Model 2 🔥
Segment Anything Model 2 (SAM 2) is a foundation model designed to address promptable
visual segmentation in both images and videos. The model extends its functionality to
video by treating images as single-frame videos. Its design, a simple transformer
architecture with streaming memory, enables real-time video processing.
"""
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
MASK_ANNOTATOR = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
CHECKPOINT = "checkpoints/sam2_hiera_large.pt"
CONFIG = "sam2_hiera_l.yaml"
sam2_model = build_sam2(CONFIG, CHECKPOINT, device=DEVICE, apply_postprocessing=False)
@spaces.GPU
def process(image_input) -> Optional[Image.Image]:
mask_generator = SAM2AutomaticMaskGenerator(sam2_model)
image = np.array(image_input.convert("RGB"))
sam_result = mask_generator.generate(image)
detections = sv.Detections.from_sam(sam_result=sam_result)
return MASK_ANNOTATOR.annotate(scene=image_input, detections=detections)
with gr.Blocks() as demo:
gr.Markdown(MARKDOWN)
with gr.Row():
with gr.Column():
image_input_component = gr.Image(type='pil', label='Upload image')
submit_button_component = gr.Button(value='Submit', variant='primary')
with gr.Column():
image_output_component = gr.Image(type='pil', label='Image Output')
submit_button_component.click(
fn=process,
inputs=[image_input_component],
outputs=[image_output_component]
)
demo.launch(debug=False, show_error=True, max_threads=1)
|