prateeksharmacoder commited on
Commit
20598ce
·
1 Parent(s): 1ac0f1d

feat: colored NDVI/NDWI/NDBI maps, remove SWIR/NBR, 7-output UI with legends

Browse files
Files changed (1) hide show
  1. app.py +132 -63
app.py CHANGED
@@ -384,6 +384,74 @@ def clean_uncertainty(image):
384
  return (image * 255.0).round().astype(np.uint8)
385
 
386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  # ============================================================
388
  # MAIN PROCESSING PIPELINE
389
  # Called from inside @spaces.GPU so all GPU ops run there.
@@ -538,8 +606,6 @@ def generate_satellite_products(
538
 
539
  ndbi = (B11 - B08) / (B11 + B08 + eps)
540
 
541
- nbr = (B08 - B12) / (B08 + B12 + eps)
542
-
543
  # --------------------------------------------------
544
  # 6. Uncertainty [GPU]
545
  # --------------------------------------------------
@@ -624,11 +690,6 @@ def generate_satellite_products(
624
  "false_color.tif"
625
  )
626
 
627
- paths["SWIR"] = save_layer(
628
- swir,
629
- "swir.tif"
630
- )
631
-
632
  paths["NDVI"] = save_layer(
633
  ndvi,
634
  "ndvi.tif"
@@ -644,16 +705,12 @@ def generate_satellite_products(
644
  "ndbi.tif"
645
  )
646
 
647
- paths["NBR"] = save_layer(
648
- nbr,
649
- "nbr.tif"
650
- )
651
-
652
  paths["Uncertainty"] = save_layer(
653
  uncertainty_np,
654
  "uncertainty.tif"
655
  )
656
 
 
657
  # --------------------------------------------------
658
  # ZIP all GeoTIFFs
659
  # --------------------------------------------------
@@ -684,11 +741,9 @@ def generate_satellite_products(
684
  "rgb": rgb,
685
  "original_rgb": original_rgb,
686
  "false_color": false_color,
687
- "swir": swir,
688
  "ndvi": ndvi,
689
  "ndwi": ndwi,
690
  "ndbi": ndbi,
691
- "nbr": nbr,
692
  "uncertainty": uncertainty_np,
693
  "files": paths,
694
  "zip": zip_path
@@ -740,15 +795,11 @@ def run_app(latitude, longitude, start_date, end_date):
740
  # -------------------------------------------------
741
  false_color_img = clean_rgb(results["false_color"])
742
 
743
- swir_img = clean_rgb(results["swir"])
744
-
745
- ndvi_img = clean_index(results["ndvi"])
746
-
747
- ndwi_img = clean_index(results["ndwi"])
748
-
749
- ndbi_img = clean_index(results["ndbi"])
750
-
751
- nbr_img = clean_index(results["nbr"])
752
 
753
  uncertainty_img = clean_uncertainty(
754
  results["uncertainty"]
@@ -760,16 +811,21 @@ def run_app(latitude, longitude, start_date, end_date):
760
  zip_file_path = results["zip"]
761
 
762
  # -------------------------------------------------
763
- # RETURN EXACTLY 9 OUTPUTS
 
 
 
 
 
 
 
764
  # -------------------------------------------------
765
  return (
766
  comparison_images,
767
  false_color_img,
768
- swir_img,
769
- ndvi_img,
770
- ndwi_img,
771
- ndbi_img,
772
- nbr_img,
773
  uncertainty_img,
774
  zip_file_path
775
  )
@@ -966,7 +1022,7 @@ with gr.Blocks(
966
 
967
 
968
  # ------------------------------------------------
969
- # FALSE COLOR + SWIR
970
  # ------------------------------------------------
971
 
972
  with gr.Row():
@@ -977,50 +1033,65 @@ with gr.Blocks(
977
  height=320
978
  )
979
 
980
- swir_output = gr.Image(
981
- label="SWIR Composite",
982
- type="numpy",
983
- height=320
984
- )
985
-
986
 
987
  # ------------------------------------------------
988
- # NDVI / NDWI / NDBI
989
  # ------------------------------------------------
990
 
991
  with gr.Row():
992
 
993
- ndvi_output = gr.Image(
994
- label="NDVI",
995
- type="numpy",
996
- height=300
997
- )
998
-
999
- ndwi_output = gr.Image(
1000
- label="NDWI",
1001
- type="numpy",
1002
- height=300
1003
- )
1004
-
1005
- ndbi_output = gr.Image(
1006
- label="NDBI",
1007
- type="numpy",
1008
- height=300
1009
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1010
 
1011
 
1012
  # ------------------------------------------------
1013
- # NBR + UNCERTAINTY
1014
  # ------------------------------------------------
1015
 
1016
  with gr.Row():
1017
 
1018
- nbr_output = gr.Image(
1019
- label="NBR",
1020
- type="numpy",
1021
- height=320
1022
- )
1023
-
1024
  uncertainty_output = gr.Image(
1025
  label="LDSR-S2 Uncertainty",
1026
  type="numpy",
@@ -1057,11 +1128,9 @@ with gr.Blocks(
1057
  outputs=[
1058
  comparison_output,
1059
  false_color_output,
1060
- swir_output,
1061
  ndvi_output,
1062
  ndwi_output,
1063
  ndbi_output,
1064
- nbr_output,
1065
  uncertainty_output,
1066
  zip_output
1067
  ]
 
384
  return (image * 255.0).round().astype(np.uint8)
385
 
386
 
387
+ # ============================================================
388
+ # INDEX COLORIZATION
389
+ # Converts a [-1, 1] spectral index to an RGB thematic map
390
+ # using a linear interpolation between three anchor colours.
391
+ # Uses only numpy (no matplotlib dependency needed).
392
+ # ============================================================
393
+
394
+ def _lerp_color(t, c0, c1):
395
+ """Linearly interpolate between two RGB tuples, t in [0, 1]."""
396
+ t = np.clip(t, 0.0, 1.0)[..., np.newaxis] # (..., 1)
397
+ return c0 * (1.0 - t) + c1 * t # (..., 3)
398
+
399
+
400
+ def colorize_index(index_arr, scheme):
401
+ """
402
+ Convert a 2-D spectral index array (values in [-1, 1]) to a
403
+ uint8 RGB image using a three-stop colour ramp.
404
+
405
+ scheme : one of 'ndvi' | 'ndwi' | 'ndbi'
406
+
407
+ Colour stops (low / mid / high) in float RGB [0, 1]:
408
+ ndvi : red (1,0,0) -> yellow (1,1,0) -> green (0,0.5,0)
409
+ ndwi : brown(0.6,0.4,0.2) -> white (1,1,1) -> blue (0,0.3,1)
410
+ ndbi : green(0,0.5,0) -> yellow (1,1,0) -> red (1,0,0)
411
+ """
412
+ arr = np.asarray(index_arr, dtype=np.float32)
413
+ arr = np.nan_to_num(arr, nan=0.0, posinf=1.0, neginf=-1.0)
414
+ arr = np.clip(arr, -1.0, 1.0)
415
+
416
+ # Map [-1, 1] -> [0, 1]
417
+ t = (arr + 1.0) / 2.0 # 0 = low, 0.5 = mid, 1 = high
418
+
419
+ COLOR_STOPS = {
420
+ # (low_rgb, mid_rgb, high_rgb)
421
+ "ndvi": (
422
+ np.array([1.00, 0.00, 0.00]), # red
423
+ np.array([1.00, 1.00, 0.00]), # yellow
424
+ np.array([0.00, 0.50, 0.00]), # green
425
+ ),
426
+ "ndwi": (
427
+ np.array([0.60, 0.40, 0.20]), # brown
428
+ np.array([1.00, 1.00, 1.00]), # white
429
+ np.array([0.00, 0.30, 1.00]), # blue
430
+ ),
431
+ "ndbi": (
432
+ np.array([0.00, 0.50, 0.00]), # green
433
+ np.array([1.00, 1.00, 0.00]), # yellow
434
+ np.array([1.00, 0.00, 0.00]), # red
435
+ ),
436
+ }
437
+
438
+ low_c, mid_c, high_c = COLOR_STOPS[scheme]
439
+
440
+ # Two-segment ramp: [0, 0.5] -> low..mid, [0.5, 1] -> mid..high
441
+ t_lo = np.clip(t / 0.5, 0.0, 1.0) # normalised within lower half
442
+ t_hi = np.clip((t - 0.5) / 0.5, 0.0, 1.0) # normalised within upper half
443
+
444
+ rgb_lo = _lerp_color(t_lo, low_c, mid_c) # (..., 3)
445
+ rgb_hi = _lerp_color(t_hi, mid_c, high_c) # (..., 3)
446
+
447
+ # Blend: use lower half for t < 0.5, upper half otherwise
448
+ mask = (t >= 0.5)[..., np.newaxis]
449
+ rgb = np.where(mask, rgb_hi, rgb_lo)
450
+
451
+ rgb = np.clip(rgb, 0.0, 1.0)
452
+ return (rgb * 255.0).round().astype(np.uint8)
453
+
454
+
455
  # ============================================================
456
  # MAIN PROCESSING PIPELINE
457
  # Called from inside @spaces.GPU so all GPU ops run there.
 
606
 
607
  ndbi = (B11 - B08) / (B11 + B08 + eps)
608
 
 
 
609
  # --------------------------------------------------
610
  # 6. Uncertainty [GPU]
611
  # --------------------------------------------------
 
690
  "false_color.tif"
691
  )
692
 
 
 
 
 
 
693
  paths["NDVI"] = save_layer(
694
  ndvi,
695
  "ndvi.tif"
 
705
  "ndbi.tif"
706
  )
707
 
 
 
 
 
 
708
  paths["Uncertainty"] = save_layer(
709
  uncertainty_np,
710
  "uncertainty.tif"
711
  )
712
 
713
+
714
  # --------------------------------------------------
715
  # ZIP all GeoTIFFs
716
  # --------------------------------------------------
 
741
  "rgb": rgb,
742
  "original_rgb": original_rgb,
743
  "false_color": false_color,
 
744
  "ndvi": ndvi,
745
  "ndwi": ndwi,
746
  "ndbi": ndbi,
 
747
  "uncertainty": uncertainty_np,
748
  "files": paths,
749
  "zip": zip_path
 
795
  # -------------------------------------------------
796
  false_color_img = clean_rgb(results["false_color"])
797
 
798
+ # Colored thematic maps for UI display.
799
+ # GeoTIFFs use the original numerical arrays (saved in pipeline).
800
+ ndvi_colored = colorize_index(results["ndvi"], "ndvi")
801
+ ndwi_colored = colorize_index(results["ndwi"], "ndwi")
802
+ ndbi_colored = colorize_index(results["ndbi"], "ndbi")
 
 
 
 
803
 
804
  uncertainty_img = clean_uncertainty(
805
  results["uncertainty"]
 
811
  zip_file_path = results["zip"]
812
 
813
  # -------------------------------------------------
814
+ # RETURN EXACTLY 7 OUTPUTS (matches UI components)
815
+ # 1. comparison slider
816
+ # 2. false color / NIR
817
+ # 3. NDVI colored
818
+ # 4. NDWI colored
819
+ # 5. NDBI colored
820
+ # 6. uncertainty
821
+ # 7. ZIP download
822
  # -------------------------------------------------
823
  return (
824
  comparison_images,
825
  false_color_img,
826
+ ndvi_colored,
827
+ ndwi_colored,
828
+ ndbi_colored,
 
 
829
  uncertainty_img,
830
  zip_file_path
831
  )
 
1022
 
1023
 
1024
  # ------------------------------------------------
1025
+ # FALSE COLOR
1026
  # ------------------------------------------------
1027
 
1028
  with gr.Row():
 
1033
  height=320
1034
  )
1035
 
 
 
 
 
 
 
1036
 
1037
  # ------------------------------------------------
1038
+ # NDVI / NDWI / NDBI (colored thematic maps)
1039
  # ------------------------------------------------
1040
 
1041
  with gr.Row():
1042
 
1043
+ with gr.Column():
1044
+ ndvi_output = gr.Image(
1045
+ label="NDVI",
1046
+ type="numpy",
1047
+ height=300
1048
+ )
1049
+ gr.HTML(
1050
+ '<div style="display:flex;justify-content:space-between;'
1051
+ 'font-size:11px;padding:2px 4px;">'
1052
+ '<span style="color:#e00;">&#9632; Low vegetation</span>'
1053
+ '<span style="color:#cc0;">&#9632; Medium</span>'
1054
+ '<span style="color:#080;">&#9632; High vegetation</span>'
1055
+ '</div>'
1056
+ )
1057
+
1058
+ with gr.Column():
1059
+ ndwi_output = gr.Image(
1060
+ label="NDWI",
1061
+ type="numpy",
1062
+ height=300
1063
+ )
1064
+ gr.HTML(
1065
+ '<div style="display:flex;justify-content:space-between;'
1066
+ 'font-size:11px;padding:2px 4px;">'
1067
+ '<span style="color:#8B4513;">&#9632; Low water</span>'
1068
+ '<span style="color:#aaa;">&#9632; Medium</span>'
1069
+ '<span style="color:#04c;">&#9632; High water</span>'
1070
+ '</div>'
1071
+ )
1072
+
1073
+ with gr.Column():
1074
+ ndbi_output = gr.Image(
1075
+ label="NDBI",
1076
+ type="numpy",
1077
+ height=300
1078
+ )
1079
+ gr.HTML(
1080
+ '<div style="display:flex;justify-content:space-between;'
1081
+ 'font-size:11px;padding:2px 4px;">'
1082
+ '<span style="color:#080;">&#9632; Low built-up</span>'
1083
+ '<span style="color:#cc0;">&#9632; Medium</span>'
1084
+ '<span style="color:#e00;">&#9632; High built-up</span>'
1085
+ '</div>'
1086
+ )
1087
 
1088
 
1089
  # ------------------------------------------------
1090
+ # UNCERTAINTY
1091
  # ------------------------------------------------
1092
 
1093
  with gr.Row():
1094
 
 
 
 
 
 
 
1095
  uncertainty_output = gr.Image(
1096
  label="LDSR-S2 Uncertainty",
1097
  type="numpy",
 
1128
  outputs=[
1129
  comparison_output,
1130
  false_color_output,
 
1131
  ndvi_output,
1132
  ndwi_output,
1133
  ndbi_output,
 
1134
  uncertainty_output,
1135
  zip_output
1136
  ]