Spaces:
Running
Running
Upload 3 files
Browse files- app.py +116 -0
- epoch_26.h5 +3 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import requests
|
7 |
+
import plotly.express as px
|
8 |
+
|
9 |
+
# Set page configuration
|
10 |
+
st.set_page_config(page_title="Scene Classifier", layout="wide")
|
11 |
+
|
12 |
+
# Load the model
|
13 |
+
@st.cache_resource
|
14 |
+
def load_model():
|
15 |
+
try:
|
16 |
+
return tf.keras.models.load_model('epoch_26.h5')
|
17 |
+
except:
|
18 |
+
st.error("Model file not found. Please make sure 'model_epoch_11.h5' is in the same directory as this script.")
|
19 |
+
return None
|
20 |
+
|
21 |
+
model = load_model()
|
22 |
+
|
23 |
+
# Define class names based on your dataset
|
24 |
+
class_names = ['buildings', 'forest', 'glacier', 'mountain','human', 'sea', 'street']
|
25 |
+
|
26 |
+
def preprocess_image(img):
|
27 |
+
"""Preprocess image for prediction"""
|
28 |
+
img = img.convert('RGB')
|
29 |
+
img = img.resize((224, 224))
|
30 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
31 |
+
img_array = tf.expand_dims(img_array, 0)
|
32 |
+
img_array = img_array / 255.0 # Normalize the image
|
33 |
+
return img_array
|
34 |
+
|
35 |
+
|
36 |
+
def predict_and_display(img):
|
37 |
+
"""Make prediction and display results with confidence scores"""
|
38 |
+
# Preprocess the image
|
39 |
+
img_array = preprocess_image(img)
|
40 |
+
|
41 |
+
# Make prediction
|
42 |
+
predictions = model.predict(img_array)[0] # Get the first (and only) prediction
|
43 |
+
|
44 |
+
# Get predicted class and confidence
|
45 |
+
predicted_index = np.argmax(predictions)
|
46 |
+
predicted_class = class_names[predicted_index]
|
47 |
+
confidence_score = predictions[predicted_index] * 100 # Convert to percentage
|
48 |
+
|
49 |
+
# Display predicted label and confidence
|
50 |
+
st.markdown(f"### **Predicted:** {predicted_class} ({confidence_score:.2f}%)")
|
51 |
+
st.markdown(f"π§ Model is **{confidence_score:.2f}%** confident that the image is a **{predicted_class}**.")
|
52 |
+
# Create a dataframe for plotting
|
53 |
+
confidence_data = {"Category": class_names, "Confidence (%)": [score * 100 for score in predictions]}
|
54 |
+
|
55 |
+
# Create a bar chart
|
56 |
+
fig = px.bar(
|
57 |
+
confidence_data,
|
58 |
+
x="Confidence (%)",
|
59 |
+
y="Category",
|
60 |
+
orientation="h",
|
61 |
+
text_auto=".2f",
|
62 |
+
title="Confidence Scores",
|
63 |
+
color="Category",
|
64 |
+
color_discrete_sequence=px.colors.qualitative.Set1
|
65 |
+
)
|
66 |
+
fig.update_traces(textposition="outside") # Show confidence values outside bars
|
67 |
+
fig.update_layout(
|
68 |
+
yaxis={"categoryorder": "total ascending"}, # Sort by confidence
|
69 |
+
height=600, # Increase chart height
|
70 |
+
width=700, # Increase chart width
|
71 |
+
margin=dict(l=30, r=30, t=50, b=50) # Adjust margins for better spacing
|
72 |
+
)
|
73 |
+
|
74 |
+
# Display image & graph side by side
|
75 |
+
col1, col2 = st.columns([1, 1])
|
76 |
+
with col1:
|
77 |
+
img = img.resize((512, 512))
|
78 |
+
st.image(img)
|
79 |
+
with col2:
|
80 |
+
st.plotly_chart(fig, use_container_width=True)
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
# Streamlit app UI
|
86 |
+
st.title("π Scene Classifier")
|
87 |
+
st.markdown("""
|
88 |
+
This app classifies images into one of the following categories:
|
89 |
+
- π’ Buildings π² Forest βοΈ Glacier β°οΈ Mountain π Sea π£οΈ Street πΉ Human
|
90 |
+
""")
|
91 |
+
|
92 |
+
# Create tabs for URL input and file upload
|
93 |
+
tab1, tab2 = st.tabs(["Upload Image", "Enter URL"])
|
94 |
+
|
95 |
+
with tab1:
|
96 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
97 |
+
if uploaded_file is not None:
|
98 |
+
try:
|
99 |
+
img = Image.open(uploaded_file)
|
100 |
+
if st.button("Classify Uploaded Image"):
|
101 |
+
predict_and_display(img)
|
102 |
+
except Exception as e:
|
103 |
+
st.error(f"Error processing the image: {e}")
|
104 |
+
|
105 |
+
with tab2:
|
106 |
+
image_url = st.text_input("Enter Image URL:")
|
107 |
+
if st.button("Classify from URL") and image_url:
|
108 |
+
try:
|
109 |
+
response = requests.get(image_url)
|
110 |
+
if response.status_code == 200:
|
111 |
+
img = Image.open(BytesIO(response.content))
|
112 |
+
predict_and_display(img)
|
113 |
+
else:
|
114 |
+
st.error(f"Error fetching the image. Status code: {response.status_code}")
|
115 |
+
except Exception as e:
|
116 |
+
st.error(f"Error processing the URL: {e}")
|
epoch_26.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d91b274f183a50abecef91d624f3124125e4fde244ce24be497a4d099bfc10d8
|
3 |
+
size 130887608
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
tensorflow==2.18.0
|
3 |
+
opencv-python
|
4 |
+
numpy
|
5 |
+
pillow
|
6 |
+
requests
|
7 |
+
plotly
|