|
from ultralytics import YOLO |
|
import gradio as gr |
|
import torch |
|
from tools import fast_process |
|
|
|
|
|
model = YOLO('checkpoints/FastSAM.pt') |
|
|
|
|
|
title = "<center><strong><font size='8'>๐ Fast Segment Anything ๐ค</font></strong></center>" |
|
|
|
news = """ # News |
|
|
|
๐ฅ Add the 'Advanced options" in Everything mode to get a more detailed adjustment. |
|
""" |
|
|
|
|
|
|
|
|
|
description = """This is a demo on Github project ๐ [Fast Segment Anything Model](https://github.com/CASIA-IVA-Lab/FastSAM). |
|
|
|
๐ฏ Upload an Image, segment it with Fast Segment Anything (Everything mode). The other modes will come soon. |
|
|
|
โ๏ธ It takes about 4~ seconds to generate segment results. The concurrency_count of queue is 1, please wait for a moment when it is crowded. |
|
|
|
๐ To get faster results, you can use a smaller input size and leave high_visual_quality unchecked. |
|
|
|
๐ฃ You can also obtain the segmentation results of any Image through this Colab: [](https://colab.research.google.com/drive/1oX14f6IneGGw612WgVlAiy91UHwFAvr9?usp=sharing) |
|
|
|
๐ A huge thanks goes out to the @HuggingFace Team for supporting us with GPU grant. |
|
|
|
๐ Check out our [Model Card ๐](https://huggingface.co/An-619/FastSAM) |
|
|
|
""" |
|
|
|
examples = [["assets/sa_8776.jpg"], ["assets/sa_414.jpg"], ["assets/sa_1309.jpg"], ["assets/sa_11025.jpg"], |
|
["assets/sa_561.jpg"], ["assets/sa_192.jpg"], ["assets/sa_10039.jpg"], ["assets/sa_862.jpg"]] |
|
|
|
default_example = examples[0] |
|
|
|
css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }" |
|
|
|
|
|
def segment_image( |
|
input, |
|
input_size=1024, |
|
iou_threshold=0.7, |
|
conf_threshold=0.25, |
|
better_quality=False, |
|
mask_random_color=True, |
|
withContours=True, |
|
points=None, |
|
bbox=None, |
|
point_label=None, |
|
use_retina=True, |
|
): |
|
input_size = int(input_size) |
|
|
|
|
|
w, h = input.size |
|
scale = input_size / max(w, h) |
|
new_w = int(w * scale) |
|
new_h = int(h * scale) |
|
input = input.resize((new_w, new_h)) |
|
|
|
results = model(input, |
|
device=device, |
|
retina_masks=True, |
|
iou=iou_threshold, |
|
conf=conf_threshold, |
|
imgsz=input_size,) |
|
fig = fast_process(annotations=results[0].masks.data, |
|
image=input, |
|
device=device, |
|
scale=(1024 // input_size), |
|
better_quality=better_quality, |
|
mask_random_color=mask_random_color, |
|
points=points, |
|
bbox=bbox, |
|
point_label=point_label, |
|
use_retina=use_retina, |
|
withContours=withContours,) |
|
return fig |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
device = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
|
|
cond_img = gr.Image(label="Input", value=default_example[0], type='pil') |
|
|
|
segm_img = gr.Image(label="Segmented Image", interactive=False, type='pil') |
|
|
|
input_size_slider = gr.components.Slider(minimum=512, |
|
maximum=1024, |
|
value=1024, |
|
step=64, |
|
label='Input_size (Our model was trained on a size of 1024)') |
|
|
|
with gr.Blocks(css=css, title='Fast Segment Anything') as demo: |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
|
|
gr.Markdown(title) |
|
|
|
with gr.Column(scale=1): |
|
|
|
gr.Markdown(news) |
|
|
|
|
|
with gr.Row(variant="panel"): |
|
with gr.Column(scale=1): |
|
cond_img.render() |
|
|
|
with gr.Column(scale=1): |
|
segm_img.render() |
|
|
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
input_size_slider.render() |
|
|
|
with gr.Row(): |
|
contour_check = gr.Checkbox(value=True, label='withContours') |
|
|
|
with gr.Column(): |
|
segment_btn = gr.Button("Segment Anything", variant='primary') |
|
|
|
|
|
|
|
|
|
gr.Markdown("Try some of the examples below โฌ๏ธ") |
|
gr.Examples(examples=examples, |
|
inputs=[cond_img], |
|
outputs=segm_img, |
|
fn=segment_image, |
|
cache_examples=True, |
|
examples_per_page=4) |
|
|
|
with gr.Column(): |
|
with gr.Accordion("Advanced options", open=False): |
|
iou_threshold = gr.Slider(0.1, 0.9, 0.7, step=0.1, label='iou_threshold') |
|
conf_threshold = gr.Slider(0.1, 0.9, 0.25, step=0.05, label='conf_threshold') |
|
mor_check = gr.Checkbox(value=False, label='better_visual_quality') |
|
|
|
|
|
gr.Markdown(description) |
|
|
|
segment_btn.click(segment_image, |
|
inputs=[cond_img, input_size_slider, iou_threshold, conf_threshold, mor_check, contour_check], |
|
outputs=segm_img) |
|
|
|
|
|
|
|
|
|
|
|
|
|
demo.queue() |
|
demo.launch() |
|
|