Spaces:
Sleeping
Sleeping
Commit ·
f1f395e
1
Parent(s): 871d2e7
Support multiple single-band GeoTIFF uploads
Browse files
app.py
CHANGED
|
@@ -246,48 +246,90 @@ def run_super_resolution(image_path, model_choice, progress=gr.Progress()):
|
|
| 246 |
# --------------------------------------------------------
|
| 247 |
# Load image (PNG/JPG or GeoTIFF)
|
| 248 |
# --------------------------------------------------------
|
| 249 |
-
if isinstance(image_path, list):
|
| 250 |
-
image_path =
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
try:
|
| 257 |
-
with rasterio.open(
|
| 258 |
-
profile =
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
data = src.read(1)
|
| 271 |
-
data = np.stack([data, data, data], axis=-1)
|
| 272 |
|
| 273 |
-
input_w, input_h = src.width, src.height
|
| 274 |
-
except rasterio.errors.RasterioIOError:
|
| 275 |
-
raise gr.Error("⚠️ Corrupted TIFF or unsupported format.")
|
| 276 |
except Exception as e:
|
| 277 |
-
raise gr.Error(f"⚠️ Error reading
|
| 278 |
-
|
| 279 |
-
if data.dtype =
|
| 280 |
-
pass
|
| 281 |
-
else:
|
| 282 |
data = normalize_rgb(data)
|
| 283 |
-
|
| 284 |
image = Image.fromarray(data, mode="RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
else:
|
| 286 |
-
|
| 287 |
-
image = Image.open(file_path).convert("RGB")
|
| 288 |
-
input_w, input_h = image.size
|
| 289 |
-
except Exception:
|
| 290 |
-
raise gr.Error("⚠️ Could not read image.")
|
| 291 |
|
| 292 |
# --------------------------------------------------------
|
| 293 |
# Basic sanity check
|
|
@@ -412,6 +454,7 @@ with gr.Blocks(
|
|
| 412 |
gr.HTML('<div class="section-label">📂 Input Image</div>')
|
| 413 |
input_image = gr.File(
|
| 414 |
label="Upload Satellite Image (PNG, JPG, TIFF)",
|
|
|
|
| 415 |
file_types=["image", ".tif", ".tiff"],
|
| 416 |
elem_id="input-img",
|
| 417 |
)
|
|
|
|
| 246 |
# --------------------------------------------------------
|
| 247 |
# Load image (PNG/JPG or GeoTIFF)
|
| 248 |
# --------------------------------------------------------
|
| 249 |
+
if not isinstance(image_path, list):
|
| 250 |
+
image_path = [image_path]
|
| 251 |
+
|
| 252 |
+
is_tiff = False
|
| 253 |
|
| 254 |
+
if len(image_path) == 3:
|
| 255 |
+
paths = [str(p) for p in image_path]
|
| 256 |
+
if not all(p.lower().endswith(('.tif', '.tiff')) for p in paths):
|
| 257 |
+
raise gr.Error("⚠️ When uploading multiple files, they must all be TIFF files.")
|
| 258 |
+
|
| 259 |
+
red_path = next((p for p in paths if "B04" in p.upper()), None)
|
| 260 |
+
green_path = next((p for p in paths if "B03" in p.upper()), None)
|
| 261 |
+
blue_path = next((p for p in paths if "B02" in p.upper()), None)
|
| 262 |
+
|
| 263 |
+
if not (red_path and green_path and blue_path):
|
| 264 |
+
paths = sorted(paths)
|
| 265 |
+
blue_path, green_path, red_path = paths[0], paths[1], paths[2]
|
| 266 |
+
|
| 267 |
try:
|
| 268 |
+
with rasterio.open(red_path) as src_r, rasterio.open(green_path) as src_g, rasterio.open(blue_path) as src_b:
|
| 269 |
+
profile = src_r.profile.copy()
|
| 270 |
+
profile.update(count=3)
|
| 271 |
+
|
| 272 |
+
if not (src_r.width == src_g.width == src_b.width and src_r.height == src_g.height == src_b.height):
|
| 273 |
+
raise gr.Error("⚠️ The uploaded bands have mismatched dimensions.")
|
| 274 |
|
| 275 |
+
r_data = src_r.read(1)
|
| 276 |
+
g_data = src_g.read(1)
|
| 277 |
+
b_data = src_b.read(1)
|
| 278 |
+
|
| 279 |
+
data = np.stack([r_data, g_data, b_data], axis=-1)
|
| 280 |
+
input_w, input_h = src_r.width, src_r.height
|
| 281 |
+
is_tiff = True
|
|
|
|
|
|
|
| 282 |
|
|
|
|
|
|
|
|
|
|
| 283 |
except Exception as e:
|
| 284 |
+
raise gr.Error(f"⚠️ Error reading 3-band TIFFs: {str(e)}")
|
| 285 |
+
|
| 286 |
+
if data.dtype != np.uint8:
|
|
|
|
|
|
|
| 287 |
data = normalize_rgb(data)
|
| 288 |
+
|
| 289 |
image = Image.fromarray(data, mode="RGB")
|
| 290 |
+
|
| 291 |
+
elif len(image_path) == 1:
|
| 292 |
+
file_path = str(image_path[0])
|
| 293 |
+
is_tiff = file_path.lower().endswith(('.tif', '.tiff'))
|
| 294 |
+
|
| 295 |
+
if is_tiff:
|
| 296 |
+
try:
|
| 297 |
+
with rasterio.open(file_path) as src:
|
| 298 |
+
profile = src.profile.copy()
|
| 299 |
+
count = src.count
|
| 300 |
+
if count not in [1, 3, 4]:
|
| 301 |
+
raise gr.Error(f"⚠️ Unsupported band count: {count}. Expected 1, 3, or 4 bands.")
|
| 302 |
+
|
| 303 |
+
if count == 3:
|
| 304 |
+
data = src.read([1, 2, 3])
|
| 305 |
+
data = np.transpose(data, (1, 2, 0))
|
| 306 |
+
elif count == 4:
|
| 307 |
+
data = src.read([1, 2, 3])
|
| 308 |
+
data = np.transpose(data, (1, 2, 0))
|
| 309 |
+
else:
|
| 310 |
+
data = src.read(1)
|
| 311 |
+
data = np.stack([data, data, data], axis=-1)
|
| 312 |
+
|
| 313 |
+
input_w, input_h = src.width, src.height
|
| 314 |
+
except rasterio.errors.RasterioIOError:
|
| 315 |
+
raise gr.Error("⚠️ Corrupted TIFF or unsupported format.")
|
| 316 |
+
except Exception as e:
|
| 317 |
+
raise gr.Error(f"⚠️ Error reading TIFF: {str(e)}")
|
| 318 |
+
|
| 319 |
+
if data.dtype == np.uint8:
|
| 320 |
+
pass
|
| 321 |
+
else:
|
| 322 |
+
data = normalize_rgb(data)
|
| 323 |
+
|
| 324 |
+
image = Image.fromarray(data, mode="RGB")
|
| 325 |
+
else:
|
| 326 |
+
try:
|
| 327 |
+
image = Image.open(file_path).convert("RGB")
|
| 328 |
+
input_w, input_h = image.size
|
| 329 |
+
except Exception:
|
| 330 |
+
raise gr.Error("⚠️ Could not read image.")
|
| 331 |
else:
|
| 332 |
+
raise gr.Error("⚠️ Please upload either 1 file (multiband image) or 3 files (B04, B03, B02 GeoTIFFs).")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
# --------------------------------------------------------
|
| 335 |
# Basic sanity check
|
|
|
|
| 454 |
gr.HTML('<div class="section-label">📂 Input Image</div>')
|
| 455 |
input_image = gr.File(
|
| 456 |
label="Upload Satellite Image (PNG, JPG, TIFF)",
|
| 457 |
+
file_count="multiple",
|
| 458 |
file_types=["image", ".tif", ".tiff"],
|
| 459 |
elem_id="input-img",
|
| 460 |
)
|