Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 6 |
|
| 7 |
-
# Baixa pesos do modelo
|
| 8 |
repo_id = "vncgabriel/instance-segmentation-model"
|
| 9 |
model_file = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
|
| 10 |
|
| 11 |
-
# Carrega o modelo UNet
|
| 12 |
-
from __main__ import UNet
|
| 13 |
model = UNet()
|
| 14 |
-
|
| 15 |
-
model.load_state_dict(
|
| 16 |
model.eval()
|
| 17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
model.to(device)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
image_resized = image.resize((512, 512))
|
| 24 |
-
tensor = torch.from_numpy((np.array(image_resized)/255.0).transpose(2,0,1)).unsqueeze(0).to(device).float()
|
| 25 |
with torch.no_grad():
|
| 26 |
-
|
| 27 |
-
mask = (
|
| 28 |
mask_img = Image.fromarray((mask*255).astype(np.uint8), mode="L").resize(image.size)
|
| 29 |
overlay = Image.new("RGBA", image.size, (255,0,0,80))
|
| 30 |
base = image.convert("RGBA")
|
| 31 |
base.paste(overlay, mask=mask_img)
|
| 32 |
return base.convert("RGB")
|
| 33 |
|
| 34 |
-
# Interface Gradio
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=segment,
|
| 37 |
inputs=gr.Image(type="pil"),
|
|
@@ -39,5 +34,4 @@ iface = gr.Interface(
|
|
| 39 |
title="Instance Segmentation Demo",
|
| 40 |
allow_flagging="never"
|
| 41 |
)
|
| 42 |
-
|
| 43 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
+
from model import UNet
|
| 7 |
|
|
|
|
| 8 |
repo_id = "vncgabriel/instance-segmentation-model"
|
| 9 |
model_file = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
|
| 10 |
|
|
|
|
|
|
|
| 11 |
model = UNet()
|
| 12 |
+
state = torch.load(model_file, map_location="cpu")
|
| 13 |
+
model.load_state_dict(state)
|
| 14 |
model.eval()
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
model.to(device)
|
| 17 |
|
| 18 |
+
def segment(image):
|
| 19 |
+
img = image.resize((512, 512))
|
| 20 |
+
tensor = torch.from_numpy((np.array(img)/255.0).transpose(2,0,1)).unsqueeze(0).to(device).float()
|
|
|
|
|
|
|
| 21 |
with torch.no_grad():
|
| 22 |
+
out = model(tensor)
|
| 23 |
+
mask = (out > 0.5).squeeze().cpu().numpy()
|
| 24 |
mask_img = Image.fromarray((mask*255).astype(np.uint8), mode="L").resize(image.size)
|
| 25 |
overlay = Image.new("RGBA", image.size, (255,0,0,80))
|
| 26 |
base = image.convert("RGBA")
|
| 27 |
base.paste(overlay, mask=mask_img)
|
| 28 |
return base.convert("RGB")
|
| 29 |
|
|
|
|
| 30 |
iface = gr.Interface(
|
| 31 |
fn=segment,
|
| 32 |
inputs=gr.Image(type="pil"),
|
|
|
|
| 34 |
title="Instance Segmentation Demo",
|
| 35 |
allow_flagging="never"
|
| 36 |
)
|
| 37 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|