| import streamlit as st |
| import tensorflow as tf |
| from PIL import Image |
| import numpy as np |
|
|
| |
| st.set_page_config(page_title="Intel Scene Classifier", page_icon="🌲", layout="centered") |
|
|
| |
| class SafeDense(tf.keras.layers.Dense): |
| def __init__(self, *args, **kwargs): |
| |
| kwargs.pop('quantization_config', None) |
| super().__init__(*args, **kwargs) |
|
|
| |
| @st.cache_resource |
| def load_my_model(): |
| |
| custom_objects = {'Dense': SafeDense} |
| try: |
| |
| return tf.keras.models.load_model('intel_scene_model.h5', custom_objects=custom_objects) |
| except Exception: |
| |
| return tf.keras.models.load_model('intel_scene_model.keras', custom_objects=custom_objects) |
|
|
| with st.spinner("Loading CNN Model... Please wait"): |
| model = load_my_model() |
|
|
| |
| CLASS_NAMES = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street'] |
|
|
| |
| st.title("🌲 Landscape Classification using CNN") |
| st.write("Upload any landscape image, and the model will instantly identify and classify it with high accuracy.") |
|
|
| |
| uploaded_file = st.file_uploader("Choose an image (JPG, JPEG, PNG)...", type=["jpg", "jpeg", "png"]) |
|
|
| if uploaded_file is not None: |
| |
| image = Image.open(uploaded_file) |
| st.image(image, caption="Uploaded Image", use_container_width=True) |
| |
| st.write("---") |
| with st.spinner("Analyzing image and predicting class..."): |
| |
| img_resized = image.convert('RGB').resize((150, 150)) |
| img_array = np.array(img_resized) / 255.0 |
| img_array = np.expand_dims(img_array, axis=0) |
| |
| |
| predictions = model.predict(img_array) |
| highest_class_idx = np.argmax(predictions[0]) |
| confidence = predictions[0][highest_class_idx] * 100 |
| predicted_class = CLASS_NAMES[highest_class_idx] |
|
|
| |
| st.success(f"**Final Prediction:** This landscape represents **{predicted_class.upper()}**") |
| st.metric(label="Confidence Level", value=f"{confidence:.2f}%") |
| |
| |
| st.subheader("Classification Probability Distribution:") |
| for name, pred in zip(CLASS_NAMES, predictions[0]): |
| st.write(f"**{name.capitalize()}:**") |
| st.progress(float(pred)) |