AndrewMaru commited on
Commit
e4a6d12
·
verified ·
1 Parent(s): e067be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -19
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import streamlit as st
2
  import tensorflow as tf
3
  import numpy as np
 
4
  from PIL import Image
5
 
6
- st.title("🌊 Flood Segmentation with U-Net")
7
 
8
  # 1. Load Model
9
  @st.cache_resource
@@ -24,36 +25,54 @@ if uploaded:
24
  # Normalisasi
25
  x = np.array(img_resized, dtype=np.float32) / 255.0
26
 
27
- # --- PERBAIKAN KHUSUS UNTUK ERROR 3 VS 6 ---
28
- # Model kamu butuh 6 channel (kemungkinan Before + After image).
29
- # Karena cuma ada 1 gambar, kita gabung gambar ini dengan dirinya sendiri
30
- # agar menjadi 6 channel (Mocking the second image).
31
  x = np.concatenate([x, x], axis=-1)
32
- # Sekarang shape x adalah (256, 256, 6)
33
- # -------------------------------------------
34
-
35
- x = np.expand_dims(x, axis=0) # Menambah batch dimension menjadi (1, 256, 256, 6)
36
 
37
  # 3. Prediksi
38
  try:
39
  infer = model.signatures["serving_default"]
40
  output_dict = infer(tf.constant(x))
41
  pred_tensor = list(output_dict.values())[0]
 
 
42
  pred = pred_tensor.numpy()[0, :, :, 0]
43
 
44
- # 4. Post-processing
45
- mask = (pred > 0.5).astype(np.uint8) * 255
46
- mask_img = Image.fromarray(mask)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- # 5. Tampilkan Hasil
49
- col1, col2 = st.columns(2)
 
 
 
50
  with col1:
51
  st.write("Original Image")
52
- st.image(img, use_container_width=True)
 
53
  with col2:
54
- st.write("Segmentation Mask")
55
- st.image(mask_img, use_container_width=True)
 
 
 
 
56
 
57
  except Exception as e:
58
- st.error(f"Terjadi error saat prediksi: {e}")
59
- st.write("Debug Info - Input Shape:", x.shape)
 
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 with Heatmap")
8
 
9
  # 1. Load Model
10
  @st.cache_resource
 
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
34
  try:
35
  infer = model.signatures["serving_default"]
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}")