File size: 2,786 Bytes
ad474bc
 
 
 
 
1d7909c
ad474bc
 
 
 
 
 
1d7909c
 
 
 
 
 
ad474bc
 
1d7909c
ad474bc
 
 
 
 
 
 
1d7909c
ad474bc
 
1d7909c
ad474bc
1d7909c
 
 
 
 
 
 
 
ad474bc
 
 
 
 
 
 
1d7909c
ad474bc
 
1d7909c
ad474bc
 
 
 
 
 
 
 
 
 
1d7909c
ad474bc
 
1d7909c
ad474bc
 
 
1d7909c
ad474bc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

# Load model klasifikasi dan regresi SPAD
spinach_model = tf.keras.models.load_model("exception.h5")  # Model klasifikasi bayam
spad_model = tf.keras.models.load_model("spad_predictor.h5", compile=False)  # Model prediksi SPAD

# Fungsi gabungan
def analyze_leaf(image):
    try:
        # Pastikan gambar dalam format RGB
        image_rgb = image.convert("RGB")

        # Preprocessing gambar
        image_resized = image_rgb.resize((299, 299))
        img_array = np.array(image_resized).astype(np.float32) / 255.0
        img_array = np.expand_dims(img_array, axis=0)

        # Prediksi klasifikasi bayam
        pred_spinach = spinach_model.predict(img_array)[0][0]
        is_spinach = pred_spinach > 0.5

        classification = "βœ… Bayam" if is_spinach else "❌ Bukan Bayam"
        confidence = pred_spinach if is_spinach else 1 - pred_spinach
        confidence_pct = f"{confidence * 100:.2f}%"

        # Inisialisasi nilai SPAD dan rekomendasi
        spad_value = "-"
        recommendation = ""

        if is_spinach:
            # Prediksi SPAD
            pred_spad = spad_model.predict(img_array)
            if pred_spad.shape == (1, 1):
                spad_value = round(float(pred_spad[0][0]), 2)
            else:
                raise ValueError("Output model SPAD tidak sesuai format yang diharapkan.")

            # Rekomendasi berdasarkan nilai SPAD
            if spad_value < 20:
                recommendation = "❗ Perawatan: Bayam kekurangan klorofil. Diperlukan peningkatan pencahayaan atau pemupukan."
            elif 20 <= spad_value < 40:
                recommendation = "βœ… Perawatan: Bayam dalam kondisi normal. Pastikan perawatan rutin."
            else:
                recommendation = "🌱 Perawatan: Bayam sehat dan kaya klorofil. Pastikan kelembapan dan suhu stabil."

        return spad_value, f"{classification} (Confidence: {confidence_pct})", recommendation, image_resized

    except Exception as e:
        return "-", "❌ Terjadi kesalahan saat memproses gambar", str(e), image


# Antarmuka Gradio
interface = gr.Interface(
    fn=analyze_leaf,
    inputs=gr.Image(type="pil", label="Upload Gambar Daun"),
    outputs=[
        gr.Number(label="Nilai SPAD (jika Bayam)"),
        gr.Label(label="Status Diagnosis Daun Bayam"),
        gr.Textbox(label="Rekomendasi Perawatan"),
        gr.Image(label="Pratinjau Gambar")
    ],
    title="🌿 Deteksi Daun Bayam + Prediksi Nilai SPAD",
    description="Aplikasi ini mendeteksi apakah gambar adalah daun bayam, dan jika ya, akan memprediksi nilai SPAD serta memberikan rekomendasi perawatan.",
    theme="soft"
)

# Jalankan aplikasi
if __name__ == "__main__":
    interface.launch()