Commit Β·
7db73fd
1
Parent(s): ccc7737
Refactor: Production-grade SatVision backend modularization, structured logging, boot safety validations, and workspace cleanup
Browse files- main.py +1 -1
- server.py +0 -790
- src/app.py +4 -1
- src/config.py +26 -0
- src/database/gee.py +4 -4
- src/database/mongo.py +4 -4
- src/services/detection.py +2 -2
- src/services/gee_service.py +5 -5
- src/services/gemini.py +1 -1
- src/services/model.py +4 -4
main.py
CHANGED
|
@@ -15,7 +15,7 @@ from src.app import create_app
|
|
| 15 |
|
| 16 |
def main():
|
| 17 |
"""Main entry point for the backend server."""
|
| 18 |
-
print("
|
| 19 |
|
| 20 |
# Create application instance
|
| 21 |
app = create_app()
|
|
|
|
| 15 |
|
| 16 |
def main():
|
| 17 |
"""Main entry point for the backend server."""
|
| 18 |
+
print("[SatVision] Production Backend Running...", flush=True)
|
| 19 |
|
| 20 |
# Create application instance
|
| 21 |
app = create_app()
|
server.py
DELETED
|
@@ -1,790 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import numpy as np
|
| 4 |
-
import tifffile as tiff
|
| 5 |
-
import cv2
|
| 6 |
-
import torch
|
| 7 |
-
import pytorch_lightning as pl
|
| 8 |
-
import segmentation_models_pytorch as smp
|
| 9 |
-
import ee
|
| 10 |
-
import requests
|
| 11 |
-
from datetime import datetime, timedelta
|
| 12 |
-
from flask import Flask, request, Response, stream_with_context, send_from_directory
|
| 13 |
-
from flask_cors import CORS
|
| 14 |
-
import math
|
| 15 |
-
from google import genai
|
| 16 |
-
from google.genai import types
|
| 17 |
-
from PIL import Image
|
| 18 |
-
from fpdf import FPDF
|
| 19 |
-
from fpdf.enums import XPos, YPos
|
| 20 |
-
import uuid
|
| 21 |
-
from pymongo import MongoClient
|
| 22 |
-
|
| 23 |
-
app = Flask(__name__)
|
| 24 |
-
CORS(app)
|
| 25 |
-
|
| 26 |
-
@app.route("/")
|
| 27 |
-
def health_check():
|
| 28 |
-
return "β
SatVision Backend is alive!", 200
|
| 29 |
-
|
| 30 |
-
# --- 1. CONFIGURATION & DATABASE ---
|
| 31 |
-
CHECKPOINT_PATH = "./models/SatVision_Model.ckpt"
|
| 32 |
-
DOWNLOADS_DIR = "server_downloads"
|
| 33 |
-
os.makedirs(DOWNLOADS_DIR, exist_ok=True)
|
| 34 |
-
|
| 35 |
-
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
-
|
| 37 |
-
MONGO_URI = os.environ.get("MONGO_URI")
|
| 38 |
-
try:
|
| 39 |
-
mongo_client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
|
| 40 |
-
db = mongo_client.satvision_db
|
| 41 |
-
generations_col = db.generations
|
| 42 |
-
print("β
MongoDB Connected Successfully!", flush=True)
|
| 43 |
-
except Exception as e:
|
| 44 |
-
print(f"β MongoDB Connection Failed: {e}", flush=True)
|
| 45 |
-
db, generations_col = None, None
|
| 46 |
-
|
| 47 |
-
# --- 2. GOOGLE EARTH ENGINE SETUP ---
|
| 48 |
-
print("π Initializing Earth Engine...", flush=True)
|
| 49 |
-
try:
|
| 50 |
-
credentials_json = os.environ.get("EE_CREDENTIALS")
|
| 51 |
-
if credentials_json:
|
| 52 |
-
key_dict = json.loads(credentials_json)
|
| 53 |
-
from google.oauth2 import service_account
|
| 54 |
-
credentials = service_account.Credentials.from_service_account_info(
|
| 55 |
-
key_dict,
|
| 56 |
-
scopes=['https://www.googleapis.com/auth/earthengine']
|
| 57 |
-
)
|
| 58 |
-
ee.Initialize(credentials, project='gen-lang-client-0114614261')
|
| 59 |
-
print("β
Earth Engine Linked via Service Account.", flush=True)
|
| 60 |
-
else:
|
| 61 |
-
ee.Initialize(project='gen-lang-client-0114614261')
|
| 62 |
-
print("β
Earth Engine Linked via Local Auth.", flush=True)
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"β Earth Engine Init Failed: {e}", flush=True)
|
| 65 |
-
|
| 66 |
-
# --- 3. MODEL DEFINITION ---
|
| 67 |
-
class DynamicEvalModel(pl.LightningModule):
|
| 68 |
-
def __init__(self, **kwargs):
|
| 69 |
-
super().__init__()
|
| 70 |
-
self.save_hyperparameters()
|
| 71 |
-
self.model_params = kwargs.get('model_params', {'model_type': 'unetplusplus', 'encoder_name': 'resnet34', 'in_channels': 6, 'out_classes': 2})
|
| 72 |
-
self._build_model()
|
| 73 |
-
|
| 74 |
-
def _build_model(self):
|
| 75 |
-
self.model = smp.UnetPlusPlus(
|
| 76 |
-
encoder_name=self.model_params.get('encoder_name', 'resnet34'),
|
| 77 |
-
encoder_weights=None,
|
| 78 |
-
in_channels=self.model_params.get('in_channels', 6),
|
| 79 |
-
classes=self.model_params.get('out_classes', 2),
|
| 80 |
-
activation=None
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
def forward(self, x):
|
| 84 |
-
return self.model(x)
|
| 85 |
-
|
| 86 |
-
print("β³ Loading model...", flush=True)
|
| 87 |
-
try:
|
| 88 |
-
model = DynamicEvalModel.load_from_checkpoint(CHECKPOINT_PATH, strict=False)
|
| 89 |
-
model.to(DEVICE)
|
| 90 |
-
model.eval()
|
| 91 |
-
print("β
Model Loaded Successfully!", flush=True)
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"β Model Load Failed: {e}", flush=True)
|
| 94 |
-
model = None
|
| 95 |
-
|
| 96 |
-
# --- 4. GEE FETCHING & UTILS ---
|
| 97 |
-
|
| 98 |
-
def download_gee_tile(image, n, s, e, w, prefix, r, c, scale=10):
|
| 99 |
-
region = ee.Geometry.BBox(w, s, e, n)
|
| 100 |
-
url = image.getDownloadURL({'crs': 'EPSG:4326', 'region': region, 'scale': scale, 'format': 'GEO_TIFF'})
|
| 101 |
-
resp = requests.get(url, timeout=120)
|
| 102 |
-
if resp.status_code != 200:
|
| 103 |
-
print(f"β Tile {r}-{c} failed.", flush=True)
|
| 104 |
-
return None
|
| 105 |
-
|
| 106 |
-
path = os.path.join(DOWNLOADS_DIR, f"{prefix}_tile_{r}_{c}.tiff")
|
| 107 |
-
with open(path, 'wb') as f:
|
| 108 |
-
f.write(resp.content)
|
| 109 |
-
return path
|
| 110 |
-
|
| 111 |
-
def fetch_optical_composite(coords, time_interval, filename_prefix):
|
| 112 |
-
try:
|
| 113 |
-
region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
|
| 114 |
-
start_date, end_date = time_interval
|
| 115 |
-
|
| 116 |
-
col = (ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
|
| 117 |
-
.filterBounds(region)
|
| 118 |
-
.filterDate(start_date, end_date)
|
| 119 |
-
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 35)))
|
| 120 |
-
|
| 121 |
-
if col.size().getInfo() == 0:
|
| 122 |
-
col = (ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
|
| 123 |
-
.filterBounds(region)
|
| 124 |
-
.filterDate(start_date, end_date)
|
| 125 |
-
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 35)))
|
| 126 |
-
|
| 127 |
-
if col.size().getInfo() == 0:
|
| 128 |
-
return None, None, None, 100.0
|
| 129 |
-
|
| 130 |
-
def maskS2clouds(image):
|
| 131 |
-
qa = image.select('QA60')
|
| 132 |
-
cloudBitMask = 1 << 10
|
| 133 |
-
cirrusBitMask = 1 << 11
|
| 134 |
-
mask = qa.bitwiseAnd(cloudBitMask).eq(0).And(qa.bitwiseAnd(cirrusBitMask).eq(0))
|
| 135 |
-
return image.updateMask(mask)
|
| 136 |
-
|
| 137 |
-
mosaic = col.map(maskS2clouds).median()
|
| 138 |
-
s2_bands = ['B2', 'B3', 'B4', 'B8', 'B11', 'B12']
|
| 139 |
-
final_image = mosaic.select(s2_bands).toInt16().unmask(0)
|
| 140 |
-
|
| 141 |
-
valid_mask = final_image.select('B4').gt(0)
|
| 142 |
-
stats = valid_mask.reduceRegion(
|
| 143 |
-
reducer=ee.Reducer.mean(),
|
| 144 |
-
geometry=region,
|
| 145 |
-
scale=100,
|
| 146 |
-
maxPixels=1e9
|
| 147 |
-
)
|
| 148 |
-
clear_fraction = stats.get('B4').getInfo()
|
| 149 |
-
|
| 150 |
-
if clear_fraction is None: clear_fraction = 0
|
| 151 |
-
cloud_percent = (1.0 - clear_fraction) * 100.0
|
| 152 |
-
|
| 153 |
-
if cloud_percent > 85.0:
|
| 154 |
-
return None, None, None, cloud_percent
|
| 155 |
-
|
| 156 |
-
actual_date = end_date
|
| 157 |
-
source_type = "Sentinel-2 Optical (Cloud-Masked Composite)"
|
| 158 |
-
|
| 159 |
-
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
|
| 160 |
-
is_large = max(n - s, e - w) > 0.5
|
| 161 |
-
MAX_DEG_STEP = 0.50 if is_large else 0.08
|
| 162 |
-
current_scale = 50 if is_large else 10
|
| 163 |
-
rows = math.ceil((n - s) / MAX_DEG_STEP)
|
| 164 |
-
cols = math.ceil((e - w) / MAX_DEG_STEP)
|
| 165 |
-
lat_steps = np.linspace(n, s, rows + 1)
|
| 166 |
-
lon_steps = np.linspace(w, e, cols + 1)
|
| 167 |
-
|
| 168 |
-
tile_arrays = []
|
| 169 |
-
for r in range(rows):
|
| 170 |
-
row_list = []
|
| 171 |
-
for c in range(cols):
|
| 172 |
-
tile_n, tile_s = lat_steps[r], lat_steps[r+1]
|
| 173 |
-
tile_w, tile_e = lon_steps[c], lon_steps[c+1]
|
| 174 |
-
tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w, filename_prefix, r, c, scale=current_scale)
|
| 175 |
-
if not tile_path: return None, None, None, 100.0
|
| 176 |
-
img = tiff.imread(tile_path)
|
| 177 |
-
row_list.append(img)
|
| 178 |
-
os.remove(tile_path)
|
| 179 |
-
|
| 180 |
-
min_h = min(tile.shape[0] for tile in row_list)
|
| 181 |
-
row_list = [tile[:min_h, :, :] for tile in row_list]
|
| 182 |
-
row_stitched = np.concatenate(row_list, axis=1)
|
| 183 |
-
tile_arrays.append(row_stitched)
|
| 184 |
-
|
| 185 |
-
min_w = min(row.shape[1] for row in tile_arrays)
|
| 186 |
-
tile_arrays = [row[:, :min_w, :] for row in tile_arrays]
|
| 187 |
-
final_stitched = np.concatenate(tile_arrays, axis=0)
|
| 188 |
-
|
| 189 |
-
save_path = os.path.join(DOWNLOADS_DIR, f"{filename_prefix}.tiff")
|
| 190 |
-
tiff.imwrite(save_path, final_stitched)
|
| 191 |
-
|
| 192 |
-
return save_path, actual_date, source_type, cloud_percent
|
| 193 |
-
|
| 194 |
-
except Exception as e:
|
| 195 |
-
print(f"β GEE Optical Error {filename_prefix}: {e}", flush=True)
|
| 196 |
-
return None, None, None, 100.0
|
| 197 |
-
|
| 198 |
-
def fetch_sar_data(coords, time_interval, filename_prefix, is_baseline=False):
|
| 199 |
-
try:
|
| 200 |
-
region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
|
| 201 |
-
start_date, end_date = time_interval
|
| 202 |
-
|
| 203 |
-
s1_col = (ee.ImageCollection('COPERNICUS/S1_GRD')
|
| 204 |
-
.filterBounds(region)
|
| 205 |
-
.filterDate(start_date, end_date)
|
| 206 |
-
.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV'))
|
| 207 |
-
.filter(ee.Filter.eq('instrumentMode', 'IW')))
|
| 208 |
-
|
| 209 |
-
if s1_col.size().getInfo() == 0: return None, None, None
|
| 210 |
-
|
| 211 |
-
if is_baseline:
|
| 212 |
-
s1_image = s1_col.median().select('VV').unmask(-99)
|
| 213 |
-
else:
|
| 214 |
-
s1_image = s1_col.mosaic().select('VV').unmask(-99)
|
| 215 |
-
|
| 216 |
-
final_image = s1_image.toFloat()
|
| 217 |
-
|
| 218 |
-
MAX_DEG_STEP = 0.08
|
| 219 |
-
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
|
| 220 |
-
rows = math.ceil((n - s) / MAX_DEG_STEP)
|
| 221 |
-
cols = math.ceil((e - w) / MAX_DEG_STEP)
|
| 222 |
-
lat_steps = np.linspace(n, s, rows + 1)
|
| 223 |
-
lon_steps = np.linspace(w, e, cols + 1)
|
| 224 |
-
|
| 225 |
-
tile_arrays = []
|
| 226 |
-
for r in range(rows):
|
| 227 |
-
row_list = []
|
| 228 |
-
for c in range(cols):
|
| 229 |
-
tile_n, tile_s = lat_steps[r], lat_steps[r+1]
|
| 230 |
-
tile_w, tile_e = lon_steps[c], lon_steps[c+1]
|
| 231 |
-
tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w, filename_prefix, r, c, scale=current_scale)
|
| 232 |
-
if not tile_path: return None, None, None
|
| 233 |
-
img = tiff.imread(tile_path)
|
| 234 |
-
row_list.append(img)
|
| 235 |
-
os.remove(tile_path)
|
| 236 |
-
|
| 237 |
-
min_h = min(tile.shape[0] for tile in row_list)
|
| 238 |
-
row_list = [tile[:min_h, ...] for tile in row_list]
|
| 239 |
-
row_stitched = np.concatenate(row_list, axis=1)
|
| 240 |
-
tile_arrays.append(row_stitched)
|
| 241 |
-
|
| 242 |
-
min_w = min(row.shape[1] for row in tile_arrays)
|
| 243 |
-
tile_arrays = [row[:, :min_w, ...] for row in tile_arrays]
|
| 244 |
-
final_stitched = np.concatenate(tile_arrays, axis=0)
|
| 245 |
-
|
| 246 |
-
actual_date = end_date
|
| 247 |
-
source_type = "Sentinel-1 SAR (VV Backscatter)" if not is_baseline else "Sentinel-1 SAR (Dry-Season Baseline)"
|
| 248 |
-
|
| 249 |
-
save_path = os.path.join(DOWNLOADS_DIR, f"{filename_prefix}_sar.tiff")
|
| 250 |
-
tiff.imwrite(save_path, final_stitched)
|
| 251 |
-
|
| 252 |
-
return save_path, actual_date, source_type
|
| 253 |
-
|
| 254 |
-
except Exception as e:
|
| 255 |
-
print(f"β SAR Error {filename_prefix}: {e}", flush=True)
|
| 256 |
-
return None, None, None
|
| 257 |
-
|
| 258 |
-
def fetch_dynamic_world_baseline(coords, time_interval, filename_prefix):
|
| 259 |
-
try:
|
| 260 |
-
region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
|
| 261 |
-
start_date, end_date = time_interval
|
| 262 |
-
|
| 263 |
-
dw_col = (ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1')
|
| 264 |
-
.filterBounds(region)
|
| 265 |
-
.filterDate(start_date, end_date))
|
| 266 |
-
|
| 267 |
-
if dw_col.size().getInfo() == 0: return None, None
|
| 268 |
-
|
| 269 |
-
final_image = dw_col.select('label').mode().eq(0).unmask(0).multiply(255).toByte()
|
| 270 |
-
|
| 271 |
-
MAX_DEG_STEP = 0.08
|
| 272 |
-
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
|
| 273 |
-
rows = math.ceil((n - s) / MAX_DEG_STEP)
|
| 274 |
-
cols = math.ceil((e - w) / MAX_DEG_STEP)
|
| 275 |
-
lat_steps = np.linspace(n, s, rows + 1)
|
| 276 |
-
lon_steps = np.linspace(w, e, cols + 1)
|
| 277 |
-
|
| 278 |
-
tile_arrays = []
|
| 279 |
-
for r in range(rows):
|
| 280 |
-
row_list = []
|
| 281 |
-
for c in range(cols):
|
| 282 |
-
tile_n, tile_s = lat_steps[r], lat_steps[r+1]
|
| 283 |
-
tile_w, tile_e = lon_steps[c], lon_steps[c+1]
|
| 284 |
-
tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w, filename_prefix, r, c)
|
| 285 |
-
if not tile_path: return None, None
|
| 286 |
-
img = tiff.imread(tile_path)
|
| 287 |
-
row_list.append(img)
|
| 288 |
-
os.remove(tile_path)
|
| 289 |
-
|
| 290 |
-
min_h = min(tile.shape[0] for tile in row_list)
|
| 291 |
-
row_list = [tile[:min_h, ...] for tile in row_list]
|
| 292 |
-
row_stitched = np.concatenate(row_list, axis=1)
|
| 293 |
-
tile_arrays.append(row_stitched)
|
| 294 |
-
|
| 295 |
-
min_w = min(row.shape[1] for row in tile_arrays)
|
| 296 |
-
tile_arrays = [row[:, :min_w, ...] for row in tile_arrays]
|
| 297 |
-
final_stitched = np.concatenate(tile_arrays, axis=0)
|
| 298 |
-
|
| 299 |
-
save_path = os.path.join(DOWNLOADS_DIR, f"{filename_prefix}.tiff")
|
| 300 |
-
tiff.imwrite(save_path, final_stitched)
|
| 301 |
-
|
| 302 |
-
return save_path, "Dynamic Dry-Season Anchor (April-May)"
|
| 303 |
-
|
| 304 |
-
except Exception as e:
|
| 305 |
-
print(f"β Dynamic World Error {filename_prefix}: {e}", flush=True)
|
| 306 |
-
return None, None
|
| 307 |
-
|
| 308 |
-
# --- 5. IMAGE PROCESSING UTILS ---
|
| 309 |
-
|
| 310 |
-
def predict_water_mask(tiff_path):
|
| 311 |
-
if not model or not os.path.exists(tiff_path): return None
|
| 312 |
-
|
| 313 |
-
image = tiff.imread(tiff_path).astype(np.float32)
|
| 314 |
-
|
| 315 |
-
if image.ndim == 3 and image.shape[2] == 6:
|
| 316 |
-
image_tensor = np.transpose(image, (2, 0, 1))
|
| 317 |
-
else:
|
| 318 |
-
image_tensor = image.copy()
|
| 319 |
-
|
| 320 |
-
scaled_tensor = np.clip(image_tensor / 10000.0, 0, 1)
|
| 321 |
-
|
| 322 |
-
tile_size, overlap = 256, 0.5
|
| 323 |
-
stride = int(tile_size * (1 - overlap))
|
| 324 |
-
c, h, w = scaled_tensor.shape
|
| 325 |
-
|
| 326 |
-
pad_h = (tile_size - (h % stride)) % stride + (tile_size - stride) if h % stride != 0 else 0
|
| 327 |
-
pad_w = (tile_size - (w % stride)) % stride + (tile_size - stride) if w % stride != 0 else 0
|
| 328 |
-
image_padded = np.pad(scaled_tensor, ((0,0), (0, pad_h), (0, pad_w)), mode='reflect')
|
| 329 |
-
|
| 330 |
-
padded_h, padded_w = image_padded.shape[1], image_padded.shape[2]
|
| 331 |
-
|
| 332 |
-
prob_map_cloud = np.zeros((padded_h, padded_w), dtype=np.float32)
|
| 333 |
-
prob_map_water = np.zeros((padded_h, padded_w), dtype=np.float32)
|
| 334 |
-
count_map = np.zeros((padded_h, padded_w), dtype=np.float32)
|
| 335 |
-
|
| 336 |
-
y_steps = list(range(0, padded_h - tile_size + 1, stride))
|
| 337 |
-
x_steps = list(range(0, padded_w - tile_size + 1, stride))
|
| 338 |
-
|
| 339 |
-
window_1d = np.hanning(tile_size)
|
| 340 |
-
window_2d = np.outer(window_1d, window_1d).astype(np.float32)
|
| 341 |
-
|
| 342 |
-
batch_size = 16 # Process 16 tiles simultaneously (adjust to 8 if you hit GPU memory limits)
|
| 343 |
-
tiles_batch = []
|
| 344 |
-
coords_batch = []
|
| 345 |
-
|
| 346 |
-
with torch.no_grad():
|
| 347 |
-
for y in y_steps:
|
| 348 |
-
for x in x_steps:
|
| 349 |
-
# 1. Extract the numpy tile and store it
|
| 350 |
-
tile = image_padded[:, y:y+tile_size, x:x+tile_size]
|
| 351 |
-
tiles_batch.append(tile)
|
| 352 |
-
coords_batch.append((x, y))
|
| 353 |
-
|
| 354 |
-
# 2. If we hit the batch size, OR we are on the very last patch, run inference
|
| 355 |
-
if len(tiles_batch) == batch_size or (y == y_steps[-1] and x == x_steps[-1]):
|
| 356 |
-
# Stack list of arrays into a single (B, C, H, W) tensor
|
| 357 |
-
batch_tensor = torch.from_numpy(np.stack(tiles_batch)).to(DEVICE)
|
| 358 |
-
|
| 359 |
-
# Forward pass
|
| 360 |
-
logits = model(batch_tensor)
|
| 361 |
-
probs = torch.sigmoid(logits).cpu().numpy()
|
| 362 |
-
|
| 363 |
-
# 3. Reassemble the probabilities back into the full-size maps
|
| 364 |
-
for i, (bx, by) in enumerate(coords_batch):
|
| 365 |
-
prob_cloud = probs[i, 0, :, :] * window_2d
|
| 366 |
-
prob_water = probs[i, 1, :, :] * window_2d
|
| 367 |
-
|
| 368 |
-
prob_map_cloud[by:by+tile_size, bx:bx+tile_size] += prob_cloud
|
| 369 |
-
prob_map_water[by:by+tile_size, bx:bx+tile_size] += prob_water
|
| 370 |
-
count_map[by:by+tile_size, bx:bx+tile_size] += window_2d
|
| 371 |
-
|
| 372 |
-
# 4. Clear lists for the next batch
|
| 373 |
-
tiles_batch = []
|
| 374 |
-
coords_batch = []
|
| 375 |
-
|
| 376 |
-
final_cloud_prob = (prob_map_cloud / (count_map + 1e-7))[:h, :w]
|
| 377 |
-
final_water_prob = (prob_map_water / (count_map + 1e-7))[:h, :w]
|
| 378 |
-
|
| 379 |
-
b2 = scaled_tensor[0, :h, :w]
|
| 380 |
-
b3 = scaled_tensor[1, :h, :w]
|
| 381 |
-
b4 = scaled_tensor[2, :h, :w]
|
| 382 |
-
b8 = scaled_tensor[3, :h, :w]
|
| 383 |
-
|
| 384 |
-
brightness = np.sqrt(b2**2 + b3**2 + b4**2)
|
| 385 |
-
ndwi = (b3 - b8) / (b3 + b8 + 1e-5)
|
| 386 |
-
|
| 387 |
-
base_cloud_mask = ((final_cloud_prob > 0.5) & (brightness > 0.35)).astype(np.uint8)
|
| 388 |
-
|
| 389 |
-
cloud_buffer_kernel = np.ones((10, 10), np.uint8)
|
| 390 |
-
cloud_and_shadow_mask = cv2.dilate(base_cloud_mask, cloud_buffer_kernel, iterations=1)
|
| 391 |
-
|
| 392 |
-
raw_water_mask = (final_water_prob > 0.35) | (ndwi > 0.15)
|
| 393 |
-
|
| 394 |
-
water_mask = raw_water_mask & (cloud_and_shadow_mask == 0) & (ndwi > -0.1)
|
| 395 |
-
clean_mask = water_mask.astype(np.uint8)
|
| 396 |
-
|
| 397 |
-
close_kernel = np.ones((3, 3), np.uint8)
|
| 398 |
-
clean_mask = cv2.morphologyEx(clean_mask, cv2.MORPH_CLOSE, close_kernel)
|
| 399 |
-
|
| 400 |
-
open_kernel = np.ones((2, 2), np.uint8)
|
| 401 |
-
clean_mask = cv2.morphologyEx(clean_mask, cv2.MORPH_OPEN, open_kernel)
|
| 402 |
-
|
| 403 |
-
contours, _ = cv2.findContours(clean_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 404 |
-
|
| 405 |
-
min_area = 50
|
| 406 |
-
for cnt in contours:
|
| 407 |
-
area = cv2.contourArea(cnt)
|
| 408 |
-
if area < min_area:
|
| 409 |
-
cv2.drawContours(clean_mask, [cnt], -1, 0, -1)
|
| 410 |
-
|
| 411 |
-
return clean_mask * 255
|
| 412 |
-
|
| 413 |
-
def process_sar_image(tiff_path, preview_filename):
|
| 414 |
-
if not os.path.exists(tiff_path): return None, None
|
| 415 |
-
|
| 416 |
-
sar_db = tiff.imread(tiff_path).astype(np.float32)
|
| 417 |
-
|
| 418 |
-
water_mask = ((sar_db < -16) & (sar_db > -90)).astype(np.uint8) * 255
|
| 419 |
-
|
| 420 |
-
close_kernel = np.ones((3, 3), np.uint8)
|
| 421 |
-
water_mask = cv2.morphologyEx(water_mask, cv2.MORPH_CLOSE, close_kernel)
|
| 422 |
-
|
| 423 |
-
visual = np.clip((sar_db + 25) / 25.0, 0, 1) * 255
|
| 424 |
-
visual = visual.astype(np.uint8)
|
| 425 |
-
|
| 426 |
-
h, w = visual.shape
|
| 427 |
-
rgba = np.zeros((h, w, 4), dtype=np.uint8)
|
| 428 |
-
rgba[:, :, 0] = visual
|
| 429 |
-
rgba[:, :, 1] = visual
|
| 430 |
-
rgba[:, :, 2] = visual
|
| 431 |
-
|
| 432 |
-
rgba[:, :, 3] = np.where(sar_db < -90, 0, 255)
|
| 433 |
-
|
| 434 |
-
save_path = os.path.join(DOWNLOADS_DIR, preview_filename)
|
| 435 |
-
cv2.imwrite(save_path, rgba)
|
| 436 |
-
|
| 437 |
-
return water_mask, preview_filename
|
| 438 |
-
|
| 439 |
-
def create_rgb_preview(tiff_path, filename):
|
| 440 |
-
if not os.path.exists(tiff_path): return None
|
| 441 |
-
image = tiff.imread(tiff_path).astype(np.float32)
|
| 442 |
-
|
| 443 |
-
if image.ndim == 2:
|
| 444 |
-
rgb = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
|
| 445 |
-
rgb[image == 255] = [255, 0, 0]
|
| 446 |
-
save_path = os.path.join(DOWNLOADS_DIR, filename)
|
| 447 |
-
cv2.imwrite(save_path, rgb)
|
| 448 |
-
return filename
|
| 449 |
-
|
| 450 |
-
if image.shape[0] == 6: image = np.transpose(image, (1, 2, 0))
|
| 451 |
-
if np.max(image) > 1.0: image = image / 10000.0
|
| 452 |
-
|
| 453 |
-
rgb = image[:, :, [0, 1, 2]]
|
| 454 |
-
rgb = np.clip(rgb * 3.0, 0, 1) * 255
|
| 455 |
-
rgb = rgb.astype(np.uint8)
|
| 456 |
-
|
| 457 |
-
gaussian = cv2.GaussianBlur(rgb, (0, 0), 2.0)
|
| 458 |
-
sharpened = cv2.addWeighted(rgb, 1.5, gaussian, -0.5, 0)
|
| 459 |
-
|
| 460 |
-
save_path = os.path.join(DOWNLOADS_DIR, filename)
|
| 461 |
-
cv2.imwrite(save_path, sharpened)
|
| 462 |
-
return filename
|
| 463 |
-
|
| 464 |
-
def create_overlay_png(mask, color, filename):
|
| 465 |
-
if mask is None: return None
|
| 466 |
-
h, w = mask.shape
|
| 467 |
-
rgba = np.zeros((h, w, 4), dtype=np.uint8)
|
| 468 |
-
b, g, r = color
|
| 469 |
-
rgba[mask == 255, 0] = b
|
| 470 |
-
rgba[mask == 255, 1] = g
|
| 471 |
-
rgba[mask == 255, 2] = r
|
| 472 |
-
rgba[mask == 255, 3] = 180
|
| 473 |
-
save_path = os.path.join(DOWNLOADS_DIR, filename)
|
| 474 |
-
cv2.imwrite(save_path, rgba)
|
| 475 |
-
return filename
|
| 476 |
-
|
| 477 |
-
# --- 6. AI REPORTING ---
|
| 478 |
-
|
| 479 |
-
def generate_flood_report(stats, coords, dates, image_path):
|
| 480 |
-
try:
|
| 481 |
-
gemini_api_key = os.environ.get("GEMINI_API_KEY")
|
| 482 |
-
if not gemini_api_key:
|
| 483 |
-
return "Error: AI Report unavailable. GEMINI_API_KEY missing in environment."
|
| 484 |
-
|
| 485 |
-
if not os.path.exists(image_path):
|
| 486 |
-
return "Error: AI Report unavailable. Flood image overlay not found."
|
| 487 |
-
|
| 488 |
-
client = genai.Client(api_key=gemini_api_key)
|
| 489 |
-
|
| 490 |
-
baseline_date = dates.get('baseline', 'Unknown Baseline')
|
| 491 |
-
latest_date = dates.get('latest', 'Unknown Date')
|
| 492 |
-
base_sq = stats.get('baseline_sq_km', 0.0)
|
| 493 |
-
curr_sq = stats.get('current_sq_km', 0.0)
|
| 494 |
-
flood_sq = stats.get('flood_sq_km', 0.0)
|
| 495 |
-
|
| 496 |
-
with Image.open(image_path) as img:
|
| 497 |
-
prompt = f"""
|
| 498 |
-
You are a Lead Geospatial Intelligence Analyst generating an executive-level Flood Damage and Loss Assessment (DaLA) report.
|
| 499 |
-
I have provided a satellite image overlay. The red pixels definitively indicate algorithmic detections of newly accumulated floodwater.
|
| 500 |
-
|
| 501 |
-
CONTEXTUAL TELEMETRY:
|
| 502 |
-
- Bounding Box Coordinates: {coords}
|
| 503 |
-
- Baseline Imagery Date: {baseline_date}
|
| 504 |
-
- Post-Flood Capture Date: {latest_date}
|
| 505 |
-
- Pre-flood Water Extent: {base_sq:.2f} sq km
|
| 506 |
-
- Current Total Water Extent: {curr_sq:.2f} sq km
|
| 507 |
-
- Net New Inundation Area: {flood_sq:.2f} sq km
|
| 508 |
-
|
| 509 |
-
YOUR DIRECTIVES:
|
| 510 |
-
1. Be Authoritative: Eliminate all speculative language (e.g., do not use "likely," "suggests," or "possibly"). Make definitive statements based on the visual evidence and telemetry provided. If a factor is indistinguishable, state that it cannot be determined at this resolution rather than guessing.
|
| 511 |
-
2. Be Analytical: Do not just read the numbers back. Calculate the percentage increase in water extent and explain what that scale of increase means for the terrain visible in the image.
|
| 512 |
-
3. Be Plain Text: You must output strictly in plain text. DO NOT use markdown formatting, asterisks, hashes, bolding, or special characters. Use standard ALL CAPS for your headers.
|
| 513 |
-
|
| 514 |
-
REQUIRED REPORT STRUCTURE:
|
| 515 |
-
EXECUTIVE SUMMARY
|
| 516 |
-
Provide a bottom-line-up-front (BLUF) statement summarizing the severity and scale of the inundation event.
|
| 517 |
-
|
| 518 |
-
GEOSPATIAL & CLIMATOLOGICAL CONTEXT
|
| 519 |
-
Identify the specific region in Pakistan based on the coordinates. Correlate the capture dates with known seasonal weather patterns (e.g., monsoon cycles or glacial melt) to establish the primary driver of the event.
|
| 520 |
-
|
| 521 |
-
INUNDATION METRICS & MORPHOLOGY
|
| 522 |
-
Analyze the quantitative data. Describe the morphological spread of the floodwaters (e.g., riverine overspill, pooling in low-lying plains, or flash flood patterns) based strictly on the shape and distribution of the red overlay.
|
| 523 |
-
|
| 524 |
-
INFRASTRUCTURE & SOCIO-ECONOMIC IMPACT
|
| 525 |
-
Examine the base imagery beneath the flood mask. Definitively state the types of terrain affected (e.g., cultivated agricultural plots, dense urban grids, rural transit corridors). Outline the immediate operational impacts on these specific sectors.
|
| 526 |
-
|
| 527 |
-
MODEL CONFIDENCE & FALSE POSITIVE ANALYSIS
|
| 528 |
-
Critically evaluate the segmentation mask. Identify specific areas in the image where the model may have misclassified topographical shadows, dense cloud shadows, or highly reflective urban surfaces as water.
|
| 529 |
-
|
| 530 |
-
STRATEGIC DIRECTIVES
|
| 531 |
-
Provide 3 highly specific, actionable recommendations for on-the-ground disaster response agencies based on the exact terrain and impact identified above.
|
| 532 |
-
"""
|
| 533 |
-
|
| 534 |
-
config = types.GenerateContentConfig(
|
| 535 |
-
temperature=0.1,
|
| 536 |
-
top_p=0.9,
|
| 537 |
-
)
|
| 538 |
-
|
| 539 |
-
response = client.models.generate_content(
|
| 540 |
-
model='gemini-2.5-flash',
|
| 541 |
-
contents=[img, prompt],
|
| 542 |
-
config=config
|
| 543 |
-
)
|
| 544 |
-
|
| 545 |
-
if response.text:
|
| 546 |
-
return response.text.strip()
|
| 547 |
-
else:
|
| 548 |
-
return "Error: Model returned an empty response."
|
| 549 |
-
|
| 550 |
-
except Exception as e:
|
| 551 |
-
import traceback
|
| 552 |
-
print(f"β AI Report Error: {e}\n{traceback.format_exc()}", flush=True)
|
| 553 |
-
return "Error: AI Report generation failed due to an internal system error."
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
# --- 7. MAIN ENDPOINTS ---
|
| 557 |
-
|
| 558 |
-
@app.route('/api/detect_stream', methods=['POST'])
|
| 559 |
-
def detect_stream():
|
| 560 |
-
data = request.json
|
| 561 |
-
coords = data.get('bbox')
|
| 562 |
-
apply_buffer = data.get('apply_buffer', True)
|
| 563 |
-
|
| 564 |
-
target_date_str = data.get('target_date')
|
| 565 |
-
if target_date_str:
|
| 566 |
-
target_date = datetime.strptime(target_date_str, '%Y-%m-%d')
|
| 567 |
-
else:
|
| 568 |
-
target_date = datetime.now()
|
| 569 |
-
|
| 570 |
-
session_id = str(uuid.uuid4())
|
| 571 |
-
session_logs = []
|
| 572 |
-
|
| 573 |
-
def generate_process():
|
| 574 |
-
def emit_log(progress, message):
|
| 575 |
-
session_logs.append({"progress": progress, "log": message, "timestamp": datetime.now().isoformat()})
|
| 576 |
-
return json.dumps({"progress": progress, "log": message}) + "\n"
|
| 577 |
-
|
| 578 |
-
yield emit_log(5, "π Locking Coordinates & Validating Dates...")
|
| 579 |
-
|
| 580 |
-
start_latest = target_date - timedelta(days=15)
|
| 581 |
-
end_latest = target_date + timedelta(days=5)
|
| 582 |
-
interval_latest = (start_latest.strftime('%Y-%m-%d'), end_latest.strftime('%Y-%m-%d'))
|
| 583 |
-
|
| 584 |
-
target_year = target_date.year
|
| 585 |
-
baseline_year = target_year - 1 if target_date.month < 6 else target_year
|
| 586 |
-
interval_baseline = (f"{baseline_year}-04-01", f"{baseline_year}-05-31")
|
| 587 |
-
|
| 588 |
-
yield emit_log(15, f"π‘ Building Optical Composite ({interval_latest[0]} to {interval_latest[1]})...")
|
| 589 |
-
|
| 590 |
-
path_latest, date_latest, src_latest, cloud_percent = fetch_optical_composite(coords, interval_latest, f"{session_id}_latest")
|
| 591 |
-
if cloud_percent is None: cloud_percent = 100
|
| 592 |
-
|
| 593 |
-
if path_latest and cloud_percent <= 85.0:
|
| 594 |
-
yield emit_log(30, f"π§ Optical Rescued ({cloud_percent:.1f}% Dead Pixels). Running U-Net++...")
|
| 595 |
-
mask_latest = predict_water_mask(path_latest)
|
| 596 |
-
rgb_latest = create_rgb_preview(path_latest, f"{session_id}_rgb_latest.jpg")
|
| 597 |
-
|
| 598 |
-
yield emit_log(50, "π‘ Fetching Dry-Season Baseline (Optical)...")
|
| 599 |
-
path_pre, src_pre = fetch_dynamic_world_baseline(coords, interval_baseline, f"{session_id}_previous")
|
| 600 |
-
date_pre = f"{interval_baseline[0]} to {interval_baseline[1]}"
|
| 601 |
-
|
| 602 |
-
if path_pre:
|
| 603 |
-
mask_pre = tiff.imread(path_pre).astype(np.uint8)
|
| 604 |
-
rgb_pre = create_rgb_preview(path_pre, f"{session_id}_rgb_previous.jpg")
|
| 605 |
-
else:
|
| 606 |
-
mask_pre = np.zeros_like(mask_latest)
|
| 607 |
-
rgb_pre = None
|
| 608 |
-
|
| 609 |
-
else:
|
| 610 |
-
yield emit_log(30, f"βοΈ Optical Failed ({cloud_percent:.1f}% Dead Pixels). Switching to SAR Payload...")
|
| 611 |
-
path_latest, date_latest, src_latest = fetch_sar_data(coords, interval_latest, f"{session_id}_latest", is_baseline=False)
|
| 612 |
-
|
| 613 |
-
if not path_latest:
|
| 614 |
-
yield json.dumps({"error": "Critical Error: Failed to acquire both Optical and SAR fallback data."}) + "\n"
|
| 615 |
-
return
|
| 616 |
-
|
| 617 |
-
mask_latest, rgb_latest = process_sar_image(path_latest, f"{session_id}_rgb_latest_sar.png")
|
| 618 |
-
|
| 619 |
-
yield emit_log(50, "π‘ Fetching Homogeneous SAR Baseline...")
|
| 620 |
-
path_pre, date_pre_raw, src_pre = fetch_sar_data(coords, interval_baseline, f"{session_id}_previous", is_baseline=True)
|
| 621 |
-
date_pre = f"{interval_baseline[0]} to {interval_baseline[1]}"
|
| 622 |
-
|
| 623 |
-
if path_pre:
|
| 624 |
-
mask_pre, rgb_pre = process_sar_image(path_pre, f"{session_id}_rgb_previous_sar.png")
|
| 625 |
-
else:
|
| 626 |
-
mask_pre = np.zeros_like(mask_latest)
|
| 627 |
-
rgb_pre = None
|
| 628 |
-
|
| 629 |
-
yield emit_log(70, "π Aligning Baseline Maps...")
|
| 630 |
-
if mask_latest.shape != mask_pre.shape:
|
| 631 |
-
mask_pre = cv2.resize(mask_pre, (mask_latest.shape[1], mask_latest.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 632 |
-
|
| 633 |
-
yield emit_log(80, "π Generating Flood Difference...")
|
| 634 |
-
if apply_buffer:
|
| 635 |
-
buffer_kernel = np.ones((5, 5), np.uint8)
|
| 636 |
-
mask_pre_buffered = cv2.dilate(mask_pre, buffer_kernel, iterations=1)
|
| 637 |
-
flood_mask = cv2.bitwise_and(mask_latest, cv2.bitwise_not(mask_pre_buffered))
|
| 638 |
-
else:
|
| 639 |
-
flood_mask = cv2.bitwise_and(mask_latest, cv2.bitwise_not(mask_pre))
|
| 640 |
-
|
| 641 |
-
base_url = request.host_url.rstrip('/')
|
| 642 |
-
|
| 643 |
-
yield emit_log(85, "π¨ Generating Visual Overlays...")
|
| 644 |
-
url_latest = create_overlay_png(mask_latest, [255, 0, 0], f"{session_id}_overlay_latest.png")
|
| 645 |
-
url_pre = create_overlay_png(mask_pre, [255, 255, 0], f"{session_id}_overlay_previous.png")
|
| 646 |
-
url_flood = create_overlay_png(flood_mask, [0, 0, 255], f"{session_id}_overlay_flood.png")
|
| 647 |
-
|
| 648 |
-
yield emit_log(90, "π Calculating Spatial Metrics...")
|
| 649 |
-
n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
|
| 650 |
-
current_scale = 50 if max(n - s, e - w) > 0.5 else 10
|
| 651 |
-
pixel_to_sq_km = (current_scale ** 2) / 1_000_000
|
| 652 |
-
stats = {
|
| 653 |
-
"baseline_sq_km": np.count_nonzero(mask_pre == 255) * pixel_to_sq_km,
|
| 654 |
-
"current_sq_km": np.count_nonzero(mask_latest == 255) * pixel_to_sq_km,
|
| 655 |
-
"flood_sq_km": np.count_nonzero(flood_mask == 255) * pixel_to_sq_km,
|
| 656 |
-
}
|
| 657 |
-
dates = {"baseline": date_pre, "latest": date_latest}
|
| 658 |
-
|
| 659 |
-
yield emit_log(95, "π€ Generating AI Assessment Report...")
|
| 660 |
-
flood_image_path = os.path.join(DOWNLOADS_DIR, f"{session_id}_overlay_flood.png")
|
| 661 |
-
ai_report_text = generate_flood_report(stats, coords, dates, flood_image_path)
|
| 662 |
-
|
| 663 |
-
yield emit_log(98, "π Compiling PDF Report...")
|
| 664 |
-
report_filename = f"flood_assessment_report_{session_id}.pdf"
|
| 665 |
-
report_path = os.path.join(DOWNLOADS_DIR, report_filename)
|
| 666 |
-
|
| 667 |
-
pdf_img_path = os.path.join(DOWNLOADS_DIR, f"pdf_img_{session_id}.jpg")
|
| 668 |
-
|
| 669 |
-
if rgb_latest:
|
| 670 |
-
base_img = cv2.imread(os.path.join(DOWNLOADS_DIR, rgb_latest))
|
| 671 |
-
if base_img is not None and base_img.shape[2] == 4:
|
| 672 |
-
base_img = cv2.cvtColor(base_img, cv2.COLOR_BGRA2BGR)
|
| 673 |
-
else:
|
| 674 |
-
base_img = None
|
| 675 |
-
|
| 676 |
-
if base_img is not None:
|
| 677 |
-
pdf_img = base_img.copy()
|
| 678 |
-
|
| 679 |
-
if flood_mask.shape != pdf_img.shape[:2]:
|
| 680 |
-
safe_flood_mask = cv2.resize(flood_mask, (pdf_img.shape[1], pdf_img.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 681 |
-
else:
|
| 682 |
-
safe_flood_mask = flood_mask
|
| 683 |
-
|
| 684 |
-
pdf_img[safe_flood_mask == 255] = (0, 0, 255)
|
| 685 |
-
cv2.imwrite(pdf_img_path, pdf_img)
|
| 686 |
-
else:
|
| 687 |
-
pdf_img_path = flood_image_path
|
| 688 |
-
|
| 689 |
-
pdf = FPDF()
|
| 690 |
-
pdf.add_page()
|
| 691 |
-
pdf.set_font("helvetica", "B", 18)
|
| 692 |
-
pdf.cell(0, 15, "SatVision Flood Assessment Report", align="C", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
| 693 |
-
pdf.ln(5)
|
| 694 |
-
pdf.image(pdf_img_path, x=15, w=180)
|
| 695 |
-
pdf.ln(10)
|
| 696 |
-
pdf.set_font("helvetica", size=11)
|
| 697 |
-
pdf.multi_cell(0, 6, text=ai_report_text, markdown=True)
|
| 698 |
-
pdf.output(report_path)
|
| 699 |
-
|
| 700 |
-
yield emit_log(100, "β
Detection Complete!")
|
| 701 |
-
|
| 702 |
-
if generations_col is not None:
|
| 703 |
-
generation_record = {
|
| 704 |
-
"session_id": session_id,
|
| 705 |
-
"timestamp": datetime.now(),
|
| 706 |
-
"coordinates": coords,
|
| 707 |
-
"target_date": target_date_str,
|
| 708 |
-
"metadata": {
|
| 709 |
-
"latest_source": src_latest,
|
| 710 |
-
"baseline_source": src_pre,
|
| 711 |
-
"latest_date": date_latest,
|
| 712 |
-
"baseline_date": date_pre
|
| 713 |
-
},
|
| 714 |
-
"metrics": stats,
|
| 715 |
-
"ai_report": ai_report_text,
|
| 716 |
-
"logs": session_logs,
|
| 717 |
-
"feedback": None
|
| 718 |
-
}
|
| 719 |
-
try:
|
| 720 |
-
generations_col.insert_one(generation_record)
|
| 721 |
-
except Exception as db_err:
|
| 722 |
-
print(f"β DB Insert Failed: {db_err}")
|
| 723 |
-
|
| 724 |
-
yield json.dumps({
|
| 725 |
-
"progress": 100,
|
| 726 |
-
"session_id": session_id,
|
| 727 |
-
"result": {
|
| 728 |
-
"latest": f"{base_url}/mask/{url_latest}",
|
| 729 |
-
"previous": f"{base_url}/mask/{url_pre}",
|
| 730 |
-
"flood": f"{base_url}/mask/{url_flood}"
|
| 731 |
-
},
|
| 732 |
-
"meta": {
|
| 733 |
-
"latest_date": date_latest,
|
| 734 |
-
"previous_date": date_pre,
|
| 735 |
-
"latest_source": src_latest,
|
| 736 |
-
"previous_source": src_pre,
|
| 737 |
-
"latest_rgb": f"{base_url}/mask/{rgb_latest}" if rgb_latest else None,
|
| 738 |
-
"previous_rgb": f"{base_url}/mask/{rgb_pre}" if rgb_pre else None
|
| 739 |
-
},
|
| 740 |
-
"report": {
|
| 741 |
-
"text": ai_report_text,
|
| 742 |
-
"download_url": f"{base_url}/mask/{report_filename}",
|
| 743 |
-
"metrics": stats
|
| 744 |
-
}
|
| 745 |
-
}) + "\n"
|
| 746 |
-
|
| 747 |
-
return Response(stream_with_context(generate_process()), mimetype='application/json')
|
| 748 |
-
|
| 749 |
-
@app.route('/api/feedback', methods=['POST'])
|
| 750 |
-
def submit_feedback():
|
| 751 |
-
if generations_col is None:
|
| 752 |
-
return {"error": "Database unavailable"}, 503
|
| 753 |
-
|
| 754 |
-
data = request.json
|
| 755 |
-
session_id = data.get('session_id')
|
| 756 |
-
feedback_text = data.get('feedback_text')
|
| 757 |
-
rating = data.get('rating')
|
| 758 |
-
|
| 759 |
-
if not session_id or not feedback_text:
|
| 760 |
-
return {"error": "Missing session_id or feedback_text"}, 400
|
| 761 |
-
|
| 762 |
-
try:
|
| 763 |
-
result = generations_col.update_one(
|
| 764 |
-
{"session_id": session_id},
|
| 765 |
-
{"$set": {
|
| 766 |
-
"feedback": {
|
| 767 |
-
"text": feedback_text,
|
| 768 |
-
"rating": rating,
|
| 769 |
-
"submitted_at": datetime.now()
|
| 770 |
-
}
|
| 771 |
-
}}
|
| 772 |
-
)
|
| 773 |
-
|
| 774 |
-
if result.matched_count == 0:
|
| 775 |
-
return {"error": "Session ID not found"}, 404
|
| 776 |
-
|
| 777 |
-
return {"message": "Feedback recorded successfully"}, 200
|
| 778 |
-
|
| 779 |
-
except Exception as e:
|
| 780 |
-
print(f"β Feedback Update Error: {e}", flush=True)
|
| 781 |
-
return {"error": "Internal database error"}, 500
|
| 782 |
-
|
| 783 |
-
@app.route('/mask/<filename>')
|
| 784 |
-
def serve_mask(filename):
|
| 785 |
-
is_pdf = filename.endswith('.pdf')
|
| 786 |
-
return send_from_directory(DOWNLOADS_DIR, filename, as_attachment=is_pdf)
|
| 787 |
-
|
| 788 |
-
if __name__ == '__main__':
|
| 789 |
-
print("π Flood Backend Running...", flush=True)
|
| 790 |
-
app.run(host="0.0.0.0",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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/app.py
CHANGED
|
@@ -20,6 +20,9 @@ def create_app():
|
|
| 20 |
"""
|
| 21 |
app = Flask(__name__)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
# ========================================================================
|
| 24 |
# CONFIGURATION
|
| 25 |
# ========================================================================
|
|
@@ -36,7 +39,7 @@ def create_app():
|
|
| 36 |
try:
|
| 37 |
gee_service.initialize_earth_engine()
|
| 38 |
except Exception as e:
|
| 39 |
-
|
| 40 |
|
| 41 |
# ========================================================================
|
| 42 |
# INITIALIZATION: MONGODB
|
|
|
|
| 20 |
"""
|
| 21 |
app = Flask(__name__)
|
| 22 |
|
| 23 |
+
# Run dynamic environment sanity validation
|
| 24 |
+
config.validate_environment()
|
| 25 |
+
|
| 26 |
# ========================================================================
|
| 27 |
# CONFIGURATION
|
| 28 |
# ========================================================================
|
|
|
|
| 39 |
try:
|
| 40 |
gee_service.initialize_earth_engine()
|
| 41 |
except Exception as e:
|
| 42 |
+
config.logger.warning(f"β οΈ Earth Engine initialization warning: {e}")
|
| 43 |
|
| 44 |
# ========================================================================
|
| 45 |
# INITIALIZATION: MONGODB
|
src/config.py
CHANGED
|
@@ -1,5 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
CHECKPOINT_PATH = os.environ.get("CHECKPOINT_PATH", "./models/SatVision_Model.ckpt")
|
| 5 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -20,6 +29,23 @@ GEMINI_MODEL_ID = "gemini-2.5-flash"
|
|
| 20 |
GEMINI_TEMPERATURE = 0.1
|
| 21 |
GEMINI_TOP_P = 0.9
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
S2_CLOUD_THRESHOLD = 35
|
| 24 |
OPTICAL_CLEAR_THRESHOLD = 85.0
|
| 25 |
GEE_SCALE_OPTICAL = 100
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
# Configure structured enterprise logging
|
| 6 |
+
logging.basicConfig(
|
| 7 |
+
level=logging.INFO,
|
| 8 |
+
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
|
| 9 |
+
handlers=[logging.StreamHandler()]
|
| 10 |
+
)
|
| 11 |
+
logger = logging.getLogger("SatVision")
|
| 12 |
|
| 13 |
CHECKPOINT_PATH = os.environ.get("CHECKPOINT_PATH", "./models/SatVision_Model.ckpt")
|
| 14 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 29 |
GEMINI_TEMPERATURE = 0.1
|
| 30 |
GEMINI_TOP_P = 0.9
|
| 31 |
|
| 32 |
+
def validate_environment():
|
| 33 |
+
"""Validates required API keys and credentials at system startup."""
|
| 34 |
+
missing = []
|
| 35 |
+
if not EE_CREDENTIALS_JSON:
|
| 36 |
+
missing.append("EE_CREDENTIALS")
|
| 37 |
+
if not GEMINI_API_KEY:
|
| 38 |
+
missing.append("GEMINI_API_KEY")
|
| 39 |
+
|
| 40 |
+
if missing:
|
| 41 |
+
logger.warning(
|
| 42 |
+
f"β οΈ [Startup Security Audit] Missing required environment variables: {', '.join(missing)}. "
|
| 43 |
+
"Google Earth Engine downloads and Gemini report generation will fail in runtime if not configured."
|
| 44 |
+
)
|
| 45 |
+
else:
|
| 46 |
+
logger.info("β
[Startup Security Audit] All core environmental secrets verified successfully.")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
S2_CLOUD_THRESHOLD = 35
|
| 50 |
OPTICAL_CLEAR_THRESHOLD = 85.0
|
| 51 |
GEE_SCALE_OPTICAL = 100
|
src/database/gee.py
CHANGED
|
@@ -11,7 +11,7 @@ def initialize_earth_engine():
|
|
| 11 |
"""
|
| 12 |
Initialize Earth Engine with service account credentials or local auth.
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
|
| 16 |
try:
|
| 17 |
if config.EE_CREDENTIALS_JSON:
|
|
@@ -24,11 +24,11 @@ def initialize_earth_engine():
|
|
| 24 |
scopes=['https://www.googleapis.com/auth/earthengine']
|
| 25 |
)
|
| 26 |
ee.Initialize(credentials, project=config.EE_PROJECT_ID)
|
| 27 |
-
|
| 28 |
else:
|
| 29 |
# Use local authentication (cached credentials)
|
| 30 |
ee.Initialize(project=config.EE_PROJECT_ID)
|
| 31 |
-
|
| 32 |
except Exception as e:
|
| 33 |
-
|
| 34 |
raise
|
|
|
|
| 11 |
"""
|
| 12 |
Initialize Earth Engine with service account credentials or local auth.
|
| 13 |
"""
|
| 14 |
+
config.logger.info("π Initializing Earth Engine...")
|
| 15 |
|
| 16 |
try:
|
| 17 |
if config.EE_CREDENTIALS_JSON:
|
|
|
|
| 24 |
scopes=['https://www.googleapis.com/auth/earthengine']
|
| 25 |
)
|
| 26 |
ee.Initialize(credentials, project=config.EE_PROJECT_ID)
|
| 27 |
+
config.logger.info("β
Earth Engine Linked via Service Account.")
|
| 28 |
else:
|
| 29 |
# Use local authentication (cached credentials)
|
| 30 |
ee.Initialize(project=config.EE_PROJECT_ID)
|
| 31 |
+
config.logger.info("β
Earth Engine Linked via Local Auth.")
|
| 32 |
except Exception as e:
|
| 33 |
+
config.logger.error(f"β Earth Engine Init Failed: {e}")
|
| 34 |
raise
|
src/database/mongo.py
CHANGED
|
@@ -19,7 +19,7 @@ def initialize_mongodb():
|
|
| 19 |
global _mongo_client, _db, _generations_col
|
| 20 |
|
| 21 |
if config.MONGO_URI is None:
|
| 22 |
-
|
| 23 |
return None, None
|
| 24 |
|
| 25 |
try:
|
|
@@ -29,10 +29,10 @@ def initialize_mongodb():
|
|
| 29 |
|
| 30 |
# Verify connection
|
| 31 |
_mongo_client.admin.command('ping')
|
| 32 |
-
|
| 33 |
return _db, _generations_col
|
| 34 |
except Exception as e:
|
| 35 |
-
|
| 36 |
_mongo_client = None
|
| 37 |
_db = None
|
| 38 |
_generations_col = None
|
|
@@ -54,4 +54,4 @@ def close_mongodb():
|
|
| 54 |
global _mongo_client
|
| 55 |
if _mongo_client:
|
| 56 |
_mongo_client.close()
|
| 57 |
-
|
|
|
|
| 19 |
global _mongo_client, _db, _generations_col
|
| 20 |
|
| 21 |
if config.MONGO_URI is None:
|
| 22 |
+
config.logger.warning("β οΈ MongoDB URI not configured (MONGO_URI env var missing)")
|
| 23 |
return None, None
|
| 24 |
|
| 25 |
try:
|
|
|
|
| 29 |
|
| 30 |
# Verify connection
|
| 31 |
_mongo_client.admin.command('ping')
|
| 32 |
+
config.logger.info("β
MongoDB Connected Successfully!")
|
| 33 |
return _db, _generations_col
|
| 34 |
except Exception as e:
|
| 35 |
+
config.logger.error(f"β MongoDB Connection Failed: {e}")
|
| 36 |
_mongo_client = None
|
| 37 |
_db = None
|
| 38 |
_generations_col = None
|
|
|
|
| 54 |
global _mongo_client
|
| 55 |
if _mongo_client:
|
| 56 |
_mongo_client.close()
|
| 57 |
+
config.logger.info("β
MongoDB connection closed")
|
src/services/detection.py
CHANGED
|
@@ -200,7 +200,7 @@ def run_detection_pipeline(coords, target_date, apply_buffer=True, base_url="htt
|
|
| 200 |
pdf.multi_cell(0, 6, text=safe_pdf_text)
|
| 201 |
pdf.output(report_path)
|
| 202 |
except Exception as pdf_err:
|
| 203 |
-
|
| 204 |
|
| 205 |
yield emit_log(100, "β
Detection Pipeline Complete!")
|
| 206 |
|
|
@@ -219,7 +219,7 @@ def run_detection_pipeline(coords, target_date, apply_buffer=True, base_url="htt
|
|
| 219 |
try:
|
| 220 |
generations_col.insert_one(generation_record)
|
| 221 |
except Exception as db_err:
|
| 222 |
-
|
| 223 |
|
| 224 |
# Sanitize URLs to point to unique session paths: e.g. base_url/mask/session_id/filename.png
|
| 225 |
c_lat = f"{session_id}/{os.path.basename(raw_url_latest)}" if raw_url_latest else ""
|
|
|
|
| 200 |
pdf.multi_cell(0, 6, text=safe_pdf_text)
|
| 201 |
pdf.output(report_path)
|
| 202 |
except Exception as pdf_err:
|
| 203 |
+
config.logger.error(f"β PDF Engine Failed: {pdf_err}")
|
| 204 |
|
| 205 |
yield emit_log(100, "β
Detection Pipeline Complete!")
|
| 206 |
|
|
|
|
| 219 |
try:
|
| 220 |
generations_col.insert_one(generation_record)
|
| 221 |
except Exception as db_err:
|
| 222 |
+
config.logger.error(f"β DB Write Blocked: {db_err}")
|
| 223 |
|
| 224 |
# Sanitize URLs to point to unique session paths: e.g. base_url/mask/session_id/filename.png
|
| 225 |
c_lat = f"{session_id}/{os.path.basename(raw_url_latest)}" if raw_url_latest else ""
|
src/services/gee_service.py
CHANGED
|
@@ -29,7 +29,7 @@ def download_gee_tile(image, n, s, e, w, prefix, r, c, scale=10):
|
|
| 29 |
try:
|
| 30 |
resp = requests.get(url, timeout=config.GEE_REQUEST_TIMEOUT)
|
| 31 |
if resp.status_code != 200:
|
| 32 |
-
|
| 33 |
return None
|
| 34 |
|
| 35 |
path = os.path.join(config.DOWNLOADS_DIR, f"{prefix}_tile_{r}_{c}.tiff")
|
|
@@ -37,7 +37,7 @@ def download_gee_tile(image, n, s, e, w, prefix, r, c, scale=10):
|
|
| 37 |
f.write(resp.content)
|
| 38 |
return path
|
| 39 |
except Exception as e:
|
| 40 |
-
|
| 41 |
return None
|
| 42 |
|
| 43 |
|
|
@@ -170,7 +170,7 @@ def fetch_optical_composite(coords, time_interval, filename_prefix, scale=10):
|
|
| 170 |
return save_path, actual_date, source_type, cloud_percent
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
-
|
| 174 |
return None, None, None, 100.0
|
| 175 |
|
| 176 |
|
|
@@ -239,7 +239,7 @@ def fetch_sar_data(coords, time_interval, filename_prefix, is_baseline=False, sc
|
|
| 239 |
return save_path, actual_date, source_type
|
| 240 |
|
| 241 |
except Exception as e:
|
| 242 |
-
|
| 243 |
return None, None, None
|
| 244 |
|
| 245 |
|
|
@@ -298,5 +298,5 @@ def fetch_dynamic_world_baseline(coords, time_interval, filename_prefix, scale=1
|
|
| 298 |
return save_path, "Dynamic Dry-Season Anchor (April-May)"
|
| 299 |
|
| 300 |
except Exception as e:
|
| 301 |
-
|
| 302 |
return None, None
|
|
|
|
| 29 |
try:
|
| 30 |
resp = requests.get(url, timeout=config.GEE_REQUEST_TIMEOUT)
|
| 31 |
if resp.status_code != 200:
|
| 32 |
+
config.logger.warning(f"β Tile {r}-{c} failed with status {resp.status_code}.")
|
| 33 |
return None
|
| 34 |
|
| 35 |
path = os.path.join(config.DOWNLOADS_DIR, f"{prefix}_tile_{r}_{c}.tiff")
|
|
|
|
| 37 |
f.write(resp.content)
|
| 38 |
return path
|
| 39 |
except Exception as e:
|
| 40 |
+
config.logger.error(f"β Tile download failed {r}-{c}: {e}")
|
| 41 |
return None
|
| 42 |
|
| 43 |
|
|
|
|
| 170 |
return save_path, actual_date, source_type, cloud_percent
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
+
config.logger.error(f"β GEE Optical Error {filename_prefix}: {e}")
|
| 174 |
return None, None, None, 100.0
|
| 175 |
|
| 176 |
|
|
|
|
| 239 |
return save_path, actual_date, source_type
|
| 240 |
|
| 241 |
except Exception as e:
|
| 242 |
+
config.logger.error(f"β SAR Error {filename_prefix}: {e}")
|
| 243 |
return None, None, None
|
| 244 |
|
| 245 |
|
|
|
|
| 298 |
return save_path, "Dynamic Dry-Season Anchor (April-May)"
|
| 299 |
|
| 300 |
except Exception as e:
|
| 301 |
+
config.logger.error(f"β Dynamic World Error {filename_prefix}: {e}")
|
| 302 |
return None, None
|
src/services/gemini.py
CHANGED
|
@@ -87,5 +87,5 @@ Provide 3 highly specific, actionable recommendations for on-the-ground disaster
|
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
import traceback
|
| 90 |
-
|
| 91 |
return "Error: AI Report generation failed due to an internal system error."
|
|
|
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
import traceback
|
| 90 |
+
config.logger.error(f"β AI Report Error: {e}\n{traceback.format_exc()}")
|
| 91 |
return "Error: AI Report generation failed due to an internal system error."
|
src/services/model.py
CHANGED
|
@@ -52,10 +52,10 @@ def initialize_model():
|
|
| 52 |
"""
|
| 53 |
global _model
|
| 54 |
|
| 55 |
-
|
| 56 |
|
| 57 |
if not os.path.exists(config.CHECKPOINT_PATH):
|
| 58 |
-
|
| 59 |
_model = None
|
| 60 |
return None
|
| 61 |
|
|
@@ -66,10 +66,10 @@ def initialize_model():
|
|
| 66 |
)
|
| 67 |
_model.to(config.DEVICE)
|
| 68 |
_model.eval()
|
| 69 |
-
|
| 70 |
return _model
|
| 71 |
except Exception as e:
|
| 72 |
-
|
| 73 |
_model = None
|
| 74 |
return None
|
| 75 |
|
|
|
|
| 52 |
"""
|
| 53 |
global _model
|
| 54 |
|
| 55 |
+
config.logger.info("β³ Loading model...")
|
| 56 |
|
| 57 |
if not os.path.exists(config.CHECKPOINT_PATH):
|
| 58 |
+
config.logger.error(f"β Model Checkpoint Not Found: {config.CHECKPOINT_PATH}")
|
| 59 |
_model = None
|
| 60 |
return None
|
| 61 |
|
|
|
|
| 66 |
)
|
| 67 |
_model.to(config.DEVICE)
|
| 68 |
_model.eval()
|
| 69 |
+
config.logger.info("β
Model Loaded Successfully!")
|
| 70 |
return _model
|
| 71 |
except Exception as e:
|
| 72 |
+
config.logger.error(f"β Model Load Failed: {e}")
|
| 73 |
_model = None
|
| 74 |
return None
|
| 75 |
|