AndrewMaru commited on
Commit
608dbde
·
verified ·
1 Parent(s): c323003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -40
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import streamlit as st
2
  import tensorflow as tf
3
  import numpy as np
 
4
  from PIL import Image
5
 
6
- st.set_page_config(layout="wide") # Agar tampilan lebih lebar
7
  st.title("🌊 Flood Segmentation")
8
 
9
  # 1. Load Model
@@ -15,23 +15,19 @@ def load_model():
15
  with st.spinner('Loading Model...'):
16
  model = load_model()
17
 
18
- # Penjelasan singkat
19
- st.write("Model ini akan memberikan lapisan merah transparan yang tegas di area yang terdeteksi sebagai banjir.")
20
-
21
  uploaded = st.file_uploader("Upload flood image", type=["jpg", "jpeg", "png"])
22
 
23
  if uploaded:
24
  # 2. Preprocessing Gambar
25
- img_pil = Image.open(uploaded).convert("RGB")
26
- # Simpan ukuran asli untuk resize balik nanti agar resolusi tinggi
27
- original_size = img_pil.size
28
-
29
- # Resize ke 256x256 untuk input model
30
- img_resized = img_pil.resize((256, 256))
31
 
32
- # Normalisasi & Preprocessing
33
  x = np.array(img_resized, dtype=np.float32) / 255.0
34
- x = np.concatenate([x, x], axis=-1) # Stack jadi 6 channel
 
 
 
35
  x = np.expand_dims(x, axis=0) # Batch dimension
36
 
37
  # 3. Prediksi
@@ -40,48 +36,43 @@ if uploaded:
40
  output_dict = infer(tf.constant(x))
41
  pred_tensor = list(output_dict.values())[0]
42
 
43
- # Ambil probabilitas (256x256)
44
  pred = pred_tensor.numpy()[0, :, :, 0]
45
 
46
- # --- BAGIAN VISUALISASI YANG DIPERJELEAS ---
47
 
48
- # 1. Thresholding Tegas
49
- # Ubah probabilitas jadi biner: Banjir (True) atau Tidak (False)
50
- # Nilai > 0.5 dianggap banjir pasti.
51
- mask_binary = pred > 0.5
52
-
53
- # 2. Buat Lapisan Merah Transparan
54
- # Siapkan array kosong seukuran gambar resized
55
- overlay_layer = np.zeros((256, 256, 4), dtype=np.uint8) # RGBA (4 channel)
56
-
57
- # Di area yang masker-nya True (Banjir), isi dengan warna Merah Transparan
58
- # R=255, G=0, B=0, Alpha=150 (agak transparan)
59
- overlay_layer[mask_binary] = [255, 0, 0, 150]
60
 
61
- # Ubah jadi gambar PIL
62
- overlay_img_pil = Image.fromarray(overlay_layer, 'RGBA')
 
63
 
64
- # 3. Gabungkan (Composite)
65
- # Ambil gambar asli yang di-resize (background)
66
- background = img_resized.convert("RGBA")
67
- # Tempelkan lapisan merah di atasnya
68
- final_composite_small = Image.alpha_composite(background, overlay_img_pil)
69
 
70
- # 4. Resize balik ke ukuran asli agar terlihat tajam di layar
71
- final_result_highres = final_composite_small.resize(original_size, Image.Resampling.LANCZOS)
 
72
 
73
  # --------------------------------
74
 
75
  # 4. Tampilkan Hasil
76
- col1, col2 = st.columns(2)
77
 
78
  with col1:
79
- st.subheader("Original Image")
80
- st.image(img_pil, use_container_width=True)
81
 
82
  with col2:
83
- st.subheader("Sharp Detection Result")
84
- st.image(final_result_highres, use_container_width=True, caption="Area Merah = Deteksi Banjir")
 
 
 
 
85
 
86
  except Exception as e:
87
  st.error(f"Terjadi error: {e}")
 
1
  import streamlit as st
2
  import tensorflow as tf
3
  import numpy as np
4
+ import matplotlib.pyplot as plt # <--- Library baru untuk membuat Heatmap
5
  from PIL import Image
6
 
 
7
  st.title("🌊 Flood Segmentation")
8
 
9
  # 1. Load Model
 
15
  with st.spinner('Loading Model...'):
16
  model = load_model()
17
 
 
 
 
18
  uploaded = st.file_uploader("Upload flood image", type=["jpg", "jpeg", "png"])
19
 
20
  if uploaded:
21
  # 2. Preprocessing Gambar
22
+ img = Image.open(uploaded).convert("RGB")
23
+ img_resized = img.resize((256, 256))
 
 
 
 
24
 
25
+ # Normalisasi
26
  x = np.array(img_resized, dtype=np.float32) / 255.0
27
+
28
+ # Stack gambar agar menjadi 6 channel (memenuhi syarat input model)
29
+ x = np.concatenate([x, x], axis=-1)
30
+
31
  x = np.expand_dims(x, axis=0) # Batch dimension
32
 
33
  # 3. Prediksi
 
36
  output_dict = infer(tf.constant(x))
37
  pred_tensor = list(output_dict.values())[0]
38
 
39
+ # Ambil nilai probabilitas mentah (0.0 sampai 1.0)
40
  pred = pred_tensor.numpy()[0, :, :, 0]
41
 
42
+ # --- BAGIAN PEMBUATAN HEATMAP ---
43
 
44
+ # 1. Buat colormap (Jet: Biru=Rendah, Merah=Tinggi)
45
+ # plt.cm.jet mengembalikan RGBA (0-1), kita ambil RGB saja dan kali 255
46
+ heatmap_colored = plt.cm.jet(pred)[:, :, :3]
47
+ heatmap_img = (heatmap_colored * 255).astype(np.uint8)
 
 
 
 
 
 
 
 
48
 
49
+ # 2. Buat Overlay (Gabungkan gambar asli dengan Heatmap)
50
+ # Kita ambil gambar asli yang sudah di-resize
51
+ original_img_arr = np.array(img_resized)
52
 
53
+ # Rumus blending: 60% Gambar Asli + 40% Heatmap
54
+ blended = (original_img_arr * 0.6 + heatmap_img * 0.4).astype(np.uint8)
 
 
 
55
 
56
+ # Konversi ke format Image agar bisa ditampilkan Streamlit
57
+ final_heatmap = Image.fromarray(blended)
58
+ pure_heatmap = Image.fromarray(heatmap_img)
59
 
60
  # --------------------------------
61
 
62
  # 4. Tampilkan Hasil
63
+ col1, col2, col3 = st.columns(3)
64
 
65
  with col1:
66
+ st.write("Original Image")
67
+ st.image(img_resized, use_container_width=True)
68
 
69
  with col2:
70
+ st.write("Pure Heatmap")
71
+ st.image(pure_heatmap, use_container_width=True, caption="Merah = Banjir")
72
+
73
+ with col3:
74
+ st.write("Overlay Result")
75
+ st.image(final_heatmap, use_container_width=True, caption="Gabungan")
76
 
77
  except Exception as e:
78
  st.error(f"Terjadi error: {e}")