File size: 28,660 Bytes
506bbca
e25c008
506bbca
 
e25c008
 
 
 
 
 
 
506bbca
 
e25c008
 
 
 
731a3f9
e25c008
 
 
 
 
f283438
e25c008
f283438
e25c008
 
506bbca
e25c008
 
 
506bbca
e25c008
 
 
29555cd
 
 
 
0d2be74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29555cd
0d2be74
 
29555cd
0d2be74
29555cd
0d2be74
e25c008
 
9df906d
506bbca
9df906d
 
 
 
506bbca
 
9df906d
 
 
 
506bbca
 
9df906d
 
 
 
506bbca
 
9df906d
 
 
 
506bbca
 
9df906d
 
506bbca
 
 
 
 
9df906d
 
 
506bbca
 
 
 
9df906d
e25c008
506bbca
e25c008
 
506bbca
 
 
 
29555cd
 
 
506bbca
29555cd
506bbca
e25c008
506bbca
 
 
e25c008
f283438
 
 
 
506bbca
 
 
 
f283438
 
e25c008
 
 
 
f283438
 
 
 
 
 
 
 
 
 
 
e25c008
 
f283438
 
 
 
506bbca
f283438
 
 
 
9df906d
506bbca
f283438
506bbca
 
 
f283438
 
506bbca
 
e25c008
 
506bbca
 
 
e25c008
506bbca
 
 
 
 
 
 
 
 
 
 
e25c008
506bbca
 
 
e25c008
e019c2e
506bbca
 
29555cd
506bbca
29555cd
e25c008
 
 
506bbca
e25c008
 
506bbca
e25c008
506bbca
 
 
9df906d
 
 
 
 
 
 
 
 
 
506bbca
e25c008
506bbca
 
 
e25c008
506bbca
 
 
 
e25c008
 
506bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e25c008
506bbca
 
 
e25c008
506bbca
 
 
e25c008
 
506bbca
 
9df906d
 
 
506bbca
9df906d
506bbca
 
 
 
9df906d
 
506bbca
9df906d
506bbca
9df906d
506bbca
 
 
e25c008
 
506bbca
 
 
e25c008
506bbca
e25c008
506bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e25c008
 
506bbca
 
 
e25c008
f5a5bfe
 
 
 
 
 
 
 
 
 
 
 
 
506bbca
e25c008
 
506bbca
 
 
 
 
 
 
 
 
e25c008
506bbca
f5a5bfe
 
506bbca
e25c008
 
 
f5a5bfe
506bbca
f5a5bfe
 
 
 
 
 
 
 
 
 
 
 
 
506bbca
 
 
 
 
 
 
 
 
 
f5a5bfe
506bbca
f5a5bfe
506bbca
e25c008
 
 
 
 
 
 
506bbca
e25c008
 
 
 
9df906d
506bbca
9df906d
 
506bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9df906d
506bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9df906d
 
e25c008
506bbca
e25c008
 
f5a5bfe
b5067f4
506bbca
e25c008
 
 
 
 
506bbca
e25c008
506bbca
e25c008
506bbca
e25c008
 
506bbca
 
e25c008
506bbca
 
 
 
9df906d
506bbca
 
e25c008
 
b0d6057
 
e25c008
506bbca
 
 
 
 
e25c008
506bbca
 
 
 
9df906d
506bbca
 
e25c008
506bbca
f5a5bfe
 
 
 
506bbca
 
 
 
 
 
f5a5bfe
 
 
 
 
 
 
506bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e25c008
b5067f4
5a8f74f
 
b5067f4
 
 
 
 
5a8f74f
 
 
b5067f4
 
 
f5a5bfe
 
b5067f4
506bbca
5a8f74f
506bbca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e25c008
 
 
9df906d
e25c008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506bbca
 
e25c008
506bbca
e25c008
 
506bbca
 
e25c008
 
 
506bbca
 
e25c008
 
506bbca
e25c008
 
 
 
506bbca
 
e25c008
 
506bbca
e25c008
 
506bbca
 
 
e25c008
 
 
 
 
9df906d
e25c008
506bbca
 
e25c008
 
29555cd
 
 
 
 
 
 
 
 
 
 
 
e25c008
506bbca
e25c008
 
 
 
 
 
 
 
 
 
 
 
506bbca
e25c008
 
 
 
506bbca
9df906d
506bbca
 
 
 
 
9df906d
 
506bbca
 
 
 
9df906d
506bbca
667734c
 
506bbca
 
9df906d
e25c008
506bbca
e25c008
506bbca
 
 
e25c008
 
 
 
506bbca
 
e25c008
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
"""Janus — Feature Extraction via SAM 3 with Tiling + Live Dashboard.

Upload large GeoTIFFs, select feature types, and watch as SAM 3 processes
tile by tile with live progress and confidence tracking.
"""

from __future__ import annotations

import math
import os
import tempfile
import time
from collections import defaultdict
from pathlib import Path

import gradio as gr
import geopandas as gpd
import spaces
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import rasterio
import torch
from affine import Affine
from PIL import Image
from pyproj import CRS
from rasterio.features import shapes
from rasterio.plot import show
from rasterio.windows import Window
from shapely.geometry import shape

# ---------------------------------------------------------------------------
# Globals
# ---------------------------------------------------------------------------
MODEL = None
PROCESSOR = None


def get_device():
    """Detect GPU at runtime, not import time."""
    import subprocess
    print(f"[GPU DEBUG] torch.cuda.is_available() = {torch.cuda.is_available()}")
    print(f"[GPU DEBUG] torch.version.cuda = {torch.version.cuda}")
    print(f"[GPU DEBUG] torch.backends.cudnn.enabled = {torch.backends.cudnn.enabled}")
    try:
        print(f"[GPU DEBUG] torch.cuda.device_count() = {torch.cuda.device_count()}")
    except Exception as e:
        print(f"[GPU DEBUG] torch.cuda.device_count() ERROR: {e}")
    try:
        result = subprocess.run(["nvidia-smi"], capture_output=True, text=True, timeout=5)
        print(f"[GPU DEBUG] nvidia-smi output:\n{result.stdout[:500]}")
        if result.stderr:
            print(f"[GPU DEBUG] nvidia-smi stderr: {result.stderr[:200]}")
    except Exception as e:
        print(f"[GPU DEBUG] nvidia-smi ERROR: {e}")
    try:
        import os
        print(f"[GPU DEBUG] CUDA_VISIBLE_DEVICES = {os.environ.get('CUDA_VISIBLE_DEVICES', 'NOT SET')}")
        print(f"[GPU DEBUG] NVIDIA_VISIBLE_DEVICES = {os.environ.get('NVIDIA_VISIBLE_DEVICES', 'NOT SET')}")
    except Exception as e:
        print(f"[GPU DEBUG] env check ERROR: {e}")
    if torch.cuda.is_available():
        name = torch.cuda.get_device_name(0)
        print(f"[GPU DEBUG] Using GPU: {name}")
        return "cuda"
    print("[GPU DEBUG] Falling back to CPU")
    return "cpu"

matplotlib.use("Agg")

# ---------------------------------------------------------------------------
# Feature presets
# ---------------------------------------------------------------------------
FEATURE_PRESETS = {
    "Building": {
        "prompt": "building",
        "min_area": 20.0, "max_area": 50000.0,
        "min_compactness": 0.25, "min_rectangularity": 0.5,
        "color": "#10b981",
    },
    "Road": {
        "prompt": "road",
        "min_area": 10.0, "max_area": 500000.0,
        "min_compactness": 0.0, "min_rectangularity": 0.0,
        "color": "#f59e0b",
    },
    "Waterbody": {
        "prompt": "water",
        "min_area": 50.0, "max_area": 5000000.0,
        "min_compactness": 0.0, "min_rectangularity": 0.0,
        "color": "#3b82f6",
    },
    "Vegetation": {
        "prompt": "tree",
        "min_area": 30.0, "max_area": 5000000.0,
        "min_compactness": 0.0, "min_rectangularity": 0.0,
        "color": "#22c55e",
    },
    "Parking Lot": {
        "prompt": "parking lot",
        "min_area": 100.0, "max_area": 200000.0,
        "min_compactness": 0.2, "min_rectangularity": 0.4,
        "color": "#8b5cf6",
    },
}

WORLD_EXT_MAP = {
    ".png": ".pgw", ".jpg": ".jgw", ".jpeg": ".jgw",
    ".tif": ".tfw", ".tiff": ".tfw",
}

# ---------------------------------------------------------------------------
# Model
# ---------------------------------------------------------------------------

def load_model():
    global MODEL, PROCESSOR
    if MODEL is None:
        from transformers import Sam3Model, Sam3Processor
        device = get_device()
        print(f"[MODEL] Loading SAM 3 on {device}...")
        MODEL = Sam3Model.from_pretrained("facebook/sam3").to(device)
        PROCESSOR = Sam3Processor.from_pretrained("facebook/sam3")
        print(f"[MODEL] SAM 3 loaded on {device}")
    return MODEL, PROCESSOR

# ---------------------------------------------------------------------------
# Ingest
# ---------------------------------------------------------------------------

def _parse_world_file(world_path: str) -> Affine:
    lines = Path(world_path).read_text().strip().splitlines()
    if len(lines) < 6:
        raise gr.Error(f"World file must have 6 lines, got {len(lines)}")
    return Affine(
        float(lines[0]), float(lines[2]), float(lines[4]),
        float(lines[1]), float(lines[3]), float(lines[5]),
    )


def ingest(image_path: str, world_path: str | None, crs: str) -> str:
    tmp = tempfile.mkdtemp(prefix="janus_")
    geotiff = os.path.join(tmp, "input.tif")

    if image_path.lower().endswith((".tif", ".tiff")):
        try:
            with rasterio.open(image_path) as src:
                if src.crs is not None and src.transform != Affine.identity():
                    import shutil
                    shutil.copy2(image_path, geotiff)
                    return geotiff
        except Exception:
            pass

    if world_path is None:
        img_p = Path(image_path)
        expected_ext = WORLD_EXT_MAP.get(img_p.suffix.lower(), ".pgw")
        auto_wf = img_p.with_suffix(expected_ext)
        if auto_wf.exists():
            world_path = str(auto_wf)
        else:
            raise gr.Error(f"No world file found. Expected: {auto_wf.name}")

    transform = _parse_world_file(world_path)
    target_crs = CRS.from_user_input(crs)
    img = Image.open(image_path).convert("RGB")
    img_array = np.array(img)
    h, w, bands = img_array.shape
    profile = {
        "driver": "GTiff", "dtype": "uint8",
        "width": w, "height": h, "count": bands,
        "crs": target_crs, "transform": transform,
    }
    with rasterio.open(geotiff, "w", **profile) as dst:
        for b in range(bands):
            dst.write(img_array[:, :, b], b + 1)
    return geotiff

# ---------------------------------------------------------------------------
# Tiling
# ---------------------------------------------------------------------------

def compute_tile_windows(img_w: int, img_h: int, tile_size: int, overlap: int) -> list[Window]:
    step = tile_size - overlap
    windows = []
    for y in range(0, img_h, step):
        for x in range(0, img_w, step):
            tw = min(tile_size, img_w - x)
            th = min(tile_size, img_h - y)
            if tw < tile_size // 4 or th < tile_size // 4:
                continue
            windows.append(Window(x, y, tw, th))
    return windows

# ---------------------------------------------------------------------------
# Per-tile segmentation
# ---------------------------------------------------------------------------

@spaces.GPU
def segment_tile(tile_rgb: np.ndarray, prompt: str, confidence: float):
    model, processor = load_model()
    device = get_device()
    image = Image.fromarray(tile_rgb)
    inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    results = processor.post_process_instance_segmentation(
        outputs, threshold=confidence, mask_threshold=0.5,
        target_sizes=inputs.get("original_sizes").tolist(),
    )[0]
    return results["masks"].cpu().numpy(), results["scores"].cpu().numpy()

# ---------------------------------------------------------------------------
# Shape metrics
# ---------------------------------------------------------------------------

def _compactness(geom):
    if geom.is_empty or geom.length == 0:
        return 0.0
    return (4.0 * math.pi * geom.area) / (geom.length ** 2)

def _rectangularity(geom):
    if geom.is_empty:
        return 0.0
    mrr = geom.minimum_rotated_rectangle
    return geom.area / mrr.area if mrr.area > 0 else 0.0

# ---------------------------------------------------------------------------
# Dedup + filter
# ---------------------------------------------------------------------------

def merge_and_deduplicate(features: list[dict], crs_obj, iou_thresh: float = 0.5) -> gpd.GeoDataFrame:
    if not features:
        return gpd.GeoDataFrame(columns=["feature_id", "feature_type", "confidence", "geometry"])
    gdf = gpd.GeoDataFrame(features, crs=crs_obj)
    if len(gdf) == 0:
        return gdf
    sindex = gdf.sindex
    drop = set()
    for idx, row in gdf.iterrows():
        if idx in drop:
            continue
        for cand_idx in sindex.intersection(row.geometry.bounds):
            if cand_idx <= idx or cand_idx in drop:
                continue
            cand = gdf.loc[cand_idx]
            if row["feature_type"] != cand["feature_type"]:
                continue
            if not row.geometry.intersects(cand.geometry):
                continue
            try:
                inter = row.geometry.intersection(cand.geometry).area
                union = row.geometry.union(cand.geometry).area
                if union > 0 and inter / union >= iou_thresh:
                    if row["confidence"] >= cand["confidence"]:
                        drop.add(cand_idx)
                    else:
                        drop.add(idx)
                        break
            except Exception:
                continue
    gdf = gdf.drop(index=drop).reset_index(drop=True)
    gdf["feature_id"] = range(1, len(gdf) + 1)
    return gdf


def filter_features(gdf: gpd.GeoDataFrame, preset: dict) -> gpd.GeoDataFrame:
    if len(gdf) == 0:
        return gdf
    if gdf.crs and gdf.crs.is_geographic:
        proj = gdf.to_crs(gdf.estimate_utm_crs())
        gdf["area_m2"] = proj.geometry.area
        pg = proj.geometry
    else:
        gdf["area_m2"] = gdf.geometry.area
        pg = gdf.geometry
    gdf = gdf[(gdf["area_m2"] >= preset["min_area"]) & (gdf["area_m2"] <= preset["max_area"])].copy()
    if len(gdf) == 0:
        return gdf
    if gdf.crs and gdf.crs.is_geographic:
        pg = gdf.to_crs(gdf.estimate_utm_crs()).geometry
    else:
        pg = gdf.geometry
    gdf["compactness"] = pg.apply(_compactness)
    if preset["min_compactness"] > 0:
        gdf = gdf[gdf["compactness"] >= preset["min_compactness"]].copy()
    if len(gdf) == 0:
        return gdf
    if preset["min_rectangularity"] > 0:
        if gdf.crs and gdf.crs.is_geographic:
            pg = gdf.to_crs(gdf.estimate_utm_crs()).geometry
        else:
            pg = gdf.geometry
        gdf["rectangularity"] = pg.apply(_rectangularity)
        gdf = gdf[gdf["rectangularity"] >= preset["min_rectangularity"]].copy()
    return gdf

# ---------------------------------------------------------------------------
# Export
# ---------------------------------------------------------------------------

def export_all(features_by_type: dict[str, gpd.GeoDataFrame], tmp_dir: str) -> list[str]:
    paths = []
    for feat_type, gdf in features_by_type.items():
        if len(gdf) == 0:
            continue
        name = feat_type.lower().replace(" ", "_")
        gpkg = os.path.join(tmp_dir, f"{name}.gpkg")
        gdf.to_file(gpkg, driver="GPKG")
        paths.append(gpkg)
        gj = os.path.join(tmp_dir, f"{name}.geojson")
        gdf.to_file(gj, driver="GeoJSON")
        paths.append(gj)
        wkt = os.path.join(tmp_dir, f"{name}.wkt")
        with open(wkt, "w") as f:
            for idx, row in enumerate(gdf.itertuples(), start=1):
                c = getattr(row, "confidence", 0.0)
                f.write(f"{idx}|{c:.3f}|{row.geometry.wkt}\n")
        paths.append(wkt)
    return paths

# ---------------------------------------------------------------------------
# Overlay
# ---------------------------------------------------------------------------

def make_overlay(
    geotiff: str,
    features_by_type: dict[str, gpd.GeoDataFrame],
    scanned_bounds: tuple | None = None,
) -> str:
    """Render before/after overlay with optional scan-progress box.

    Args:
        geotiff: Path to the source GeoTIFF.
        features_by_type: Dict of feature_type -> GeoDataFrame.
        scanned_bounds: (left, bottom, right, top) in CRS coordinates showing
                        the area processed so far. Drawn as a dashed box.
    """
    fig, axes = plt.subplots(1, 2, figsize=(16, 7), dpi=100)
    fig.patch.set_facecolor("#fafafa")
    with rasterio.open(geotiff) as src:
        max_dim = 2000
        scale = min(1.0, max_dim / max(src.width, src.height))
        out_w = int(src.width * scale)
        out_h = int(src.height * scale)
        data = src.read(
            out_shape=(src.count, out_h, out_w),
            resampling=rasterio.enums.Resampling.bilinear,
        )
        extent = [src.bounds.left, src.bounds.right, src.bounds.bottom, src.bounds.top]

    rgb = np.transpose(data[:3], (1, 2, 0))

    # Left panel: input image
    axes[0].imshow(rgb, extent=extent)
    axes[0].set_title("Input", fontsize=14, fontweight=600, color="#18181b", pad=12)
    axes[0].tick_params(labelsize=7, colors="#71717a")

    # Right panel: image + detected features
    axes[1].imshow(rgb, extent=extent)

    # Draw scan progress box
    if scanned_bounds is not None:
        from matplotlib.patches import Rectangle
        left, bottom, right, top = scanned_bounds
        rect = Rectangle(
            (left, bottom), right - left, top - bottom,
            linewidth=1.5, edgecolor="#71717a", facecolor="none",
            linestyle="--", alpha=0.6,
        )
        axes[1].add_patch(rect)

    # Plot features color-coded by type
    total = 0
    legend_items = []
    for feat_type, gdf in features_by_type.items():
        if len(gdf) == 0:
            continue
        color = FEATURE_PRESETS.get(feat_type, {}).get("color", "#ef4444")
        gdf.plot(ax=axes[1], edgecolor=color, facecolor=color, alpha=0.35, linewidth=1)
        total += len(gdf)
        legend_items.append(f"{feat_type}: {len(gdf)}")

    title = f"{total} Features"
    if legend_items:
        title += f"  ({', '.join(legend_items)})"
    axes[1].set_title(title, fontsize=12, fontweight=600, color="#18181b", pad=12)
    axes[1].tick_params(labelsize=7, colors="#71717a")

    for ax in axes:
        for spine in ax.spines.values():
            spine.set_color("#e4e4e7")

    plt.tight_layout(pad=2)
    out = os.path.join(os.path.dirname(geotiff), "overlay.png")
    plt.savefig(out, bbox_inches="tight", facecolor="#fafafa")
    plt.close()
    return out

# ---------------------------------------------------------------------------
# Dashboard formatting
# ---------------------------------------------------------------------------

class ConfidenceTracker:
    """Track running confidence stats per feature type."""
    def __init__(self):
        self.scores: dict[str, list[float]] = defaultdict(list)

    def add(self, feat_type: str, score: float):
        self.scores[feat_type].append(score)

    def count(self, feat_type: str) -> int:
        return len(self.scores[feat_type])

    def total(self) -> int:
        return sum(len(v) for v in self.scores.values())

    def stats(self, feat_type: str) -> dict:
        s = self.scores[feat_type]
        if not s:
            return {"count": 0, "avg": 0, "min": 0, "max": 0, "low": 0}
        return {
            "count": len(s),
            "avg": sum(s) / len(s),
            "min": min(s),
            "max": max(s),
            "low": sum(1 for x in s if x < 0.5),
        }


def format_dashboard(
    image_name: str,
    img_w: int,
    img_h: int,
    tile_idx: int,
    total_tiles: int,
    elapsed: float,
    tracker: ConfidenceTracker,
    feature_types: list[str],
    status: str = "Processing",
) -> str:
    pct = (tile_idx / total_tiles * 100) if total_tiles > 0 else 0
    elapsed_str = _fmt_time(elapsed)

    if tile_idx > 0 and tile_idx < total_tiles:
        rate = elapsed / tile_idx
        remaining = rate * (total_tiles - tile_idx)
        remaining_str = _fmt_time(remaining)
    else:
        remaining_str = "--"

    md = f"**{status}: {image_name}** ({img_w:,} x {img_h:,} px)\n\n"
    md += f"Tile {tile_idx} / {total_tiles} | {pct:.0f}% | "
    md += f"Elapsed: {elapsed_str} | Remaining: ~{remaining_str}\n\n"

    md += "| Feature | Count | Avg Conf | Min Conf | Max Conf | Low (<0.5) |\n"
    md += "|---------|-------|----------|----------|----------|------------|\n"

    for ft in feature_types:
        st = tracker.stats(ft)
        if st["count"] > 0:
            md += (
                f"| {ft} | {st['count']} | {st['avg']:.2f} | "
                f"{st['min']:.2f} | {st['max']:.2f} | {st['low']} |\n"
            )
        else:
            md += f"| {ft} | 0 | -- | -- | -- | -- |\n"

    return md


def format_final_summary(
    tracker: ConfidenceTracker,
    feature_types: list[str],
    final_counts: dict[str, int],
    elapsed: float,
) -> str:
    md = f"**Extraction Complete** | Total time: {_fmt_time(elapsed)}\n\n"

    md += "| Feature | Raw | Final | Avg Conf | Min | Max | Low (<0.5) |\n"
    md += "|---------|-----|-------|----------|-----|-----|------------|\n"

    for ft in feature_types:
        st = tracker.stats(ft)
        final = final_counts.get(ft, 0)
        if st["count"] > 0:
            md += (
                f"| {ft} | {st['count']} | {final} | "
                f"{st['avg']:.2f} | {st['min']:.2f} | {st['max']:.2f} | {st['low']} |\n"
            )
        else:
            md += f"| {ft} | 0 | 0 | -- | -- | -- | -- |\n"

    md += "\n*Adjust the confidence threshold and re-run to improve results.*"
    return md


def _fmt_time(secs: float) -> str:
    if secs < 60:
        return f"{secs:.0f}s"
    m, s = divmod(int(secs), 60)
    return f"{m}m {s:02d}s"


# ---------------------------------------------------------------------------
# Main pipeline (generator for live updates)
# ---------------------------------------------------------------------------

PREVIEW_INTERVAL = 10  # update overlay every N tiles
CHECKPOINT_INTERVAL = 100  # save downloadable files every N tiles


def run_pipeline(
    image_file,
    world_file,
    crs: str,
    feature_types: list[str],
    confidence: float,
    tile_size: int,
):
    """Generator: yields (overlay_image, dashboard_md, files) after each tile."""
    if image_file is None:
        raise gr.Error("Please upload an aerial image.")
    if not feature_types:
        raise gr.Error("Select at least one feature type.")

    start_time = time.time()
    tracker = ConfidenceTracker()
    all_features: list[dict] = []
    tmp_dir = tempfile.mkdtemp(prefix="janus_")

    # -- Ingest --
    yield None, "**Ingesting image...**", None
    geotiff = ingest(image_file, world_file, crs)

    # Model loads on first segment_tile call (inside @spaces.GPU context)
    yield None, "**Starting extraction...** (model loads on first tile)", None

    # -- Compute tiles --
    with rasterio.open(geotiff) as src:
        img_w, img_h = src.width, src.height
        crs_obj = src.crs
        transform = src.transform

    image_name = Path(image_file).name
    overlap = tile_size // 8
    windows = compute_tile_windows(img_w, img_h, tile_size, overlap)
    total_tiles = len(windows)

    dash = format_dashboard(image_name, img_w, img_h, 0, total_tiles, 0, tracker, feature_types, "Starting")
    yield None, dash, None

    # -- Process tiles --
    # Track the scanned region for progress visualization
    scan_left = scan_bottom = float("inf")
    scan_right = scan_top = float("-inf")

    with rasterio.open(geotiff) as src:
        for tile_idx, window in enumerate(windows):
            tile_data = src.read([1, 2, 3], window=window)
            tile_rgb = np.transpose(tile_data, (1, 2, 0))
            tile_transform = rasterio.windows.transform(window, transform)

            # Update scanned bounds
            tile_bounds = rasterio.windows.bounds(window, transform)
            scan_left = min(scan_left, tile_bounds[0])
            scan_bottom = min(scan_bottom, tile_bounds[1])
            scan_right = max(scan_right, tile_bounds[2])
            scan_top = max(scan_top, tile_bounds[3])

            for ft in feature_types:
                preset = FEATURE_PRESETS[ft]
                masks, scores = segment_tile(tile_rgb, preset["prompt"], confidence)

                if len(masks) == 0:
                    continue

                labeled = np.zeros((tile_rgb.shape[0], tile_rgb.shape[1]), dtype=np.int32)
                for i, mask in enumerate(masks):
                    labeled[mask > 0] = i + 1

                for geom, val in shapes(labeled, mask=(labeled > 0), transform=tile_transform):
                    val = int(val)
                    score = float(scores[val - 1]) if val - 1 < len(scores) else 0.0
                    tracker.add(ft, score)
                    all_features.append({
                        "geometry": shape(geom),
                        "feature_type": ft,
                        "confidence": round(score, 3),
                    })

            elapsed = time.time() - start_time
            dash = format_dashboard(
                image_name, img_w, img_h,
                tile_idx + 1, total_tiles,
                elapsed, tracker, feature_types,
            )

            # Checkpoint: save downloadable files every N tiles
            # Update overlay + save files every PREVIEW_INTERVAL tiles
            if (tile_idx + 1) % PREVIEW_INTERVAL == 0 or tile_idx == total_tiles - 1 or tile_idx == 0:
                temp_by_type = {}
                for ft in feature_types:
                    ft_feats = [f for f in all_features if f["feature_type"] == ft]
                    if ft_feats:
                        temp_by_type[ft] = gpd.GeoDataFrame(ft_feats, crs=crs_obj)

                # Save files every update so they're always downloadable
                checkpoint_files = None
                if temp_by_type:
                    checkpoint_files = export_all(temp_by_type, tmp_dir)

                scanned = (scan_left, scan_bottom, scan_right, scan_top)
                overlay = make_overlay(geotiff, temp_by_type, scanned_bounds=scanned)
                yield overlay, dash, checkpoint_files
            else:
                yield gr.update(), dash, gr.update()

    # -- Deduplicate + filter --
    elapsed = time.time() - start_time
    yield gr.update(), format_dashboard(
        image_name, img_w, img_h, total_tiles, total_tiles,
        elapsed, tracker, feature_types, "Deduplicating & filtering",
    ), None

    final_by_type: dict[str, gpd.GeoDataFrame] = {}
    final_counts: dict[str, int] = {}

    for ft in feature_types:
        ft_feats = [f for f in all_features if f["feature_type"] == ft]
        if not ft_feats:
            final_counts[ft] = 0
            continue
        gdf = merge_and_deduplicate(ft_feats, crs_obj)
        gdf = filter_features(gdf, FEATURE_PRESETS[ft])
        final_by_type[ft] = gdf
        final_counts[ft] = len(gdf)

    # -- Export --
    export_paths = export_all(final_by_type, tmp_dir)

    # -- Final overlay --
    final_overlay = make_overlay(geotiff, final_by_type)

    elapsed = time.time() - start_time
    final_dash = format_final_summary(tracker, feature_types, final_counts, elapsed)

    yield final_overlay, final_dash, export_paths


# ---------------------------------------------------------------------------
# Gradio UI
# ---------------------------------------------------------------------------

CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
    --body-background-fill: #fafafa !important;
    --block-background-fill: #ffffff !important;
    --block-border-color: #e4e4e7 !important;
    --block-label-text-color: #3f3f46 !important;
    --block-title-text-color: #18181b !important;
    --button-primary-background-fill: #18181b !important;
    --button-primary-text-color: #fafafa !important;
    --button-primary-background-fill-hover: #27272a !important;
    --input-background-fill: #ffffff !important;
    --border-color-primary: #e4e4e7 !important;
}

* { font-family: 'Outfit', system-ui, -apple-system, sans-serif !important; }
code, pre, .code, [class*="mono"] { font-family: 'JetBrains Mono', monospace !important; }

.gradio-container { max-width: 1400px !important; margin: 0 auto !important; background: #fafafa !important; }

.gr-button-primary {
    border-radius: 12px !important; font-weight: 600 !important;
    letter-spacing: -0.01em !important; padding: 12px 32px !important;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1) !important;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08) !important;
}
.gr-button-primary:hover { transform: translateY(-1px) !important; box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important; }
.gr-button-primary:active { transform: translateY(0) scale(0.98) !important; }

.gr-panel, .gr-box, .gr-form {
    border-radius: 20px !important; border: 1px solid #e4e4e7 !important;
    box-shadow: 0 20px 40px -15px rgba(0,0,0,0.04) !important;
}

.gr-input, .gr-textbox textarea, select {
    border-radius: 12px !important; border: 1px solid #e4e4e7 !important;
    font-size: 14px !important; transition: border-color 0.2s ease !important;
}
.gr-input:focus, .gr-textbox textarea:focus {
    border-color: #18181b !important; box-shadow: 0 0 0 3px rgba(24,24,27,0.06) !important;
}

h1 { font-size: 2.25rem !important; font-weight: 700 !important; letter-spacing: -0.03em !important; line-height: 1.1 !important; color: #18181b !important; }
h2, h3, .gr-block-label { font-weight: 600 !important; letter-spacing: -0.02em !important; color: #3f3f46 !important; }
.markdown-text p { color: #52525b !important; line-height: 1.6 !important; max-width: 65ch !important; }
footer { display: none !important; }
"""

HEADER_MD = """
# Janus
### GIS-ready feature extraction from aerial imagery

Upload a GeoTIFF (any size), select feature types to extract, and watch SAM 3 process
tile by tile with live confidence tracking.
"""

def _gpu_status():
    try:
        if torch.cuda.is_available():
            return (
                f"Running on **{torch.cuda.get_device_name(0)}** "
                f"({torch.cuda.get_device_properties(0).total_memory / (1024**3):.0f} GB)"
            )
    except Exception:
        pass
    return "No GPU detected — inference will be slow"

gpu_status = _gpu_status()

with gr.Blocks(css=CUSTOM_CSS, title="Janus — Feature Extraction") as demo:
    gr.Markdown(HEADER_MD)
    gr.Markdown(f"*{gpu_status}*")

    with gr.Row(equal_height=False):
        with gr.Column(scale=3):
            with gr.Group():
                image_input = gr.File(
                    label="Aerial Image",
                    file_types=[".png", ".jpg", ".jpeg", ".tif", ".tiff"],
                    type="filepath",
                )
                world_input = gr.File(
                    label="World File (optional for GeoTIFF)",
                    file_types=[".pgw", ".jgw", ".tfw"],
                    type="filepath",
                )

            crs_input = gr.Textbox(value="EPSG:4326", label="CRS")

            feature_checks = gr.CheckboxGroup(
                choices=list(FEATURE_PRESETS.keys()),
                value=["Building"],
                label="Feature Types",
                info="Select one or more feature types to extract",
            )

            with gr.Row():
                confidence_slider = gr.Slider(
                    minimum=0.1, maximum=0.95, value=0.5, step=0.05,
                    label="Confidence Threshold",
                )
                tile_size_dropdown = gr.Dropdown(
                    choices=[256, 512, 1024, 2048],
                    value=256,
                    label="Tile Size (px)",
                    info="Larger = fewer tiles but more GPU memory",
                )

            run_btn = gr.Button("Extract Features", variant="primary", size="lg")

        with gr.Column(scale=5):
            dashboard = gr.Markdown(value="*Upload an image and click Extract Features to begin.*")
            output_image = gr.Image(label="Live Preview", type="filepath", show_download_button=True)
            file_output = gr.Files(label="Download GIS Files")

    run_btn.click(
        fn=run_pipeline,
        inputs=[image_input, world_input, crs_input, feature_checks, confidence_slider, tile_size_dropdown],
        outputs=[output_image, dashboard, file_output],
    )

if __name__ == "__main__":
    demo.launch()