Spaces:
Sleeping
Sleeping
Commit
·
acd1613
1
Parent(s):
68d1156
Test1
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_fastai
|
2 |
+
import gradio as gr
|
3 |
+
from fastai.vision.all import *
|
4 |
+
|
5 |
+
import PIL
|
6 |
+
import torchvision.transforms as transforms
|
7 |
+
def transform_image(image):
|
8 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
9 |
+
transforms.Normalize(
|
10 |
+
[0.485, 0.456, 0.406],
|
11 |
+
[0.229, 0.224, 0.225])])
|
12 |
+
image_aux = image
|
13 |
+
return my_transforms(image_aux).unsqueeze(0).to(device)
|
14 |
+
|
15 |
+
repo_id = "Ignaciobfp/segmentacion-dron-marras"
|
16 |
+
|
17 |
+
learner = from_pretrained_fastai(repo_id)
|
18 |
+
|
19 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
20 |
+
model = learner.model
|
21 |
+
model = model.cpu()
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
# Definimos una función que se encarga de llevar a cabo las predicciones
|
26 |
+
def predict(img):
|
27 |
+
#img = PILImage.create(img)
|
28 |
+
image = transforms.Resize((400,400))(img)
|
29 |
+
tensor = transform_image(image=image)
|
30 |
+
model.to(device)
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model(tensor)
|
33 |
+
outputs = torch.argmax(outputs,1)
|
34 |
+
mask = np.array(outputs.cpu())
|
35 |
+
mask[mask==1]=255
|
36 |
+
mask=np.reshape(mask,(400,400))
|
37 |
+
return mask
|
38 |
+
|
39 |
+
# Creamos la interfaz y la lanzamos.
|
40 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(400, 400)), outputs="image").launch(share=False)
|
41 |
+
|