Spaces:
Running on Zero
Running on Zero
space-doctor run 20260506T132411Z-acca0806
Browse files
app.py
CHANGED
|
@@ -6,10 +6,9 @@ from transformers import SamModel, SamProcessor
|
|
| 6 |
from gradio_image_prompter import ImagePrompter
|
| 7 |
import spaces
|
| 8 |
|
| 9 |
-
|
| 10 |
-
sam_model = SamModel.from_pretrained("facebook/sam-vit-huge").to("cuda")
|
| 11 |
sam_processor = SamProcessor.from_pretrained("facebook/sam-vit-huge")
|
| 12 |
-
slimsam_model = SamModel.from_pretrained("nielsr/slimsam-50-uniform")
|
| 13 |
slimsam_processor = SamProcessor.from_pretrained("nielsr/slimsam-50-uniform")
|
| 14 |
|
| 15 |
def get_processor_and_model(slim: bool):
|
|
@@ -21,12 +20,13 @@ def get_processor_and_model(slim: bool):
|
|
| 21 |
def sam_box_inference(image, x_min, y_min, x_max, y_max, *, slim=False):
|
| 22 |
|
| 23 |
processor, model = get_processor_and_model(slim)
|
| 24 |
-
|
|
|
|
| 25 |
inputs = processor(
|
| 26 |
Image.fromarray(image),
|
| 27 |
input_boxes=[[[[x_min, y_min, x_max, y_max]]]],
|
| 28 |
return_tensors="pt"
|
| 29 |
-
).to(
|
| 30 |
|
| 31 |
with torch.no_grad():
|
| 32 |
outputs = model(**inputs)
|
|
@@ -45,11 +45,12 @@ def sam_box_inference(image, x_min, y_min, x_max, y_max, *, slim=False):
|
|
| 45 |
def sam_point_inference(image, x, y, *, slim=False):
|
| 46 |
|
| 47 |
processor, model = get_processor_and_model(slim)
|
| 48 |
-
|
|
|
|
| 49 |
inputs = processor(
|
| 50 |
image,
|
| 51 |
input_points=[[[x, y]]],
|
| 52 |
-
return_tensors="pt").to(
|
| 53 |
|
| 54 |
with torch.no_grad():
|
| 55 |
outputs = model(**inputs)
|
|
@@ -88,7 +89,10 @@ def infer_point(img):
|
|
| 88 |
|
| 89 |
def infer_box(prompts):
|
| 90 |
# background (original image) layers[0] ( point prompt) composite (total image)
|
| 91 |
-
image =
|
|
|
|
|
|
|
|
|
|
| 92 |
if image is None:
|
| 93 |
raise gr.Error("Please upload an image and draw a box before submitting")
|
| 94 |
points = prompts["points"][0]
|
|
@@ -135,4 +139,4 @@ with gr.Blocks(title="SlimSAM") as demo:
|
|
| 135 |
output_sam = gr.AnnotatedImage(label="SAM Output")
|
| 136 |
|
| 137 |
im.change(infer_point, inputs=im, outputs=[output_slimsam, output_sam])
|
| 138 |
-
demo.launch(
|
|
|
|
| 6 |
from gradio_image_prompter import ImagePrompter
|
| 7 |
import spaces
|
| 8 |
|
| 9 |
+
sam_model = SamModel.from_pretrained("facebook/sam-vit-huge")
|
|
|
|
| 10 |
sam_processor = SamProcessor.from_pretrained("facebook/sam-vit-huge")
|
| 11 |
+
slimsam_model = SamModel.from_pretrained("nielsr/slimsam-50-uniform")
|
| 12 |
slimsam_processor = SamProcessor.from_pretrained("nielsr/slimsam-50-uniform")
|
| 13 |
|
| 14 |
def get_processor_and_model(slim: bool):
|
|
|
|
| 20 |
def sam_box_inference(image, x_min, y_min, x_max, y_max, *, slim=False):
|
| 21 |
|
| 22 |
processor, model = get_processor_and_model(slim)
|
| 23 |
+
model.to("cuda")
|
| 24 |
+
|
| 25 |
inputs = processor(
|
| 26 |
Image.fromarray(image),
|
| 27 |
input_boxes=[[[[x_min, y_min, x_max, y_max]]]],
|
| 28 |
return_tensors="pt"
|
| 29 |
+
).to("cuda")
|
| 30 |
|
| 31 |
with torch.no_grad():
|
| 32 |
outputs = model(**inputs)
|
|
|
|
| 45 |
def sam_point_inference(image, x, y, *, slim=False):
|
| 46 |
|
| 47 |
processor, model = get_processor_and_model(slim)
|
| 48 |
+
model.to("cuda")
|
| 49 |
+
|
| 50 |
inputs = processor(
|
| 51 |
image,
|
| 52 |
input_points=[[[x, y]]],
|
| 53 |
+
return_tensors="pt").to("cuda")
|
| 54 |
|
| 55 |
with torch.no_grad():
|
| 56 |
outputs = model(**inputs)
|
|
|
|
| 89 |
|
| 90 |
def infer_box(prompts):
|
| 91 |
# background (original image) layers[0] ( point prompt) composite (total image)
|
| 92 |
+
image = None
|
| 93 |
+
if prompts is None:
|
| 94 |
+
raise gr.Error("Please upload an image and draw a box before submitting")
|
| 95 |
+
image = prompts.get("image")
|
| 96 |
if image is None:
|
| 97 |
raise gr.Error("Please upload an image and draw a box before submitting")
|
| 98 |
points = prompts["points"][0]
|
|
|
|
| 139 |
output_sam = gr.AnnotatedImage(label="SAM Output")
|
| 140 |
|
| 141 |
im.change(infer_point, inputs=im, outputs=[output_slimsam, output_sam])
|
| 142 |
+
demo.launch()
|