import streamlit as st
import tensorflow as tf
from tensorflow.keras.utils import load_img, img_to_array
from io import BytesIO
st.set_page_config(page_title="Forest Fire Detection", page_icon="🔥", layout="centered")
# Custom header with emoji
st.markdown(
"""
🔥 Forest Fire Detection Demo 🔥
Upload a forest image and let AI detect fire!
🌲🌳🌴
""",
unsafe_allow_html=True
)
st.sidebar.title("About")
st.sidebar.info(
"Upload a forest image to detect fire using a deep learning model. "
"This demo is powered by TensorFlow and Streamlit."
)
# Load model only once
@st.cache_resource
def load_model():
return tf.keras.models.load_model('FFD.keras')
model = load_model()
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
with st.spinner("Analyzing image..."):
img = load_img(BytesIO(uploaded_file.read()), target_size=(150, 150))
img_array = img_to_array(img) / 255.0
img_array = img_array.reshape(1, 150, 150, 3)
prediction = model.predict(img_array)
confidence = float(prediction[0][0])
result = 'Fire Detected' if confidence > 0.5 else 'No Fire'
# Improved two-column layout with centered content and card-style result
col1, col2 = st.columns([1.2, 1])
with col1:
st.markdown("", unsafe_allow_html=True)
st.image(img, caption="Uploaded Image", width=260)
st.markdown("
", unsafe_allow_html=True)
with col2:
st.markdown(
f"""
{"🔥" if result=="Fire Detected" else "🌲"} {result}
Confidence: {confidence:.2f}
""",
unsafe_allow_html=True
)
st.markdown("
", unsafe_allow_html=True)
else:
st.info("Please upload an image to get started.")
# Footer
st.markdown(
"
"
"Made with ❤️ by CoderKP using Streamlit & TensorFlow"
"
",
unsafe_allow_html=True
)