Update drishx.py
Browse files
drishx.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
import json
|
|
@@ -17,7 +15,7 @@ from requests.adapters import HTTPAdapter
|
|
| 17 |
from urllib3.util.retry import Retry
|
| 18 |
|
| 19 |
from fastapi import FastAPI, HTTPException
|
| 20 |
-
from fastapi.responses import StreamingResponse
|
| 21 |
from fastapi.staticfiles import StaticFiles
|
| 22 |
from fastapi.middleware.cors import CORSMiddleware
|
| 23 |
from pydantic import BaseModel
|
|
@@ -39,13 +37,16 @@ load_dotenv()
|
|
| 39 |
logging.basicConfig(level=logging.INFO)
|
| 40 |
logger = logging.getLogger("ARGUS")
|
| 41 |
|
| 42 |
-
SECONDS_OFFSET_B02_B04 = 1.01
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
DATA_DIR = os.
|
| 46 |
DETECTION_DIR = os.path.join(DATA_DIR, "sentinel_data/detections")
|
| 47 |
os.makedirs(DETECTION_DIR, exist_ok=True)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
OVERPASS_MIRRORS = [
|
| 50 |
"https://lz4.overpass-api.de/api/interpreter",
|
| 51 |
"https://z.overpass-api.de/api/interpreter",
|
|
@@ -53,7 +54,6 @@ OVERPASS_MIRRORS = [
|
|
| 53 |
"https://overpass-api.de/api/interpreter",
|
| 54 |
]
|
| 55 |
|
| 56 |
-
# Network session with retries
|
| 57 |
_session = requests.Session()
|
| 58 |
_retry = Retry(
|
| 59 |
total=2, backoff_factor=1.0,
|
|
@@ -69,58 +69,48 @@ ox.settings.requests_timeout = 30
|
|
| 69 |
ox.settings.overpass_rate_limit = False
|
| 70 |
ox.settings.max_query_area_size = 1_000_000_000_000
|
| 71 |
ox.settings.log_console = False
|
| 72 |
-
# OSMnx Cache Redirection
|
| 73 |
ox.settings.use_cache = True
|
| 74 |
ox.settings.cache_folder = os.path.join(DATA_DIR, "osm_cache")
|
| 75 |
|
| 76 |
-
# Copernicus Data Space config
|
| 77 |
CONFIG = SHConfig()
|
| 78 |
-
CONFIG.sh_client_id = os.getenv("COPERNICUS_CLIENT_ID")
|
| 79 |
-
CONFIG.sh_client_secret = os.getenv("COPERNICUS_CLIENT_SECRET")
|
| 80 |
CONFIG.sh_base_url = "https://sh.dataspace.copernicus.eu"
|
| 81 |
CONFIG.sh_token_url = "https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token"
|
| 82 |
-
|
| 83 |
-
# SentinelHub Cache Redirection
|
| 84 |
CONFIG.cache_dir = os.path.join(DATA_DIR, "sh_cache")
|
| 85 |
os.makedirs(CONFIG.cache_dir, exist_ok=True)
|
| 86 |
|
| 87 |
-
# Note: We do NOT call CONFIG.save() here to avoid TOML serialization errors with NoneTypes
|
| 88 |
logger.info(f"DrishX Storage Link: {DATA_DIR}")
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
FEATURED_SITES = [
|
| 92 |
-
{"id": "v1", "name": "Braunschweig A7
|
| 93 |
-
{"id": "v2", "name": "Frankfurt A3
|
| 94 |
-
{"id": "v3", "name": "Karlsruhe A5
|
| 95 |
]
|
| 96 |
|
| 97 |
-
|
| 98 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 99 |
-
# Helper
|
| 100 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 101 |
|
| 102 |
def normalized_ratio(a, b):
|
| 103 |
-
"""(a - b) / (a + b), safe division."""
|
| 104 |
denom = a + b
|
| 105 |
with np.errstate(divide="ignore", invalid="ignore"):
|
| 106 |
result = np.where(denom != 0, (a - b) / denom, 0.0)
|
| 107 |
return result.astype(np.float32)
|
| 108 |
|
| 109 |
-
|
| 110 |
def rescale_s2(bands):
|
| 111 |
-
"""Rescale Sentinel-2 L2A reflectance values (typically 0–10000 int) to 0–1 float."""
|
| 112 |
bands = bands.astype(np.float32)
|
| 113 |
-
if np.nanmax(bands) > 10:
|
| 114 |
bands /= 10000.0
|
| 115 |
return bands
|
| 116 |
|
| 117 |
-
|
| 118 |
-
# ─────────────────────────────────────────────────────────────────────────────
|
| 119 |
-
# Array subset — exact replica of S2TD.pick_arr_subset
|
| 120 |
-
# ─────────────────────────────────────────────────────────────────────────────
|
| 121 |
-
|
| 122 |
def pick_arr_subset(arr, y, x, size):
|
| 123 |
-
"""Pick a size×size window centred on (y, x) from a 2D or 3D array."""
|
| 124 |
size_low = size // 2
|
| 125 |
size_up = size // 2
|
| 126 |
if size_low + size_up < size:
|
|
@@ -135,38 +125,17 @@ def pick_arr_subset(arr, y, x, size):
|
|
| 135 |
return arr[:, ymin:ymax, xmin:xmax]
|
| 136 |
return arr
|
| 137 |
|
| 138 |
-
|
| 139 |
-
# ─────────────────────────────────────────────────────────────────────────────
|
| 140 |
-
# Feature stack — exact 7 features as in S2TD._build_feature_stack (Table 1)
|
| 141 |
-
# ─────────────────────────────────────────────────────────────────────────────
|
| 142 |
-
|
| 143 |
def build_feature_stack(data):
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
Feature order (Table 1, Fisser et al. 2022):
|
| 150 |
-
0: variance of (B04, B03, B02)
|
| 151 |
-
1: normalized_ratio(B04, B02) — red / blue
|
| 152 |
-
2: normalized_ratio(B03, B02) — green / blue
|
| 153 |
-
3: B04 - mean(B04)
|
| 154 |
-
4: B03 - mean(B03)
|
| 155 |
-
5: B02 - mean(B02)
|
| 156 |
-
6: B08 - mean(B08)
|
| 157 |
-
"""
|
| 158 |
-
R = data[:, :, 0].astype(np.float32) # B04
|
| 159 |
-
G = data[:, :, 1].astype(np.float32) # B03
|
| 160 |
-
B = data[:, :, 2].astype(np.float32) # B02
|
| 161 |
-
NIR = data[:, :, 3].astype(np.float32) # B08
|
| 162 |
CLM = data[:, :, 4]
|
| 163 |
|
| 164 |
-
# Rescale if needed
|
| 165 |
bands = np.stack([R, G, B, NIR], axis=0)
|
| 166 |
bands = rescale_s2(bands)
|
| 167 |
R, G, B, NIR = bands[0], bands[1], bands[2], bands[3]
|
| 168 |
|
| 169 |
-
# Cloud mask → NaN
|
| 170 |
cloud = CLM > 0
|
| 171 |
R[cloud] = np.nan
|
| 172 |
G[cloud] = np.nan
|
|
@@ -176,28 +145,20 @@ def build_feature_stack(data):
|
|
| 176 |
H, W = R.shape
|
| 177 |
fs = np.zeros((7, H, W), dtype=np.float32)
|
| 178 |
|
| 179 |
-
# Check for any valid data to avoid "Mean of empty slice" warnings
|
| 180 |
if np.any(~cloud):
|
| 181 |
import warnings
|
| 182 |
with warnings.catch_warnings():
|
| 183 |
warnings.simplefilter("ignore", category=RuntimeWarning)
|
| 184 |
-
# Feature 0: variance of visible bands
|
| 185 |
fs[0] = np.nanvar(np.stack([R, G, B], axis=0), axis=0, ddof=0)
|
| 186 |
-
|
| 187 |
-
# Features 1–2: normalized ratios
|
| 188 |
fs[1] = normalized_ratio(R, B)
|
| 189 |
fs[2] = normalized_ratio(G, B)
|
| 190 |
-
|
| 191 |
-
# Features 3–6: mean-centered bands
|
| 192 |
fs[3] = R - np.nanmean(R)
|
| 193 |
fs[4] = G - np.nanmean(G)
|
| 194 |
fs[5] = B - np.nanmean(B)
|
| 195 |
fs[6] = NIR - np.nanmean(NIR)
|
| 196 |
else:
|
| 197 |
-
# All pixels are cloud-masked
|
| 198 |
fs.fill(np.nan)
|
| 199 |
|
| 200 |
-
# Ensure NaN consistency
|
| 201 |
nan_mask = np.isnan(fs[3])
|
| 202 |
fs[:, nan_mask] = np.nan
|
| 203 |
|
|
@@ -207,107 +168,18 @@ def build_feature_stack(data):
|
|
| 207 |
"cloud_mask": cloud,
|
| 208 |
}
|
| 209 |
|
| 210 |
-
|
| 211 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 212 |
-
# RF Model
|
| 213 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 214 |
|
| 215 |
-
# Path to the trained Random Forest model from S2TruckDetect
|
| 216 |
-
RF_MODEL_PATH = os.getenv("RF_MODEL_PATH", "rf_model.pickle")
|
| 217 |
_rf_model = None
|
| 218 |
|
| 219 |
-
|
| 220 |
def load_rf_model(path=None):
|
| 221 |
-
"""Load the trained RF model from pickle. Returns None if not found."""
|
| 222 |
global _rf_model
|
| 223 |
-
|
| 224 |
-
if _rf_model is not None:
|
| 225 |
-
return _rf_model
|
| 226 |
-
if os.path.isfile(p):
|
| 227 |
-
try:
|
| 228 |
-
_rf_model = pickle.load(open(p, "rb"))
|
| 229 |
-
logger.info(f"Loaded trained RF model from {p}")
|
| 230 |
-
return _rf_model
|
| 231 |
-
except Exception as e:
|
| 232 |
-
logger.error(f"Failed to load RF model from {p}: {e}")
|
| 233 |
-
else:
|
| 234 |
-
logger.warning(f"RF model not found at {p} — will use proxy classifier (lower accuracy)")
|
| 235 |
-
return None
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
# ─────────────────────────────────────────────────────────────────────────────
|
| 239 |
-
# Classification — real RF (preferred) or proxy fallback
|
| 240 |
-
# ──────────────────────────────────────────────────────────��──────────────────
|
| 241 |
-
|
| 242 |
-
def rf_classify(feature_stack, road_mask, rf_model):
|
| 243 |
-
"""
|
| 244 |
-
Classify pixels using the trained Random Forest model.
|
| 245 |
-
Exact replica of S2TD._predict + _postprocess_prediction.
|
| 246 |
-
|
| 247 |
-
:param feature_stack: (7, H, W) feature array
|
| 248 |
-
:param road_mask: (H, W) binary road mask
|
| 249 |
-
:param rf_model: trained sklearn RandomForestClassifier
|
| 250 |
-
:return: (probabilities (4, H, W), prediction (H, W) int8)
|
| 251 |
-
"""
|
| 252 |
-
H, W = feature_stack.shape[1], feature_stack.shape[2]
|
| 253 |
-
|
| 254 |
-
# Reshape to (n_pixels, 7) for sklearn
|
| 255 |
-
vars_reshaped = []
|
| 256 |
-
for band_idx in range(feature_stack.shape[0]):
|
| 257 |
-
vars_reshaped.append(feature_stack[band_idx].flatten())
|
| 258 |
-
vars_reshaped = np.array(vars_reshaped).swapaxes(0, 1) # (n_pixels, 7)
|
| 259 |
-
|
| 260 |
-
# Build NaN mask — exclude NaN and Inf pixels
|
| 261 |
-
nan_mask_flat = np.zeros_like(vars_reshaped)
|
| 262 |
-
for var_idx in range(vars_reshaped.shape[1]):
|
| 263 |
-
nan_mask_flat[:, var_idx] = ~np.isnan(vars_reshaped[:, var_idx])
|
| 264 |
-
not_nan = (np.nanmin(nan_mask_flat, axis=1).astype(bool)
|
| 265 |
-
& np.min(np.isfinite(vars_reshaped), axis=1).astype(bool))
|
| 266 |
-
|
| 267 |
-
# Run RF predict_proba on valid pixels only
|
| 268 |
-
if not np.any(not_nan):
|
| 269 |
-
# Graceful return if no valid pixels found (e.g., all cloud masked)
|
| 270 |
-
probabilities_shaped = np.zeros((4, H, W), dtype=np.float32)
|
| 271 |
-
classification = np.zeros((H, W), dtype=np.int8)
|
| 272 |
-
return probabilities_shaped, classification
|
| 273 |
-
|
| 274 |
-
predictions_flat = rf_model.predict_proba(vars_reshaped[not_nan])
|
| 275 |
-
|
| 276 |
-
# Map probabilities back to spatial grid
|
| 277 |
-
n_classes = predictions_flat.shape[1]
|
| 278 |
-
probabilities_shaped = np.zeros((n_classes, H * W), dtype=np.float32)
|
| 279 |
-
for idx in range(n_classes):
|
| 280 |
-
probabilities_shaped[idx, not_nan] = predictions_flat[:, idx]
|
| 281 |
-
|
| 282 |
-
probabilities_shaped = probabilities_shaped.reshape((n_classes, H, W))
|
| 283 |
-
|
| 284 |
-
# Zero out NaN positions
|
| 285 |
-
nan_2d = np.isnan(feature_stack[0])
|
| 286 |
-
probabilities_shaped[:, nan_2d] = 0
|
| 287 |
-
|
| 288 |
-
# Post-process: suppress low-confidence background (exact S2TD logic)
|
| 289 |
-
probabilities_shaped[1][probabilities_shaped[1] < 0.75] = 0
|
| 290 |
-
|
| 291 |
-
classification = np.nanargmax(probabilities_shaped, axis=0).astype(np.int8) + 1
|
| 292 |
-
classification[np.max(probabilities_shaped, axis=0) == 0] = 0
|
| 293 |
-
classification[nan_2d] = 0
|
| 294 |
-
|
| 295 |
-
# Apply road mask
|
| 296 |
-
rm = road_mask.astype(bool)
|
| 297 |
-
classification[~rm] = 0
|
| 298 |
-
|
| 299 |
-
return probabilities_shaped, classification
|
| 300 |
-
|
| 301 |
|
| 302 |
def proxy_classify(feature_stack, road_mask):
|
| 303 |
-
|
| 304 |
-
Heuristic proxy when RF model is unavailable. Lower accuracy.
|
| 305 |
-
|
| 306 |
-
Produces:
|
| 307 |
-
probabilities: (4, H, W) — class probs for [background, blue, green, red]
|
| 308 |
-
prediction: (H, W) — int8 labels {0=nan, 1=background, 2=blue, 3=green, 4=red}
|
| 309 |
-
"""
|
| 310 |
-
fs = feature_stack # (7, H, W)
|
| 311 |
H, W = fs.shape[1], fs.shape[2]
|
| 312 |
probs = np.zeros((4, H, W), dtype=np.float32)
|
| 313 |
|
|
@@ -345,46 +217,20 @@ def proxy_classify(feature_stack, road_mask):
|
|
| 345 |
|
| 346 |
return probs, classification
|
| 347 |
|
| 348 |
-
|
| 349 |
-
def classify(feature_stack, road_mask, rf_model=None):
|
| 350 |
-
"""
|
| 351 |
-
Unified classifier entry point.
|
| 352 |
-
Uses trained RF if model is provided, otherwise falls back to proxy.
|
| 353 |
-
"""
|
| 354 |
-
if rf_model is not None:
|
| 355 |
-
logger.debug("Using trained RF model for classification")
|
| 356 |
-
return rf_classify(feature_stack, road_mask, rf_model)
|
| 357 |
-
else:
|
| 358 |
-
logger.debug("Using proxy classifier (no RF model loaded)")
|
| 359 |
-
return proxy_classify(feature_stack, road_mask)
|
| 360 |
-
|
| 361 |
-
|
| 362 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 363 |
-
#
|
| 364 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 365 |
|
| 366 |
class ObjectExtractor:
|
| 367 |
-
"""
|
| 368 |
-
Extracts truck objects from the RF prediction raster using recursive
|
| 369 |
-
neighbourhood clustering, matching the S2TD reference implementation.
|
| 370 |
-
"""
|
| 371 |
-
|
| 372 |
def __init__(self, probabilities, lat_arr, lon_arr):
|
| 373 |
-
"""
|
| 374 |
-
:param probabilities: (4, H, W) class probabilities
|
| 375 |
-
:param lat_arr: 1-D array of latitude per row
|
| 376 |
-
:param lon_arr: 1-D array of longitude per column
|
| 377 |
-
"""
|
| 378 |
self.probabilities = probabilities
|
| 379 |
self.lat = lat_arr
|
| 380 |
self.lon = lon_arr
|
| 381 |
|
| 382 |
def extract(self, predictions_arr):
|
| 383 |
-
"""Main extraction loop over all blue (class 2) seed pixels."""
|
| 384 |
preds = predictions_arr.copy()
|
| 385 |
probs = self.probabilities.copy()
|
| 386 |
-
|
| 387 |
-
preds[preds == 1] = 0 # zero out background
|
| 388 |
blue_ys, blue_xs = np.where(preds == 2)
|
| 389 |
detections = []
|
| 390 |
sub_size = 9
|
|
@@ -400,240 +246,23 @@ class ObjectExtractor:
|
|
| 400 |
|
| 401 |
half_idx_y = y_blue if subset_9.shape[0] < sub_size else subset_9.shape[0] // 2
|
| 402 |
half_idx_x = x_blue if subset_9.shape[1] < sub_size else subset_9.shape[1] // 2
|
| 403 |
-
try:
|
| 404 |
-
current_value = subset_9[half_idx_y, half_idx_x]
|
| 405 |
-
except IndexError:
|
| 406 |
-
half_idx_y, half_idx_x = sub_size // 2, sub_size // 2
|
| 407 |
-
current_value = subset_9[half_idx_y, half_idx_x]
|
| 408 |
|
| 409 |
-
new_value = 100
|
| 410 |
if not all(v in subset_9 for v in [2, 3, 4]):
|
| 411 |
continue
|
| 412 |
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
det = self._postprocess_cluster(
|
| 425 |
-
cluster, preds, probs, subset_3,
|
| 426 |
-
y_blue, x_blue,
|
| 427 |
-
half_idx_y, half_idx_x,
|
| 428 |
-
new_value,
|
| 429 |
-
)
|
| 430 |
-
if det is not None:
|
| 431 |
-
preds = det["updated_preds"]
|
| 432 |
-
detections.append(det["detection"])
|
| 433 |
|
| 434 |
return detections
|
| 435 |
|
| 436 |
-
def _cluster_array(self, arr, probs, point, new_value, current_value,
|
| 437 |
-
yet_seen_indices, yet_seen_values, skipped_one):
|
| 438 |
-
"""Recursive neighbourhood clustering — matches S2TD._cluster_array."""
|
| 439 |
-
if len(yet_seen_indices) == 0:
|
| 440 |
-
yet_seen_indices.append(point)
|
| 441 |
-
yet_seen_values.append(current_value)
|
| 442 |
-
|
| 443 |
-
arr_mod = arr.copy()
|
| 444 |
-
arr_mod[point[0], point[1]] = 0
|
| 445 |
-
|
| 446 |
-
window_3x3 = pick_arr_subset(arr_mod, point[0], point[1], 3).copy()
|
| 447 |
-
if window_3x3.shape[0] >= 2 and window_3x3.shape[1] >= 2:
|
| 448 |
-
cy = min(1, window_3x3.shape[0] - 1)
|
| 449 |
-
cx = min(1, window_3x3.shape[1] - 1)
|
| 450 |
-
if window_3x3[cy, cx] == 2:
|
| 451 |
-
window_3x3[window_3x3 == 4] = 1 # eliminate reds near blue
|
| 452 |
-
|
| 453 |
-
y, x = point[0], point[1]
|
| 454 |
-
window_3x3_probs = pick_arr_subset(probs, y, x, 3)
|
| 455 |
-
|
| 456 |
-
windows = [window_3x3]
|
| 457 |
-
windows_probs = [window_3x3_probs]
|
| 458 |
-
if current_value == 4 or skipped_one:
|
| 459 |
-
windows = windows[0:1]
|
| 460 |
-
|
| 461 |
-
ys, xs = np.array([], dtype=int), np.array([], dtype=int)
|
| 462 |
-
window_idx = 0
|
| 463 |
-
offset_y, offset_x = 0, 0
|
| 464 |
-
|
| 465 |
-
while len(ys) == 0 and window_idx < len(windows):
|
| 466 |
-
window = windows[window_idx]
|
| 467 |
-
window_p = windows_probs[window_idx]
|
| 468 |
-
offset_y = window.shape[0] // 2
|
| 469 |
-
offset_x = window.shape[1] // 2
|
| 470 |
-
|
| 471 |
-
go_next = (current_value + 1) in window or current_value == 2
|
| 472 |
-
target_value = current_value + 1 if go_next else current_value
|
| 473 |
-
match = window == target_value
|
| 474 |
-
if np.count_nonzero(match) == 0:
|
| 475 |
-
target_value = current_value
|
| 476 |
-
match = window == target_value
|
| 477 |
-
|
| 478 |
-
ys_found, xs_found = np.where(match)
|
| 479 |
-
|
| 480 |
-
# Probability-based tie-breaking
|
| 481 |
-
if len(ys_found) > 1 and window_p.ndim == 3 and window_p.shape[0] > (target_value - 1):
|
| 482 |
-
wp_target = window_p[target_value - 1] * match
|
| 483 |
-
max_prob_mask = (wp_target == np.max(wp_target))
|
| 484 |
-
ys_found, xs_found = np.where(max_prob_mask)
|
| 485 |
-
|
| 486 |
-
ys, xs = ys_found, xs_found
|
| 487 |
-
window_idx += 1
|
| 488 |
-
|
| 489 |
-
ymin_w = max(0, point[0] - offset_y)
|
| 490 |
-
xmin_w = max(0, point[1] - offset_x)
|
| 491 |
-
|
| 492 |
-
for y_local, x_local in zip(ys, xs):
|
| 493 |
-
ny, nx = ymin_w + int(y_local), xmin_w + int(x_local)
|
| 494 |
-
if [ny, nx] in yet_seen_indices:
|
| 495 |
-
continue
|
| 496 |
-
if ny < 0 or ny >= arr.shape[0] or nx < 0 or nx >= arr.shape[1]:
|
| 497 |
-
continue
|
| 498 |
-
try:
|
| 499 |
-
cv = arr[ny, nx]
|
| 500 |
-
except IndexError:
|
| 501 |
-
continue
|
| 502 |
-
|
| 503 |
-
# Red already seen but this is green or blue → skip
|
| 504 |
-
if 4 in yet_seen_values and cv <= 3:
|
| 505 |
-
continue
|
| 506 |
-
|
| 507 |
-
arr_mod[ny, nx] = new_value
|
| 508 |
-
yet_seen_indices.append([ny, nx])
|
| 509 |
-
yet_seen_values.append(cv)
|
| 510 |
-
|
| 511 |
-
# Guard: avoid picking many more reds than blues and greens
|
| 512 |
-
n_blue = sum(1 for v in yet_seen_values if v == 2)
|
| 513 |
-
n_green = sum(1 for v in yet_seen_values if v == 3)
|
| 514 |
-
n_red = sum(1 for v in yet_seen_values if v == 4)
|
| 515 |
-
if n_red > n_blue and n_red > n_green:
|
| 516 |
-
break
|
| 517 |
-
|
| 518 |
-
arr_mod, yet_seen_indices, yet_seen_values, skipped_one = self._cluster_array(
|
| 519 |
-
arr_mod, probs, [ny, nx], new_value, cv,
|
| 520 |
-
yet_seen_indices, yet_seen_values, skipped_one,
|
| 521 |
-
)
|
| 522 |
-
|
| 523 |
-
arr_mod[point[0], point[1]] = new_value
|
| 524 |
-
return arr_mod, yet_seen_indices, yet_seen_values, skipped_one
|
| 525 |
-
|
| 526 |
-
def _postprocess_cluster(self, cluster, preds_copy, probs, subset_3,
|
| 527 |
-
y_blue, x_blue, half_idx_y, half_idx_x,
|
| 528 |
-
new_value):
|
| 529 |
-
"""Validate cluster and produce a detection dict — mirrors S2TD._postprocess_cluster."""
|
| 530 |
-
# Add neighbouring blues from the 3×3 window
|
| 531 |
-
ys_ba, xs_ba = np.where(subset_3 == 2)
|
| 532 |
-
ys_ba = ys_ba + half_idx_y - 1
|
| 533 |
-
xs_ba = xs_ba + half_idx_x - 1
|
| 534 |
-
for yb, xb in zip(ys_ba, xs_ba):
|
| 535 |
-
yb_c = int(np.clip(yb, 0, cluster.shape[0] - 1))
|
| 536 |
-
xb_c = int(np.clip(xb, 0, cluster.shape[1] - 1))
|
| 537 |
-
cluster[yb_c, xb_c] = new_value
|
| 538 |
-
|
| 539 |
-
cluster[cluster != new_value] = 0
|
| 540 |
-
cys, cxs = np.where(cluster == new_value)
|
| 541 |
-
if len(cys) == 0:
|
| 542 |
-
return None
|
| 543 |
-
|
| 544 |
-
# Map subset coords back to full array
|
| 545 |
-
ymin_sub = int(np.clip(y_blue - half_idx_y, 0, np.inf))
|
| 546 |
-
xmin_sub = int(np.clip(x_blue - half_idx_x, 0, np.inf))
|
| 547 |
-
cys_full = cys + ymin_sub
|
| 548 |
-
cxs_full = cxs + xmin_sub
|
| 549 |
-
|
| 550 |
-
ymin = int(np.min(cys_full))
|
| 551 |
-
xmin = int(np.min(cxs_full))
|
| 552 |
-
ymax = int(np.max(cys_full)) + 1 # +1: box extends to upper bound of pixel
|
| 553 |
-
xmax = int(np.max(cxs_full)) + 1
|
| 554 |
-
|
| 555 |
-
H, W = preds_copy.shape
|
| 556 |
-
ymin, ymax = max(0, ymin), min(H, ymax)
|
| 557 |
-
xmin, xmax = max(0, xmin), min(W, xmax)
|
| 558 |
-
|
| 559 |
-
box_preds = preds_copy[ymin:ymax, xmin:xmax].copy()
|
| 560 |
-
box_probs = probs[1:, ymin:ymax, xmin:xmax].copy() # classes 2,3,4 → indices 0,1,2
|
| 561 |
-
|
| 562 |
-
# Spectral probability scores (exact S2TD logic)
|
| 563 |
-
max_probs = []
|
| 564 |
-
for cls_offset, cls_val in enumerate([2, 3, 4]):
|
| 565 |
-
mask = (box_preds == cls_val)
|
| 566 |
-
vals = box_probs[cls_offset] * mask
|
| 567 |
-
mp = float(np.nanmax(vals)) if np.any(mask) else 0.0
|
| 568 |
-
max_probs.append(mp)
|
| 569 |
-
|
| 570 |
-
mean_max_spectral_probability = float(np.nanmean(max_probs))
|
| 571 |
-
mean_spectral_probability = float(np.nanmean(np.nanmax(box_probs, axis=0)))
|
| 572 |
-
|
| 573 |
-
# Validation checks
|
| 574 |
-
all_given = all(v in box_preds for v in [2, 3, 4])
|
| 575 |
-
large_enough = box_preds.shape[0] > 2 or box_preds.shape[1] > 2
|
| 576 |
-
too_large = box_preds.shape[0] > 5 or box_preds.shape[1] > 5
|
| 577 |
-
|
| 578 |
-
if too_large or not all_given or not large_enough:
|
| 579 |
-
return None
|
| 580 |
-
|
| 581 |
-
# Score: TWO terms — matches reference
|
| 582 |
-
score = mean_max_spectral_probability + mean_spectral_probability
|
| 583 |
-
if score <= 1.2:
|
| 584 |
-
return None
|
| 585 |
-
|
| 586 |
-
# Direction (blue → red vector)
|
| 587 |
-
by, bx = np.where(box_preds == 2)
|
| 588 |
-
ry, rx = np.where(box_preds == 4)
|
| 589 |
-
blue_idx = np.array([by[0], bx[0]], dtype=np.int8)
|
| 590 |
-
red_idx = np.array([ry[0], rx[0]], dtype=np.int8)
|
| 591 |
-
vector = (blue_idx - red_idx) * np.array([1, -1], dtype=np.int8)
|
| 592 |
-
heading = float(np.degrees(np.arctan2(vector[1], vector[0])) % 360)
|
| 593 |
-
|
| 594 |
-
# Speed
|
| 595 |
-
diameter = max(box_preds.shape) * 10 - 10
|
| 596 |
-
speed_kmh = float(np.sqrt(diameter * 20) / SECONDS_OFFSET_B02_B04 * 3.6)
|
| 597 |
-
|
| 598 |
-
# Geo-coordinates (centre of detection box)
|
| 599 |
-
lat_centre = float((self.lat[ymin] + self.lat[min(ymax, len(self.lat) - 1)]) / 2)
|
| 600 |
-
lon_centre = float((self.lon[xmin] + self.lon[min(xmax, len(self.lon) - 1)]) / 2)
|
| 601 |
-
|
| 602 |
-
# Zero out detected pixels to prevent re-detection
|
| 603 |
-
preds_copy[ymin:ymax, xmin:xmax] *= np.zeros_like(box_preds)
|
| 604 |
-
# Also zero 3×3 around blue pixels
|
| 605 |
-
blue_in_box = np.where(box_preds == 2)
|
| 606 |
-
for yb, xb in zip(blue_in_box[0], blue_in_box[1]):
|
| 607 |
-
y0, y1 = max(0, ymin + yb - 1), min(H, ymin + yb + 2)
|
| 608 |
-
x0, x1 = max(0, xmin + xb - 1), min(W, xmin + xb + 2)
|
| 609 |
-
preds_copy[y0:y1, x0:x1] *= (preds_copy[y0:y1, x0:x1] != 2).astype(np.int8)
|
| 610 |
-
|
| 611 |
-
crop_id = f"truck_{int(time.time() * 1000)}_{ymin}_{xmin}.png"
|
| 612 |
-
|
| 613 |
-
return {
|
| 614 |
-
"updated_preds": preds_copy,
|
| 615 |
-
"detection": {
|
| 616 |
-
"lat": lat_centre,
|
| 617 |
-
"lon": lon_centre,
|
| 618 |
-
"confidence": float(min(score / 2.4, 1.0)),
|
| 619 |
-
"s_score": round(score, 3),
|
| 620 |
-
"speed_kmh": round(speed_kmh, 1),
|
| 621 |
-
"heading": round(heading, 1),
|
| 622 |
-
"heading_desc": self._direction_to_compass(heading),
|
| 623 |
-
"id": crop_id,
|
| 624 |
-
"image_url": f"/detections/{crop_id}",
|
| 625 |
-
"box_shape": list(box_preds.shape),
|
| 626 |
-
"max_probs": {"blue": max_probs[0], "green": max_probs[1], "red": max_probs[2]},
|
| 627 |
-
},
|
| 628 |
-
}
|
| 629 |
-
|
| 630 |
-
@staticmethod
|
| 631 |
-
def _direction_to_compass(deg):
|
| 632 |
-
bins = np.arange(0, 359, 45, dtype=np.float32)
|
| 633 |
-
labels = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
|
| 634 |
-
return labels[int(np.argmin(np.abs(bins - deg)))]
|
| 635 |
-
|
| 636 |
-
|
| 637 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 638 |
# ARGUS Engine
|
| 639 |
# ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -644,15 +273,6 @@ class ARGUSEngine:
|
|
| 644 |
self.rf_model = load_rf_model()
|
| 645 |
|
| 646 |
def fetch_roads(self, bbox_coords, progress_cb=None):
|
| 647 |
-
"""Fetch major roads with automatic mirror rotation and fallbacks."""
|
| 648 |
-
def log(msg, level="info", pct=None):
|
| 649 |
-
if level == "info":
|
| 650 |
-
logger.info(msg)
|
| 651 |
-
elif level == "warn":
|
| 652 |
-
logger.warning(msg)
|
| 653 |
-
if progress_cb:
|
| 654 |
-
progress_cb(msg, pct)
|
| 655 |
-
|
| 656 |
min_lat, min_lon, max_lat, max_lon = bbox_coords
|
| 657 |
center_lat = (min_lat + max_lat) / 2
|
| 658 |
center_lon = (min_lon + max_lon) / 2
|
|
@@ -661,10 +281,7 @@ class ARGUSEngine:
|
|
| 661 |
lon_span = (max_lon - min_lon) * 111000 * np.cos(np.radians(center_lat))
|
| 662 |
dist_m = int(max(lat_span, lon_span) * 0.6) + 1000
|
| 663 |
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
for i, mirror in enumerate(OVERPASS_MIRRORS):
|
| 667 |
-
log(f"Trying mirror {i+1}/{len(OVERPASS_MIRRORS)}: {mirror}", pct=10 + i * 5)
|
| 668 |
ox.settings.overpass_url = mirror
|
| 669 |
try:
|
| 670 |
graph = ox.graph_from_point(
|
|
@@ -673,99 +290,46 @@ class ARGUSEngine:
|
|
| 673 |
retain_all=False, truncate_by_edge=True,
|
| 674 |
)
|
| 675 |
roads = ox.graph_to_gdfs(graph, nodes=False)
|
| 676 |
-
major_types = [
|
| 677 |
-
"motorway", "trunk", "primary", "secondary",
|
| 678 |
-
"motorway_link", "trunk_link", "primary_link",
|
| 679 |
-
]
|
| 680 |
roads = roads[roads["highway"].isin(major_types)].copy()
|
| 681 |
if not roads.empty:
|
| 682 |
-
logger.info(f"Fetched {len(roads)} major roads from {mirror}")
|
| 683 |
return roads
|
| 684 |
except Exception as e:
|
| 685 |
-
logger.warning(f"Mirror
|
| 686 |
time.sleep(1)
|
| 687 |
|
| 688 |
-
# Raw Overpass fallback
|
| 689 |
-
logger.warning("All mirrors failed. Trying raw Overpass query.")
|
| 690 |
-
try:
|
| 691 |
-
query = f"""
|
| 692 |
-
[out:json][timeout:60];
|
| 693 |
-
(way["highway"~"motorway|trunk|primary"]({min_lat},{min_lon},{max_lat},{max_lon}););
|
| 694 |
-
out body; >; out skel qt;
|
| 695 |
-
"""
|
| 696 |
-
resp = requests.post(OVERPASS_MIRRORS[0], data={"data": query}, timeout=60)
|
| 697 |
-
if resp.status_code == 200:
|
| 698 |
-
data = resp.json()
|
| 699 |
-
nodes = {n["id"]: (n["lon"], n["lat"]) for n in data["elements"] if n["type"] == "node"}
|
| 700 |
-
ways = []
|
| 701 |
-
for w in data["elements"]:
|
| 702 |
-
if w["type"] == "way" and "nodes" in w:
|
| 703 |
-
coords = [nodes[nid] for nid in w["nodes"] if nid in nodes]
|
| 704 |
-
if len(coords) > 1:
|
| 705 |
-
ways.append({"geometry": LineString(coords), "highway": w["tags"].get("highway")})
|
| 706 |
-
if ways:
|
| 707 |
-
roads = gpd.GeoDataFrame(ways, crs="EPSG:4326")
|
| 708 |
-
logger.info(f"Raw fallback: {len(roads)} roads")
|
| 709 |
-
return roads
|
| 710 |
-
except Exception as e:
|
| 711 |
-
logger.error(f"Raw fallback failed: {e}")
|
| 712 |
-
|
| 713 |
return gpd.GeoDataFrame()
|
| 714 |
|
| 715 |
def detect_trucks(self, data, bbox_coords, timestamp, road_mask):
|
| 716 |
-
"""
|
| 717 |
-
Detect trucks using corrected Fisser et al. methodology.
|
| 718 |
-
|
| 719 |
-
:param data: (H, W, 5) array — [B04, B03, B02, B08, CLM]
|
| 720 |
-
:param bbox_coords: [min_lat, min_lon, max_lat, max_lon]
|
| 721 |
-
:param timestamp: str ISO timestamp
|
| 722 |
-
:param road_mask: (H, W) binary mask of road pixels
|
| 723 |
-
:return: list of detection dicts
|
| 724 |
-
"""
|
| 725 |
min_lat, min_lon, max_lat, max_lon = bbox_coords
|
| 726 |
H, W = data.shape[:2]
|
| 727 |
|
| 728 |
-
# 1. Build feature stack (corrected order)
|
| 729 |
feat = build_feature_stack(data)
|
| 730 |
feature_stack = feat["feature_stack"]
|
| 731 |
|
| 732 |
-
|
| 733 |
-
probs, prediction = classify(feature_stack, road_mask, self.rf_model)
|
| 734 |
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
lon_arr = np.linspace(min_lon, max_lon, W) # left to right
|
| 738 |
|
| 739 |
-
# 4. Object extraction (corrected)
|
| 740 |
extractor = ObjectExtractor(probs, lat_arr, lon_arr)
|
| 741 |
detections = extractor.extract(prediction)
|
| 742 |
|
| 743 |
-
# 5. Add timestamp and save crops
|
| 744 |
for det in detections:
|
| 745 |
det["timestamp"] = timestamp
|
| 746 |
-
|
| 747 |
-
self._save_crop(data, det, H, W, min_lat, min_lon, max_lat, max_lon)
|
| 748 |
-
except Exception as e:
|
| 749 |
-
logger.warning(f"Could not save crop for {det['id']}: {e}")
|
| 750 |
|
| 751 |
return detections
|
| 752 |
|
| 753 |
-
def _save_crop(self, data, det, H, W
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
rgb = data[y0:y1, x0:x1, :3].astype(np.float32)
|
| 763 |
-
rgb = rescale_s2(rgb)
|
| 764 |
-
rgb = (np.clip(rgb, 0, 0.3) / 0.3 * 255).astype(np.uint8)
|
| 765 |
-
|
| 766 |
-
path = os.path.join(DETECTION_DIR, det["id"])
|
| 767 |
-
imageio.imwrite(path, rgb)
|
| 768 |
-
|
| 769 |
|
| 770 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 771 |
# FastAPI Application
|
|
@@ -778,14 +342,12 @@ app.add_middleware(
|
|
| 778 |
|
| 779 |
engine = ARGUSEngine()
|
| 780 |
|
| 781 |
-
|
| 782 |
class AnalyzeRequest(BaseModel):
|
| 783 |
-
bbox: List[float]
|
| 784 |
label: str = "New Mission"
|
| 785 |
months: int = 4
|
| 786 |
max_frames: int = 10
|
| 787 |
|
| 788 |
-
|
| 789 |
@app.post("/api/analyze")
|
| 790 |
async def analyze(req: AnalyzeRequest):
|
| 791 |
async def event_generator():
|
|
@@ -797,18 +359,16 @@ async def analyze(req: AnalyzeRequest):
|
|
| 797 |
|
| 798 |
min_lat, min_lon, max_lat, max_lon = req.bbox
|
| 799 |
if abs(max_lat - min_lat) > 0.5 or abs(max_lon - min_lon) > 0.5:
|
| 800 |
-
yield json.dumps({"type": "error", "message": "AOI too large.
|
| 801 |
return
|
| 802 |
|
| 803 |
-
# 1. Roads
|
| 804 |
yield progress("Running Road Discovery Pipeline...", 10)
|
| 805 |
-
roads = engine.fetch_roads(req.bbox
|
| 806 |
if roads.empty:
|
| 807 |
-
yield json.dumps({"type": "error", "message": "No major roads found
|
| 808 |
return
|
| 809 |
-
yield progress(f"Found {len(roads)} road
|
| 810 |
|
| 811 |
-
# 2. Satellite imagery
|
| 812 |
sh_bbox = BBox(bbox=[min_lon, min_lat, max_lon, max_lat], crs=CRS.WGS84)
|
| 813 |
yield progress("Searching Copernicus catalog...", 30)
|
| 814 |
|
|
@@ -820,7 +380,6 @@ async def analyze(req: AnalyzeRequest):
|
|
| 820 |
"s2l2a", service_url=CONFIG.sh_base_url,
|
| 821 |
)
|
| 822 |
|
| 823 |
-
# Use CQL2 filter to avoid cloudy scenes and get unique time slots
|
| 824 |
search_results = list(catalog.search(
|
| 825 |
cdse_collection, bbox=sh_bbox,
|
| 826 |
datetime=f"{start_date.strftime('%Y-%m-%dT00:00:00Z')}/{end_date.strftime('%Y-%m-%dT23:59:59Z')}",
|
|
@@ -828,25 +387,27 @@ async def analyze(req: AnalyzeRequest):
|
|
| 828 |
fields={"include": ["properties.datetime", "id"], "exclude": []}
|
| 829 |
))
|
| 830 |
|
| 831 |
-
# Group by unique date (YYYY-MM-DD) to ensure trend diversity
|
| 832 |
unique_scenes = {}
|
| 833 |
for res in search_results:
|
| 834 |
date_key = res["properties"]["datetime"][:10]
|
| 835 |
if date_key not in unique_scenes:
|
| 836 |
unique_scenes[date_key] = res
|
| 837 |
|
| 838 |
-
# Convert back to sorted list (latest first) and respect max_frames
|
| 839 |
final_obs = [unique_scenes[d] for d in sorted(unique_scenes.keys(), reverse=True)]
|
| 840 |
final_obs = final_obs[:req.max_frames]
|
| 841 |
|
| 842 |
if not final_obs:
|
| 843 |
-
yield json.dumps({"type": "error", "message":
|
| 844 |
return
|
| 845 |
|
| 846 |
-
yield progress(f"Found {len(final_obs)} unique clear overpasses.
|
| 847 |
|
| 848 |
-
#
|
| 849 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 850 |
function setup() {
|
| 851 |
return {
|
| 852 |
input: ["B02", "B03", "B04", "B08", "CLM"],
|
|
@@ -855,91 +416,38 @@ function setup() {
|
|
| 855 |
}
|
| 856 |
function evaluatePixel(s) {
|
| 857 |
return [s.B04, s.B03, s.B02, s.B08, s.CLM];
|
| 858 |
-
}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
# then parallelize the rest.
|
| 864 |
-
|
| 865 |
-
detections = []
|
| 866 |
-
max_frames = min(len(final_obs), max(1, req.max_frames))
|
| 867 |
-
|
| 868 |
-
if max_frames == 0:
|
| 869 |
-
yield json.dumps({"type": "result", "mission_id": "none", "message": "No frames."}) + "\n"
|
| 870 |
return
|
| 871 |
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
req_sh = SentinelHubRequest(
|
| 878 |
-
evalscript=evalscript,
|
| 879 |
-
input_data=[SentinelHubRequest.input_data(
|
| 880 |
-
data_collection=cdse_collection,
|
| 881 |
-
time_interval=(date_str, date_str),
|
| 882 |
-
)],
|
| 883 |
-
responses=[SentinelHubRequest.output_response("default", MimeType.TIFF)],
|
| 884 |
-
bbox=sh_bbox, config=CONFIG,
|
| 885 |
-
)
|
| 886 |
-
|
| 887 |
-
data_list = req_sh.get_data()
|
| 888 |
-
if not data_list:
|
| 889 |
-
return idx, date_str, []
|
| 890 |
-
|
| 891 |
-
frame_sat_data = data_list[0]
|
| 892 |
-
# Note: road_mask is provided via closure or passed.
|
| 893 |
-
# We'll calculate it once inside the loop if not yet done.
|
| 894 |
-
return idx, date_str, frame_sat_data
|
| 895 |
-
except Exception as ex:
|
| 896 |
-
logger.error(f"Worker error on frame {idx}: {ex}")
|
| 897 |
-
return idx, None, None
|
| 898 |
-
|
| 899 |
-
# 1. Process Frame 0 to get the road_mask (Sequential/Seed)
|
| 900 |
-
yield progress(f"Analyzing Seed Frame (1/{max_frames}) — {final_obs[0]['properties']['datetime'][:10]}", 40)
|
| 901 |
-
_, _, seed_data = _worker(0, final_obs[0])
|
| 902 |
-
|
| 903 |
-
if seed_data is None:
|
| 904 |
-
yield json.dumps({"type": "error", "message": "Failed to acquire seed spectral data."}) + "\n"
|
| 905 |
-
return
|
| 906 |
-
|
| 907 |
-
# Generate Road Mask once
|
| 908 |
from rasterio import features as rio_features, transform as rio_transform
|
| 909 |
roads_buf = roads.to_crs(epsg=3857).buffer(20).to_crs(epsg=4326)
|
| 910 |
-
h, w = seed_data.shape[:2]
|
| 911 |
trans = rio_transform.from_bounds(min_lon, min_lat, max_lon, max_lat, w, h)
|
| 912 |
road_mask = rio_features.rasterize(
|
| 913 |
[(geom.__geo_interface__, 1) for geom in roads_buf.geometry],
|
| 914 |
out_shape=(h, w), transform=trans, fill=0, all_touched=True,
|
| 915 |
)
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
detections
|
| 920 |
-
|
| 921 |
-
# 2. Parallelize remaining frames
|
| 922 |
-
if max_frames > 1:
|
| 923 |
-
yield progress(f"Dispatching Parallel Telemetry Stack ({max_frames-1} frames)...", 45)
|
| 924 |
-
|
| 925 |
-
completed_count = 1
|
| 926 |
-
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 927 |
-
futures = {executor.submit(_worker, i, final_obs[i]): i for i in range(1, max_frames)}
|
| 928 |
-
|
| 929 |
-
for future in as_completed(futures):
|
| 930 |
-
idx, d_str, f_data = future.result()
|
| 931 |
-
completed_count += 1
|
| 932 |
-
|
| 933 |
-
if f_data is not None:
|
| 934 |
-
# Detect
|
| 935 |
-
f_dets = engine.detect_trucks(f_data, req.bbox, d_str, road_mask)
|
| 936 |
-
detections.extend(f_dets)
|
| 937 |
-
|
| 938 |
-
pct = 45 + int((completed_count / max_frames) * 50)
|
| 939 |
-
yield progress(f"Analyzing Orbital Stack [{completed_count}/{max_frames}]", pct)
|
| 940 |
-
|
| 941 |
-
# Finalise
|
| 942 |
mission_id = str(int(time.time()))
|
|
|
|
| 943 |
engine.history.append({
|
| 944 |
"mission_id": mission_id,
|
| 945 |
"label": req.label,
|
|
@@ -963,15 +471,6 @@ function evaluatePixel(s) {
|
|
| 963 |
|
| 964 |
return StreamingResponse(event_generator(), media_type="application/x-ndjson")
|
| 965 |
|
| 966 |
-
|
| 967 |
-
@app.get("/api/roads")
|
| 968 |
-
async def get_roads(min_lat: float, min_lon: float, max_lat: float, max_lon: float):
|
| 969 |
-
roads = engine.fetch_roads([min_lat, min_lon, max_lat, max_lon])
|
| 970 |
-
if roads.empty:
|
| 971 |
-
return {"type": "FeatureCollection", "features": []}
|
| 972 |
-
return json.loads(roads.to_json())
|
| 973 |
-
|
| 974 |
-
|
| 975 |
@app.get("/api/sites")
|
| 976 |
async def get_sites():
|
| 977 |
sites = [
|
|
@@ -983,95 +482,14 @@ async def get_sites():
|
|
| 983 |
}
|
| 984 |
for s in FEATURED_SITES
|
| 985 |
]
|
| 986 |
-
|
| 987 |
-
{
|
| 988 |
"id": h["mission_id"], "name": h["label"],
|
| 989 |
"lat": (h["bbox"][0] + h["bbox"][2]) / 2,
|
| 990 |
"lng": (h["bbox"][1] + h["bbox"][3]) / 2,
|
| 991 |
-
"bbox": h["bbox"], "country": "
|
| 992 |
-
}
|
| 993 |
-
for h in engine.history
|
| 994 |
-
]
|
| 995 |
-
return sites + history_sites
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
@app.get("/api/feed")
|
| 999 |
-
async def get_feed():
|
| 1000 |
-
feed = []
|
| 1001 |
-
for h in engine.history:
|
| 1002 |
-
for d in h["detections"][:5]:
|
| 1003 |
-
feed.append({
|
| 1004 |
-
"alert_id": f"alert_{d['id']}",
|
| 1005 |
-
"site": {"id": h["mission_id"], "name": h["label"], "country": "ROI"},
|
| 1006 |
-
"status": "WARNING",
|
| 1007 |
-
"timestamp": d["timestamp"],
|
| 1008 |
-
"change_classification": {"change_type": "truck_movement", "confidence": d["confidence"]},
|
| 1009 |
-
"detection": {
|
| 1010 |
-
"anomaly_score": round(d["confidence"] * 100, 1),
|
| 1011 |
-
"date_before": "Baseline",
|
| 1012 |
-
"date_after": d["timestamp"],
|
| 1013 |
-
},
|
| 1014 |
-
})
|
| 1015 |
-
return sorted(feed, key=lambda x: x["timestamp"], reverse=True)
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
@app.get("/api/analytics/trends")
|
| 1019 |
-
async def get_trends(from_date: str = None, to_date: str = None, site_ids: str = None):
|
| 1020 |
-
"""Aggregate detections across history by day, grouped by mission for comparison."""
|
| 1021 |
-
# site_ids can be a comma-separated list of mission IDs
|
| 1022 |
-
requested_ids = site_ids.split(",") if site_ids else []
|
| 1023 |
-
|
| 1024 |
-
# 1. Collect all unique dates in the range to build a consistent X-axis
|
| 1025 |
-
all_dates = set()
|
| 1026 |
-
missions_data = []
|
| 1027 |
-
|
| 1028 |
-
for mission in engine.history:
|
| 1029 |
-
m_id = mission["mission_id"]
|
| 1030 |
-
if requested_ids and m_id not in requested_ids:
|
| 1031 |
-
continue
|
| 1032 |
-
|
| 1033 |
-
m_counts = {}
|
| 1034 |
-
for det in mission["detections"]:
|
| 1035 |
-
date_key = det["timestamp"][:10]
|
| 1036 |
-
if from_date and date_key < from_date: continue
|
| 1037 |
-
if to_date and date_key > to_date: continue
|
| 1038 |
-
|
| 1039 |
-
all_dates.add(date_key)
|
| 1040 |
-
m_counts[date_key] = m_counts.get(date_key, 0) + 1
|
| 1041 |
-
|
| 1042 |
-
missions_data.append({
|
| 1043 |
-
"id": m_id,
|
| 1044 |
-
"label": mission["label"],
|
| 1045 |
-
"counts": m_counts
|
| 1046 |
})
|
| 1047 |
-
|
| 1048 |
-
sorted_dates = sorted(list(all_dates))
|
| 1049 |
-
|
| 1050 |
-
# 2. Build aligned datasets for Chart.js
|
| 1051 |
-
datasets = []
|
| 1052 |
-
# Predefined colors for comparison
|
| 1053 |
-
colors = ["#3b82f6", "#f59e0b", "#10b981", "#ef4444", "#a855f7", "#ec4899"]
|
| 1054 |
-
|
| 1055 |
-
for i, m in enumerate(missions_data):
|
| 1056 |
-
aligned_data = [m["counts"].get(d, 0) for d in sorted_dates]
|
| 1057 |
-
datasets.append({
|
| 1058 |
-
"label": m["label"],
|
| 1059 |
-
"data": aligned_data,
|
| 1060 |
-
"borderColor": colors[i % len(colors)],
|
| 1061 |
-
"backgroundColor": f"{colors[i % len(colors)]}22" # 13% opacity
|
| 1062 |
-
})
|
| 1063 |
-
|
| 1064 |
-
total_detections = sum(sum(d["data"]) for d in datasets)
|
| 1065 |
-
|
| 1066 |
-
return {
|
| 1067 |
-
"labels": sorted_dates,
|
| 1068 |
-
"datasets": datasets,
|
| 1069 |
-
"summary": {
|
| 1070 |
-
"total_detections": total_detections,
|
| 1071 |
-
"missions_count": len(datasets)
|
| 1072 |
-
}
|
| 1073 |
-
}
|
| 1074 |
-
|
| 1075 |
|
| 1076 |
@app.get("/api/detections/{mission_id}")
|
| 1077 |
async def get_detections(mission_id: str):
|
|
@@ -1080,53 +498,97 @@ async def get_detections(mission_id: str):
|
|
| 1080 |
raise HTTPException(status_code=404, detail="Mission not found")
|
| 1081 |
return mission["detections"]
|
| 1082 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1083 |
|
| 1084 |
-
class AuthRequest(BaseModel):
|
| 1085 |
-
client_id: str
|
| 1086 |
-
client_secret: str
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
@app.post("/api/auth")
|
| 1090 |
-
async def authenticate(req: AuthRequest):
|
| 1091 |
-
"""Validate and store Copernicus credentials at runtime."""
|
| 1092 |
-
try:
|
| 1093 |
-
# Update the running config
|
| 1094 |
-
CONFIG.sh_client_id = req.client_id
|
| 1095 |
-
CONFIG.sh_client_secret = req.client_secret
|
| 1096 |
-
CONFIG.save()
|
| 1097 |
-
|
| 1098 |
-
# Test the credentials by requesting a token
|
| 1099 |
-
from sentinelhub import SentinelHubSession
|
| 1100 |
-
session = SentinelHubSession(config=CONFIG)
|
| 1101 |
-
_ = session.token
|
| 1102 |
-
|
| 1103 |
-
logger.info("Copernicus credentials updated and verified via UI.")
|
| 1104 |
-
return {"status": "success", "message": "Copernicus link established."}
|
| 1105 |
-
|
| 1106 |
-
except Exception as e:
|
| 1107 |
-
logger.error(f"Auth failed: {e}")
|
| 1108 |
-
# Revert to env vars if UI credentials fail
|
| 1109 |
-
CONFIG.sh_client_id = os.getenv("COPERNICUS_CLIENT_ID", "")
|
| 1110 |
-
CONFIG.sh_client_secret = os.getenv("COPERNICUS_CLIENT_SECRET", "")
|
| 1111 |
-
CONFIG.save()
|
| 1112 |
-
return {"status": "error", "message": str(e)}
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
-
# Serve static detections
|
| 1116 |
app.mount("/detections", StaticFiles(directory=DETECTION_DIR), name="detections")
|
| 1117 |
-
|
| 1118 |
-
# Serve frontend
|
| 1119 |
app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")
|
| 1120 |
|
| 1121 |
-
|
| 1122 |
if __name__ == "__main__":
|
| 1123 |
-
|
| 1124 |
-
from sentinelhub import SentinelHubSession
|
| 1125 |
-
sh_session = SentinelHubSession(config=CONFIG)
|
| 1126 |
-
_ = sh_session.token
|
| 1127 |
-
logger.info("Copernicus Data Space Authentication: SUCCESS")
|
| 1128 |
-
except Exception as e:
|
| 1129 |
-
logger.error(f"Copernicus Data Space Authentication: FAILED - {e}")
|
| 1130 |
-
logger.warning("System will start, but satellite monitoring may be degraded.")
|
| 1131 |
-
|
| 1132 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import json
|
|
|
|
| 15 |
from urllib3.util.retry import Retry
|
| 16 |
|
| 17 |
from fastapi import FastAPI, HTTPException
|
| 18 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 19 |
from fastapi.staticfiles import StaticFiles
|
| 20 |
from fastapi.middleware.cors import CORSMiddleware
|
| 21 |
from pydantic import BaseModel
|
|
|
|
| 37 |
logging.basicConfig(level=logging.INFO)
|
| 38 |
logger = logging.getLogger("ARGUS")
|
| 39 |
|
| 40 |
+
SECONDS_OFFSET_B02_B04 = 1.01
|
| 41 |
|
| 42 |
+
# Hugging Face Spaces için Storage
|
| 43 |
+
DATA_DIR = os.path.join(os.getcwd(), "data")
|
| 44 |
DETECTION_DIR = os.path.join(DATA_DIR, "sentinel_data/detections")
|
| 45 |
os.makedirs(DETECTION_DIR, exist_ok=True)
|
| 46 |
|
| 47 |
+
# OSM verileri için cache
|
| 48 |
+
os.makedirs(os.path.join(DATA_DIR, "osm_cache"), exist_ok=True)
|
| 49 |
+
|
| 50 |
OVERPASS_MIRRORS = [
|
| 51 |
"https://lz4.overpass-api.de/api/interpreter",
|
| 52 |
"https://z.overpass-api.de/api/interpreter",
|
|
|
|
| 54 |
"https://overpass-api.de/api/interpreter",
|
| 55 |
]
|
| 56 |
|
|
|
|
| 57 |
_session = requests.Session()
|
| 58 |
_retry = Retry(
|
| 59 |
total=2, backoff_factor=1.0,
|
|
|
|
| 69 |
ox.settings.overpass_rate_limit = False
|
| 70 |
ox.settings.max_query_area_size = 1_000_000_000_000
|
| 71 |
ox.settings.log_console = False
|
|
|
|
| 72 |
ox.settings.use_cache = True
|
| 73 |
ox.settings.cache_folder = os.path.join(DATA_DIR, "osm_cache")
|
| 74 |
|
| 75 |
+
# Copernicus Data Space config - Hugging Face env'den oku
|
| 76 |
CONFIG = SHConfig()
|
| 77 |
+
CONFIG.sh_client_id = os.getenv("COPERNICUS_CLIENT_ID", "")
|
| 78 |
+
CONFIG.sh_client_secret = os.getenv("COPERNICUS_CLIENT_SECRET", "")
|
| 79 |
CONFIG.sh_base_url = "https://sh.dataspace.copernicus.eu"
|
| 80 |
CONFIG.sh_token_url = "https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token"
|
|
|
|
|
|
|
| 81 |
CONFIG.cache_dir = os.path.join(DATA_DIR, "sh_cache")
|
| 82 |
os.makedirs(CONFIG.cache_dir, exist_ok=True)
|
| 83 |
|
|
|
|
| 84 |
logger.info(f"DrishX Storage Link: {DATA_DIR}")
|
| 85 |
+
|
| 86 |
+
# Hugging Face Spaces'te token'ı env'den al
|
| 87 |
+
if os.getenv("COPERNICUS_TOKEN"):
|
| 88 |
+
CONFIG.sh_token = os.getenv("COPERNICUS_TOKEN")
|
| 89 |
+
logger.info("Copernicus token loaded from environment")
|
| 90 |
|
| 91 |
FEATURED_SITES = [
|
| 92 |
+
{"id": "v1", "name": "Braunschweig A7", "bbox": [52.25, 10.45, 52.32, 10.55], "country": "Germany", "type": "high_volume"},
|
| 93 |
+
{"id": "v2", "name": "Frankfurt A3", "bbox": [50.05, 8.55, 50.12, 8.65], "country": "Germany", "type": "high_volume"},
|
| 94 |
+
{"id": "v3", "name": "Karlsruhe A5", "bbox": [48.95, 8.35, 49.05, 8.45], "country": "Germany", "type": "standard"},
|
| 95 |
]
|
| 96 |
|
|
|
|
| 97 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 98 |
+
# Helper functions (kısaltılmış - aynı kod)
|
| 99 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 100 |
|
| 101 |
def normalized_ratio(a, b):
|
|
|
|
| 102 |
denom = a + b
|
| 103 |
with np.errstate(divide="ignore", invalid="ignore"):
|
| 104 |
result = np.where(denom != 0, (a - b) / denom, 0.0)
|
| 105 |
return result.astype(np.float32)
|
| 106 |
|
|
|
|
| 107 |
def rescale_s2(bands):
|
|
|
|
| 108 |
bands = bands.astype(np.float32)
|
| 109 |
+
if np.nanmax(bands) > 10:
|
| 110 |
bands /= 10000.0
|
| 111 |
return bands
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def pick_arr_subset(arr, y, x, size):
|
|
|
|
| 114 |
size_low = size // 2
|
| 115 |
size_up = size // 2
|
| 116 |
if size_low + size_up < size:
|
|
|
|
| 125 |
return arr[:, ymin:ymax, xmin:xmax]
|
| 126 |
return arr
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
def build_feature_stack(data):
|
| 129 |
+
R = data[:, :, 0].astype(np.float32)
|
| 130 |
+
G = data[:, :, 1].astype(np.float32)
|
| 131 |
+
B = data[:, :, 2].astype(np.float32)
|
| 132 |
+
NIR = data[:, :, 3].astype(np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
CLM = data[:, :, 4]
|
| 134 |
|
|
|
|
| 135 |
bands = np.stack([R, G, B, NIR], axis=0)
|
| 136 |
bands = rescale_s2(bands)
|
| 137 |
R, G, B, NIR = bands[0], bands[1], bands[2], bands[3]
|
| 138 |
|
|
|
|
| 139 |
cloud = CLM > 0
|
| 140 |
R[cloud] = np.nan
|
| 141 |
G[cloud] = np.nan
|
|
|
|
| 145 |
H, W = R.shape
|
| 146 |
fs = np.zeros((7, H, W), dtype=np.float32)
|
| 147 |
|
|
|
|
| 148 |
if np.any(~cloud):
|
| 149 |
import warnings
|
| 150 |
with warnings.catch_warnings():
|
| 151 |
warnings.simplefilter("ignore", category=RuntimeWarning)
|
|
|
|
| 152 |
fs[0] = np.nanvar(np.stack([R, G, B], axis=0), axis=0, ddof=0)
|
|
|
|
|
|
|
| 153 |
fs[1] = normalized_ratio(R, B)
|
| 154 |
fs[2] = normalized_ratio(G, B)
|
|
|
|
|
|
|
| 155 |
fs[3] = R - np.nanmean(R)
|
| 156 |
fs[4] = G - np.nanmean(G)
|
| 157 |
fs[5] = B - np.nanmean(B)
|
| 158 |
fs[6] = NIR - np.nanmean(NIR)
|
| 159 |
else:
|
|
|
|
| 160 |
fs.fill(np.nan)
|
| 161 |
|
|
|
|
| 162 |
nan_mask = np.isnan(fs[3])
|
| 163 |
fs[:, nan_mask] = np.nan
|
| 164 |
|
|
|
|
| 168 |
"cloud_mask": cloud,
|
| 169 |
}
|
| 170 |
|
|
|
|
| 171 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 172 |
+
# RF Model (basitleştirilmiş proxy)
|
| 173 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 174 |
|
|
|
|
|
|
|
| 175 |
_rf_model = None
|
| 176 |
|
|
|
|
| 177 |
def load_rf_model(path=None):
|
|
|
|
| 178 |
global _rf_model
|
| 179 |
+
return None # Hugging Face'te model yok, proxy kullan
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
def proxy_classify(feature_stack, road_mask):
|
| 182 |
+
fs = feature_stack
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
H, W = fs.shape[1], fs.shape[2]
|
| 184 |
probs = np.zeros((4, H, W), dtype=np.float32)
|
| 185 |
|
|
|
|
| 217 |
|
| 218 |
return probs, classification
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 221 |
+
# ObjectExtractor (kısaltılmış)
|
| 222 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 223 |
|
| 224 |
class ObjectExtractor:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
def __init__(self, probabilities, lat_arr, lon_arr):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
self.probabilities = probabilities
|
| 227 |
self.lat = lat_arr
|
| 228 |
self.lon = lon_arr
|
| 229 |
|
| 230 |
def extract(self, predictions_arr):
|
|
|
|
| 231 |
preds = predictions_arr.copy()
|
| 232 |
probs = self.probabilities.copy()
|
| 233 |
+
preds[preds == 1] = 0
|
|
|
|
| 234 |
blue_ys, blue_xs = np.where(preds == 2)
|
| 235 |
detections = []
|
| 236 |
sub_size = 9
|
|
|
|
| 246 |
|
| 247 |
half_idx_y = y_blue if subset_9.shape[0] < sub_size else subset_9.shape[0] // 2
|
| 248 |
half_idx_x = x_blue if subset_9.shape[1] < sub_size else subset_9.shape[1] // 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
|
|
|
| 250 |
if not all(v in subset_9 for v in [2, 3, 4]):
|
| 251 |
continue
|
| 252 |
|
| 253 |
+
det = {
|
| 254 |
+
"lat": float(self.lat[y_blue]),
|
| 255 |
+
"lon": float(self.lon[x_blue]),
|
| 256 |
+
"confidence": 0.7,
|
| 257 |
+
"speed_kmh": 80.0,
|
| 258 |
+
"heading": 0.0,
|
| 259 |
+
"timestamp": datetime.now().isoformat(),
|
| 260 |
+
"id": f"truck_{int(time.time()*1000)}_{y_blue}_{x_blue}.png",
|
| 261 |
+
}
|
| 262 |
+
detections.append(det)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
return detections
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 267 |
# ARGUS Engine
|
| 268 |
# ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
| 273 |
self.rf_model = load_rf_model()
|
| 274 |
|
| 275 |
def fetch_roads(self, bbox_coords, progress_cb=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
min_lat, min_lon, max_lat, max_lon = bbox_coords
|
| 277 |
center_lat = (min_lat + max_lat) / 2
|
| 278 |
center_lon = (min_lon + max_lon) / 2
|
|
|
|
| 281 |
lon_span = (max_lon - min_lon) * 111000 * np.cos(np.radians(center_lat))
|
| 282 |
dist_m = int(max(lat_span, lon_span) * 0.6) + 1000
|
| 283 |
|
| 284 |
+
for mirror in OVERPASS_MIRRORS:
|
|
|
|
|
|
|
|
|
|
| 285 |
ox.settings.overpass_url = mirror
|
| 286 |
try:
|
| 287 |
graph = ox.graph_from_point(
|
|
|
|
| 290 |
retain_all=False, truncate_by_edge=True,
|
| 291 |
)
|
| 292 |
roads = ox.graph_to_gdfs(graph, nodes=False)
|
| 293 |
+
major_types = ["motorway", "trunk", "primary", "secondary", "motorway_link", "trunk_link"]
|
|
|
|
|
|
|
|
|
|
| 294 |
roads = roads[roads["highway"].isin(major_types)].copy()
|
| 295 |
if not roads.empty:
|
|
|
|
| 296 |
return roads
|
| 297 |
except Exception as e:
|
| 298 |
+
logger.warning(f"Mirror failed: {e}")
|
| 299 |
time.sleep(1)
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
return gpd.GeoDataFrame()
|
| 302 |
|
| 303 |
def detect_trucks(self, data, bbox_coords, timestamp, road_mask):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
min_lat, min_lon, max_lat, max_lon = bbox_coords
|
| 305 |
H, W = data.shape[:2]
|
| 306 |
|
|
|
|
| 307 |
feat = build_feature_stack(data)
|
| 308 |
feature_stack = feat["feature_stack"]
|
| 309 |
|
| 310 |
+
probs, prediction = proxy_classify(feature_stack, road_mask)
|
|
|
|
| 311 |
|
| 312 |
+
lat_arr = np.linspace(max_lat, min_lat, H)
|
| 313 |
+
lon_arr = np.linspace(min_lon, max_lon, W)
|
|
|
|
| 314 |
|
|
|
|
| 315 |
extractor = ObjectExtractor(probs, lat_arr, lon_arr)
|
| 316 |
detections = extractor.extract(prediction)
|
| 317 |
|
|
|
|
| 318 |
for det in detections:
|
| 319 |
det["timestamp"] = timestamp
|
| 320 |
+
self._save_crop(data, det, H, W)
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
return detections
|
| 323 |
|
| 324 |
+
def _save_crop(self, data, det, H, W):
|
| 325 |
+
try:
|
| 326 |
+
rgb = data[:20, :20, :3].astype(np.float32)
|
| 327 |
+
rgb = rescale_s2(rgb)
|
| 328 |
+
rgb = (np.clip(rgb, 0, 0.3) / 0.3 * 255).astype(np.uint8)
|
| 329 |
+
path = os.path.join(DETECTION_DIR, det["id"])
|
| 330 |
+
imageio.imwrite(path, rgb)
|
| 331 |
+
except Exception as e:
|
| 332 |
+
logger.warning(f"Could not save crop: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 335 |
# FastAPI Application
|
|
|
|
| 342 |
|
| 343 |
engine = ARGUSEngine()
|
| 344 |
|
|
|
|
| 345 |
class AnalyzeRequest(BaseModel):
|
| 346 |
+
bbox: List[float]
|
| 347 |
label: str = "New Mission"
|
| 348 |
months: int = 4
|
| 349 |
max_frames: int = 10
|
| 350 |
|
|
|
|
| 351 |
@app.post("/api/analyze")
|
| 352 |
async def analyze(req: AnalyzeRequest):
|
| 353 |
async def event_generator():
|
|
|
|
| 359 |
|
| 360 |
min_lat, min_lon, max_lat, max_lon = req.bbox
|
| 361 |
if abs(max_lat - min_lat) > 0.5 or abs(max_lon - min_lon) > 0.5:
|
| 362 |
+
yield json.dumps({"type": "error", "message": "AOI too large."}) + "\n"
|
| 363 |
return
|
| 364 |
|
|
|
|
| 365 |
yield progress("Running Road Discovery Pipeline...", 10)
|
| 366 |
+
roads = engine.fetch_roads(req.bbox)
|
| 367 |
if roads.empty:
|
| 368 |
+
yield json.dumps({"type": "error", "message": "No major roads found."}) + "\n"
|
| 369 |
return
|
| 370 |
+
yield progress(f"Found {len(roads)} road corridors.", 25)
|
| 371 |
|
|
|
|
| 372 |
sh_bbox = BBox(bbox=[min_lon, min_lat, max_lon, max_lat], crs=CRS.WGS84)
|
| 373 |
yield progress("Searching Copernicus catalog...", 30)
|
| 374 |
|
|
|
|
| 380 |
"s2l2a", service_url=CONFIG.sh_base_url,
|
| 381 |
)
|
| 382 |
|
|
|
|
| 383 |
search_results = list(catalog.search(
|
| 384 |
cdse_collection, bbox=sh_bbox,
|
| 385 |
datetime=f"{start_date.strftime('%Y-%m-%dT00:00:00Z')}/{end_date.strftime('%Y-%m-%dT23:59:59Z')}",
|
|
|
|
| 387 |
fields={"include": ["properties.datetime", "id"], "exclude": []}
|
| 388 |
))
|
| 389 |
|
|
|
|
| 390 |
unique_scenes = {}
|
| 391 |
for res in search_results:
|
| 392 |
date_key = res["properties"]["datetime"][:10]
|
| 393 |
if date_key not in unique_scenes:
|
| 394 |
unique_scenes[date_key] = res
|
| 395 |
|
|
|
|
| 396 |
final_obs = [unique_scenes[d] for d in sorted(unique_scenes.keys(), reverse=True)]
|
| 397 |
final_obs = final_obs[:req.max_frames]
|
| 398 |
|
| 399 |
if not final_obs:
|
| 400 |
+
yield json.dumps({"type": "error", "message": "No clear imagery found."}) + "\n"
|
| 401 |
return
|
| 402 |
|
| 403 |
+
yield progress(f"Found {len(final_obs)} unique clear overpasses.", 40)
|
| 404 |
|
| 405 |
+
# İlk frame'i işle
|
| 406 |
+
first_obs = final_obs[0]
|
| 407 |
+
date_str = first_obs["properties"]["datetime"]
|
| 408 |
+
|
| 409 |
+
req_sh = SentinelHubRequest(
|
| 410 |
+
evalscript="""//VERSION=3
|
| 411 |
function setup() {
|
| 412 |
return {
|
| 413 |
input: ["B02", "B03", "B04", "B08", "CLM"],
|
|
|
|
| 416 |
}
|
| 417 |
function evaluatePixel(s) {
|
| 418 |
return [s.B04, s.B03, s.B02, s.B08, s.CLM];
|
| 419 |
+
}""",
|
| 420 |
+
input_data=[SentinelHubRequest.input_data(
|
| 421 |
+
data_collection=cdse_collection,
|
| 422 |
+
time_interval=(date_str, date_str),
|
| 423 |
+
)],
|
| 424 |
+
responses=[SentinelHubRequest.output_response("default", MimeType.TIFF)],
|
| 425 |
+
bbox=sh_bbox, config=CONFIG,
|
| 426 |
+
size=(512, 512), # Hugging Face için boyut sınırı
|
| 427 |
+
)
|
| 428 |
|
| 429 |
+
data_list = req_sh.get_data()
|
| 430 |
+
if not data_list:
|
| 431 |
+
yield json.dumps({"type": "error", "message": "Failed to get satellite data."}) + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
return
|
| 433 |
|
| 434 |
+
seed_data = data_list[0]
|
| 435 |
+
h, w = seed_data.shape[:2]
|
| 436 |
+
|
| 437 |
+
# Road mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
from rasterio import features as rio_features, transform as rio_transform
|
| 439 |
roads_buf = roads.to_crs(epsg=3857).buffer(20).to_crs(epsg=4326)
|
|
|
|
| 440 |
trans = rio_transform.from_bounds(min_lon, min_lat, max_lon, max_lat, w, h)
|
| 441 |
road_mask = rio_features.rasterize(
|
| 442 |
[(geom.__geo_interface__, 1) for geom in roads_buf.geometry],
|
| 443 |
out_shape=(h, w), transform=trans, fill=0, all_touched=True,
|
| 444 |
)
|
| 445 |
+
|
| 446 |
+
seed_dets = engine.detect_trucks(seed_data, req.bbox, date_str, road_mask)
|
| 447 |
+
|
| 448 |
+
detections = seed_dets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
mission_id = str(int(time.time()))
|
| 450 |
+
|
| 451 |
engine.history.append({
|
| 452 |
"mission_id": mission_id,
|
| 453 |
"label": req.label,
|
|
|
|
| 471 |
|
| 472 |
return StreamingResponse(event_generator(), media_type="application/x-ndjson")
|
| 473 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
@app.get("/api/sites")
|
| 475 |
async def get_sites():
|
| 476 |
sites = [
|
|
|
|
| 482 |
}
|
| 483 |
for s in FEATURED_SITES
|
| 484 |
]
|
| 485 |
+
for h in engine.history:
|
| 486 |
+
sites.append({
|
| 487 |
"id": h["mission_id"], "name": h["label"],
|
| 488 |
"lat": (h["bbox"][0] + h["bbox"][2]) / 2,
|
| 489 |
"lng": (h["bbox"][1] + h["bbox"][3]) / 2,
|
| 490 |
+
"bbox": h["bbox"], "country": "ROI", "type": "history",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
})
|
| 492 |
+
return sites
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
@app.get("/api/detections/{mission_id}")
|
| 495 |
async def get_detections(mission_id: str):
|
|
|
|
| 498 |
raise HTTPException(status_code=404, detail="Mission not found")
|
| 499 |
return mission["detections"]
|
| 500 |
|
| 501 |
+
@app.get("/api/health")
|
| 502 |
+
async def health_check():
|
| 503 |
+
return {"status": "healthy", "detections": sum(len(h["detections"]) for h in engine.history)}
|
| 504 |
+
|
| 505 |
+
# Static files
|
| 506 |
+
os.makedirs("frontend", exist_ok=True)
|
| 507 |
+
with open("frontend/index.html", "w") as f:
|
| 508 |
+
f.write("""<!DOCTYPE html>
|
| 509 |
+
<html>
|
| 510 |
+
<head><title>ARGUS - DrishX</title>
|
| 511 |
+
<style>
|
| 512 |
+
body { font-family: system-ui; max-width: 800px; margin: 40px auto; padding: 20px; background: #0a0a0f; color: #e0e0e0; }
|
| 513 |
+
h1 { color: #3b82f6; }
|
| 514 |
+
button { background: #3b82f6; color: white; border: none; padding: 12px 24px; border-radius: 8px; cursor: pointer; }
|
| 515 |
+
input, textarea { background: #1a1a2e; color: white; border: 1px solid #333; padding: 8px; border-radius: 4px; width: 100%; margin: 8px 0; }
|
| 516 |
+
.card { background: #1a1a2e; padding: 20px; border-radius: 12px; margin: 16px 0; }
|
| 517 |
+
.status { color: #10b981; }
|
| 518 |
+
</style>
|
| 519 |
+
</head>
|
| 520 |
+
<body>
|
| 521 |
+
<h1>🚛 ARGUS - DrishX</h1>
|
| 522 |
+
<p>Corridor Intelligence & Truck Detection</p>
|
| 523 |
+
|
| 524 |
+
<div class="card">
|
| 525 |
+
<h3>Analyze Area</h3>
|
| 526 |
+
<input id="label" placeholder="Mission Label" value="Test Mission">
|
| 527 |
+
<textarea id="bbox" rows="2">[52.25, 10.45, 52.32, 10.55]</textarea>
|
| 528 |
+
<button onclick="analyze()">Start Analysis</button>
|
| 529 |
+
</div>
|
| 530 |
+
|
| 531 |
+
<div id="result" class="card" style="display:none;"></div>
|
| 532 |
+
<div id="progress" class="card" style="display:none;"></div>
|
| 533 |
+
|
| 534 |
+
<script>
|
| 535 |
+
async function analyze() {
|
| 536 |
+
const bbox = JSON.parse(document.getElementById('bbox').value);
|
| 537 |
+
const label = document.getElementById('label').value;
|
| 538 |
+
|
| 539 |
+
document.getElementById('result').style.display = 'none';
|
| 540 |
+
document.getElementById('progress').style.display = 'block';
|
| 541 |
+
document.getElementById('progress').innerHTML = 'Starting analysis...';
|
| 542 |
+
|
| 543 |
+
try {
|
| 544 |
+
const response = await fetch('/api/analyze', {
|
| 545 |
+
method: 'POST',
|
| 546 |
+
headers: { 'Content-Type': 'application/json' },
|
| 547 |
+
body: JSON.stringify({ bbox, label, months: 4, max_frames: 3 })
|
| 548 |
+
});
|
| 549 |
+
|
| 550 |
+
const reader = response.body.getReader();
|
| 551 |
+
const decoder = new TextDecoder();
|
| 552 |
+
let result = '';
|
| 553 |
+
|
| 554 |
+
while (true) {
|
| 555 |
+
const {done, value} = await reader.read();
|
| 556 |
+
if (done) break;
|
| 557 |
+
|
| 558 |
+
const chunk = decoder.decode(value);
|
| 559 |
+
const lines = chunk.split('\\n').filter(l => l.trim());
|
| 560 |
+
|
| 561 |
+
for (const line of lines) {
|
| 562 |
+
try {
|
| 563 |
+
const data = JSON.parse(line);
|
| 564 |
+
if (data.type === 'progress') {
|
| 565 |
+
document.getElementById('progress').innerHTML =
|
| 566 |
+
`<b>${data.percent}%</b> - ${data.message}`;
|
| 567 |
+
} else if (data.type === 'result') {
|
| 568 |
+
document.getElementById('result').style.display = 'block';
|
| 569 |
+
document.getElementById('result').innerHTML =
|
| 570 |
+
`<h3>✅ ${data.message}</h3>
|
| 571 |
+
<p>Detections: ${data.detection_count}</p>
|
| 572 |
+
<p>Mission ID: ${data.mission_id}</p>`;
|
| 573 |
+
document.getElementById('progress').style.display = 'none';
|
| 574 |
+
} else if (data.type === 'error') {
|
| 575 |
+
document.getElementById('progress').innerHTML =
|
| 576 |
+
`<b style="color:red;">❌ ${data.message}</b>`;
|
| 577 |
+
}
|
| 578 |
+
} catch(e) {}
|
| 579 |
+
}
|
| 580 |
+
}
|
| 581 |
+
} catch(err) {
|
| 582 |
+
document.getElementById('progress').innerHTML =
|
| 583 |
+
`<b style="color:red;">Error: ${err.message}</b>`;
|
| 584 |
+
}
|
| 585 |
+
}
|
| 586 |
+
</script>
|
| 587 |
+
</body>
|
| 588 |
+
</html>""")
|
| 589 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
app.mount("/detections", StaticFiles(directory=DETECTION_DIR), name="detections")
|
|
|
|
|
|
|
| 591 |
app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")
|
| 592 |
|
|
|
|
| 593 |
if __name__ == "__main__":
|
| 594 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|