vncgabriel commited on
Commit
f3acb1c
·
verified ·
1 Parent(s): 4b12641

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -12
app.py CHANGED
@@ -6,42 +6,34 @@ from inference import load_model, predict
6
  from PIL import Image
7
  import numpy as np
8
 
9
- # 1. Baixa o arquivo .bin do seu repositório de modelo (público)
10
  model_path = hf_hub_download(
11
  repo_id="vncgabriel/instancia-segmentation-model",
12
- filename="pytorch_model.bin", # nome exato enviado ao Hub
13
  repo_type="model",
14
  )
15
 
16
- # 2. Carrega o modelo na CPU ou GPU
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  model = load_model(model_path, device=device)
19
 
20
- # 3. Função de predição para o Gradio
21
  def segment(image: Image.Image):
22
  img_arr = np.array(image).astype(np.float32) / 255.0
23
  img_tensor = torch.from_numpy(img_arr).permute(2, 0, 1).to(device)
24
-
25
  mask = predict(model, img_tensor).cpu().numpy()
26
  bin_mask = (mask > 0.5).astype(np.uint8) * 255
27
-
28
  overlay = image.convert("RGBA")
29
  mask_img = Image.fromarray(bin_mask).convert("L")
30
  red = Image.new("RGBA", image.size, color=(255, 0, 0, 100))
31
  overlay.paste(red, mask=mask_img)
32
  return overlay
33
 
34
- # 4. Define a interface Gradio
35
  iface = gr.Interface(
36
  fn=segment,
37
  inputs=gr.Image(type="pil"),
38
  outputs=gr.Image(type="pil"),
39
  title="Segmentação de Instâncias",
40
- description="Envie uma imagem e obtenha a segmentação de instâncias usando UNet pré-treinado.",
41
- examples=[
42
- ["example1.jpg"],
43
- ["example2.jpg"],
44
- ],
45
  )
46
 
47
  if __name__ == "__main__":
 
6
  from PIL import Image
7
  import numpy as np
8
 
9
+ # Baixa o peso .bin do Model Hub
10
  model_path = hf_hub_download(
11
  repo_id="vncgabriel/instancia-segmentation-model",
12
+ filename="pytorch_model.bin",
13
  repo_type="model",
14
  )
15
 
16
+ # Carrega o modelo
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  model = load_model(model_path, device=device)
19
 
 
20
  def segment(image: Image.Image):
21
  img_arr = np.array(image).astype(np.float32) / 255.0
22
  img_tensor = torch.from_numpy(img_arr).permute(2, 0, 1).to(device)
 
23
  mask = predict(model, img_tensor).cpu().numpy()
24
  bin_mask = (mask > 0.5).astype(np.uint8) * 255
 
25
  overlay = image.convert("RGBA")
26
  mask_img = Image.fromarray(bin_mask).convert("L")
27
  red = Image.new("RGBA", image.size, color=(255, 0, 0, 100))
28
  overlay.paste(red, mask=mask_img)
29
  return overlay
30
 
 
31
  iface = gr.Interface(
32
  fn=segment,
33
  inputs=gr.Image(type="pil"),
34
  outputs=gr.Image(type="pil"),
35
  title="Segmentação de Instâncias",
36
+ description="Envie uma imagem e obtenha a segmentação de instâncias usando UNet pré-treinado."
 
 
 
 
37
  )
38
 
39
  if __name__ == "__main__":