Spaces:
Sleeping
Sleeping
Download app.py from nithinare/geospatial-intelligence: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/nithinare/geospatial-intelligence/resolve/main/app.py
- Command line
-
hf download hf://spaces/nithinare/geospatial-intelligence/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nithinare/geospatial-intelligence/resolve/main/app.py
1.22 kB
| import gradio as gr | |
| import torch | |
| import torchvision.transforms as transforms | |
| from PIL import Image | |
| import numpy as np | |
| import segmentation_models_pytorch as smp | |
| # DEVICE | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| # MODEL | |
| model = smp.UnetPlusPlus( | |
| encoder_name="resnet34", | |
| encoder_weights=None, | |
| in_channels=3, | |
| classes=1, | |
| activation=None | |
| ).to(device) | |
| model.load_state_dict( | |
| torch.load("buildings_unetpp_model.pth", map_location=device) | |
| ) | |
| model.eval() | |
| # TRANSFORM | |
| transform = transforms.Compose([ | |
| transforms.Resize((256, 256)), | |
| transforms.ToTensor() | |
| ]) | |
| # PREDICT | |
| def predict(image): | |
| original_size = image.size # save size | |
| x = transform(image).unsqueeze(0).to(device) | |
| with torch.no_grad(): | |
| pred = model(x)[0][0].cpu().numpy() | |
| mask = (pred > 0.5).astype(np.uint8) * 255 | |
| # resize back | |
| mask = Image.fromarray(mask).resize(original_size) | |
| return np.array(mask) | |
| # UI | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil"), # 🔥 FIX HERE | |
| outputs=gr.Image(type="numpy"), | |
| title="🏗️ Building Extraction (U-Net++)", | |
| description="Upload satellite/drone image to detect buildings" | |
| ) | |
| demo.launch(ssr_mode=False) |