Spaces:
Configuration error
Configuration error
Use image url
Browse files- app.py +14 -5
- requirements.txt +2 -1
app.py
CHANGED
@@ -6,14 +6,25 @@ import torch
|
|
6 |
import typing
|
7 |
import os
|
8 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
def image_to_sam_image_embedding(
|
12 |
-
|
13 |
model_size: typing.Literal["base", "large", "huge"] = "base",
|
14 |
) -> str:
|
15 |
"""Generate an image embedding."""
|
16 |
|
|
|
|
|
|
|
|
|
17 |
# Select model size
|
18 |
if model_size == "base":
|
19 |
predictor = base_predictor
|
@@ -77,10 +88,8 @@ if __name__ == "__main__":
|
|
77 |
app = gr.Interface(
|
78 |
fn=image_to_sam_image_embedding,
|
79 |
inputs=[
|
80 |
-
gr.components.
|
81 |
-
gr.components.Radio(
|
82 |
-
choices=["base", "large", "huge"], label="Model Size", default="base"
|
83 |
-
),
|
84 |
],
|
85 |
outputs="text",
|
86 |
)
|
|
|
6 |
import typing
|
7 |
import os
|
8 |
import subprocess
|
9 |
+
import requests
|
10 |
+
import PIL
|
11 |
+
|
12 |
+
|
13 |
+
def download_image(url) -> PIL.Image.Image:
|
14 |
+
"""Download an image from a URL and return it as a PIL image."""
|
15 |
+
return PIL.Image.open(requests.get(url, stream=True).raw)
|
16 |
|
17 |
|
18 |
def image_to_sam_image_embedding(
|
19 |
+
image_url: str,
|
20 |
model_size: typing.Literal["base", "large", "huge"] = "base",
|
21 |
) -> str:
|
22 |
"""Generate an image embedding."""
|
23 |
|
24 |
+
image = download_image(image_url)
|
25 |
+
image = image.convert("RGB")
|
26 |
+
image = np.asarray(image)
|
27 |
+
|
28 |
# Select model size
|
29 |
if model_size == "base":
|
30 |
predictor = base_predictor
|
|
|
88 |
app = gr.Interface(
|
89 |
fn=image_to_sam_image_embedding,
|
90 |
inputs=[
|
91 |
+
gr.components.Textbox(label="Image URL"),
|
92 |
+
gr.components.Radio(choices=["base", "large", "huge"], label="Model Size"),
|
|
|
|
|
93 |
],
|
94 |
outputs="text",
|
95 |
)
|
requirements.txt
CHANGED
@@ -4,4 +4,5 @@ torchvision
|
|
4 |
git+https://github.com/facebookresearch/segment-anything.git
|
5 |
pycocotools
|
6 |
onnxruntime
|
7 |
-
onnx
|
|
|
|
4 |
git+https://github.com/facebookresearch/segment-anything.git
|
5 |
pycocotools
|
6 |
onnxruntime
|
7 |
+
onnx
|
8 |
+
Pillow
|