putriemas commited on
Commit
1d7909c
Β·
verified Β·
1 Parent(s): 21812ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -3,35 +3,42 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
 
6
- # Load kedua model
7
  spinach_model = tf.keras.models.load_model("exception.h5") # Model klasifikasi bayam
8
  spad_model = tf.keras.models.load_model("spad_predictor.h5", compile=False) # Model prediksi SPAD
9
 
10
  # Fungsi gabungan
11
  def analyze_leaf(image):
12
  try:
13
- # Preprocessing
14
- image_resized = image.resize((299, 299))
15
- img_array = np.array(image_resized) / 255.0
 
 
 
16
  img_array = np.expand_dims(img_array, axis=0)
17
 
18
- # Deteksi bayam
19
  pred_spinach = spinach_model.predict(img_array)[0][0]
20
  is_spinach = pred_spinach > 0.5
21
 
22
- # Hasil klasifikasi
23
  classification = "βœ… Bayam" if is_spinach else "❌ Bukan Bayam"
24
  confidence = pred_spinach if is_spinach else 1 - pred_spinach
25
  confidence_pct = f"{confidence * 100:.2f}%"
26
 
27
- # Prediksi SPAD jika Bayam
28
  spad_value = "-"
29
  recommendation = ""
 
30
  if is_spinach:
31
-
32
- spad_value = round(float(spad_model.predict(img_array)[0][0]), 2)
33
-
34
- # Rekomendasi perawatan berdasarkan nilai SPAD
 
 
 
 
35
  if spad_value < 20:
36
  recommendation = "❗ Perawatan: Bayam kekurangan klorofil. Diperlukan peningkatan pencahayaan atau pemupukan."
37
  elif 20 <= spad_value < 40:
@@ -39,10 +46,10 @@ def analyze_leaf(image):
39
  else:
40
  recommendation = "🌱 Perawatan: Bayam sehat dan kaya klorofil. Pastikan kelembapan dan suhu stabil."
41
 
42
- return spad_value, classification, recommendation, image_resized
43
 
44
  except Exception as e:
45
- return str(e), "❌ Terjadi kesalahan", "", image
46
 
47
 
48
  # Antarmuka Gradio
@@ -53,12 +60,13 @@ interface = gr.Interface(
53
  gr.Number(label="Nilai SPAD (jika Bayam)"),
54
  gr.Label(label="Status Diagnosis Daun Bayam"),
55
  gr.Textbox(label="Rekomendasi Perawatan"),
 
56
  ],
57
  title="🌿 Deteksi Daun Bayam + Prediksi Nilai SPAD",
58
- description="Aplikasi ini mendeteksi apakah gambar adalah bayam, dan jika ya, akan memprediksi nilai SPAD serta memberikan rekomendasi perawatan.",
59
  theme="soft"
60
  )
61
 
62
- # Jalankan secara lokal
63
  if __name__ == "__main__":
64
  interface.launch()
 
3
  import numpy as np
4
  from PIL import Image
5
 
6
+ # Load model klasifikasi dan regresi SPAD
7
  spinach_model = tf.keras.models.load_model("exception.h5") # Model klasifikasi bayam
8
  spad_model = tf.keras.models.load_model("spad_predictor.h5", compile=False) # Model prediksi SPAD
9
 
10
  # Fungsi gabungan
11
  def analyze_leaf(image):
12
  try:
13
+ # Pastikan gambar dalam format RGB
14
+ image_rgb = image.convert("RGB")
15
+
16
+ # Preprocessing gambar
17
+ image_resized = image_rgb.resize((299, 299))
18
+ img_array = np.array(image_resized).astype(np.float32) / 255.0
19
  img_array = np.expand_dims(img_array, axis=0)
20
 
21
+ # Prediksi klasifikasi bayam
22
  pred_spinach = spinach_model.predict(img_array)[0][0]
23
  is_spinach = pred_spinach > 0.5
24
 
 
25
  classification = "βœ… Bayam" if is_spinach else "❌ Bukan Bayam"
26
  confidence = pred_spinach if is_spinach else 1 - pred_spinach
27
  confidence_pct = f"{confidence * 100:.2f}%"
28
 
29
+ # Inisialisasi nilai SPAD dan rekomendasi
30
  spad_value = "-"
31
  recommendation = ""
32
+
33
  if is_spinach:
34
+ # Prediksi SPAD
35
+ pred_spad = spad_model.predict(img_array)
36
+ if pred_spad.shape == (1, 1):
37
+ spad_value = round(float(pred_spad[0][0]), 2)
38
+ else:
39
+ raise ValueError("Output model SPAD tidak sesuai format yang diharapkan.")
40
+
41
+ # Rekomendasi berdasarkan nilai SPAD
42
  if spad_value < 20:
43
  recommendation = "❗ Perawatan: Bayam kekurangan klorofil. Diperlukan peningkatan pencahayaan atau pemupukan."
44
  elif 20 <= spad_value < 40:
 
46
  else:
47
  recommendation = "🌱 Perawatan: Bayam sehat dan kaya klorofil. Pastikan kelembapan dan suhu stabil."
48
 
49
+ return spad_value, f"{classification} (Confidence: {confidence_pct})", recommendation, image_resized
50
 
51
  except Exception as e:
52
+ return "-", "❌ Terjadi kesalahan saat memproses gambar", str(e), image
53
 
54
 
55
  # Antarmuka Gradio
 
60
  gr.Number(label="Nilai SPAD (jika Bayam)"),
61
  gr.Label(label="Status Diagnosis Daun Bayam"),
62
  gr.Textbox(label="Rekomendasi Perawatan"),
63
+ gr.Image(label="Pratinjau Gambar")
64
  ],
65
  title="🌿 Deteksi Daun Bayam + Prediksi Nilai SPAD",
66
+ description="Aplikasi ini mendeteksi apakah gambar adalah daun bayam, dan jika ya, akan memprediksi nilai SPAD serta memberikan rekomendasi perawatan.",
67
  theme="soft"
68
  )
69
 
70
+ # Jalankan aplikasi
71
  if __name__ == "__main__":
72
  interface.launch()