Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,37 @@
|
|
1 |
import torch
|
2 |
-
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
-
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
model =
|
10 |
|
11 |
-
#
|
12 |
-
def
|
13 |
-
#
|
14 |
-
|
|
|
15 |
|
16 |
-
#
|
17 |
with torch.no_grad():
|
18 |
-
|
19 |
-
|
20 |
-
# فرض میکنیم خروجی یک ماسک است (مثل کلاسبندی پیکسلها)
|
21 |
-
segmentation = outputs.logits.argmax(dim=1).detach().cpu().numpy()[0]
|
22 |
|
23 |
-
#
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
fn=
|
29 |
inputs=gr.Image(type="pil"),
|
30 |
outputs=gr.Image(type="pil"),
|
31 |
-
title="Sapiens Body Part Segmentation"
|
|
|
32 |
)
|
33 |
|
34 |
-
#
|
35 |
-
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
+
from io import BytesIO
|
6 |
|
7 |
+
# Load the model
|
8 |
+
model_url = "https://huggingface.co/facebook/sapiens/resolve/main/sapiens_lite_host/torchscript/normal/checkpoints/sapiens_0.3b/sapiens_0.3b.pt"
|
9 |
+
model = torch.jit.load(model_url, map_location=torch.device('cpu'))
|
10 |
|
11 |
+
# Define inference function
|
12 |
+
def predict(image):
|
13 |
+
# Preprocess image
|
14 |
+
image = image.convert("RGB")
|
15 |
+
input_tensor = torch.from_numpy(np.array(image)).permute(2, 0, 1).unsqueeze(0).float() / 255.0
|
16 |
|
17 |
+
# Run model
|
18 |
with torch.no_grad():
|
19 |
+
output = model(input_tensor)
|
|
|
|
|
|
|
20 |
|
21 |
+
# Postprocess output
|
22 |
+
output_image = output.squeeze().permute(1, 2, 0).numpy()
|
23 |
+
output_image = (output_image * 255).astype(np.uint8)
|
24 |
+
return Image.fromarray(output_image)
|
25 |
|
26 |
+
# Gradio Interface
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=predict,
|
29 |
inputs=gr.Image(type="pil"),
|
30 |
outputs=gr.Image(type="pil"),
|
31 |
+
title="Sapiens Body Part Segmentation",
|
32 |
+
description="Upload an image to segment body parts using the Sapiens model."
|
33 |
)
|
34 |
|
35 |
+
# Launch the interface
|
36 |
+
if __name__ == "__main__":
|
37 |
+
iface.launch()
|