File size: 4,103 Bytes
2305b98
 
 
 
f70e222
2305b98
 
 
94d3010
2305b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94d3010
 
 
 
2305b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94d3010
2305b98
 
94d3010
2305b98
 
 
94d3010
2305b98
 
 
94d3010
2305b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94d3010
 
 
 
 
 
2305b98
 
 
94d3010
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import joblib
import numpy as np
import pandas as pd
import streamlit as st
from PIL import Image#f
from tensorflow.keras.models import load_model

# ---------------- Load CSV ----------------
CSV_PATH = "best_models_summary.csv"  # Must be in the same folder
df = pd.read_csv(CSV_PATH)

# ---------------- Streamlit Page Config ----------------
st.set_page_config(
    page_title="🔥 Wildfire Classification",
    layout="wide",
    page_icon="🔥"
)

# ---------------- Custom CSS for Colors ----------------
st.markdown("""
<style>
h1 {
    color: #ff6f61;
    text-align: center;
}
.stButton>button {
    background-color: #ff6f61;
    color:white;
    font-weight:bold;
}
.stSelectbox>div>div>div>span {
    color: #ff6f61;
}
.stFileUploader>div>div>div>label {
    font-weight:bold;
}
.stMarkdown p {
    font-size:16px;
}
.stImage>div>div>img {
    border: 2px solid #ff6f61;
    border-radius: 10px;
}
</style>
""", unsafe_allow_html=True)

# ---------------- Header ----------------
st.markdown("<h1>🔥 Wildfire Image Classification 🔥</h1>", unsafe_allow_html=True)
st.markdown("Upload an image, select dataset type and metric priority. The app will automatically select the best model.", unsafe_allow_html=True)
st.markdown("---")

# ---------------- Layout ----------------
col1, col2 = st.columns(2)

with col1:
    uploaded_file = st.file_uploader("Upload Image", type=['png','jpg','jpeg'])

with col2:
    dataset_type = st.selectbox("Select Dataset Type", ['satellite','uav'])
    metric_priority = st.selectbox("Select Metric Priority", ['accuracy','precision','recall','f1_score','best_overall'])

# ---------------- Process Uploaded Image ----------------
if uploaded_file:
    img = Image.open(uploaded_file).convert('RGB')
    st.image(img, caption='Uploaded Image', use_container_width=True)

    # ---------------- Select Model ----------------
    subset = df[df['dataset']==dataset_type]

    if metric_priority != "best_overall":
        subset = subset[subset['metric']==metric_priority]
    else:
        # Best overall: max f1_score
        subset = subset.loc[subset['f1_score'].idxmax():subset['f1_score'].idxmax()+1]

    model_row = subset.iloc[0]
    model_path = model_row['model_path']
    model_type = model_row['model_type']

    st.markdown(f"### Using Model: **{model_row['model_name']}** ({model_type})")

    # ---------------- Load Model ----------------
    model = None
    try:
        if model_type.lower() in ["h5","keras"]:
            model = load_model(model_path)
        elif model_type.lower() == "joblib":
            model = joblib.load(model_path)
        else:
            st.error("Unsupported model type!")
    except Exception as e:
        st.error(f"Error loading model: {e}")

    # ---------------- Preprocess Image ----------------
    img_array = np.array(img.resize((224,224)))/255.0
    img_array_exp = np.expand_dims(img_array, axis=0)

    # ---------------- Predict ----------------
    pred = None
    try:
        if model_type.lower() in ['h5','keras']:
            pred_probs = model.predict(img_array_exp)
            pred_class = np.argmax(pred_probs, axis=1)[0]
            pred = pred_class
        elif model_type.lower() == 'joblib':
            # Flatten image for ML models if needed
            pred = model.predict(img_array_exp.reshape(1,-1))[0]
    except Exception as e:
        st.error(f"Error during prediction: {e}")

    # ---------------- Display Results ----------------
    st.markdown("---")
    st.markdown("## Prediction Result")
    if pred is not None:
        st.success(f"Predicted Class: **{pred}** (class index for now)")
        st.info(f"Metric Priority: **{metric_priority}**")
        st.write(f"Model Accuracy: {model_row['accuracy']:.4f} | Precision: {model_row['precision']:.4f} | Recall: {model_row['recall']:.4f} | F1 Score: {model_row['f1_score']:.4f}")
    else:
        st.warning("Prediction could not be made. Check model compatibility.")
    
    # Optional style enhancements
    st.markdown("<hr style='border:2px solid #ff6f61'>", unsafe_allow_html=True)
    st.balloons()