Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,14 @@ from tensorflow.keras.models import load_model
|
|
| 4 |
from tensorflow.keras.preprocessing.image import img_to_array
|
| 5 |
from PIL import Image, UnidentifiedImageError
|
| 6 |
|
| 7 |
-
#
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="Animal Classifier",
|
| 10 |
layout="centered",
|
| 11 |
initial_sidebar_state="auto"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
@st.cache_resource
|
| 16 |
def load_model_once():
|
| 17 |
return load_model("animal_model.keras")
|
|
@@ -24,11 +24,11 @@ model = load_model_once()
|
|
| 24 |
class_indices = load_labels()
|
| 25 |
class_labels = list(class_indices.keys())
|
| 26 |
|
| 27 |
-
#
|
| 28 |
st.title("๐พ Animal Image Classifier")
|
| 29 |
-
st.markdown("Upload up to **3 images**
|
| 30 |
|
| 31 |
-
#
|
| 32 |
uploaded_files = st.file_uploader(
|
| 33 |
"Upload animal images",
|
| 34 |
type=["jpg", "jpeg", "png"],
|
|
@@ -36,43 +36,37 @@ uploaded_files = st.file_uploader(
|
|
| 36 |
)
|
| 37 |
|
| 38 |
if uploaded_files:
|
| 39 |
-
uploaded_files = uploaded_files[:3] # Limit to 3
|
| 40 |
cols = st.columns(len(uploaded_files))
|
| 41 |
|
| 42 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 43 |
with cols[idx]:
|
| 44 |
st.markdown(f"### Image {idx + 1}")
|
| 45 |
try:
|
| 46 |
-
# ๐ผ๏ธ Load & preview image
|
| 47 |
image = Image.open(uploaded_file).convert("RGB")
|
| 48 |
preview = image.copy()
|
| 49 |
preview.thumbnail((150, 150))
|
| 50 |
-
st.image(preview, caption="
|
| 51 |
-
|
| 52 |
-
# ๐งช Preprocess image
|
| 53 |
-
resized = image.resize((128, 128)) # Adjust size as per your model
|
| 54 |
-
st.image(resized, caption="๐งช Resized for Model (128x128)", use_container_width=False)
|
| 55 |
|
|
|
|
|
|
|
| 56 |
img_array = img_to_array(resized) / 255.0
|
| 57 |
img_array = np.expand_dims(img_array, axis=0)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
preds = model.predict(img_array)[0]
|
| 61 |
top_indices = preds.argsort()[-3:][::-1]
|
| 62 |
top_labels = [class_labels[i] for i in top_indices]
|
| 63 |
top_scores = [preds[i] for i in top_indices]
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
st.success(f"
|
| 67 |
-
st.markdown("#####
|
| 68 |
for label, score in zip(top_labels, top_scores):
|
| 69 |
st.write(f"**{label.capitalize()}** โ {score:.4f}")
|
| 70 |
|
| 71 |
except UnidentifiedImageError:
|
| 72 |
-
st.error("
|
| 73 |
except Exception as e:
|
| 74 |
-
st.error(f"
|
| 75 |
|
| 76 |
-
# ๐ Footer message
|
| 77 |
-
st.sidebar.markdown("---")
|
| 78 |
-
st.sidebar.markdown("Built with โค๏ธ using Streamlit and Keras.")
|
|
|
|
| 4 |
from tensorflow.keras.preprocessing.image import img_to_array
|
| 5 |
from PIL import Image, UnidentifiedImageError
|
| 6 |
|
| 7 |
+
# Configure Streamlit
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="Animal Classifier",
|
| 10 |
layout="centered",
|
| 11 |
initial_sidebar_state="auto"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Load model and class labels
|
| 15 |
@st.cache_resource
|
| 16 |
def load_model_once():
|
| 17 |
return load_model("animal_model.keras")
|
|
|
|
| 24 |
class_indices = load_labels()
|
| 25 |
class_labels = list(class_indices.keys())
|
| 26 |
|
| 27 |
+
# Title
|
| 28 |
st.title("๐พ Animal Image Classifier")
|
| 29 |
+
st.markdown("Upload up to **3 images** to get predictions")
|
| 30 |
|
| 31 |
+
# File uploader
|
| 32 |
uploaded_files = st.file_uploader(
|
| 33 |
"Upload animal images",
|
| 34 |
type=["jpg", "jpeg", "png"],
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
if uploaded_files:
|
| 39 |
+
uploaded_files = uploaded_files[:3] # Limit to 3 files
|
| 40 |
cols = st.columns(len(uploaded_files))
|
| 41 |
|
| 42 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 43 |
with cols[idx]:
|
| 44 |
st.markdown(f"### Image {idx + 1}")
|
| 45 |
try:
|
|
|
|
| 46 |
image = Image.open(uploaded_file).convert("RGB")
|
| 47 |
preview = image.copy()
|
| 48 |
preview.thumbnail((150, 150))
|
| 49 |
+
st.image(preview, caption="Preview", use_container_width=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Resize and preprocess
|
| 52 |
+
resized = image.resize((128, 128))
|
| 53 |
img_array = img_to_array(resized) / 255.0
|
| 54 |
img_array = np.expand_dims(img_array, axis=0)
|
| 55 |
|
| 56 |
+
# Prediction
|
| 57 |
preds = model.predict(img_array)[0]
|
| 58 |
top_indices = preds.argsort()[-3:][::-1]
|
| 59 |
top_labels = [class_labels[i] for i in top_indices]
|
| 60 |
top_scores = [preds[i] for i in top_indices]
|
| 61 |
|
| 62 |
+
# Display predictions
|
| 63 |
+
st.success(f"Top Prediction: **{top_labels[0].capitalize()}**")
|
| 64 |
+
st.markdown("##### Top 3 Predictions:")
|
| 65 |
for label, score in zip(top_labels, top_scores):
|
| 66 |
st.write(f"**{label.capitalize()}** โ {score:.4f}")
|
| 67 |
|
| 68 |
except UnidentifiedImageError:
|
| 69 |
+
st.error("Error: Invalid image file.")
|
| 70 |
except Exception as e:
|
| 71 |
+
st.error(f"Unexpected error: {str(e)}")
|
| 72 |
|
|
|
|
|
|
|
|
|