Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from fastai.vision.all import *
|
| 4 |
+
import torch
|
| 5 |
+
import torchvision.transforms as transforms
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
repo_id = "hafsa101010/Practica3"
|
| 10 |
+
|
| 11 |
+
model_path = hf_hub_download(repo_id=repo_id, filename="unet.pth")
|
| 12 |
+
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
model = torch.jit.load(model_path, map_location=device)
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
def transform_image(image):
|
| 18 |
+
image = image.resize((640, 480)) # Asegurar tamaño correcto
|
| 19 |
+
my_transforms = transforms.Compose([
|
| 20 |
+
transforms.ToTensor(),
|
| 21 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 22 |
+
])
|
| 23 |
+
return my_transforms(image).unsqueeze(0).to(device)
|
| 24 |
+
|
| 25 |
+
def predict(img):
|
| 26 |
+
tensor = transform_image(img)
|
| 27 |
+
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(tensor)
|
| 30 |
+
|
| 31 |
+
outputs = torch.argmax(outputs, 1).cpu().numpy().squeeze()
|
| 32 |
+
|
| 33 |
+
mask = np.zeros_like(outputs, dtype=np.uint8)
|
| 34 |
+
mask[outputs == 1] = 255 # grape
|
| 35 |
+
mask[outputs == 2] = 150 # leaves
|
| 36 |
+
mask[outputs == 3] = 76 # pole
|
| 37 |
+
mask[outputs == 4] = 74 # pole
|
| 38 |
+
mask[outputs == 5] = 29 # wood
|
| 39 |
+
mask[outputs == 6] = 25 # wood
|
| 40 |
+
|
| 41 |
+
return Image.fromarray(mask)
|
| 42 |
+
|
| 43 |
+
gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs=gr.Image(type="pil"), examples=["color_181.jpg", "color_155.jpg"]).launch()
|