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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
import json
|
| 6 |
+
import logging
|
| 7 |
+
import pickle
|
| 8 |
+
import requests
|
| 9 |
+
import imageio.v3 as imageio
|
| 10 |
+
import numpy as np
|
| 11 |
+
import geopandas as gpd
|
| 12 |
+
from datetime import datetime, timedelta
|
| 13 |
+
from typing import List
|
| 14 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 15 |
+
from shapely.geometry import LineString
|
| 16 |
+
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
|
| 24 |
+
import uvicorn
|
| 25 |
+
|
| 26 |
+
import osmnx as ox
|
| 27 |
+
from sentinelhub import (
|
| 28 |
+
SHConfig, SentinelHubRequest, DataCollection,
|
| 29 |
+
BBox, CRS, MimeType, SentinelHubCatalog,
|
| 30 |
+
)
|
| 31 |
+
from dotenv import load_dotenv
|
| 32 |
+
|
| 33 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 34 |
+
# Setup
|
| 35 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 36 |
+
|
| 37 |
+
load_dotenv()
|
| 38 |
+
|
| 39 |
+
logging.basicConfig(level=logging.INFO)
|
| 40 |
+
logger = logging.getLogger("ARGUS")
|
| 41 |
+
|
| 42 |
+
SECONDS_OFFSET_B02_B04 = 1.01 # Sentinel-2 temporal sensing offset between B02 and B04
|
| 43 |
+
|
| 44 |
+
# Dynamic Storage Root (Expansion Drive or Local Fallback)
|
| 45 |
+
DATA_DIR = os.getenv("DRISHX_DATA_DIR", os.path.join(os.getcwd(), "drishx_data"))
|
| 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",
|
| 52 |
+
"https://overpass.osm.ch/api/interpreter",
|
| 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,
|
| 60 |
+
status_forcelist=[429, 500, 502, 503, 504],
|
| 61 |
+
allowed_methods=["GET", "POST"],
|
| 62 |
+
)
|
| 63 |
+
_adapter = HTTPAdapter(max_retries=_retry)
|
| 64 |
+
_session.mount("http://", _adapter)
|
| 65 |
+
_session.mount("https://", _adapter)
|
| 66 |
+
|
| 67 |
+
ox.settings.requests_session = _session
|
| 68 |
+
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 |
+
logger.info("Copernicus Data Space Authentication: CONFIGURED FOR CDSE")
|
| 90 |
+
|
| 91 |
+
FEATURED_SITES = [
|
| 92 |
+
{"id": "v1", "name": "Braunschweig A7 (Research-Grade)", "bbox": [52.25, 10.45, 52.32, 10.55], "country": "Germany", "type": "high_volume"},
|
| 93 |
+
{"id": "v2", "name": "Frankfurt A3 (High-Density)", "bbox": [50.05, 8.55, 50.12, 8.65], "country": "Germany", "type": "high_volume"},
|
| 94 |
+
{"id": "v3", "name": "Karlsruhe A5 (Research-Standard)", "bbox": [48.95, 8.35, 49.05, 8.45], "country": "Germany", "type": "standard"},
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 99 |
+
# Helper math — mirrors S2TD.array_utils.math
|
| 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: # likely DN scale
|
| 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:
|
| 127 |
+
size_up += 1
|
| 128 |
+
ymin = max(0, y - size_low)
|
| 129 |
+
ymax = max(0, y + size_up)
|
| 130 |
+
xmin = max(0, x - size_low)
|
| 131 |
+
xmax = max(0, x + size_up)
|
| 132 |
+
if arr.ndim == 2:
|
| 133 |
+
return arr[ymin:ymax, xmin:xmax]
|
| 134 |
+
elif arr.ndim == 3:
|
| 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 |
+
Build the 7-feature stack from Sentinel-2 bands.
|
| 146 |
+
|
| 147 |
+
Input `data` shape: (H, W, 5) with channels [B04(R), B03(G), B02(B), B08(NIR), CLM].
|
| 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
|
| 173 |
+
B[cloud] = np.nan
|
| 174 |
+
NIR[cloud] = np.nan
|
| 175 |
+
|
| 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 |
+
|
| 204 |
+
return {
|
| 205 |
+
"feature_stack": fs,
|
| 206 |
+
"bands": {"R": R, "G": G, "B": B, "NIR": NIR},
|
| 207 |
+
"cloud_mask": cloud,
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 212 |
+
# RF Model loading
|
| 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 |
+
p = path or RF_MODEL_PATH
|
| 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 |
+
|
| 314 |
+
centered_R = fs[3]
|
| 315 |
+
centered_G = fs[4]
|
| 316 |
+
centered_B = fs[5]
|
| 317 |
+
var_feat = fs[0]
|
| 318 |
+
nratio_rb = fs[1]
|
| 319 |
+
nratio_gb = fs[2]
|
| 320 |
+
|
| 321 |
+
rm = road_mask.astype(bool)
|
| 322 |
+
nan_mask = np.isnan(centered_R)
|
| 323 |
+
|
| 324 |
+
blue_score = np.clip(-nratio_rb * 2 + centered_B * 5 + var_feat * 10, 0, None)
|
| 325 |
+
blue_score[~rm | nan_mask] = 0
|
| 326 |
+
|
| 327 |
+
green_score = np.clip(nratio_gb * 2 + centered_G * 5 + var_feat * 10, 0, None)
|
| 328 |
+
green_score[~rm | nan_mask] = 0
|
| 329 |
+
|
| 330 |
+
red_score = np.clip(nratio_rb * 2 + centered_R * 5 + var_feat * 10, 0, None)
|
| 331 |
+
red_score[~rm | nan_mask] = 0
|
| 332 |
+
|
| 333 |
+
total = blue_score + green_score + red_score + 1e-8
|
| 334 |
+
probs[1] = blue_score / total
|
| 335 |
+
probs[2] = green_score / total
|
| 336 |
+
probs[3] = red_score / total
|
| 337 |
+
probs[0] = 1.0 - np.max(probs[1:], axis=0)
|
| 338 |
+
|
| 339 |
+
probs[0][probs[0] < 0.75] = 0
|
| 340 |
+
|
| 341 |
+
classification = np.nanargmax(probs, axis=0).astype(np.int8) + 1
|
| 342 |
+
classification[np.max(probs, axis=0) == 0] = 0
|
| 343 |
+
classification[nan_mask] = 0
|
| 344 |
+
classification[~rm] = 0
|
| 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 |
+
# Object extraction — faithful port of S2TD ObjectExtractor
|
| 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
|
| 391 |
+
|
| 392 |
+
for i in range(len(blue_ys)):
|
| 393 |
+
y_blue, x_blue = int(blue_ys[i]), int(blue_xs[i])
|
| 394 |
+
if preds[y_blue, x_blue] == 0:
|
| 395 |
+
continue
|
| 396 |
+
|
| 397 |
+
subset_9 = pick_arr_subset(preds, y_blue, x_blue, sub_size).copy()
|
| 398 |
+
subset_3 = pick_arr_subset(preds, y_blue, x_blue, 3).copy()
|
| 399 |
+
subset_9_probs = pick_arr_subset(probs, y_blue, x_blue, sub_size).copy()
|
| 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 |
+
cluster, seen_idx, seen_vals, _ = self._cluster_array(
|
| 414 |
+
arr=subset_9, probs=subset_9_probs,
|
| 415 |
+
point=[half_idx_y, half_idx_x],
|
| 416 |
+
new_value=new_value, current_value=current_value,
|
| 417 |
+
yet_seen_indices=[], yet_seen_values=[],
|
| 418 |
+
skipped_one=False,
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
if np.count_nonzero(cluster == new_value) < 3:
|
| 422 |
+
continue
|
| 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 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 640 |
+
|
| 641 |
+
class ARGUSEngine:
|
| 642 |
+
def __init__(self):
|
| 643 |
+
self.history = []
|
| 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
|
| 659 |
+
|
| 660 |
+
lat_span = (max_lat - min_lat) * 111000
|
| 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 |
+
log(f"Starting road discovery (ROI: {center_lat:.4f}, {center_lon:.4f})", pct=5)
|
| 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(
|
| 671 |
+
(center_lat, center_lon), dist=dist_m,
|
| 672 |
+
network_type="drive", simplify=True,
|
| 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 {mirror} failed: {e}")
|
| 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 |
+
# 2. Classify (real RF if loaded, proxy fallback otherwise)
|
| 733 |
+
probs, prediction = classify(feature_stack, road_mask, self.rf_model)
|
| 734 |
+
|
| 735 |
+
# 3. Lat/lon arrays for geo-referencing
|
| 736 |
+
lat_arr = np.linspace(max_lat, min_lat, H) # top to bottom
|
| 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 |
+
try:
|
| 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, min_lat, min_lon, max_lat, max_lon):
|
| 754 |
+
"""Save a 20×20 RGB crop centred on the detection."""
|
| 755 |
+
cy = int((max_lat - det["lat"]) / (max_lat - min_lat + 1e-9) * H)
|
| 756 |
+
cx = int((det["lon"] - min_lon) / (max_lon - min_lon + 1e-9) * W)
|
| 757 |
+
cy, cx = int(np.clip(cy, 0, H - 1)), int(np.clip(cx, 0, W - 1))
|
| 758 |
+
|
| 759 |
+
y0, y1 = max(0, cy - 10), min(H, cy + 10)
|
| 760 |
+
x0, x1 = max(0, cx - 10), min(W, cx + 10)
|
| 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
|
| 772 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 773 |
+
|
| 774 |
+
app = FastAPI(title="ARGUS Corridor Intelligence")
|
| 775 |
+
app.add_middleware(
|
| 776 |
+
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],
|
| 777 |
+
)
|
| 778 |
+
|
| 779 |
+
engine = ARGUSEngine()
|
| 780 |
+
|
| 781 |
+
|
| 782 |
+
class AnalyzeRequest(BaseModel):
|
| 783 |
+
bbox: List[float] # [min_lat, min_lon, max_lat, max_lon]
|
| 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():
|
| 792 |
+
try:
|
| 793 |
+
def progress(msg, pct):
|
| 794 |
+
return json.dumps({"type": "progress", "message": msg, "percent": pct}) + "\n"
|
| 795 |
+
|
| 796 |
+
yield progress(f"Starting analysis for: {req.label}", 0)
|
| 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. Max strategic sector is ~55 km x 55 km."}) + "\n"
|
| 801 |
+
return
|
| 802 |
+
|
| 803 |
+
# 1. Roads
|
| 804 |
+
yield progress("Running Road Discovery Pipeline...", 10)
|
| 805 |
+
roads = engine.fetch_roads(req.bbox, progress_cb=lambda m, p: None)
|
| 806 |
+
if roads.empty:
|
| 807 |
+
yield json.dumps({"type": "error", "message": "No major roads found in AOI."}) + "\n"
|
| 808 |
+
return
|
| 809 |
+
yield progress(f"Found {len(roads)} road corridor segments.", 25)
|
| 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 |
+
|
| 815 |
+
catalog = SentinelHubCatalog(config=CONFIG)
|
| 816 |
+
end_date = datetime.now()
|
| 817 |
+
start_date = end_date - timedelta(days=max(1, req.months) * 30)
|
| 818 |
+
|
| 819 |
+
cdse_collection = DataCollection.SENTINEL2_L2A.define_from(
|
| 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')}",
|
| 827 |
+
filter="eo:cloud_cover < 60",
|
| 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": f"No clear imagery found in the last {req.months} months."}) + "\n"
|
| 844 |
+
return
|
| 845 |
+
|
| 846 |
+
yield progress(f"Found {len(final_obs)} unique clear overpasses. Starting analysis...", 40)
|
| 847 |
+
|
| 848 |
+
# Evalscript: output order = B04(R), B03(G), B02(B), B08(NIR), CLM
|
| 849 |
+
evalscript = """//VERSION=3
|
| 850 |
+
function setup() {
|
| 851 |
+
return {
|
| 852 |
+
input: ["B02", "B03", "B04", "B08", "CLM"],
|
| 853 |
+
output: { id: "default", bands: 5, sampleType: "FLOAT32" }
|
| 854 |
+
};
|
| 855 |
+
}
|
| 856 |
+
function evaluatePixel(s) {
|
| 857 |
+
return [s.B04, s.B03, s.B02, s.B08, s.CLM];
|
| 858 |
+
}"""
|
| 859 |
+
|
| 860 |
+
# --- Optimization: Pre-calculate Road Mask ---
|
| 861 |
+
# To get dimensions, we could do one small request or calculate.
|
| 862 |
+
# Here we'll do the first frame sequentially to establish the grid,
|
| 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 |
+
# Helper for processing a single frame
|
| 873 |
+
def _worker(idx, res_obs):
|
| 874 |
+
try:
|
| 875 |
+
date_str = res_obs["properties"]["datetime"]
|
| 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 |
+
# Detect on seed
|
| 918 |
+
seed_dets = engine.detect_trucks(seed_data, req.bbox, final_obs[0]['properties']['datetime'], road_mask)
|
| 919 |
+
detections.extend(seed_dets)
|
| 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,
|
| 946 |
+
"bbox": req.bbox,
|
| 947 |
+
"road_count": len(roads),
|
| 948 |
+
"detections": detections,
|
| 949 |
+
"timestamp": datetime.now().isoformat(),
|
| 950 |
+
})
|
| 951 |
+
|
| 952 |
+
yield json.dumps({
|
| 953 |
+
"type": "result",
|
| 954 |
+
"mission_id": mission_id,
|
| 955 |
+
"road_count": len(roads),
|
| 956 |
+
"detection_count": len(detections),
|
| 957 |
+
"message": f"Complete: {len(detections)} truck signatures detected.",
|
| 958 |
+
}) + "\n"
|
| 959 |
+
|
| 960 |
+
except Exception as e:
|
| 961 |
+
logger.error(f"Stream error: {e}", exc_info=True)
|
| 962 |
+
yield json.dumps({"type": "error", "message": str(e)}) + "\n"
|
| 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 = [
|
| 978 |
+
{
|
| 979 |
+
"id": s["id"], "name": s["name"],
|
| 980 |
+
"lat": (s["bbox"][0] + s["bbox"][2]) / 2,
|
| 981 |
+
"lng": (s["bbox"][1] + s["bbox"][3]) / 2,
|
| 982 |
+
"bbox": s["bbox"], "country": s["country"], "type": s["type"],
|
| 983 |
+
}
|
| 984 |
+
for s in FEATURED_SITES
|
| 985 |
+
]
|
| 986 |
+
history_sites = [
|
| 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": "Analysis ROI", "type": "history",
|
| 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):
|
| 1078 |
+
mission = next((h for h in engine.history if h["mission_id"] == mission_id), None)
|
| 1079 |
+
if not mission:
|
| 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 |
+
try:
|
| 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)
|