File size: 1,953 Bytes
d874003
e90699a
 
 
def7ce4
e90699a
d874003
 
e90699a
d874003
 
d4236e1
 
e90699a
d874003
 
e90699a
a403959
d874003
 
a403959
54f7336
a403959
d874003
e90699a
d874003
 
e90699a
d874003
 
def7ce4
a403959
 
 
 
 
 
 
e90699a
d874003
e90699a
a403959
e90699a
a403959
 
 
e90699a
def7ce4
a403959
def7ce4
 
d874003
 
def7ce4
 
d874003
 
 
 
 
 
 
def7ce4
e90699a
 
d874003
e90699a
a403959
d874003
e90699a
 
d874003
e90699a
d874003
 
e90699a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import gradio as gr
import torch
import numpy as np
import cv2
from PIL import Image
import rasterio
from rasterio.transform import xy
from model_arch import get_model

# ---------- Load Model ----------
MODEL_PATH = os.path.join(os.path.dirname(__file__), "marida_final_model.pth")
model = get_model(MODEL_PATH)

# ---------- Prediction Function ----------
def predict(file):

    # Read GeoTIFF
    with rasterio.open(file.name) as src:
        transform = src.transform
        img = src.read()  # (bands, H, W)

    # Use first 11 bands
    img = img[:11].astype(np.float32)

    # Normalize
    img = img / (np.max(img) + 1e-8)

    # Convert to tensor
    img_tensor = torch.tensor(img).unsqueeze(0)

    # ---- PAD to multiple of 32 ----
    _, c, h, w = img_tensor.shape
    pad_h = (32 - h % 32) % 32
    pad_w = (32 - w % 32) % 32
    img_tensor = torch.nn.functional.pad(img_tensor, (0, pad_w, 0, pad_h))

    # Model inference
    with torch.no_grad():
        y = model(img_tensor)

    # Remove padding from output
    mask = y.squeeze().numpy()
    mask = mask[:h, :w]

    # Threshold
    mask = (mask > 0.5).astype(np.uint8) * 255

    # Find regions
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    geo_boxes = []

    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        lon1, lat1 = xy(transform, y, x)
        lon2, lat2 = xy(transform, y + h, x + w)

        geo_boxes.append({
            "top_left": (lat1, lon1),
            "bottom_right": (lat2, lon2)
        })

    mask_img = Image.fromarray(mask)

    return mask_img, str(geo_boxes)


# ---------- Gradio Interface ----------
demo = gr.Interface(
    fn=predict,
    inputs=gr.File(label="Upload Satellite GeoTIFF"),
    outputs=["image", "text"],
    title="Satellite Plastic Detection System",
    description="Upload a multi-band GeoTIFF to detect plastic and get map coordinates"
)

demo.launch()