Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# 1. Page Configuration
|
| 7 |
+
st.set_page_config(page_title="Intel Scene Classifier", page_icon="🌲", layout="centered")
|
| 8 |
+
|
| 9 |
+
# Custom layer loader to bypass the quantization_config error on Hugging Face
|
| 10 |
+
class SafeDense(tf.keras.layers.Dense):
|
| 11 |
+
def __init__(self, *args, **kwargs):
|
| 12 |
+
# Remove the problematic argument if it exists in newer Keras versions
|
| 13 |
+
kwargs.pop('quantization_config', None)
|
| 14 |
+
super().__init__(*args, **kwargs)
|
| 15 |
+
|
| 16 |
+
# 2. Cache and Load the Full Model Safely
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_my_model():
|
| 19 |
+
# We pass SafeDense to bypass version mismatch errors automatically
|
| 20 |
+
custom_objects = {'Dense': SafeDense}
|
| 21 |
+
try:
|
| 22 |
+
# Replace 'intel_scene_model.h5' with your exact model filename if different
|
| 23 |
+
return tf.keras.models.load_model('intel_scene_model.h5', custom_objects=custom_objects)
|
| 24 |
+
except Exception:
|
| 25 |
+
# Fallback if your model file uses the newer .keras format extension
|
| 26 |
+
return tf.keras.models.load_model('intel_scene_model.keras', custom_objects=custom_objects)
|
| 27 |
+
|
| 28 |
+
with st.spinner("Loading CNN Model... Please wait"):
|
| 29 |
+
model = load_my_model()
|
| 30 |
+
|
| 31 |
+
# Class names sorted exactly as in the dataset
|
| 32 |
+
CLASS_NAMES = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street']
|
| 33 |
+
|
| 34 |
+
# 3. User Interface
|
| 35 |
+
st.title("🌲 Landscape Classification using CNN")
|
| 36 |
+
st.write("Upload any landscape image, and the model will instantly identify and classify it with high accuracy.")
|
| 37 |
+
|
| 38 |
+
# Image Upload Tool
|
| 39 |
+
uploaded_file = st.file_uploader("Choose an image (JPG, JPEG, PNG)...", type=["jpg", "jpeg", "png"])
|
| 40 |
+
|
| 41 |
+
if uploaded_file is not None:
|
| 42 |
+
# Display the uploaded image
|
| 43 |
+
image = Image.open(uploaded_file)
|
| 44 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 45 |
+
|
| 46 |
+
st.write("---")
|
| 47 |
+
with st.spinner("Analyzing image and predicting class..."):
|
| 48 |
+
# 4. Image Preprocessing
|
| 49 |
+
img_resized = image.convert('RGB').resize((150, 150))
|
| 50 |
+
img_array = np.array(img_resized) / 255.0 # Rescaling
|
| 51 |
+
img_array = np.expand_dims(img_array, axis=0) # Add Batch dimension
|
| 52 |
+
|
| 53 |
+
# 5. Model Inference & Prediction
|
| 54 |
+
predictions = model.predict(img_array)
|
| 55 |
+
highest_class_idx = np.argmax(predictions[0])
|
| 56 |
+
confidence = predictions[0][highest_class_idx] * 100
|
| 57 |
+
predicted_class = CLASS_NAMES[highest_class_idx]
|
| 58 |
+
|
| 59 |
+
# 6. Display Results Dynamically
|
| 60 |
+
st.success(f"**Final Prediction:** This landscape represents **{predicted_class.upper()}**")
|
| 61 |
+
st.metric(label="Confidence Level", value=f"{confidence:.2f}%")
|
| 62 |
+
|
| 63 |
+
# Visualizing prediction distribution across all classes
|
| 64 |
+
st.subheader("Classification Probability Distribution:")
|
| 65 |
+
for name, pred in zip(CLASS_NAMES, predictions[0]):
|
| 66 |
+
st.write(f"**{name.capitalize()}:**")
|
| 67 |
+
st.progress(float(pred))
|