KurtLin's picture
Initial Commit
00a3d3d
raw
history blame
2.58 kB
import numpy as np
import matplotlib.pyplot as plt
import cv2
import torch
from segment_anything import sam_model_registry, SamPredictor
from preprocess import show_mask, show_points, show_box
import gradio as gr
sam_checkpoint = {
"ViT-base": "weights/sam_vit_b_01ec64.pth",
"ViT-large": "weights/sam_vit_l_0b3195.pth",
"ViT-huge": "weights/sam_vit_h_4b8939.pth",
}
model_type = {
"ViT-base": "vit_b",
"ViT-large": "vit_l",
"ViT-huge": "vit_h",
}
device = "cuda" if torch.cuda.is_available() else "cpu"
def get_coords(evt: gr.SelectData):
return f"{evt.index[0]}, {evt.index[1]}"
def inference(image, input_label, model_choice):
sam = sam_model_registry[model_type[model_choice]](checkpoint=sam_checkpoint[model_choice])
sam.to(device=device)
predictor = SamPredictor(sam)
predictor.set_image(image)
input_point = np.array([[int(input_label['label'].split(',')[0]), int(input_label['label'].split(',')[1])]])
input_label = np.array([1])
masks, scores, logits = predictor.predict(
point_coords=input_point,
point_labels=input_label,
multimask_output=True,
)
mask = masks[0]
image2 = image.copy()
image2[mask, 0] = 255
return image2
my_app = gr.Blocks()
with my_app:
gr.Markdown("Segment Anything Testing")
with gr.Tabs():
with gr.TabItem("Select your image"):
with gr.Row():
with gr.Column():
img_source = gr.Image(label="Please select picture and click the part to segment",
value='./images/truck.jpg', shape=(1024, 1024))
coords = gr.Label(label="Image Coordinate")
model_choice = gr.Dropdown(['ViT-base', 'ViT-large', 'ViT-huge'], label='Model Backbone')
infer = gr.Button(label="Segment")
with gr.Column():
img_output = gr.Image(label="Output Mask", shape=(1024, 1024))
img_source.select(get_coords, [], coords)
infer.click(
inference,
[
img_source,
coords,
model_choice
],
[
img_output
]
)
my_app.launch(debug=True)