Spaces:
Configuration error
Configuration error
segments-arnaud
commited on
Commit
•
9974436
1
Parent(s):
a4654d8
Add gradio app
Browse files
app.py
CHANGED
@@ -1,7 +1,86 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
import segment_anything
|
4 |
+
import imutils
|
5 |
+
import numpy as np
|
6 |
+
import base64
|
7 |
+
import torch
|
8 |
+
import typing
|
9 |
+
import os
|
10 |
+
import subprocess
|
11 |
|
|
|
|
|
12 |
|
13 |
+
def image_to_sam_image_embedding(
|
14 |
+
image_url: str,
|
15 |
+
model_size: typing.Literal["base", "large", "huge"] = "base",
|
16 |
+
) -> str:
|
17 |
+
"""Generate an image embedding."""
|
18 |
+
# Load image
|
19 |
+
image = imutils.url_to_image(image_url)
|
20 |
+
|
21 |
+
# Select model size
|
22 |
+
if model_size == "base":
|
23 |
+
predictor = base_predictor
|
24 |
+
elif model_size == "large":
|
25 |
+
predictor = large_predictor
|
26 |
+
elif model_size == "huge":
|
27 |
+
predictor = huge_predictor
|
28 |
+
|
29 |
+
# Run model
|
30 |
+
predictor.set_image(image)
|
31 |
+
# Output shape is (1, 256, 64, 64)
|
32 |
+
image_embedding = predictor.get_image_embedding().cpu().numpy()
|
33 |
+
|
34 |
+
# Flatten the array to a 1D array
|
35 |
+
flat_arr = image_embedding.flatten()
|
36 |
+
# Convert the 1D array to bytes
|
37 |
+
bytes_arr = flat_arr.astype(np.float32).tobytes()
|
38 |
+
# Encode the bytes to base64
|
39 |
+
base64_str = base64.b64encode(bytes_arr).decode("utf-8")
|
40 |
+
|
41 |
+
return base64_str
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
|
46 |
+
# Load the model into memory to make running multiple predictions efficient
|
47 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
48 |
+
|
49 |
+
base_sam_checkpoint = "sam_vit_b_01ec64.pth" # 375 MB
|
50 |
+
large_sam_checkpoint = "sam_vit_l_0b3195.pth" # 1.25 GB
|
51 |
+
huge_sam_checkpoint = "sam_vit_h_4b8939.pth" # 2.56 GB
|
52 |
+
|
53 |
+
# Download the model checkpoints
|
54 |
+
for model in [base_sam_checkpoint, large_sam_checkpoint, huge_sam_checkpoint]:
|
55 |
+
if not os.path.exists(f"./{model}"):
|
56 |
+
result = subprocess.run(
|
57 |
+
["wget", f"https://dl.fbaipublicfiles.com/segment_anything/{model}"],
|
58 |
+
check=True,
|
59 |
+
)
|
60 |
+
print(f"wget {model} result = {result}")
|
61 |
+
|
62 |
+
base_sam = segment_anything.sam_model_registry["vit_b"](
|
63 |
+
checkpoint=base_sam_checkpoint
|
64 |
+
)
|
65 |
+
large_sam = segment_anything.sam_model_registry["vit_l"](
|
66 |
+
checkpoint=large_sam_checkpoint
|
67 |
+
)
|
68 |
+
huge_sam = segment_anything.sam_model_registry["vit_h"](
|
69 |
+
checkpoint=huge_sam_checkpoint
|
70 |
+
)
|
71 |
+
|
72 |
+
base_sam.to(device=device)
|
73 |
+
large_sam.to(device=device)
|
74 |
+
huge_sam.to(device=device)
|
75 |
+
|
76 |
+
base_predictor = segment_anything.SamPredictor(base_sam)
|
77 |
+
large_predictor = segment_anything.SamPredictor(large_sam)
|
78 |
+
huge_predictor = segment_anything.SamPredictor(huge_sam)
|
79 |
+
|
80 |
+
# Gradio app
|
81 |
+
app = gr.Interface(
|
82 |
+
fn=image_to_sam_image_embedding,
|
83 |
+
inputs="text",
|
84 |
+
outputs="text",
|
85 |
+
)
|
86 |
+
app.launch()
|