prateeksharmacoder commited on
Commit
871d2e7
·
1 Parent(s): c17f158

Add GeoTIFF support

Browse files
Files changed (1) hide show
  1. app.py +69 -20
app.py CHANGED
@@ -7,6 +7,9 @@ from pathlib import Path
7
  import gradio as gr
8
  from PIL import Image
9
  import spaces
 
 
 
10
 
11
  # ============================================================
12
  # PATHS
@@ -223,12 +226,12 @@ body, .gradio-container {
223
  # INFERENCE FUNCTION
224
  # ============================================================
225
 
226
- def run_super_resolution(image, model_choice, progress=gr.Progress()):
227
 
228
  # --------------------------------------------------------
229
  # Validate input
230
  # --------------------------------------------------------
231
- if image is None:
232
  raise gr.Error("⚠️ Please upload a satellite image before running.")
233
 
234
  # --------------------------------------------------------
@@ -241,13 +244,50 @@ def run_super_resolution(image, model_choice, progress=gr.Progress()):
241
  raise gr.Error("⚠️ Please select a valid model.")
242
 
243
  # --------------------------------------------------------
244
- # Ensure PIL image + RGB
245
  # --------------------------------------------------------
246
- if not isinstance(image, Image.Image):
247
- image = Image.fromarray(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
- image = image.convert("RGB")
250
- input_w, input_h = image.size
 
 
 
 
 
251
 
252
  # --------------------------------------------------------
253
  # Basic sanity check
@@ -286,14 +326,24 @@ def run_super_resolution(image, model_choice, progress=gr.Progress()):
286
  output_w, output_h = result.size
287
 
288
  # --------------------------------------------------------
289
- # Save to a temp PNG for download
290
  # --------------------------------------------------------
291
- tmp = tempfile.NamedTemporaryFile(
292
- suffix=f"_sr_{selected_model}_4x.png",
293
- delete=False
294
- )
295
- result.save(tmp.name, format="PNG")
296
- tmp.close()
 
 
 
 
 
 
 
 
 
 
297
 
298
  # --------------------------------------------------------
299
  # Info markdown
@@ -316,7 +366,7 @@ def run_super_resolution(image, model_choice, progress=gr.Progress()):
316
  """
317
 
318
  progress(1.0, desc="Done.")
319
- return result, info, tmp.name
320
 
321
 
322
  # ============================================================
@@ -360,10 +410,9 @@ with gr.Blocks(
360
  with gr.Column(scale=5):
361
 
362
  gr.HTML('<div class="section-label">📂 Input Image</div>')
363
- input_image = gr.Image(
364
- label="Upload Satellite Image",
365
- type="pil",
366
- sources=["upload", "clipboard"],
367
  elem_id="input-img",
368
  )
369
 
@@ -410,7 +459,7 @@ with gr.Blocks(
410
  )
411
 
412
  download_file = gr.File(
413
- label="⬇️ Download Result (PNG)",
414
  interactive=False,
415
  visible=True,
416
  elem_id="download-btn",
 
7
  import gradio as gr
8
  from PIL import Image
9
  import spaces
10
+ import numpy as np
11
+ import rasterio
12
+ from inference.geotiff import normalize_rgb, save_sr_geotiff
13
 
14
  # ============================================================
15
  # PATHS
 
226
  # INFERENCE FUNCTION
227
  # ============================================================
228
 
229
+ def run_super_resolution(image_path, model_choice, progress=gr.Progress()):
230
 
231
  # --------------------------------------------------------
232
  # Validate input
233
  # --------------------------------------------------------
234
+ if image_path is None:
235
  raise gr.Error("⚠️ Please upload a satellite image before running.")
236
 
237
  # --------------------------------------------------------
 
244
  raise gr.Error("⚠️ Please select a valid model.")
245
 
246
  # --------------------------------------------------------
247
+ # Load image (PNG/JPG or GeoTIFF)
248
  # --------------------------------------------------------
249
+ if isinstance(image_path, list):
250
+ image_path = image_path[0]
251
+
252
+ file_path = str(image_path)
253
+ is_tiff = file_path.lower().endswith(('.tif', '.tiff'))
254
+
255
+ if is_tiff:
256
+ try:
257
+ with rasterio.open(file_path) as src:
258
+ profile = src.profile.copy()
259
+ count = src.count
260
+ if count not in [1, 3, 4]:
261
+ raise gr.Error(f"⚠️ Unsupported band count: {count}. Expected 1, 3, or 4 bands.")
262
+
263
+ if count == 3:
264
+ data = src.read([1, 2, 3])
265
+ data = np.transpose(data, (1, 2, 0))
266
+ elif count == 4:
267
+ data = src.read([1, 2, 3])
268
+ data = np.transpose(data, (1, 2, 0))
269
+ else:
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 TIFF: {str(e)}")
278
+
279
+ if data.dtype == np.uint8:
280
+ pass
281
+ else:
282
+ data = normalize_rgb(data)
283
 
284
+ image = Image.fromarray(data, mode="RGB")
285
+ else:
286
+ try:
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
 
326
  output_w, output_h = result.size
327
 
328
  # --------------------------------------------------------
329
+ # Save to a temp file for download
330
  # --------------------------------------------------------
331
+ if is_tiff:
332
+ tmp = tempfile.NamedTemporaryFile(
333
+ suffix=f"_sr_{selected_model}_4x_georef.tif",
334
+ delete=False
335
+ )
336
+ tmp.close()
337
+ save_sr_geotiff(result, profile, tmp.name, scale=4)
338
+ out_file_path = tmp.name
339
+ else:
340
+ tmp = tempfile.NamedTemporaryFile(
341
+ suffix=f"_sr_{selected_model}_4x.png",
342
+ delete=False
343
+ )
344
+ result.save(tmp.name, format="PNG")
345
+ tmp.close()
346
+ out_file_path = tmp.name
347
 
348
  # --------------------------------------------------------
349
  # Info markdown
 
366
  """
367
 
368
  progress(1.0, desc="Done.")
369
+ return result, info, out_file_path
370
 
371
 
372
  # ============================================================
 
410
  with gr.Column(scale=5):
411
 
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
  )
418
 
 
459
  )
460
 
461
  download_file = gr.File(
462
+ label="⬇️ Download Result",
463
  interactive=False,
464
  visible=True,
465
  elem_id="download-btn",