Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from PIL import Image
|
|
| 6 |
|
| 7 |
# 🔧 Configure Streamlit
|
| 8 |
st.set_page_config(
|
| 9 |
-
page_title="Animal
|
| 10 |
layout="centered",
|
| 11 |
initial_sidebar_state="auto"
|
| 12 |
)
|
|
@@ -24,42 +24,50 @@ model = load_model_once()
|
|
| 24 |
class_indices = load_labels()
|
| 25 |
class_labels = list(class_indices.keys())
|
| 26 |
|
| 27 |
-
# 🏷 App
|
| 28 |
-
st.title("
|
| 29 |
-
st.markdown("Upload up to **3 images** and
|
| 30 |
|
| 31 |
# 📤 Upload Images
|
| 32 |
uploaded_files = st.file_uploader(
|
| 33 |
-
"Upload animal
|
| 34 |
type=["jpg", "jpeg", "png"],
|
| 35 |
accept_multiple_files=True
|
| 36 |
)
|
| 37 |
|
| 38 |
if uploaded_files:
|
| 39 |
-
# Limit to 3 images
|
| 40 |
uploaded_files = uploaded_files[:3]
|
| 41 |
-
cols = st.columns(len(uploaded_files))
|
| 42 |
|
| 43 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 44 |
with cols[idx]:
|
| 45 |
-
st.markdown(f"
|
| 46 |
|
| 47 |
-
# 🖼️ Load
|
| 48 |
image = Image.open(uploaded_file).convert("RGB")
|
| 49 |
preview = image.copy()
|
| 50 |
-
preview.thumbnail((150, 150))
|
| 51 |
-
st.image(preview, caption="Preview", use_container_width=True)
|
| 52 |
|
| 53 |
-
# 🧪 Preprocess
|
| 54 |
-
resized = image.resize((128, 128))
|
| 55 |
img_array = img_to_array(resized) / 255.0
|
| 56 |
img_array = np.expand_dims(img_array, axis=0)
|
| 57 |
|
| 58 |
-
# 🔮
|
| 59 |
preds = model.predict(img_array)[0]
|
| 60 |
-
top_indices = preds.argsort()[-3:][::-1]
|
| 61 |
-
top_labels = [class_labels[i] for i in top_indices]
|
| 62 |
-
top_scores = [preds[i] for i in top_indices]
|
| 63 |
|
| 64 |
-
# ✅
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# 🔧 Configure Streamlit
|
| 8 |
st.set_page_config(
|
| 9 |
+
page_title="WildVision 🐾 | Animal Identifier",
|
| 10 |
layout="centered",
|
| 11 |
initial_sidebar_state="auto"
|
| 12 |
)
|
|
|
|
| 24 |
class_indices = load_labels()
|
| 25 |
class_labels = list(class_indices.keys())
|
| 26 |
|
| 27 |
+
# 🏷 App Header
|
| 28 |
+
st.title("🦁 WildVision - Smart Animal Identifier")
|
| 29 |
+
st.markdown("Upload up to **3 animal images** and discover what species they are!")
|
| 30 |
|
| 31 |
# 📤 Upload Images
|
| 32 |
uploaded_files = st.file_uploader(
|
| 33 |
+
"📸 Upload your animal photos",
|
| 34 |
type=["jpg", "jpeg", "png"],
|
| 35 |
accept_multiple_files=True
|
| 36 |
)
|
| 37 |
|
| 38 |
if uploaded_files:
|
| 39 |
+
# Limit to 3 images
|
| 40 |
uploaded_files = uploaded_files[:3]
|
| 41 |
+
cols = st.columns(len(uploaded_files))
|
| 42 |
|
| 43 |
for idx, uploaded_file in enumerate(uploaded_files):
|
| 44 |
with cols[idx]:
|
| 45 |
+
st.markdown(f"#### 📷 Image {idx + 1}")
|
| 46 |
|
| 47 |
+
# 🖼️ Load and preview
|
| 48 |
image = Image.open(uploaded_file).convert("RGB")
|
| 49 |
preview = image.copy()
|
| 50 |
+
preview.thumbnail((150, 150))
|
| 51 |
+
st.image(preview, caption="Image Preview", use_container_width=True)
|
| 52 |
|
| 53 |
+
# 🧪 Preprocess for prediction
|
| 54 |
+
resized = image.resize((128, 128))
|
| 55 |
img_array = img_to_array(resized) / 255.0
|
| 56 |
img_array = np.expand_dims(img_array, axis=0)
|
| 57 |
|
| 58 |
+
# 🔮 Make prediction
|
| 59 |
preds = model.predict(img_array)[0]
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# ✅ Safe check
|
| 62 |
+
if len(preds) != len(class_labels):
|
| 63 |
+
st.error("⚠️ Mismatch between model outputs and label classes.")
|
| 64 |
+
else:
|
| 65 |
+
top_indices = preds.argsort()[-3:][::-1]
|
| 66 |
+
top_labels = [class_labels[i] for i in top_indices]
|
| 67 |
+
top_scores = [preds[i] for i in top_indices]
|
| 68 |
+
|
| 69 |
+
st.markdown("#### 🧠 Top Predictions:")
|
| 70 |
+
for label, score in zip(top_labels, top_scores):
|
| 71 |
+
st.write(f"➡️ **{label.capitalize()}**: {score:.2%}")
|
| 72 |
+
|
| 73 |
+
st.success(f"🎯 **Most Likely:** *{top_labels[0].capitalize()}* 🐾")
|