amadmateen commited on
Commit
fab54be
·
0 Parent(s):

Setup: Unified repository structure with SatVision model and configs

Browse files
Dockerfile ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # 1. Start as root to handle system packages and folder creation
4
+ USER root
5
+
6
+ # Install essential system dependencies required for OpenCV (cv2) and OS operations
7
+ RUN apt-get update && apt-get install -y --no-install-recommends \
8
+ libgl1 \
9
+ libglib2.0-0 \
10
+ build-essential \
11
+ && rm -rf /var/lib/apt/lists/*
12
+
13
+ # Set the working directory
14
+ WORKDIR /app
15
+
16
+ # 2. CRITICAL FIX: Create the directories as root FIRST, so they exist in the file tree
17
+ RUN mkdir -p /app/models /app/server_downloads
18
+
19
+ # 3. Create the Hugging Face space user
20
+ RUN useradd -m -u 1000 user
21
+
22
+ # 4. CRITICAL FIX: Change ownership of the entire /app workspace over to the non-root user
23
+ RUN chown -R user:user /app
24
+
25
+ # 5. Copy python dependencies layout
26
+ COPY --chown=user requirements.txt /app/
27
+
28
+ # 6. Switch to the Hugging Face user safely now that permissions are locked down
29
+ USER user
30
+ ENV PATH="/home/user/.local/bin:$PATH"
31
+
32
+ RUN pip install --no-cache-dir --upgrade pip && \
33
+ pip install --no-cache-dir -r requirements.txt
34
+
35
+ # 7. Copy the rest of the application files with user permissions
36
+ COPY --chown=user . /app/
37
+
38
+ # Expose standard routing port mapping interface
39
+ EXPOSE 7860
40
+
41
+ # Start production server using your correct modular entry point
42
+ CMD ["python", "main.py"]
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: SatVision
3
+ emoji: 🌊
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: false
9
+ ---
10
+
11
+ # SatVision Backend
12
+ This is the Dockerized Flask backend for the SatVision flood segmentation tool.
main.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ SatVision Backend - Entry Point Script
4
+
5
+ Refactored monolithic server.py into a modular, production-grade Flask application.
6
+ Implements Application Factory pattern with clean separation of concerns.
7
+
8
+ Usage:
9
+ python main.py
10
+ """
11
+
12
+ from src import config
13
+ from src.app import create_app
14
+
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()
22
+
23
+ # Run Flask application server
24
+ app.run(
25
+ host=config.FLASK_HOST,
26
+ port=config.FLASK_PORT,
27
+ threaded=config.FLASK_THREADED,
28
+ debug=False
29
+ )
30
+
31
+
32
+ if __name__ == '__main__':
33
+ main()
models/SatVision_Model.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7359d42de491d640daacc606d6e4b69c6769d8d8ac62a2c730eb57c6723219e9
3
+ size 313423583
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ build-essential
requirements.txt ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cpu
2
+
3
+ Flask==3.0.3
4
+ flask-cors==4.0.1
5
+ numpy==1.26.4
6
+ tifffile==2024.2.12
7
+ opencv-python-headless==4.9.0.80
8
+ torch==2.2.1+cpu
9
+ torchvision==0.17.1+cpu
10
+ torchaudio==2.2.1+cpu
11
+ pytorch-lightning==2.2.1
12
+ segmentation-models-pytorch==0.3.3
13
+ earthengine-api==0.1.396
14
+ requests==2.31.0
15
+ six
16
+ huggingface_hub>=0.20.0
17
+ google-genai
18
+ pillow
19
+ fpdf2
20
+ pymongo
21
+ gunicorn==21.2.0
server.py ADDED
@@ -0,0 +1,790 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", port=7860, threaded=True)
src/__init__.py ADDED
File without changes
src/app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Flask Application Factory.
3
+ """
4
+
5
+ from flask import Flask
6
+ from flask_cors import CORS
7
+ from src import config
8
+ from src.database import mongo as mongo_service
9
+ from src.database import gee as gee_service
10
+ from src.services import model as model_service
11
+ from src.routes.health import health_bp
12
+ from src.routes.detection import detection_bp
13
+ from src.routes.feedback import feedback_bp
14
+ from src.routes.assets import assets_bp
15
+
16
+
17
+ def create_app():
18
+ """
19
+ Application factory function.
20
+ """
21
+ app = Flask(__name__)
22
+
23
+ # ========================================================================
24
+ # CONFIGURATION
25
+ # ========================================================================
26
+ app.config['JSON_SORT_KEYS'] = False
27
+
28
+ # ========================================================================
29
+ # CORS SETUP
30
+ # ========================================================================
31
+ CORS(app)
32
+
33
+ # ========================================================================
34
+ # INITIALIZATION: EARTH ENGINE
35
+ # ========================================================================
36
+ try:
37
+ gee_service.initialize_earth_engine()
38
+ except Exception as e:
39
+ print(f"⚠️ Earth Engine initialization warning: {e}", flush=True)
40
+
41
+ # ========================================================================
42
+ # INITIALIZATION: MONGODB
43
+ # ========================================================================
44
+ db, generations_col = mongo_service.initialize_mongodb()
45
+
46
+ # ========================================================================
47
+ # INITIALIZATION: MODEL
48
+ # ========================================================================
49
+ model = model_service.initialize_model()
50
+
51
+ # ========================================================================
52
+ # BLUEPRINT REGISTRATION
53
+ # ========================================================================
54
+ # Explicitly registering routing layers
55
+ app.register_blueprint(health_bp)
56
+ app.register_blueprint(detection_bp)
57
+ app.register_blueprint(feedback_bp)
58
+
59
+ # Keep assets mounted directly to the root url context /mask/<filename>
60
+ # to perfectly mirror the old working monolithic environment
61
+ app.register_blueprint(assets_bp)
62
+
63
+ # ========================================================================
64
+ # ERROR HANDLERS
65
+ # ========================================================================
66
+ @app.errorhandler(404)
67
+ def not_found(error):
68
+ return {"error": "Not found"}, 404
69
+
70
+ @app.errorhandler(500)
71
+ def internal_error(error):
72
+ return {"error": "Internal server error"}, 500
73
+
74
+ return app
src/config.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
6
+
7
+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8
+ DOWNLOADS_DIR = os.environ.get("DOWNLOADS_DIR", os.path.join(BASE_DIR, "server_downloads"))
9
+ os.makedirs(DOWNLOADS_DIR, exist_ok=True)
10
+
11
+ MONGO_URI = os.environ.get("MONGO_URI", "mongodb://localhost:27017/")
12
+ MONGO_DB_NAME = "satvision_db"
13
+ MONGO_COLLECTION_NAME = "generations"
14
+
15
+ EE_CREDENTIALS_JSON = os.environ.get("EE_CREDENTIALS")
16
+ EE_PROJECT_ID = "gen-lang-client-0114614261"
17
+
18
+ GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
19
+ 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
26
+ GEE_REQUEST_TIMEOUT = 240 # Increased timeout limit to protect massive calculations
27
+
28
+ # Dynamic Tile Segmentation Rules based on Coordinate Span Degrees
29
+ MAX_DEG_STEP_XLARGE = 0.80
30
+ MAX_DEG_STEP_LARGE = 0.40
31
+ MAX_DEG_STEP_SMALL = 0.08
32
+
33
+ # Meter Resolution Adjustments (Higher numbers = rougher zoom but significantly faster)
34
+ SCALE_XLARGE = 90 # For presets like Lake Manchar / Dadu
35
+ SCALE_LARGE = 40 # For regional maps
36
+ SCALE_SMALL = 10 # For highly focused cities
37
+
38
+ TILE_SIZE = 256
39
+ TILE_OVERLAP_RATIO = 0.5
40
+ BATCH_SIZE = 16
41
+
42
+ CLOUD_BUFFER_KERNEL_SIZE = (10, 10)
43
+ CLOUD_DETECTION_THRESHOLD = 0.5
44
+ BRIGHTNESS_THRESHOLD = 0.35
45
+ NDWI_CLOUD_THRESHOLD = 0.15
46
+ NDWI_WATER_THRESHOLD = 0.35
47
+ NDWI_MIN_THRESHOLD = -0.1
48
+
49
+ CLOSE_KERNEL_SIZE = (3, 3)
50
+ OPEN_KERNEL_SIZE = (2, 2)
51
+ MIN_CONTOUR_AREA = 50
52
+
53
+ SAR_WATER_DB_RANGE = (-16, -90)
54
+ SAR_DB_OFFSET = 25
55
+
56
+ RGB_BAND_INDICES = [0, 1, 2]
57
+ RGB_SCALING_FACTOR = 3.0
58
+ OPTICAL_NORMALIZATION_FACTOR = 10000.0
59
+ GAUSSIAN_BLUR_SIGMA = 2.0
60
+ GAUSSIAN_BLUR_SHARPEN_STRENGTH = 1.5
61
+ GAUSSIAN_BLUR_SUBTRACT_STRENGTH = -0.5
62
+
63
+ OVERLAY_WATER_COLOR_BGR = (255, 0, 0)
64
+ OVERLAY_PREVIOUS_COLOR_BGR = (255, 255, 0)
65
+ OVERLAY_FLOOD_COLOR_BGR = (0, 0, 255)
66
+ OVERLAY_ALPHA = 180
67
+
68
+ FLASK_HOST = "0.0.0.0"
69
+ FLASK_PORT = int(os.environ.get("FLASK_PORT", 7860))
70
+ FLASK_THREADED = True
71
+
72
+ def calculate_pixel_to_sq_km(scale_meters):
73
+ return (scale_meters ** 2) / 1_000_000
74
+
75
+ S2_BANDS = ['B2', 'B3', 'B4', 'B8', 'B11', 'B12']
76
+ S1_POLARIZATION = 'VV'
77
+ S1_INSTRUMENT_MODE = 'IW'
78
+ DW_WATER_CLASS = 0
src/database/__init__.py ADDED
File without changes
src/database/gee.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Google Earth Engine initialization and authentication.
3
+ """
4
+
5
+ import json
6
+ import ee
7
+ from src import config
8
+
9
+
10
+ def initialize_earth_engine():
11
+ """
12
+ Initialize Earth Engine with service account credentials or local auth.
13
+ """
14
+ print("🌍 Initializing Earth Engine...", flush=True)
15
+
16
+ try:
17
+ if config.EE_CREDENTIALS_JSON:
18
+ # Use service account credentials from environment
19
+ key_dict = json.loads(config.EE_CREDENTIALS_JSON)
20
+ from google.oauth2 import service_account
21
+
22
+ credentials = service_account.Credentials.from_service_account_info(
23
+ key_dict,
24
+ scopes=['https://www.googleapis.com/auth/earthengine']
25
+ )
26
+ ee.Initialize(credentials, project=config.EE_PROJECT_ID)
27
+ print("✅ Earth Engine Linked via Service Account.", flush=True)
28
+ else:
29
+ # Use local authentication (cached credentials)
30
+ ee.Initialize(project=config.EE_PROJECT_ID)
31
+ print("✅ Earth Engine Linked via Local Auth.", flush=True)
32
+ except Exception as e:
33
+ print(f"❌ Earth Engine Init Failed: {e}", flush=True)
34
+ raise
src/database/mongo.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ MongoDB client initialization and connection management.
3
+ """
4
+
5
+ from pymongo import MongoClient
6
+ from src import config
7
+
8
+ # Global MongoDB client and database references
9
+ _mongo_client = None
10
+ _db = None
11
+ _generations_col = None
12
+
13
+
14
+ def initialize_mongodb():
15
+ """
16
+ Initialize MongoDB connection with error handling.
17
+ Returns tuple of (db, generations_collection) or (None, None) on failure.
18
+ """
19
+ global _mongo_client, _db, _generations_col
20
+
21
+ if config.MONGO_URI is None:
22
+ print("⚠️ MongoDB URI not configured (MONGO_URI env var missing)", flush=True)
23
+ return None, None
24
+
25
+ try:
26
+ _mongo_client = MongoClient(config.MONGO_URI, serverSelectionTimeoutMS=5000)
27
+ _db = _mongo_client[config.MONGO_DB_NAME]
28
+ _generations_col = _db[config.MONGO_COLLECTION_NAME]
29
+
30
+ # Verify connection
31
+ _mongo_client.admin.command('ping')
32
+ print("✅ MongoDB Connected Successfully!", flush=True)
33
+ return _db, _generations_col
34
+ except Exception as e:
35
+ print(f"❌ MongoDB Connection Failed: {e}", flush=True)
36
+ _mongo_client = None
37
+ _db = None
38
+ _generations_col = None
39
+ return None, None
40
+
41
+
42
+ def get_database():
43
+ """Get the initialized database instance."""
44
+ return _db
45
+
46
+
47
+ def get_generations_collection():
48
+ """Get the generations collection instance."""
49
+ return _generations_col
50
+
51
+
52
+ def close_mongodb():
53
+ """Close MongoDB connection."""
54
+ global _mongo_client
55
+ if _mongo_client:
56
+ _mongo_client.close()
57
+ print("✅ MongoDB connection closed", flush=True)
src/routes/__init__.py ADDED
File without changes
src/routes/assets.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Flask Blueprints for serving generated assets across isolated concurrent sessions.
3
+ """
4
+
5
+ import os
6
+ from flask import Blueprint, send_from_directory, abort
7
+ from src import config
8
+
9
+ assets_bp = Blueprint('assets', __name__)
10
+
11
+ @assets_bp.route('/mask/<path:filename>')
12
+ def serve_mask(filename):
13
+ """
14
+ Serve generated assets safely by supporting flat configurations or session sub-folders.
15
+ """
16
+ # Try locating file in isolated session directory first, fallback to root downloads
17
+ parts = filename.split('/')
18
+ if len(parts) > 1:
19
+ # e.g. /mask/session_uuid/filename.png
20
+ safe_path = os.path.join(config.DOWNLOADS_DIR, filename)
21
+ else:
22
+ # Check if the filename itself starts with a uuid prefix we can extract
23
+ prefix = filename.split('_')[0]
24
+ possible_session_dir = os.path.join(config.DOWNLOADS_DIR, prefix)
25
+ if os.path.exists(os.path.join(possible_session_dir, filename)):
26
+ return send_from_directory(possible_session_dir, filename, as_attachment=filename.lower().endswith('.pdf'))
27
+
28
+ safe_path = os.path.join(config.DOWNLOADS_DIR, filename)
29
+
30
+ if not os.path.exists(safe_path):
31
+ print(f"❌ Asset Serving Failed: File not found at path {safe_path}", flush=True)
32
+ abort(404)
33
+
34
+ return send_from_directory(
35
+ os.path.dirname(safe_path),
36
+ os.path.basename(safe_path),
37
+ as_attachment=filename.lower().endswith('.pdf')
38
+ )
src/routes/detection.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Flask Blueprints for flood detection endpoint with streaming response.
3
+ """
4
+
5
+ from flask import Blueprint, request, Response, stream_with_context
6
+ from datetime import datetime
7
+ from src.services import detection as detection_service
8
+
9
+ detection_bp = Blueprint('detection', __name__, url_prefix='/api')
10
+
11
+
12
+ @detection_bp.route('/detect_stream', methods=['POST'])
13
+ def detect_stream():
14
+ """
15
+ Main detection endpoint with streaming response.
16
+
17
+ Expects JSON POST body:
18
+ {
19
+ "bbox": {"north": float, "south": float, "east": float, "west": float},
20
+ "apply_buffer": bool (optional, default true),
21
+ "target_date": "YYYY-MM-DD" (optional, defaults to today)
22
+ }
23
+ """
24
+ data = request.json
25
+ coords = data.get('bbox')
26
+ apply_buffer = data.get('apply_buffer', True)
27
+
28
+ target_date_str = data.get('target_date')
29
+ if target_date_str:
30
+ target_date = datetime.strptime(target_date_str, '%Y-%m-%d')
31
+ else:
32
+ target_date = datetime.now()
33
+
34
+ def generate_process():
35
+ """Generator for streaming detection results."""
36
+ # Extract base host URL path to capture local domain ports or cloud host spaces dynamically
37
+ base_url = request.host_url.rstrip('/')
38
+
39
+ # Run detection pipeline orchestration logic with the host URL correctly wired down
40
+ for chunk in detection_service.run_detection_pipeline(
41
+ coords=coords,
42
+ target_date=target_date,
43
+ apply_buffer=apply_buffer,
44
+ base_url=base_url
45
+ ):
46
+ yield chunk
47
+
48
+ return Response(stream_with_context(generate_process()), mimetype='application/json')
src/routes/feedback.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Flask Blueprints for feedback submission endpoint.
3
+ """
4
+
5
+ from flask import Blueprint, request
6
+ from datetime import datetime
7
+ from src.database.mongo import get_generations_collection
8
+
9
+ feedback_bp = Blueprint('feedback', __name__, url_prefix='/api')
10
+
11
+
12
+ @feedback_bp.route('/feedback', methods=['POST'])
13
+ def submit_feedback():
14
+ """
15
+ Submit feedback for a detection session.
16
+
17
+ Expects JSON POST body:
18
+ {
19
+ "session_id": str,
20
+ "feedback_text": str,
21
+ "rating": int (optional)
22
+ }
23
+ """
24
+ generations_col = get_generations_collection()
25
+
26
+ if generations_col is None:
27
+ return {"error": "Database unavailable"}, 503
28
+
29
+ data = request.json
30
+ session_id = data.get('session_id')
31
+ feedback_text = data.get('feedback_text')
32
+ rating = data.get('rating')
33
+
34
+ if not session_id or not feedback_text:
35
+ return {"error": "Missing session_id or feedback_text"}, 400
36
+
37
+ try:
38
+ result = generations_col.update_one(
39
+ {"session_id": session_id},
40
+ {"$set": {
41
+ "feedback": {
42
+ "text": feedback_text,
43
+ "rating": rating,
44
+ "submitted_at": datetime.now()
45
+ }
46
+ }}
47
+ )
48
+
49
+ if result.matched_count == 0:
50
+ return {"error": "Session ID not found"}, 404
51
+
52
+ return {"message": "Feedback recorded successfully"}, 200
53
+
54
+ except Exception as e:
55
+ print(f"❌ Feedback Update Error: {e}", flush=True)
56
+ return {"error": "Internal database error"}, 500
src/routes/health.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Flask Blueprints for health check endpoint.
3
+ """
4
+
5
+ from flask import Blueprint
6
+
7
+ health_bp = Blueprint('health', __name__)
8
+
9
+
10
+ @health_bp.route('/', methods=['GET'])
11
+ def health_check():
12
+ """Health check endpoint."""
13
+ return "✅ SatVision Backend is alive!", 200
src/services/__init__.py ADDED
File without changes
src/services/cv2.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ OpenCV-based image processing services.
3
+ Handles water mask prediction, SAR processing, RGB preview generation, and overlay creation.
4
+ """
5
+
6
+ import os
7
+ import numpy as np
8
+ import cv2
9
+ import tifffile as tiff
10
+ from src import config
11
+
12
+
13
+ def apply_hanning_window(tile_size):
14
+ """Generate a 2D Hanning window for smooth tile blending."""
15
+ window_1d = np.hanning(tile_size)
16
+ return np.outer(window_1d, window_1d).astype(np.float32)
17
+
18
+
19
+ def apply_cloud_detection(prob_cloud, brightness):
20
+ """Detect cloud pixels based on probability and brightness."""
21
+ return ((prob_cloud > config.CLOUD_DETECTION_THRESHOLD) &
22
+ (brightness > config.BRIGHTNESS_THRESHOLD)).astype(np.uint8)
23
+
24
+
25
+ def apply_water_detection(prob_water, ndwi, cloud_mask):
26
+ """Detect water pixels based on probability, NDWI, and cloud exclusion."""
27
+ raw_water = (prob_water > config.NDWI_WATER_THRESHOLD) | (ndwi > config.NDWI_CLOUD_THRESHOLD)
28
+ return raw_water & (cloud_mask == 0) & (ndwi > config.NDWI_MIN_THRESHOLD)
29
+
30
+
31
+ def apply_morphological_cleanup(mask):
32
+ """Apply closing and opening operations to clean mask."""
33
+ close_kernel = np.ones(config.CLOSE_KERNEL_SIZE, np.uint8)
34
+ mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, close_kernel)
35
+
36
+ open_kernel = np.ones(config.OPEN_KERNEL_SIZE, np.uint8)
37
+ mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, open_kernel)
38
+
39
+ return mask
40
+
41
+
42
+ def filter_small_contours(mask):
43
+ """Remove contours smaller than minimum area threshold."""
44
+ contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
45
+
46
+ for cnt in contours:
47
+ area = cv2.contourArea(cnt)
48
+ if area < config.MIN_CONTOUR_AREA:
49
+ cv2.drawContours(mask, [cnt], -1, 0, -1)
50
+
51
+ return mask
52
+
53
+
54
+ def process_sar_image(tiff_path, preview_filename):
55
+ """
56
+ Process SAR image for water detection and visualization.
57
+ """
58
+ if not os.path.exists(tiff_path):
59
+ return None, None
60
+
61
+ try:
62
+ sar_db = tiff.imread(tiff_path).astype(np.float32)
63
+
64
+ water_min, water_max = config.SAR_WATER_DB_RANGE
65
+ water_mask = ((sar_db < water_min) & (sar_db > water_max)).astype(np.uint8) * 255
66
+
67
+ close_kernel = np.ones(config.CLOSE_KERNEL_SIZE, np.uint8)
68
+ water_mask = cv2.morphologyEx(water_mask, cv2.MORPH_CLOSE, close_kernel)
69
+
70
+ visual = np.clip((sar_db + config.SAR_DB_OFFSET) / config.SAR_DB_OFFSET, 0, 1) * 255
71
+ visual = visual.astype(np.uint8)
72
+
73
+ h, w = visual.shape
74
+ rgba = np.zeros((h, w, 4), dtype=np.uint8)
75
+ rgba[:, :, 0] = visual # B
76
+ rgba[:, :, 1] = visual # G
77
+ rgba[:, :, 2] = visual # R
78
+ rgba[:, :, 3] = np.where(sar_db < -90, 0, 255) # Transparency check for missing background data
79
+
80
+ save_path = os.path.join(config.DOWNLOADS_DIR, preview_filename)
81
+ cv2.imwrite(save_path, rgba)
82
+
83
+ return water_mask, preview_filename
84
+
85
+ except Exception as e:
86
+ print(f"❌ SAR Processing Error {tiff_path}: {e}", flush=True)
87
+ return None, None
88
+
89
+
90
+ def create_rgb_preview(tiff_path, filename):
91
+ """
92
+ Create an enhanced RGB preview from optical or water mask data.
93
+ """
94
+ if not os.path.exists(tiff_path):
95
+ return None
96
+
97
+ try:
98
+ image = tiff.imread(tiff_path).astype(np.float32)
99
+
100
+ if image.ndim == 2:
101
+ rgb = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
102
+ rgb[image == 255] = [255, 0, 0]
103
+ save_path = os.path.join(config.DOWNLOADS_DIR, filename)
104
+ cv2.imwrite(save_path, rgb)
105
+ return filename
106
+
107
+ if image.shape[0] == 6:
108
+ image = np.transpose(image, (1, 2, 0))
109
+ if np.max(image) > 1.0:
110
+ image = image / config.OPTICAL_NORMALIZATION_FACTOR
111
+
112
+ rgb = image[:, :, config.RGB_BAND_INDICES]
113
+ rgb = np.clip(rgb * config.RGB_SCALING_FACTOR, 0, 1) * 255
114
+ rgb = rgb.astype(np.uint8)
115
+
116
+ gaussian = cv2.GaussianBlur(rgb, (0, 0), config.GAUSSIAN_BLUR_SIGMA)
117
+ sharpened = cv2.addWeighted(
118
+ rgb,
119
+ config.GAUSSIAN_BLUR_SHARPEN_STRENGTH,
120
+ gaussian,
121
+ config.GAUSSIAN_BLUR_SUBTRACT_STRENGTH,
122
+ 0
123
+ )
124
+
125
+ save_path = os.path.join(config.DOWNLOADS_DIR, filename)
126
+ cv2.imwrite(save_path, sharpened)
127
+ return filename
128
+
129
+ except Exception as e:
130
+ print(f"❌ RGB Preview Error {tiff_path}: {e}", flush=True)
131
+ return None
132
+
133
+
134
+ def create_overlay_png(mask, color, filename):
135
+ """
136
+ Create a semi-transparent PNG overlay from a binary mask.
137
+ """
138
+ if mask is None:
139
+ return None
140
+
141
+ try:
142
+ h, w = mask.shape
143
+ rgba = np.zeros((h, w, 4), dtype=np.uint8)
144
+ b, g, r = color
145
+
146
+ rgba[mask == 255, 0] = b
147
+ rgba[mask == 255, 1] = g
148
+ rgba[mask == 255, 2] = r
149
+ rgba[mask == 255, 3] = config.OVERLAY_ALPHA
150
+
151
+ save_path = os.path.join(config.DOWNLOADS_DIR, filename)
152
+ cv2.imwrite(save_path, rgba)
153
+ return filename
154
+
155
+ except Exception as e:
156
+ print(f"❌ Overlay Error {filename}: {e}", flush=True)
157
+ return None
src/services/detection.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Concurrency-insulated, adaptive resolution flood detection pipeline.
3
+ """
4
+
5
+ import os
6
+ import json
7
+ from datetime import datetime, timedelta
8
+ import numpy as np
9
+ import tifffile as tiff
10
+ import cv2
11
+ import uuid
12
+ from fpdf import FPDF
13
+ from fpdf.enums import XPos, YPos
14
+
15
+ from src import config
16
+ from src.services import model as model_service
17
+ from src.services import gee_service
18
+ from src.services import cv2 as cv2_service
19
+ from src.services import gemini as gemini_service
20
+ from src.database.mongo import get_generations_collection
21
+
22
+
23
+ def run_detection_pipeline(coords, target_date, apply_buffer=True, base_url="http://localhost:7860"):
24
+ """
25
+ Insulated pipeline preventing concurrent leakages by running operations
26
+ inside runtime session directories and scaling resolution dynamically.
27
+ """
28
+ session_id = str(uuid.uuid4())
29
+
30
+ # CRITICAL: Create isolated concurrent session folder workspace
31
+ session_dir = os.path.join(config.DOWNLOADS_DIR, session_id)
32
+ os.makedirs(session_dir, exist_ok=True)
33
+
34
+ session_logs = []
35
+
36
+ def emit_log(progress, message):
37
+ session_logs.append({
38
+ "progress": progress,
39
+ "log": message,
40
+ "timestamp": datetime.now().isoformat()
41
+ })
42
+ return json.dumps({"progress": progress, "log": message}) + "\n"
43
+
44
+ yield emit_log(5, "🌍 Locking Coordinates & Validating Dates...")
45
+
46
+ start_latest = target_date - timedelta(days=15)
47
+ end_latest = target_date + timedelta(days=5)
48
+ interval_latest = (start_latest.strftime('%Y-%m-%d'), end_latest.strftime('%Y-%m-%d'))
49
+
50
+ target_year = target_date.year
51
+ baseline_year = target_year - 1 if target_date.month < 6 else target_year
52
+ interval_baseline = (f"{baseline_year}-04-01", f"{baseline_year}-05-31")
53
+
54
+ # Determine dynamic scale parameters based on area span size to prevent timeouts (e.g. Lake Manchar)
55
+ n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
56
+ span = max(n - s, e - w)
57
+
58
+ if span > 0.6:
59
+ current_scale = config.SCALE_XLARGE # 90m scale for mega fields like Dadu
60
+ yield emit_log(10, f"⚡ Massive Region Detected ({span:.2f}°). Optimizing resolution scale to {current_scale}m to prevent timeouts...")
61
+ elif span > 0.2:
62
+ current_scale = config.SCALE_LARGE # 40m scale
63
+ yield emit_log(10, f"⚡ Large Region Detected ({span:.2f}°). Adjusting resolution scale to {current_scale}m...")
64
+ else:
65
+ current_scale = config.SCALE_SMALL # 10m crisp resolution scale
66
+
67
+ yield emit_log(15, f"📡 Building Cloud-Free Composite ({interval_latest[0]} to {interval_latest[1]})...")
68
+
69
+ # Pass down the runtime session directory path context instead of flat paths
70
+ path_latest, date_latest, src_latest, cloud_percent = gee_service.fetch_optical_composite(
71
+ coords, interval_latest, os.path.join(session_id, f"{session_id}_latest"), scale=current_scale
72
+ )
73
+ if cloud_percent is None:
74
+ cloud_percent = 100
75
+
76
+ if path_latest and cloud_percent <= config.OPTICAL_CLEAR_THRESHOLD:
77
+ yield emit_log(30, f"🧠 Optical Clear ({cloud_percent:.1f}% cloud cover). Launching inference...")
78
+
79
+ mask_latest = model_service.predict_water_mask(path_latest)
80
+ if mask_latest.ndim == 3:
81
+ mask_latest = mask_latest[:, :, 0]
82
+
83
+ rgb_latest = cv2_service.create_rgb_preview(path_latest, os.path.join(session_id, f"{session_id}_rgb_latest.jpg"))
84
+
85
+ yield emit_log(50, "📡 Fetching Dry-Season Anchor Baseline...")
86
+ path_pre, src_pre = gee_service.fetch_dynamic_world_baseline(
87
+ coords, interval_baseline, os.path.join(session_id, f"{session_id}_previous"), scale=current_scale
88
+ )
89
+ date_pre = f"{interval_baseline[0]} to {interval_baseline[1]}"
90
+
91
+ if path_pre:
92
+ mask_pre = tiff.imread(path_pre).astype(np.uint8)
93
+ if mask_pre.ndim == 3:
94
+ mask_pre = mask_pre[:, :, 0]
95
+ rgb_pre = cv2_service.create_rgb_preview(path_pre, os.path.join(session_id, f"{session_id}_rgb_previous.jpg"))
96
+ else:
97
+ mask_pre = np.zeros_like(mask_latest)
98
+ rgb_pre = None
99
+ else:
100
+ yield emit_log(30, f"⛈️ Cloud Blockage Obstruction ({cloud_percent:.1f}% cover). Activating Radar SAR Fallback...")
101
+
102
+ path_latest, date_latest, src_latest = gee_service.fetch_sar_data(
103
+ coords, interval_latest, os.path.join(session_id, f"{session_id}_latest"), is_baseline=False, scale=current_scale
104
+ )
105
+
106
+ if not path_latest:
107
+ yield json.dumps({"error": "Critical Error: Failed to acquire tracking maps."}) + "\n"
108
+ return
109
+
110
+ mask_latest, rgb_latest = cv2_service.process_sar_image(
111
+ path_latest, os.path.join(session_id, f"{session_id}_rgb_latest_sar.png")
112
+ )
113
+ if mask_latest.ndim == 3:
114
+ mask_latest = mask_latest[:, :, 0]
115
+
116
+ yield emit_log(50, "📡 Fetching Baseline SAR Map Matrix...")
117
+ path_pre, date_pre_raw, src_pre = gee_service.fetch_sar_data(
118
+ coords, interval_baseline, os.path.join(session_id, f"{session_id}_previous"), is_baseline=True, scale=current_scale
119
+ )
120
+ date_pre = f"{interval_baseline[0]} to {interval_baseline[1]}"
121
+
122
+ if path_pre:
123
+ mask_pre, rgb_pre = cv2_service.process_sar_image(
124
+ path_pre, os.path.join(session_id, f"{session_id}_rgb_previous_sar.png")
125
+ )
126
+ if mask_pre.ndim == 3:
127
+ mask_pre = mask_pre[:, :, 0]
128
+ else:
129
+ mask_pre = np.zeros_like(mask_latest)
130
+ rgb_pre = None
131
+
132
+ yield emit_log(70, "🌊 Conforming Geospatial Matrices...")
133
+ if mask_latest.shape != mask_pre.shape:
134
+ mask_pre = cv2.resize(mask_pre, (mask_latest.shape[1], mask_latest.shape[0]), interpolation=cv2.INTER_NEAREST)
135
+
136
+ yield emit_log(80, "🌊 Isolating Extent Variance (Change Vector)...")
137
+ if apply_buffer:
138
+ buffer_kernel = np.ones((5, 5), np.uint8)
139
+ mask_pre_buffered = cv2.dilate(mask_pre, buffer_kernel, iterations=1)
140
+ flood_mask = cv2.bitwise_and(mask_latest, cv2.bitwise_not(mask_pre_buffered))
141
+ else:
142
+ flood_mask = cv2.bitwise_and(mask_latest, cv2.bitwise_not(mask_pre))
143
+
144
+ yield emit_log(85, "🎨 Rendering Semi-Transparent Visual Overlays...")
145
+ raw_url_latest = cv2_service.create_overlay_png(mask_latest, [255, 0, 0], os.path.join(session_id, f"{session_id}_overlay_latest.png"))
146
+ raw_url_pre = cv2_service.create_overlay_png(mask_pre, [255, 255, 0], os.path.join(session_id, f"{session_id}_overlay_previous.png"))
147
+ raw_url_flood = cv2_service.create_overlay_png(flood_mask, [0, 0, 255], os.path.join(session_id, f"{session_id}_overlay_flood.png"))
148
+
149
+ yield emit_log(90, "📊 Calculating Inundation Area Metrics...")
150
+ pixel_to_sq_km = config.calculate_pixel_to_sq_km(current_scale)
151
+ stats = {
152
+ "baseline_sq_km": float(np.count_nonzero(mask_pre == 255) * pixel_to_sq_km),
153
+ "current_sq_km": float(np.count_nonzero(mask_latest == 255) * pixel_to_sq_km),
154
+ "flood_sq_km": float(np.count_nonzero(flood_mask == 255) * pixel_to_sq_km),
155
+ }
156
+ dates = {"baseline": date_pre, "latest": date_latest}
157
+
158
+ yield emit_log(95, "🤖 Generating Multi-modal Executive AI Summary Report...")
159
+ flood_image_path = os.path.join(config.DOWNLOADS_DIR, session_id, f"{session_id}_overlay_flood.png")
160
+
161
+ try:
162
+ ai_report_text = gemini_service.generate_flood_report(stats, coords, dates, flood_image_path)
163
+ except Exception as gem_err:
164
+ ai_report_text = "The satellite flood assessment executive summary text is currently undergoing maintenance."
165
+
166
+ yield emit_log(98, "📄 Compiling Document Artifact PDF...")
167
+ report_filename = f"flood_assessment_report_{session_id}.pdf"
168
+ report_path = os.path.join(config.DOWNLOADS_DIR, session_id, report_filename)
169
+ pdf_img_path = os.path.join(config.DOWNLOADS_DIR, session_id, f"pdf_img_{session_id}.jpg")
170
+
171
+ if rgb_latest:
172
+ clean_rgb_latest = os.path.basename(rgb_latest)
173
+ base_img = cv2.imread(os.path.join(config.DOWNLOADS_DIR, session_id, clean_rgb_latest))
174
+ if base_img is not None and base_img.shape[2] == 4:
175
+ base_img = cv2.cvtColor(base_img, cv2.COLOR_BGRA2BGR)
176
+ else:
177
+ base_img = None
178
+
179
+ if base_img is not None:
180
+ pdf_img = base_img.copy()
181
+ if flood_mask.shape != pdf_img.shape[:2]:
182
+ safe_flood_mask = cv2.resize(flood_mask, (pdf_img.shape[1], pdf_img.shape[0]), interpolation=cv2.INTER_NEAREST)
183
+ else:
184
+ safe_flood_mask = flood_mask
185
+ pdf_img[safe_flood_mask == 255] = (0, 0, 255)
186
+ cv2.imwrite(pdf_img_path, pdf_img)
187
+ else:
188
+ pdf_img_path = flood_image_path
189
+
190
+ try:
191
+ pdf = FPDF()
192
+ pdf.add_page()
193
+ pdf.set_font("helvetica", "B", 18)
194
+ pdf.cell(0, 15, "SatVision Flood Assessment Report", align="C", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
195
+ pdf.ln(5)
196
+ pdf.image(pdf_img_path, x=15, w=180)
197
+ pdf.ln(10)
198
+ pdf.set_font("helvetica", size=11)
199
+ safe_pdf_text = str(ai_report_text).encode('latin-1', 'replace').decode('latin-1')
200
+ pdf.multi_cell(0, 6, text=safe_pdf_text)
201
+ pdf.output(report_path)
202
+ except Exception as pdf_err:
203
+ print(f"❌ PDF Engine Failed: {pdf_err}", flush=True)
204
+
205
+ yield emit_log(100, "✅ Detection Pipeline Complete!")
206
+
207
+ generations_col = get_generations_collection()
208
+ if generations_col is not None:
209
+ generation_record = {
210
+ "session_id": session_id,
211
+ "timestamp": datetime.now(),
212
+ "coordinates": coords,
213
+ "target_date": target_date.strftime('%Y-%m-%d'),
214
+ "metrics": stats,
215
+ "ai_report": ai_report_text,
216
+ "logs": session_logs,
217
+ "feedback": None
218
+ }
219
+ try:
220
+ generations_col.insert_one(generation_record)
221
+ except Exception as db_err:
222
+ print(f"❌ DB Write Blocked: {db_err}", flush=True)
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 ""
226
+ c_pre = f"{session_id}/{os.path.basename(raw_url_pre)}" if raw_url_pre else ""
227
+ c_fld = f"{session_id}/{os.path.basename(raw_url_flood)}" if raw_url_flood else ""
228
+ c_rgb_lat = f"{session_id}/{os.path.basename(rgb_latest)}" if rgb_latest else None
229
+ c_rgb_pre = f"{session_id}/{os.path.basename(rgb_pre)}" if rgb_pre else None
230
+
231
+ yield json.dumps({
232
+ "progress": 100,
233
+ "session_id": session_id,
234
+ "result": {
235
+ "latest": f"{base_url.rstrip('/')}/mask/{c_lat}",
236
+ "previous": f"{base_url.rstrip('/')}/mask/{c_pre}",
237
+ "flood": f"{base_url.rstrip('/')}/mask/{c_fld}"
238
+ },
239
+ "meta": {
240
+ "latest_date": date_latest,
241
+ "previous_date": date_pre,
242
+ "latest_source": src_latest,
243
+ "previous_source": src_pre,
244
+ "latest_rgb": f"{base_url.rstrip('/')}/mask/{c_rgb_lat}" if c_rgb_lat else None,
245
+ "previous_rgb": f"{base_url.rstrip('/')}/mask/{c_rgb_pre}" if c_rgb_pre else None
246
+ },
247
+ "report": {
248
+ "text": ai_report_text,
249
+ "download_url": f"{base_url.rstrip('/')}/mask/{session_id}/{report_filename}",
250
+ "metrics": stats
251
+ }
252
+ }) + "\n"
src/services/gee_service.py ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Google Earth Engine data fetching services.
3
+ Handles optical (Sentinel-2), SAR (Sentinel-1), and Dynamic World baselines.
4
+ Production hardened to eliminate sub-tile grid cutting edge artifacts via spatial padding.
5
+ """
6
+
7
+ import os
8
+ import math
9
+ import numpy as np
10
+ import tifffile as tiff
11
+ import requests
12
+ import ee
13
+ from src import config
14
+ import cv2
15
+
16
+
17
+ def download_gee_tile(image, n, s, e, w, prefix, r, c, scale=10):
18
+ """
19
+ Download a partitioned tile slice from Earth Engine as a localized GeoTIFF.
20
+ """
21
+ region = ee.Geometry.BBox(w, s, e, n)
22
+ url = image.getDownloadURL({
23
+ 'crs': 'EPSG:4326',
24
+ 'region': region,
25
+ 'scale': scale,
26
+ 'format': 'GEO_TIFF'
27
+ })
28
+
29
+ try:
30
+ resp = requests.get(url, timeout=config.GEE_REQUEST_TIMEOUT)
31
+ if resp.status_code != 200:
32
+ print(f"❌ Tile {r}-{c} failed with status {resp.status_code}.", flush=True)
33
+ return None
34
+
35
+ path = os.path.join(config.DOWNLOADS_DIR, f"{prefix}_tile_{r}_{c}.tiff")
36
+ with open(path, 'wb') as f:
37
+ f.write(resp.content)
38
+ return path
39
+ except Exception as e:
40
+ print(f"❌ Tile download failed {r}-{c}: {e}", flush=True)
41
+ return None
42
+
43
+
44
+ def stitch_tiles_safely(grid, is_3d=False):
45
+ """
46
+ Stitch a 2D list of image tiles using zero-padding instead of truncation.
47
+ Eliminates geometric square-notch boundary artifacts completely.
48
+ """
49
+ # Step 1: Uniformly stitch columns inside each individual row block
50
+ stitched_rows = []
51
+ for r_idx, row_list in enumerate(grid):
52
+ max_h = max(tile.shape[0] for tile in row_list)
53
+ padded_row_tiles = []
54
+
55
+ for tile in row_list:
56
+ h, w = tile.shape[:2]
57
+ if h < max_h:
58
+ # Pad the bottom margin with zeros to maintain resolution grid alignment
59
+ pad_bottom = max_h - h
60
+ border_shape = (pad_bottom, w, tile.shape[2]) if is_3d else (pad_bottom, w)
61
+ padding = np.zeros(border_shape, dtype=tile.dtype)
62
+ tile = np.concatenate([tile, padding], axis=0)
63
+ padded_row_tiles.append(tile)
64
+
65
+ stitched_rows.append(np.concatenate(padded_row_tiles, axis=1))
66
+
67
+ # Step 2: Uniformly vertical-stitch all rows together into the final matrix
68
+ max_w = max(row.shape[1] for row in stitched_rows)
69
+ padded_rows = []
70
+ for row in stitched_rows:
71
+ h, w = row.shape[:2]
72
+ if w < max_w:
73
+ # Pad the right margin with zeros to seamlessly join row transitions
74
+ pad_right = max_w - w
75
+ border_shape = (h, pad_right, row.shape[2]) if is_3d else (h, pad_right)
76
+ padding = np.zeros(border_shape, dtype=row.dtype)
77
+ row = np.concatenate([row, padding], axis=1)
78
+ padded_rows.append(row)
79
+
80
+ return np.concatenate(padded_rows, axis=0)
81
+
82
+
83
+ def fetch_optical_composite(coords, time_interval, filename_prefix, scale=10):
84
+ """
85
+ Fetch Sentinel-2 optical composite with cloud masking.
86
+ Automatically segments massive regions dynamically using lossless padding alignment.
87
+ """
88
+ try:
89
+ region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
90
+ start_date, end_date = time_interval
91
+
92
+ col = (ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
93
+ .filterBounds(region)
94
+ .filterDate(start_date, end_date)
95
+ .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', config.S2_CLOUD_THRESHOLD)))
96
+
97
+ if col.size().getInfo() == 0:
98
+ col = (ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
99
+ .filterBounds(region)
100
+ .filterDate(start_date, end_date)
101
+ .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', config.S2_CLOUD_THRESHOLD)))
102
+
103
+ if col.size().getInfo() == 0:
104
+ return None, None, None, 100.0
105
+
106
+ def maskS2clouds(image):
107
+ qa = image.select('QA60')
108
+ cloudBitMask = 1 << 10
109
+ cirrusBitMask = 1 << 11
110
+ mask = qa.bitwiseAnd(cloudBitMask).eq(0).And(qa.bitwiseAnd(cirrusBitMask).eq(0))
111
+ return image.updateMask(mask)
112
+
113
+ mosaic = col.map(maskS2clouds).median()
114
+ final_image = mosaic.select(config.S2_BANDS).toInt16().unmask(0)
115
+
116
+ valid_mask = final_image.select('B4').gt(0)
117
+ stats = valid_mask.reduceRegion(
118
+ reducer=ee.Reducer.mean(),
119
+ geometry=region,
120
+ scale=config.GEE_SCALE_OPTICAL,
121
+ maxPixels=1e9
122
+ )
123
+ clear_fraction = stats.get('B4').getInfo()
124
+ if clear_fraction is None:
125
+ clear_fraction = 0
126
+ cloud_percent = (1.0 - clear_fraction) * 100.0
127
+
128
+ if cloud_percent > config.OPTICAL_CLEAR_THRESHOLD:
129
+ return None, None, None, cloud_percent
130
+
131
+ actual_date = end_date
132
+ source_type = "Sentinel-2 Optical (Cloud-Masked Composite)"
133
+
134
+ n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
135
+
136
+ if scale == config.SCALE_XLARGE:
137
+ max_deg_step = config.MAX_DEG_STEP_XLARGE
138
+ elif scale == config.SCALE_LARGE:
139
+ max_deg_step = config.MAX_DEG_STEP_LARGE
140
+ else:
141
+ max_deg_step = config.MAX_DEG_STEP_SMALL
142
+
143
+ rows = math.ceil((n - s) / max_deg_step)
144
+ cols = math.ceil((e - w) / max_deg_step)
145
+ lat_steps = np.linspace(n, s, rows + 1)
146
+ lon_steps = np.linspace(w, e, cols + 1)
147
+
148
+ grid_data = []
149
+ for r in range(rows):
150
+ row_list = []
151
+ for c in range(cols):
152
+ tile_n, tile_s = lat_steps[r], lat_steps[r+1]
153
+ tile_w, tile_e = lon_steps[c], lon_steps[c+1]
154
+ tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w,
155
+ filename_prefix, r, c, scale=scale)
156
+ if not tile_path:
157
+ return None, None, None, 100.0
158
+
159
+ img = tiff.imread(tile_path)
160
+ row_list.append(img)
161
+ os.remove(tile_path)
162
+ grid_data.append(row_list)
163
+
164
+ # Safe Stitching Execution
165
+ final_stitched = stitch_tiles_safely(grid_data, is_3d=True)
166
+
167
+ save_path = os.path.join(config.DOWNLOADS_DIR, f"{filename_prefix}.tiff")
168
+ tiff.imwrite(save_path, final_stitched)
169
+
170
+ return save_path, actual_date, source_type, cloud_percent
171
+
172
+ except Exception as e:
173
+ print(f"❌ GEE Optical Error {filename_prefix}: {e}", flush=True)
174
+ return None, None, None, 100.0
175
+
176
+
177
+ def fetch_sar_data(coords, time_interval, filename_prefix, is_baseline=False, scale=10):
178
+ """
179
+ Fetch Sentinel-1 SAR imagery matrix (VV backscatter polarization).
180
+ """
181
+ try:
182
+ region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
183
+ start_date, end_date = time_interval
184
+
185
+ s1_col = (ee.ImageCollection('COPERNICUS/S1_GRD')
186
+ .filterBounds(region)
187
+ .filterDate(start_date, end_date)
188
+ .filter(ee.Filter.listContains('transmitterReceiverPolarisation', config.S1_POLARIZATION))
189
+ .filter(ee.Filter.eq('instrumentMode', config.S1_INSTRUMENT_MODE)))
190
+
191
+ if s1_col.size().getInfo() == 0:
192
+ return None, None, None
193
+
194
+ if is_baseline:
195
+ s1_image = s1_col.median().select(config.S1_POLARIZATION).unmask(-99)
196
+ else:
197
+ s1_image = s1_col.mosaic().select(config.S1_POLARIZATION).unmask(-99)
198
+
199
+ final_image = s1_image.toFloat()
200
+
201
+ n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
202
+
203
+ if scale == config.SCALE_XLARGE:
204
+ max_deg_step = config.MAX_DEG_STEP_XLARGE
205
+ elif scale == config.SCALE_LARGE:
206
+ max_deg_step = config.MAX_DEG_STEP_LARGE
207
+ else:
208
+ max_deg_step = config.MAX_DEG_STEP_SMALL
209
+
210
+ rows = math.ceil((n - s) / max_deg_step)
211
+ cols = math.ceil((e - w) / max_deg_step)
212
+ lat_steps = np.linspace(n, s, rows + 1)
213
+ lon_steps = np.linspace(w, e, cols + 1)
214
+
215
+ grid_data = []
216
+ for r in range(rows):
217
+ row_list = []
218
+ for c in range(cols):
219
+ tile_n, tile_s = lat_steps[r], lat_steps[r+1]
220
+ tile_w, tile_e = lon_steps[c], lon_steps[c+1]
221
+ tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w,
222
+ filename_prefix, r, c, scale=scale)
223
+ if not tile_path:
224
+ return None, None, None
225
+
226
+ img = tiff.imread(tile_path)
227
+ row_list.append(img)
228
+ os.remove(tile_path)
229
+ grid_data.append(row_list)
230
+
231
+ final_stitched = stitch_tiles_safely(grid_data, is_3d=False)
232
+
233
+ actual_date = end_date
234
+ source_type = "Sentinel-1 SAR (VV Backscatter)" if not is_baseline else "Sentinel-1 SAR (Dry-Season Baseline)"
235
+
236
+ save_path = os.path.join(config.DOWNLOADS_DIR, f"{filename_prefix}_sar.tiff")
237
+ tiff.imwrite(save_path, final_stitched)
238
+
239
+ return save_path, actual_date, source_type
240
+
241
+ except Exception as e:
242
+ print(f"❌ SAR Error {filename_prefix}: {e}", flush=True)
243
+ return None, None, None
244
+
245
+
246
+ def fetch_dynamic_world_baseline(coords, time_interval, filename_prefix, scale=10):
247
+ """
248
+ Fetch Dynamic World land cover discrete classification mapping for historical baselines.
249
+ """
250
+ try:
251
+ region = ee.Geometry.BBox(coords['west'], coords['south'], coords['east'], coords['north'])
252
+ start_date, end_date = time_interval
253
+
254
+ dw_col = (ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1')
255
+ .filterBounds(region)
256
+ .filterDate(start_date, end_date))
257
+
258
+ if dw_col.size().getInfo() == 0:
259
+ return None, None
260
+
261
+ final_image = dw_col.select('label').mode().eq(config.DW_WATER_CLASS).unmask(0).multiply(255).toByte()
262
+
263
+ n, s, e, w = coords['north'], coords['south'], coords['east'], coords['west']
264
+
265
+ if scale == config.SCALE_XLARGE:
266
+ max_deg_step = config.MAX_DEG_STEP_XLARGE
267
+ elif scale == config.SCALE_LARGE:
268
+ max_deg_step = config.MAX_DEG_STEP_LARGE
269
+ else:
270
+ max_deg_step = config.MAX_DEG_STEP_SMALL
271
+
272
+ rows = math.ceil((n - s) / max_deg_step)
273
+ cols = math.ceil((e - w) / max_deg_step)
274
+ lat_steps = np.linspace(n, s, rows + 1)
275
+ lon_steps = np.linspace(w, e, cols + 1)
276
+
277
+ grid_data = []
278
+ for r in range(rows):
279
+ row_list = []
280
+ for c in range(cols):
281
+ tile_n, tile_s = lat_steps[r], lat_steps[r+1]
282
+ tile_w, tile_e = lon_steps[c], lon_steps[c+1]
283
+ tile_path = download_gee_tile(final_image, tile_n, tile_s, tile_e, tile_w,
284
+ filename_prefix, r, c, scale=scale)
285
+ if not tile_path:
286
+ return None, None
287
+
288
+ img = tiff.imread(tile_path)
289
+ row_list.append(img)
290
+ os.remove(tile_path)
291
+ grid_data.append(row_list)
292
+
293
+ final_stitched = stitch_tiles_safely(grid_data, is_3d=False)
294
+
295
+ save_path = os.path.join(config.DOWNLOADS_DIR, f"{filename_prefix}.tiff")
296
+ tiff.imwrite(save_path, final_stitched)
297
+
298
+ return save_path, "Dynamic Dry-Season Anchor (April-May)"
299
+
300
+ except Exception as e:
301
+ print(f"❌ Dynamic World Error {filename_prefix}: {e}", flush=True)
302
+ return None, None
src/services/gemini.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Google Gemini AI service for generating flood damage assessment reports.
3
+ """
4
+
5
+ import os
6
+ from google import genai
7
+ from google.genai import types
8
+ from PIL import Image
9
+ from src import config
10
+
11
+
12
+ def generate_flood_report(stats, coords, dates, image_path):
13
+ """
14
+ Generate an executive-level flood damage assessment report using Gemini AI.
15
+ """
16
+ try:
17
+ if not config.GEMINI_API_KEY:
18
+ return "Error: AI Report unavailable. GEMINI_API_KEY missing in environment."
19
+
20
+ if not os.path.exists(image_path):
21
+ return "Error: AI Report unavailable. Flood image overlay not found."
22
+
23
+ client = genai.Client(api_key=config.GEMINI_API_KEY)
24
+
25
+ # Extract statistics
26
+ baseline_date = dates.get('baseline', 'Unknown Baseline')
27
+ latest_date = dates.get('latest', 'Unknown Date')
28
+ base_sq = stats.get('baseline_sq_km', 0.0)
29
+ curr_sq = stats.get('current_sq_km', 0.0)
30
+ flood_sq = stats.get('flood_sq_km', 0.0)
31
+
32
+ # Build comprehensive prompt
33
+ prompt = f"""
34
+ You are a Lead Geospatial Intelligence Analyst generating an executive-level Flood Damage and Loss Assessment (DaLA) report.
35
+ I have provided a satellite image overlay. The red pixels definitively indicate algorithmic detections of newly accumulated floodwater.
36
+
37
+ CONTEXTUAL TELEMETRY:
38
+ - Bounding Box Coordinates: {coords}
39
+ - Baseline Imagery Date: {baseline_date}
40
+ - Post-Flood Capture Date: {latest_date}
41
+ - Pre-flood Water Extent: {base_sq:.2f} sq km
42
+ - Current Total Water Extent: {curr_sq:.2f} sq km
43
+ - Net New Inundation Area: {flood_sq:.2f} sq km
44
+
45
+ YOUR DIRECTIVES:
46
+ 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.
47
+ 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.
48
+ 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.
49
+
50
+ REQUIRED REPORT STRUCTURE:
51
+ EXECUTIVE SUMMARY
52
+ Provide a bottom-line-up-front (BLUF) statement summarizing the severity and scale of the inundation event.
53
+
54
+ GEOSPATIAL & CLIMATOLOGICAL CONTEXT
55
+ 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.
56
+
57
+ INUNDATION METRICS & MORPHOLOGY
58
+ 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.
59
+
60
+ INFRASTRUCTURE & SOCIO-ECONOMIC IMPACT
61
+ 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.
62
+
63
+ MODEL CONFIDENCE & FALSE POSITIVE ANALYSIS
64
+ 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.
65
+
66
+ STRATEGIC DIRECTIVES
67
+ Provide 3 highly specific, actionable recommendations for on-the-ground disaster response agencies based on the exact terrain and impact identified above.
68
+ """
69
+
70
+ # Load image for multimodal input
71
+ with Image.open(image_path) as img:
72
+ config_obj = types.GenerateContentConfig(
73
+ temperature=config.GEMINI_TEMPERATURE,
74
+ top_p=config.GEMINI_TOP_P,
75
+ )
76
+
77
+ response = client.models.generate_content(
78
+ model=config.GEMINI_MODEL_ID,
79
+ contents=[img, prompt],
80
+ config=config_obj
81
+ )
82
+
83
+ if response.text:
84
+ return response.text.strip()
85
+ else:
86
+ return "Error: Model returned an empty response."
87
+
88
+ except Exception as e:
89
+ import traceback
90
+ print(f"❌ AI Report Error: {e}\n{traceback.format_exc()}", flush=True)
91
+ return "Error: AI Report generation failed due to an internal system error."
src/services/model.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Model service for loading and running PyTorch Lightning inference.
3
+ Implements thread-safe model loading and prediction with torch.no_grad() optimization.
4
+ """
5
+
6
+ import os
7
+ import torch
8
+ import pytorch_lightning as pl
9
+ import segmentation_models_pytorch as smp
10
+ import numpy as np
11
+ from src import config
12
+
13
+ # Global model instance
14
+ _model = None
15
+
16
+
17
+ class DynamicEvalModel(pl.LightningModule):
18
+ """
19
+ PyTorch Lightning model wrapper for U-Net++ segmentation.
20
+ Used for water/cloud segmentation tasks.
21
+ """
22
+
23
+ def __init__(self, **kwargs):
24
+ super().__init__()
25
+ self.save_hyperparameters()
26
+ self.model_params = kwargs.get('model_params', {
27
+ 'model_type': 'unetplusplus',
28
+ 'encoder_name': 'resnet34',
29
+ 'in_channels': 6,
30
+ 'out_classes': 2
31
+ })
32
+ self._build_model()
33
+
34
+ def _build_model(self):
35
+ """Build the U-Net++ segmentation model."""
36
+ self.model = smp.UnetPlusPlus(
37
+ encoder_name=self.model_params.get('encoder_name', 'resnet34'),
38
+ encoder_weights=None,
39
+ in_channels=self.model_params.get('in_channels', 6),
40
+ classes=self.model_params.get('out_classes', 2),
41
+ activation=None
42
+ )
43
+
44
+ def forward(self, x):
45
+ return self.model(x)
46
+
47
+
48
+ def initialize_model():
49
+ """
50
+ Load the PyTorch Lightning model from checkpoint.
51
+ Model is placed on the configured device (GPU/CPU) and set to eval mode.
52
+ """
53
+ global _model
54
+
55
+ print("⏳ Loading model...", flush=True)
56
+
57
+ if not os.path.exists(config.CHECKPOINT_PATH):
58
+ print(f"❌ Model Checkpoint Not Found: {config.CHECKPOINT_PATH}", flush=True)
59
+ _model = None
60
+ return None
61
+
62
+ try:
63
+ _model = DynamicEvalModel.load_from_checkpoint(
64
+ config.CHECKPOINT_PATH,
65
+ strict=False
66
+ )
67
+ _model.to(config.DEVICE)
68
+ _model.eval()
69
+ print("✅ Model Loaded Successfully!", flush=True)
70
+ return _model
71
+ except Exception as e:
72
+ print(f"❌ Model Load Failed: {e}", flush=True)
73
+ _model = None
74
+ return None
75
+
76
+
77
+ def get_model():
78
+ """Retrieve the loaded model instance."""
79
+ return _model
80
+
81
+
82
+ def predict_water_mask(tiff_path):
83
+ """
84
+ Run inference on a TIFF image to predict water and cloud masks.
85
+
86
+ Args:
87
+ tiff_path: Path to the input TIFF file (6-channel optical image)
88
+
89
+ Returns:
90
+ Clean water mask (numpy array, uint8) or None on failure
91
+ """
92
+ import tifffile as tiff
93
+ import cv2
94
+ from src.services.cv2 import (
95
+ apply_hanning_window, apply_cloud_detection,
96
+ apply_water_detection, apply_morphological_cleanup,
97
+ filter_small_contours
98
+ )
99
+
100
+ if _model is None or not os.path.exists(tiff_path):
101
+ return None
102
+
103
+ # Load and prepare image
104
+ image = tiff.imread(tiff_path).astype(np.float32)
105
+
106
+ if image.ndim == 3 and image.shape[2] == 6:
107
+ image_tensor = np.transpose(image, (2, 0, 1))
108
+ else:
109
+ image_tensor = image.copy()
110
+
111
+ # Normalize to [0, 1]
112
+ scaled_tensor = np.clip(image_tensor / config.OPTICAL_NORMALIZATION_FACTOR, 0, 1)
113
+
114
+ # Tiling parameters
115
+ tile_size = config.TILE_SIZE
116
+ overlap_ratio = config.TILE_OVERLAP_RATIO
117
+ stride = int(tile_size * (1 - overlap_ratio))
118
+ c, h, w = scaled_tensor.shape
119
+
120
+ # Padding for tile overlap
121
+ pad_h = (tile_size - (h % stride)) % stride + (tile_size - stride) if h % stride != 0 else 0
122
+ pad_w = (tile_size - (w % stride)) % stride + (tile_size - stride) if w % stride != 0 else 0
123
+ image_padded = np.pad(scaled_tensor, ((0, 0), (0, pad_h), (0, pad_w)), mode='reflect')
124
+
125
+ padded_h, padded_w = image_padded.shape[1], image_padded.shape[2]
126
+
127
+ # Initialize output maps
128
+ prob_map_cloud = np.zeros((padded_h, padded_w), dtype=np.float32)
129
+ prob_map_water = np.zeros((padded_h, padded_w), dtype=np.float32)
130
+ count_map = np.zeros((padded_h, padded_w), dtype=np.float32)
131
+
132
+ # Generate tile coordinates
133
+ y_steps = list(range(0, padded_h - tile_size + 1, stride))
134
+ x_steps = list(range(0, padded_w - tile_size + 1, stride))
135
+
136
+ # Hanning window for smooth blending
137
+ window_1d = np.hanning(tile_size)
138
+ window_2d = np.outer(window_1d, window_1d).astype(np.float32)
139
+
140
+ # Process tiles in batches
141
+ tiles_batch = []
142
+ coords_batch = []
143
+
144
+ with torch.no_grad(): # Disable gradients for inference
145
+ for y in y_steps:
146
+ for x in x_steps:
147
+ tile = image_padded[:, y:y+tile_size, x:x+tile_size]
148
+ tiles_batch.append(tile)
149
+ coords_batch.append((x, y))
150
+
151
+ # Process batch when full or at end
152
+ if len(tiles_batch) == config.BATCH_SIZE or (y == y_steps[-1] and x == x_steps[-1]):
153
+ batch_tensor = torch.from_numpy(np.stack(tiles_batch)).to(config.DEVICE)
154
+ logits = _model(batch_tensor)
155
+ probs = torch.sigmoid(logits).cpu().numpy()
156
+
157
+ # Accumulate predictions
158
+ for i, (bx, by) in enumerate(coords_batch):
159
+ prob_cloud = probs[i, 0, :, :] * window_2d
160
+ prob_water = probs[i, 1, :, :] * window_2d
161
+
162
+ prob_map_cloud[by:by+tile_size, bx:bx+tile_size] += prob_cloud
163
+ prob_map_water[by:by+tile_size, bx:bx+tile_size] += prob_water
164
+ count_map[by:by+tile_size, bx:bx+tile_size] += window_2d
165
+
166
+ tiles_batch = []
167
+ coords_batch = []
168
+
169
+ # Normalize by count map
170
+ final_cloud_prob = (prob_map_cloud / (count_map + 1e-7))[:h, :w]
171
+ final_water_prob = (prob_map_water / (count_map + 1e-7))[:h, :w]
172
+
173
+ # Extract optical bands for cloud/water detection
174
+ b2 = scaled_tensor[0, :h, :w]
175
+ b3 = scaled_tensor[1, :h, :w]
176
+ b4 = scaled_tensor[2, :h, :w]
177
+ b8 = scaled_tensor[3, :h, :w]
178
+
179
+ # Calculate brightness and NDWI
180
+ brightness = np.sqrt(b2**2 + b3**2 + b4**2)
181
+ ndwi = (b3 - b8) / (b3 + b8 + 1e-5)
182
+
183
+ # Apply cloud detection
184
+ base_cloud_mask = ((final_cloud_prob > config.CLOUD_DETECTION_THRESHOLD) &
185
+ (brightness > config.BRIGHTNESS_THRESHOLD)).astype(np.uint8)
186
+
187
+ # Dilate cloud mask for shadow buffering
188
+ cloud_buffer_kernel = np.ones(config.CLOUD_BUFFER_KERNEL_SIZE, np.uint8)
189
+ cloud_and_shadow_mask = cv2.dilate(base_cloud_mask, cloud_buffer_kernel, iterations=1)
190
+
191
+ # Apply water detection
192
+ raw_water_mask = (final_water_prob > config.NDWI_WATER_THRESHOLD) | (ndwi > config.NDWI_CLOUD_THRESHOLD)
193
+ water_mask = raw_water_mask & (cloud_and_shadow_mask == 0) & (ndwi > config.NDWI_MIN_THRESHOLD)
194
+ clean_mask = water_mask.astype(np.uint8)
195
+
196
+ # Morphological cleanup
197
+ clean_mask = apply_morphological_cleanup(clean_mask)
198
+
199
+ # Remove small contours
200
+ clean_mask = filter_small_contours(clean_mask)
201
+
202
+ return clean_mask * 255