Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,701 +1,765 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
import
|
| 12 |
-
|
| 13 |
-
import
|
| 14 |
-
import
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import
|
| 18 |
-
from
|
| 19 |
-
import
|
| 20 |
-
import
|
| 21 |
-
from
|
| 22 |
-
import
|
| 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 |
-
def
|
| 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 |
-
def
|
| 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 |
-
return
|
| 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 |
)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SONAR 2.0 - Archaeological Site Detection
|
| 3 |
+
Beautiful Interactive Interface with Full AOI Visualization
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import matplotlib
|
| 10 |
+
matplotlib.use('Agg')
|
| 11 |
+
import plotly.graph_objects as go
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
import torch
|
| 14 |
+
import io
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import warnings
|
| 17 |
+
import zipfile
|
| 18 |
+
from scipy.ndimage import sobel, gaussian_filter
|
| 19 |
+
from matplotlib import cm
|
| 20 |
+
import rasterio
|
| 21 |
+
from rasterio.transform import rowcol
|
| 22 |
+
import folium
|
| 23 |
+
import base64
|
| 24 |
+
warnings.filterwarnings('ignore')
|
| 25 |
+
|
| 26 |
+
from utils import (
|
| 27 |
+
ResUNetAutoencoder, ResUNetEncoder, load_patches,
|
| 28 |
+
load_model, load_kmeans_model, load_unified_probability_matrix
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# ==============================================================================
|
| 32 |
+
# CONFIGURATION
|
| 33 |
+
# ==============================================================================
|
| 34 |
+
|
| 35 |
+
class Config:
|
| 36 |
+
AUTOENCODER_PATH = Path('models/best_model_aoi.pth')
|
| 37 |
+
ENCODER_DIM = 128
|
| 38 |
+
IFOREST_PATH = Path('models/isolation_forest_model_128dim.pkl')
|
| 39 |
+
KMEANS_PATH = Path('models/kmeans_model_128dim.pkl')
|
| 40 |
+
GATE_MODEL_PKL = Path('models/gate_mlp_model.pkl')
|
| 41 |
+
GATE_SCALER_PATH = Path('models/gate_scaler.pkl')
|
| 42 |
+
|
| 43 |
+
DATA_BASE = Path('Test_dataset')
|
| 44 |
+
PATCHES_DIR = Path('patches_final_file')
|
| 45 |
+
UNIFIED_PROB_DIR = Path('test_unified_probablity_matrices_with_gate')
|
| 46 |
+
|
| 47 |
+
MODELS_ZIP = Path('models.zip')
|
| 48 |
+
PATCHES_ZIP = Path('patches_final_file.zip')
|
| 49 |
+
UNIFIED_PROB_ZIP = Path('test_unified_probablity_matrices_with_gate.zip')
|
| 50 |
+
DATASET_ZIP = Path('Test_dataset.zip')
|
| 51 |
+
|
| 52 |
+
BATCH_SIZE = 32
|
| 53 |
+
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 54 |
+
|
| 55 |
+
@staticmethod
|
| 56 |
+
def extract_data_files():
|
| 57 |
+
print("\n" + "="*50)
|
| 58 |
+
print("Starting SONAR 2.0...")
|
| 59 |
+
print("="*50)
|
| 60 |
+
|
| 61 |
+
if Config.MODELS_ZIP.exists() and (not Path('models').exists() or not any(Path('models').iterdir())):
|
| 62 |
+
print("Extracting models...")
|
| 63 |
+
with zipfile.ZipFile(Config.MODELS_ZIP, 'r') as zip_ref:
|
| 64 |
+
zip_ref.extractall('.')
|
| 65 |
+
|
| 66 |
+
if Config.PATCHES_ZIP.exists() and (not Config.PATCHES_DIR.exists() or not any(Config.PATCHES_DIR.iterdir())):
|
| 67 |
+
print("Extracting patches...")
|
| 68 |
+
with zipfile.ZipFile(Config.PATCHES_ZIP, 'r') as zip_ref:
|
| 69 |
+
zip_ref.extractall('.')
|
| 70 |
+
|
| 71 |
+
if Config.UNIFIED_PROB_ZIP.exists() and (not Config.UNIFIED_PROB_DIR.exists() or not any(Config.UNIFIED_PROB_DIR.iterdir())):
|
| 72 |
+
print("Extracting probability matrices...")
|
| 73 |
+
with zipfile.ZipFile(Config.UNIFIED_PROB_ZIP, 'r') as zip_ref:
|
| 74 |
+
zip_ref.extractall('.')
|
| 75 |
+
|
| 76 |
+
if Config.DATASET_ZIP.exists() and not Config.DATA_BASE.exists():
|
| 77 |
+
print("Extracting Test_dataset.zip...")
|
| 78 |
+
with zipfile.ZipFile(Config.DATASET_ZIP, 'r') as zip_ref:
|
| 79 |
+
zip_ref.extractall('.')
|
| 80 |
+
print("β Test dataset extracted")
|
| 81 |
+
|
| 82 |
+
print("Ready!\n")
|
| 83 |
+
|
| 84 |
+
config = Config()
|
| 85 |
+
|
| 86 |
+
# ==============================================================================
|
| 87 |
+
# DATA MANAGER
|
| 88 |
+
# ==============================================================================
|
| 89 |
+
|
| 90 |
+
class DataManager:
|
| 91 |
+
def __init__(self):
|
| 92 |
+
self.aoi_list = []
|
| 93 |
+
self.current_aoi = None
|
| 94 |
+
self.current_patches = None
|
| 95 |
+
self.current_metadata = None
|
| 96 |
+
self.current_unified_matrix = None
|
| 97 |
+
self.full_dtm = None
|
| 98 |
+
self.reference_transform = None
|
| 99 |
+
self.reference_shape = None
|
| 100 |
+
self.reference_bounds = None
|
| 101 |
+
self.reference_crs = None
|
| 102 |
+
|
| 103 |
+
def discover_aois(self):
|
| 104 |
+
if not config.PATCHES_DIR.exists():
|
| 105 |
+
return []
|
| 106 |
+
patch_files = list(config.PATCHES_DIR.glob("AOI_*_all_patches.npz"))
|
| 107 |
+
aoi_names = sorted([f.stem.replace('_all_patches', '') for f in patch_files])
|
| 108 |
+
self.aoi_list = aoi_names
|
| 109 |
+
return aoi_names
|
| 110 |
+
|
| 111 |
+
def load_original_raster(self, aoi_name: str):
|
| 112 |
+
"""Load original DTM from Test_dataset"""
|
| 113 |
+
try:
|
| 114 |
+
meta_dir = config.DATA_BASE / aoi_name / 'meta'
|
| 115 |
+
if not meta_dir.exists():
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
tif_files = list(meta_dir.glob('*.tif'))
|
| 119 |
+
if not tif_files:
|
| 120 |
+
return None
|
| 121 |
+
|
| 122 |
+
dtm_path = tif_files[0]
|
| 123 |
+
print(f"β Loading DTM: {dtm_path.name}")
|
| 124 |
+
|
| 125 |
+
with rasterio.open(dtm_path) as src:
|
| 126 |
+
dtm = src.read(1).astype(np.float32)
|
| 127 |
+
if src.nodata is not None:
|
| 128 |
+
dtm[dtm == src.nodata] = np.nan
|
| 129 |
+
|
| 130 |
+
self.reference_transform = src.transform
|
| 131 |
+
self.reference_shape = (src.height, src.width)
|
| 132 |
+
self.reference_bounds = src.bounds
|
| 133 |
+
self.reference_crs = src.crs
|
| 134 |
+
|
| 135 |
+
return dtm
|
| 136 |
+
except Exception as e:
|
| 137 |
+
print(f"Error loading raster: {e}")
|
| 138 |
+
return None
|
| 139 |
+
|
| 140 |
+
def load_aoi(self, aoi_name: str):
|
| 141 |
+
try:
|
| 142 |
+
patches_file = config.PATCHES_DIR / f"{aoi_name}_all_patches.npz"
|
| 143 |
+
self.current_patches, self.current_metadata = load_patches(patches_file)
|
| 144 |
+
|
| 145 |
+
matrix_file = config.UNIFIED_PROB_DIR / f"{aoi_name}_unified_prob_matrix.npz"
|
| 146 |
+
if matrix_file.exists():
|
| 147 |
+
self.current_unified_matrix, _, _ = load_unified_probability_matrix(
|
| 148 |
+
aoi_name, config.UNIFIED_PROB_DIR
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
self.full_dtm = self.load_original_raster(aoi_name)
|
| 152 |
+
|
| 153 |
+
if self.full_dtm is not None:
|
| 154 |
+
max_row = max(m['row'] + 64 for m in self.current_metadata)
|
| 155 |
+
max_col = max(m['col'] + 64 for m in self.current_metadata)
|
| 156 |
+
dtm_height, dtm_width = self.full_dtm.shape
|
| 157 |
+
|
| 158 |
+
if max_row > dtm_height or max_col > dtm_width:
|
| 159 |
+
print(f"β οΈ Patch bounds exceed raster, falling back to reconstruction")
|
| 160 |
+
self.full_dtm = None
|
| 161 |
+
|
| 162 |
+
self.current_aoi = aoi_name
|
| 163 |
+
return f"β
{aoi_name} loaded ({len(self.current_patches)} patches)"
|
| 164 |
+
except Exception as e:
|
| 165 |
+
return f"β Error: {e}"
|
| 166 |
+
|
| 167 |
+
def get_patch(self, patch_idx: int):
|
| 168 |
+
if self.current_patches is None or patch_idx >= len(self.current_patches):
|
| 169 |
+
return None, None
|
| 170 |
+
return self.current_patches[patch_idx], self.current_metadata[patch_idx]
|
| 171 |
+
|
| 172 |
+
def find_patch_at_pixel(self, row: int, col: int):
|
| 173 |
+
for idx, meta in enumerate(self.current_metadata):
|
| 174 |
+
patch_row, patch_col = meta['row'], meta['col']
|
| 175 |
+
if (patch_row <= row < patch_row + 64 and patch_col <= col < patch_col + 64):
|
| 176 |
+
return idx
|
| 177 |
+
return None
|
| 178 |
+
|
| 179 |
+
data_manager = DataManager()
|
| 180 |
+
|
| 181 |
+
# ==============================================================================
|
| 182 |
+
# MAP GENERATION WITH MULTIPLE OVERLAYS (LIKE STREAMLIT VERSION)
|
| 183 |
+
# ==============================================================================
|
| 184 |
+
|
| 185 |
+
def create_interactive_map(aoi_name, threshold=0.5):
|
| 186 |
+
"""
|
| 187 |
+
Create beautiful Folium map with multiple terrain overlays
|
| 188 |
+
Matches the Streamlit visualization quality
|
| 189 |
+
"""
|
| 190 |
+
|
| 191 |
+
if data_manager.full_dtm is None or data_manager.reference_bounds is None:
|
| 192 |
+
# Fallback: create a simple map showing patches without terrain overlays
|
| 193 |
+
if data_manager.current_patches is None:
|
| 194 |
+
return "<div style='padding: 20px; text-align: center;'>β οΈ Load an AOI first</div>"
|
| 195 |
+
|
| 196 |
+
# Calculate approximate center from patches
|
| 197 |
+
if data_manager.current_metadata:
|
| 198 |
+
rows = [m['row'] for m in data_manager.current_metadata]
|
| 199 |
+
cols = [m['col'] for m in data_manager.current_metadata]
|
| 200 |
+
center_row = (min(rows) + max(rows)) / 2
|
| 201 |
+
center_col = (min(cols) + max(cols)) / 2
|
| 202 |
+
|
| 203 |
+
# Use approximate lat/lon (this is a fallback)
|
| 204 |
+
center_lat, center_lon = 0, 0 # Will be set properly below
|
| 205 |
+
|
| 206 |
+
m = folium.Map(
|
| 207 |
+
location=[center_lat, center_lon],
|
| 208 |
+
zoom_start=14,
|
| 209 |
+
tiles='OpenStreetMap'
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
# Add patch markers
|
| 213 |
+
if data_manager.current_unified_matrix is not None:
|
| 214 |
+
gate_channel = data_manager.current_unified_matrix[:, :, :, 4]
|
| 215 |
+
|
| 216 |
+
for idx, meta in enumerate(data_manager.current_metadata):
|
| 217 |
+
patch_score = np.mean(gate_channel[idx])
|
| 218 |
+
|
| 219 |
+
if patch_score >= threshold:
|
| 220 |
+
# For fallback, just use patch indices as approximate locations
|
| 221 |
+
popup_html = f"""
|
| 222 |
+
<b>π΄ Anomaly Detected</b><br>
|
| 223 |
+
<hr>
|
| 224 |
+
<b>Score:</b> {patch_score:.3f}<br>
|
| 225 |
+
<b>Patch ID:</b> {idx}<br>
|
| 226 |
+
<hr>
|
| 227 |
+
<i>β οΈ DTM not available - using patch coordinates</i>
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
# This is approximate - will work better with actual DTM
|
| 231 |
+
lat_approx = meta['row'] / 1000.0
|
| 232 |
+
lon_approx = meta['col'] / 1000.0
|
| 233 |
+
|
| 234 |
+
folium.CircleMarker(
|
| 235 |
+
location=[lat_approx, lon_approx],
|
| 236 |
+
radius=8,
|
| 237 |
+
popup=folium.Popup(popup_html, max_width=300),
|
| 238 |
+
tooltip=f"Patch {idx} - Score: {patch_score:.3f}",
|
| 239 |
+
color='red',
|
| 240 |
+
fill=True,
|
| 241 |
+
fillColor='orange',
|
| 242 |
+
fillOpacity=0.7,
|
| 243 |
+
weight=2
|
| 244 |
+
).add_to(m)
|
| 245 |
+
|
| 246 |
+
folium.LayerControl().add_to(m)
|
| 247 |
+
|
| 248 |
+
return m._repr_html_()
|
| 249 |
+
|
| 250 |
+
return "<div style='padding: 20px; text-align: center; background: #fff3cd; border-radius: 8px;'>β οΈ No DTM data available for this AOI. Map visualization limited.</div>"
|
| 251 |
+
|
| 252 |
+
bounds = data_manager.reference_bounds
|
| 253 |
+
center_lat = (bounds.bottom + bounds.top) / 2
|
| 254 |
+
center_lon = (bounds.left + bounds.right) / 2
|
| 255 |
+
|
| 256 |
+
# Create base map
|
| 257 |
+
m = folium.Map(
|
| 258 |
+
location=[center_lat, center_lon],
|
| 259 |
+
zoom_start=14,
|
| 260 |
+
tiles='OpenStreetMap'
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
# Add center marker
|
| 264 |
+
folium.Marker(
|
| 265 |
+
location=[center_lat, center_lon],
|
| 266 |
+
popup=f'AOI Center<br>Lat: {center_lat:.6f}<br>Lon: {center_lon:.6f}',
|
| 267 |
+
tooltip='AOI Center',
|
| 268 |
+
icon=folium.Icon(color='blue', icon='info-sign')
|
| 269 |
+
).add_to(m)
|
| 270 |
+
|
| 271 |
+
# Generate terrain overlays
|
| 272 |
+
dtm = data_manager.full_dtm
|
| 273 |
+
valid_mask = ~np.isnan(dtm)
|
| 274 |
+
|
| 275 |
+
if valid_mask.any():
|
| 276 |
+
dtm_filled = dtm.copy()
|
| 277 |
+
dtm_filled[~valid_mask] = np.nanmedian(dtm)
|
| 278 |
+
|
| 279 |
+
# ============================================================
|
| 280 |
+
# LAYER 1: Local Relief Model (ARCHAEOLOGICAL GOLD!)
|
| 281 |
+
# ============================================================
|
| 282 |
+
dtm_smooth = gaussian_filter(dtm_filled, sigma=10)
|
| 283 |
+
local_relief = dtm_filled - dtm_smooth
|
| 284 |
+
|
| 285 |
+
relief_clipped = np.clip(local_relief, -2, 2)
|
| 286 |
+
relief_norm = (relief_clipped + 2) / 4
|
| 287 |
+
|
| 288 |
+
rdbu_cmap = cm.get_cmap('RdBu_r')
|
| 289 |
+
relief_rgba = rdbu_cmap(relief_norm)
|
| 290 |
+
relief_rgb = (relief_rgba[:, :, :3] * 255).astype(np.uint8)
|
| 291 |
+
relief_rgb[~valid_mask] = [128, 128, 128]
|
| 292 |
+
|
| 293 |
+
img_relief = Image.fromarray(relief_rgb, mode='RGB')
|
| 294 |
+
buffered = io.BytesIO()
|
| 295 |
+
img_relief.save(buffered, format="PNG")
|
| 296 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 297 |
+
|
| 298 |
+
folium.raster_layers.ImageOverlay(
|
| 299 |
+
image=f'data:image/png;base64,{img_str}',
|
| 300 |
+
bounds=[[bounds.bottom, bounds.left], [bounds.top, bounds.right]],
|
| 301 |
+
opacity=0.75,
|
| 302 |
+
name='ποΈ Local Relief Model (Archaeological)',
|
| 303 |
+
overlay=True,
|
| 304 |
+
control=True,
|
| 305 |
+
show=True # DEFAULT ON
|
| 306 |
+
).add_to(m)
|
| 307 |
+
|
| 308 |
+
# ============================================================
|
| 309 |
+
# LAYER 2: Multi-Directional Hillshade
|
| 310 |
+
# ============================================================
|
| 311 |
+
dx = sobel(dtm_filled, axis=1) / 8.0
|
| 312 |
+
dy = sobel(dtm_filled, axis=0) / 8.0
|
| 313 |
+
slope = np.arctan(np.sqrt(dx**2 + dy**2))
|
| 314 |
+
aspect = np.arctan2(-dy, dx)
|
| 315 |
+
|
| 316 |
+
azimuths = [315, 45, 225, 135]
|
| 317 |
+
altitude = 45
|
| 318 |
+
hillshades = []
|
| 319 |
+
|
| 320 |
+
for az_deg in azimuths:
|
| 321 |
+
azimuth = np.radians(az_deg)
|
| 322 |
+
alt_rad = np.radians(altitude)
|
| 323 |
+
hs = (np.sin(alt_rad) * np.sin(slope) +
|
| 324 |
+
np.cos(alt_rad) * np.cos(slope) *
|
| 325 |
+
np.cos(azimuth - aspect))
|
| 326 |
+
hillshades.append(hs)
|
| 327 |
+
|
| 328 |
+
hillshade_multi = np.mean(hillshades, axis=0)
|
| 329 |
+
hillshade_multi = np.clip(hillshade_multi, -1, 1)
|
| 330 |
+
hillshade_multi = ((hillshade_multi + 1) / 2 * 255).astype(np.uint8)
|
| 331 |
+
hillshade_multi[~valid_mask] = 128
|
| 332 |
+
|
| 333 |
+
hillshade_multi_rgb = np.stack([hillshade_multi, hillshade_multi, hillshade_multi], axis=-1)
|
| 334 |
+
|
| 335 |
+
# Add color tinting
|
| 336 |
+
dtm_norm = (dtm - np.nanpercentile(dtm[valid_mask], 2)) / \
|
| 337 |
+
(np.nanpercentile(dtm[valid_mask], 98) -
|
| 338 |
+
np.nanpercentile(dtm[valid_mask], 2))
|
| 339 |
+
dtm_norm = np.clip(dtm_norm, 0, 1)
|
| 340 |
+
|
| 341 |
+
terrain_cmap = cm.get_cmap('terrain')
|
| 342 |
+
terrain_rgba = terrain_cmap(dtm_norm)
|
| 343 |
+
terrain_rgb = (terrain_rgba[:, :, :3] * 255).astype(np.uint8)
|
| 344 |
+
|
| 345 |
+
hillshade_multi_rgb = (hillshade_multi_rgb * 0.75 + terrain_rgb * 0.25).astype(np.uint8)
|
| 346 |
+
hillshade_multi_rgb[~valid_mask] = [128, 128, 128]
|
| 347 |
+
|
| 348 |
+
img_multi = Image.fromarray(hillshade_multi_rgb, mode='RGB')
|
| 349 |
+
buffered_multi = io.BytesIO()
|
| 350 |
+
img_multi.save(buffered_multi, format="PNG")
|
| 351 |
+
img_str_multi = base64.b64encode(buffered_multi.getvalue()).decode()
|
| 352 |
+
|
| 353 |
+
folium.raster_layers.ImageOverlay(
|
| 354 |
+
image=f'data:image/png;base64,{img_str_multi}',
|
| 355 |
+
bounds=[[bounds.bottom, bounds.left], [bounds.top, bounds.right]],
|
| 356 |
+
opacity=0.7,
|
| 357 |
+
name='π» Multi-Directional Hillshade',
|
| 358 |
+
overlay=True,
|
| 359 |
+
control=True,
|
| 360 |
+
show=False
|
| 361 |
+
).add_to(m)
|
| 362 |
+
|
| 363 |
+
# ============================================================
|
| 364 |
+
# LAYER 3: Standard Terrain
|
| 365 |
+
# ============================================================
|
| 366 |
+
dtm_norm_basic = (dtm - np.nanpercentile(dtm[valid_mask], 2)) / \
|
| 367 |
+
(np.nanpercentile(dtm[valid_mask], 98) -
|
| 368 |
+
np.nanpercentile(dtm[valid_mask], 2))
|
| 369 |
+
dtm_norm_basic = np.clip(dtm_norm_basic, 0, 1)
|
| 370 |
+
|
| 371 |
+
terrain_basic_rgba = terrain_cmap(dtm_norm_basic)
|
| 372 |
+
terrain_basic_rgb = (terrain_basic_rgba[:, :, :3] * 255).astype(np.uint8)
|
| 373 |
+
terrain_basic_rgb[~valid_mask] = [128, 128, 128]
|
| 374 |
+
|
| 375 |
+
img_basic = Image.fromarray(terrain_basic_rgb, mode='RGB')
|
| 376 |
+
buffered_basic = io.BytesIO()
|
| 377 |
+
img_basic.save(buffered_basic, format="PNG")
|
| 378 |
+
img_str_basic = base64.b64encode(buffered_basic.getvalue()).decode()
|
| 379 |
+
|
| 380 |
+
folium.raster_layers.ImageOverlay(
|
| 381 |
+
image=f'data:image/png;base64,{img_str_basic}',
|
| 382 |
+
bounds=[[bounds.bottom, bounds.left], [bounds.top, bounds.right]],
|
| 383 |
+
opacity=0.65,
|
| 384 |
+
name='π Standard Terrain',
|
| 385 |
+
overlay=True,
|
| 386 |
+
control=True,
|
| 387 |
+
show=False
|
| 388 |
+
).add_to(m)
|
| 389 |
+
|
| 390 |
+
# Add anomaly markers
|
| 391 |
+
if data_manager.current_unified_matrix is not None:
|
| 392 |
+
gate_channel = data_manager.current_unified_matrix[:, :, :, 4]
|
| 393 |
+
|
| 394 |
+
for idx, meta in enumerate(data_manager.current_metadata):
|
| 395 |
+
patch_score = np.mean(gate_channel[idx])
|
| 396 |
+
|
| 397 |
+
if patch_score >= threshold:
|
| 398 |
+
row, col = meta['row'], meta['col']
|
| 399 |
+
center_row = row + 32
|
| 400 |
+
center_col = col + 32
|
| 401 |
+
x, y = rasterio.transform.xy(data_manager.reference_transform, center_row, center_col)
|
| 402 |
+
|
| 403 |
+
if data_manager.reference_crs != 'EPSG:4326':
|
| 404 |
+
from rasterio.warp import transform as transform_coords
|
| 405 |
+
lon, lat = transform_coords(data_manager.reference_crs, 'EPSG:4326', [x], [y])
|
| 406 |
+
lat, lon = lat[0], lon[0]
|
| 407 |
+
else:
|
| 408 |
+
lat, lon = y, x
|
| 409 |
+
|
| 410 |
+
popup_html = f"""
|
| 411 |
+
<b>π΄ Anomaly Detected</b><br>
|
| 412 |
+
<hr>
|
| 413 |
+
<b>Score:</b> {patch_score:.3f}<br>
|
| 414 |
+
<b>Patch ID:</b> {idx}<br>
|
| 415 |
+
<b>Location:</b><br>
|
| 416 |
+
Lat: {lat:.6f}<br>
|
| 417 |
+
Lon: {lon:.6f}<br>
|
| 418 |
+
<hr>
|
| 419 |
+
<i>Click map at this location to inspect</i>
|
| 420 |
+
"""
|
| 421 |
+
|
| 422 |
+
folium.CircleMarker(
|
| 423 |
+
location=[lat, lon],
|
| 424 |
+
radius=8,
|
| 425 |
+
popup=folium.Popup(popup_html, max_width=300),
|
| 426 |
+
tooltip=f"Anomaly Score: {patch_score:.3f}",
|
| 427 |
+
color='red',
|
| 428 |
+
fill=True,
|
| 429 |
+
fillColor='orange',
|
| 430 |
+
fillOpacity=0.7,
|
| 431 |
+
weight=2
|
| 432 |
+
).add_to(m)
|
| 433 |
+
|
| 434 |
+
# Add boundary
|
| 435 |
+
folium.Rectangle(
|
| 436 |
+
bounds=[[bounds.bottom, bounds.left], [bounds.top, bounds.right]],
|
| 437 |
+
color='red',
|
| 438 |
+
fill=False,
|
| 439 |
+
weight=2,
|
| 440 |
+
popup=f'{aoi_name} boundary'
|
| 441 |
+
).add_to(m)
|
| 442 |
+
|
| 443 |
+
folium.LayerControl().add_to(m)
|
| 444 |
+
|
| 445 |
+
return m._repr_html_()
|
| 446 |
+
|
| 447 |
+
# ==============================================================================
|
| 448 |
+
# PATCH VISUALIZATION
|
| 449 |
+
# ==============================================================================
|
| 450 |
+
|
| 451 |
+
def create_patch_viz(patch: np.ndarray, metadata: dict):
|
| 452 |
+
"""Beautiful 2D patch visualization"""
|
| 453 |
+
channel_names = ['DTM', 'Slope', 'Roughness', 'NDVI', 'NDWI', 'Flow Acc', 'Flow Dir']
|
| 454 |
+
|
| 455 |
+
fig, axes = plt.subplots(2, 4, figsize=(16, 8), facecolor='white')
|
| 456 |
+
axes = axes.flatten()
|
| 457 |
+
|
| 458 |
+
for i in range(7):
|
| 459 |
+
ax = axes[i]
|
| 460 |
+
data = patch[i]
|
| 461 |
+
cmap = ['terrain', 'YlOrRd', 'viridis', 'RdYlGn', 'Blues', 'cividis', 'twilight'][i]
|
| 462 |
+
|
| 463 |
+
im = ax.imshow(data, cmap=cmap, interpolation='bilinear')
|
| 464 |
+
ax.set_title(channel_names[i], fontsize=12, fontweight='bold', pad=8)
|
| 465 |
+
ax.axis('off')
|
| 466 |
+
plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 467 |
+
|
| 468 |
+
axes[7].axis('off')
|
| 469 |
+
|
| 470 |
+
plt.suptitle(f"Patch {metadata['patch_id']} | Row {metadata['row']} | Col {metadata['col']}",
|
| 471 |
+
fontsize=16, fontweight='bold', y=0.98)
|
| 472 |
+
plt.tight_layout()
|
| 473 |
+
|
| 474 |
+
buf = io.BytesIO()
|
| 475 |
+
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
|
| 476 |
+
plt.close(fig)
|
| 477 |
+
buf.seek(0)
|
| 478 |
+
|
| 479 |
+
return Image.open(buf)
|
| 480 |
+
|
| 481 |
+
def create_3d_terrain(patch: np.ndarray, metadata: dict):
|
| 482 |
+
"""Enhanced 3D terrain visualization"""
|
| 483 |
+
dtm = patch[0]
|
| 484 |
+
dtm_clean = np.nan_to_num(dtm, nan=np.nanmedian(dtm))
|
| 485 |
+
|
| 486 |
+
rows, cols = dtm.shape
|
| 487 |
+
x, y = np.arange(cols), np.arange(rows)
|
| 488 |
+
X, Y = np.meshgrid(x, y)
|
| 489 |
+
|
| 490 |
+
fig = go.Figure(data=[go.Surface(
|
| 491 |
+
z=dtm_clean, x=X, y=Y,
|
| 492 |
+
colorscale='earth',
|
| 493 |
+
showscale=True,
|
| 494 |
+
lighting=dict(
|
| 495 |
+
ambient=0.4,
|
| 496 |
+
diffuse=0.8,
|
| 497 |
+
fresnel=0.2,
|
| 498 |
+
specular=0.3,
|
| 499 |
+
roughness=0.5
|
| 500 |
+
),
|
| 501 |
+
contours=dict(
|
| 502 |
+
z=dict(
|
| 503 |
+
show=True,
|
| 504 |
+
usecolormap=True,
|
| 505 |
+
highlightcolor="limegreen",
|
| 506 |
+
project=dict(z=True)
|
| 507 |
+
)
|
| 508 |
+
)
|
| 509 |
+
)])
|
| 510 |
+
|
| 511 |
+
fig.update_layout(
|
| 512 |
+
title=f"3D Terrain Β· Patch {metadata['patch_id']}",
|
| 513 |
+
scene=dict(
|
| 514 |
+
xaxis_title='X (pixels)',
|
| 515 |
+
yaxis_title='Y (pixels)',
|
| 516 |
+
zaxis_title='Elevation (m)',
|
| 517 |
+
camera=dict(eye=dict(x=1.5, y=1.5, z=1.3)),
|
| 518 |
+
aspectmode='manual',
|
| 519 |
+
aspectratio=dict(x=1, y=1, z=0.5)
|
| 520 |
+
),
|
| 521 |
+
height=600,
|
| 522 |
+
margin=dict(l=0, r=0, t=40, b=0)
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
return fig
|
| 526 |
+
|
| 527 |
+
# ==============================================================================
|
| 528 |
+
# UI FUNCTIONS
|
| 529 |
+
# ==============================================================================
|
| 530 |
+
|
| 531 |
+
def load_aoi_and_generate_map(aoi_name, threshold):
|
| 532 |
+
"""Load AOI and generate beautiful map"""
|
| 533 |
+
status = data_manager.load_aoi(aoi_name)
|
| 534 |
+
|
| 535 |
+
if "β
" in status:
|
| 536 |
+
map_html = create_interactive_map(aoi_name, threshold)
|
| 537 |
+
|
| 538 |
+
# Generate statistics (with safe access)
|
| 539 |
+
stats_html = f"""
|
| 540 |
+
<div style='padding: 15px; background: #f0f7ff; border-radius: 8px; margin: 10px 0;'>
|
| 541 |
+
<h3 style='margin-top: 0; color: #1976d2;'>π AOI Statistics</h3>
|
| 542 |
+
<p><b>Total Patches:</b> {len(data_manager.current_patches)}</p>
|
| 543 |
+
"""
|
| 544 |
+
|
| 545 |
+
if data_manager.reference_shape:
|
| 546 |
+
stats_html += f"<p><b>Raster Shape:</b> {data_manager.reference_shape}</p>"
|
| 547 |
+
|
| 548 |
+
if data_manager.reference_crs:
|
| 549 |
+
stats_html += f"<p><b>CRS:</b> {data_manager.reference_crs}</p>"
|
| 550 |
+
|
| 551 |
+
if data_manager.reference_bounds:
|
| 552 |
+
center_lat = (data_manager.reference_bounds.bottom + data_manager.reference_bounds.top) / 2
|
| 553 |
+
center_lon = (data_manager.reference_bounds.left + data_manager.reference_bounds.right) / 2
|
| 554 |
+
stats_html += f"<p><b>Center:</b> {center_lat:.6f}, {center_lon:.6f}</p>"
|
| 555 |
+
|
| 556 |
+
if data_manager.full_dtm is not None:
|
| 557 |
+
stats_html += "<p><b>DTM Status:</b> β
Loaded</p>"
|
| 558 |
+
else:
|
| 559 |
+
stats_html += "<p><b>DTM Status:</b> β οΈ Not available (using reconstructed patches)</p>"
|
| 560 |
+
|
| 561 |
+
stats_html += "</div>"
|
| 562 |
+
|
| 563 |
+
return status, map_html, stats_html, "", None, None
|
| 564 |
+
|
| 565 |
+
return status, "<div style='padding: 20px;'>β Failed to load AOI</div>", "", "", None, None
|
| 566 |
+
|
| 567 |
+
def update_threshold(threshold):
|
| 568 |
+
"""Update anomaly detection threshold"""
|
| 569 |
+
if data_manager.current_aoi:
|
| 570 |
+
return create_interactive_map(data_manager.current_aoi, threshold)
|
| 571 |
+
return "<div style='padding: 20px;'>β οΈ Load an AOI first</div>"
|
| 572 |
+
|
| 573 |
+
def handle_map_click(lat, lon):
|
| 574 |
+
"""Handle click on map - extract and visualize patch"""
|
| 575 |
+
if data_manager.full_dtm is None:
|
| 576 |
+
return "β οΈ Load an AOI first", None, None
|
| 577 |
+
|
| 578 |
+
from rasterio.warp import transform as transform_coords
|
| 579 |
+
|
| 580 |
+
if data_manager.reference_crs != 'EPSG:4326':
|
| 581 |
+
x, y = transform_coords('EPSG:4326', data_manager.reference_crs, [lon], [lat])
|
| 582 |
+
lon, lat = x[0], y[0]
|
| 583 |
+
|
| 584 |
+
row, col = rowcol(data_manager.reference_transform, lon, lat)
|
| 585 |
+
patch_idx = data_manager.find_patch_at_pixel(row, col)
|
| 586 |
+
|
| 587 |
+
if patch_idx is None:
|
| 588 |
+
return f"β No patch at ({row}, {col})", None, None
|
| 589 |
+
|
| 590 |
+
patch, metadata = data_manager.get_patch(patch_idx)
|
| 591 |
+
|
| 592 |
+
if patch is None:
|
| 593 |
+
return "β Error loading patch", None, None
|
| 594 |
+
|
| 595 |
+
# Generate visualizations
|
| 596 |
+
img_2d = create_patch_viz(patch, metadata)
|
| 597 |
+
fig_3d = create_3d_terrain(patch, metadata)
|
| 598 |
+
|
| 599 |
+
# Get score
|
| 600 |
+
score_text = ""
|
| 601 |
+
if data_manager.current_unified_matrix is not None:
|
| 602 |
+
gate_channel = data_manager.current_unified_matrix[:, :, :, 4]
|
| 603 |
+
patch_score = np.mean(gate_channel[patch_idx])
|
| 604 |
+
score_text = f" | <span style='color: {'red' if patch_score > 0.5 else 'green'}; font-weight: bold;'>Score: {patch_score:.3f}</span>"
|
| 605 |
+
|
| 606 |
+
info = f"<div style='padding: 10px; background: #e8f5e9; border-radius: 5px;'><b>β
Patch {patch_idx}</b> | ID: {metadata['patch_id']}{score_text}</div>"
|
| 607 |
+
|
| 608 |
+
return info, img_2d, fig_3d
|
| 609 |
+
|
| 610 |
+
# ==============================================================================
|
| 611 |
+
# BUILD INTERFACE
|
| 612 |
+
# ==============================================================================
|
| 613 |
+
|
| 614 |
+
def build_interface():
|
| 615 |
+
|
| 616 |
+
custom_css = """
|
| 617 |
+
.gradio-container {
|
| 618 |
+
max-width: 1400px !important;
|
| 619 |
+
}
|
| 620 |
+
.map-container {
|
| 621 |
+
border: 2px solid #1976d2;
|
| 622 |
+
border-radius: 8px;
|
| 623 |
+
overflow: hidden;
|
| 624 |
+
}
|
| 625 |
+
"""
|
| 626 |
+
|
| 627 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="SONAR 2.0") as app:
|
| 628 |
+
|
| 629 |
+
gr.Markdown("""
|
| 630 |
+
# πΊοΈ SONAR 2.0 - Archaeological Anomaly Detection System
|
| 631 |
+
### Interactive geospatial analysis with multi-layer terrain visualization
|
| 632 |
+
""")
|
| 633 |
+
|
| 634 |
+
with gr.Row():
|
| 635 |
+
# Left sidebar - Controls
|
| 636 |
+
with gr.Column(scale=1):
|
| 637 |
+
gr.Markdown("### ποΈ Control Panel")
|
| 638 |
+
|
| 639 |
+
aoi_dropdown = gr.Dropdown(
|
| 640 |
+
choices=data_manager.aoi_list,
|
| 641 |
+
label="π Select Area of Interest (AOI)",
|
| 642 |
+
value=data_manager.aoi_list[0] if data_manager.aoi_list else None,
|
| 643 |
+
info="Choose an AOI to analyze"
|
| 644 |
+
)
|
| 645 |
+
|
| 646 |
+
threshold_slider = gr.Slider(
|
| 647 |
+
minimum=0.0,
|
| 648 |
+
maximum=1.0,
|
| 649 |
+
value=0.5,
|
| 650 |
+
step=0.05,
|
| 651 |
+
label="π― Detection Threshold",
|
| 652 |
+
info="Higher = fewer, more confident detections"
|
| 653 |
+
)
|
| 654 |
+
|
| 655 |
+
load_btn = gr.Button("π Load AOI & Generate Map", variant="primary", size="lg")
|
| 656 |
+
|
| 657 |
+
status_box = gr.HTML(label="Status")
|
| 658 |
+
stats_box = gr.HTML(label="Statistics")
|
| 659 |
+
|
| 660 |
+
gr.Markdown("""
|
| 661 |
+
---
|
| 662 |
+
### π How to Use
|
| 663 |
+
|
| 664 |
+
1. **Select AOI** from dropdown
|
| 665 |
+
2. **Click "Load AOI"** to generate map
|
| 666 |
+
3. **Explore layers** using map controls:
|
| 667 |
+
- ποΈ Local Relief (archaeological features)
|
| 668 |
+
- π» Multi-directional hillshade
|
| 669 |
+
- π Standard terrain
|
| 670 |
+
4. **Click anywhere** on the map to inspect
|
| 671 |
+
5. **View 2D/3D** visualizations below
|
| 672 |
+
|
| 673 |
+
π΄ **Red markers** = Detected anomalies
|
| 674 |
+
π΅ **Blue marker** = AOI center
|
| 675 |
+
""")
|
| 676 |
+
|
| 677 |
+
# Right side - Map
|
| 678 |
+
with gr.Column(scale=3):
|
| 679 |
+
gr.Markdown("### πΊοΈ Interactive Map (Click to Inspect)")
|
| 680 |
+
map_display = gr.HTML(
|
| 681 |
+
value="<div style='padding: 40px; text-align: center; background: #f5f5f5; border-radius: 8px;'>π Select an AOI and click 'Load AOI' to view map</div>",
|
| 682 |
+
elem_classes=["map-container"]
|
| 683 |
+
)
|
| 684 |
+
|
| 685 |
+
gr.Markdown("### π Manual Inspection")
|
| 686 |
+
gr.Markdown("*Enter coordinates from map click (Lat/Lon):*")
|
| 687 |
+
|
| 688 |
+
with gr.Row():
|
| 689 |
+
lat_input = gr.Number(label="Latitude", precision=6, scale=1)
|
| 690 |
+
lon_input = gr.Number(label="Longitude", precision=6, scale=1)
|
| 691 |
+
inspect_btn = gr.Button("π Inspect Patch", variant="primary", scale=1)
|
| 692 |
+
|
| 693 |
+
patch_info = gr.HTML(label="Patch Information")
|
| 694 |
+
|
| 695 |
+
gr.Markdown("---")
|
| 696 |
+
gr.Markdown("### π Detailed Patch Analysis")
|
| 697 |
+
|
| 698 |
+
with gr.Row():
|
| 699 |
+
with gr.Column():
|
| 700 |
+
gr.Markdown("#### πΌοΈ Multi-Channel 2D View")
|
| 701 |
+
patch_2d = gr.Image(label="2D Layer Analysis", type="pil")
|
| 702 |
+
|
| 703 |
+
with gr.Column():
|
| 704 |
+
gr.Markdown("#### ποΈ 3D Terrain Model")
|
| 705 |
+
terrain_3d = gr.Plot(label="Interactive 3D Visualization")
|
| 706 |
+
|
| 707 |
+
# Event handlers
|
| 708 |
+
load_btn.click(
|
| 709 |
+
fn=load_aoi_and_generate_map,
|
| 710 |
+
inputs=[aoi_dropdown, threshold_slider],
|
| 711 |
+
outputs=[status_box, map_display, stats_box, patch_info, patch_2d, terrain_3d]
|
| 712 |
+
)
|
| 713 |
+
|
| 714 |
+
threshold_slider.change(
|
| 715 |
+
fn=update_threshold,
|
| 716 |
+
inputs=[threshold_slider],
|
| 717 |
+
outputs=[map_display]
|
| 718 |
+
)
|
| 719 |
+
|
| 720 |
+
inspect_btn.click(
|
| 721 |
+
fn=handle_map_click,
|
| 722 |
+
inputs=[lat_input, lon_input],
|
| 723 |
+
outputs=[patch_info, patch_2d, terrain_3d]
|
| 724 |
+
)
|
| 725 |
+
|
| 726 |
+
gr.Markdown("""
|
| 727 |
+
---
|
| 728 |
+
### π‘ Tips
|
| 729 |
+
- Toggle between layers using the map control (top-right)
|
| 730 |
+
- Adjust threshold to see more/fewer anomalies
|
| 731 |
+
- Click directly on red markers for quick inspection
|
| 732 |
+
- Use 3D view to assess terrain relief and features
|
| 733 |
+
|
| 734 |
+
**Powered by SONAR 2.0** | Archaeological AI Detection System
|
| 735 |
+
""")
|
| 736 |
+
|
| 737 |
+
return app
|
| 738 |
+
|
| 739 |
+
# ==============================================================================
|
| 740 |
+
# MAIN
|
| 741 |
+
# ==============================================================================
|
| 742 |
+
|
| 743 |
+
if __name__ == "__main__":
|
| 744 |
+
print("\n" + "="*60)
|
| 745 |
+
print("πΊοΈ SONAR 2.0 - Archaeological Site Detection")
|
| 746 |
+
print("="*60)
|
| 747 |
+
|
| 748 |
+
config.extract_data_files()
|
| 749 |
+
data_manager.discover_aois()
|
| 750 |
+
|
| 751 |
+
print(f"\nπ System Information:")
|
| 752 |
+
print(f" Device: {config.DEVICE}")
|
| 753 |
+
print(f" Available AOIs: {len(data_manager.aoi_list)}")
|
| 754 |
+
print(f" AOI Names: {', '.join(data_manager.aoi_list)}")
|
| 755 |
+
print("="*60 + "\n")
|
| 756 |
+
|
| 757 |
+
app = build_interface()
|
| 758 |
+
|
| 759 |
+
app.launch(
|
| 760 |
+
server_name="0.0.0.0",
|
| 761 |
+
server_port=7860,
|
| 762 |
+
share=False, # Set to True for public URL
|
| 763 |
+
show_error=True,
|
| 764 |
+
show_api=True
|
| 765 |
)
|