CoolFace
Apppublic

Yves-Tana/CC_Transfer_Learning

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
app.py49 linesDownload Raw Back to root
1import streamlit as st2import tensorflow as tf3from PIL import Image4import numpy as np5 6# 1. Page Configuration7st.set_page_config(page_title="Terrain Classifier", page_icon="๐ŸŒ")8 9# 2. Load the Model10# We use st.cache_resource so the model only loads once, saving memory11@st.cache_resource12def load_my_model():13    return tf.keras.models.load_model(14    "model_ResNet50.h5",15    compile=False16)17 18model = load_my_model()19class_names = ['Desert', 'Forest', 'Mountain', 'Plains']20 21# 3. UI Elements22st.title("๐ŸŒ Terrain Category Classifier")23st.write("Upload an image of a landscape, and the AI will tell you if it's a **Desert, Forest, Mountain, or Plain**.")24 25uploaded_file = st.file_uploader("Choose a terrain image...", type=["jpg", "jpeg", "png"])26 27if uploaded_file is not None:28    # Display the uploaded image29    image = Image.open(uploaded_file)30    st.image(image, caption='Uploaded Image', use_container_width=True)31    32    st.write("---")33    with st.spinner('Analyzing terrain...'):34        # Preprocess the image35        img = image.resize((180, 180))36        img_array = tf.keras.utils.img_to_array(img)37        img_array = tf.expand_dims(img_array, 0) # Create a batch38 39        # Make Prediction40        predictions = model.predict(img_array)41        score = tf.nn.softmax(predictions[0])42        43        result_label = class_names[np.argmax(score)]44        confidence = 100 * np.max(score)45 46    # 4. Show Results47    st.subheader(f"Result: {result_label}")48    st.progress(int(confidence))49    st.write(f"**Confidence Level:** {confidence:.2f}%")