Spaces:
Sleeping
Sleeping
File size: 3,336 Bytes
014679a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import streamlit as st
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from PIL import Image
# ๐ง Configure Streamlit
st.set_page_config(
page_title="WildVision ๐พ | Animal Identifier",
layout="centered",
initial_sidebar_state="auto"
)
st.title("๐ค Smart Animal Identifier")
st.markdown("Upload up to **3 animal images** and discover what species they are!")
# โ
Debug Check
st.write("๐ง App started successfully")
# ๐ฆ Load model and class labels with error handling
@st.cache_resource
def load_model_once():
return load_model("animal_model.keras")
@st.cache_data
def load_labels():
return np.load("class_labels.npy", allow_pickle=True).item()
try:
model = load_model_once()
st.success("โ
Model loaded successfully")
except Exception as e:
st.error(f"โ Failed to load model: {e}")
st.stop()
try:
class_indices = load_labels()
st.success("โ
Labels loaded successfully")
except Exception as e:
st.error(f"โ Failed to load label file: {e}")
st.stop()
# ๐ Reverse index to get labels from prediction index
index_to_label = {v: k for k, v in class_indices.items()}
# ๐ค Upload Images
uploaded_files = st.file_uploader(
"๐ธ Upload your animal photos",
type=["jpg", "jpeg", "png"],
accept_multiple_files=True
)
if uploaded_files and len(uploaded_files) > 0:
uploaded_files = uploaded_files[:3] # Limit to 3 images
cols = st.columns(len(uploaded_files))
for idx, uploaded_file in enumerate(uploaded_files):
with cols[idx]:
st.markdown(f"#### ๐ท Image {idx + 1}")
# ๐ผ๏ธ Load and preview
try:
image = Image.open(uploaded_file).convert("RGB")
preview = image.copy()
preview.thumbnail((150, 150))
st.image(preview, caption="Image Preview", use_container_width=True)
except Exception as e:
st.error(f"โ ๏ธ Could not load image: {e}")
continue
# ๐งช Preprocess image
resized = image.resize((128, 128))
img_array = img_to_array(resized) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# ๐ฎ Make prediction
try:
preds = model.predict(img_array)[0]
except Exception as e:
st.error(f"โ Prediction failed: {e}")
continue
if len(preds) != len(index_to_label):
st.error("โ ๏ธ Mismatch between model output and class labels.")
continue
# ๐ Display predictions
top_indices = preds.argsort()[-3:][::-1]
top_labels = [index_to_label[i] for i in top_indices]
top_scores = [preds[i] for i in top_indices]
st.markdown("#### ๐ง Top Predictions:")
for label, score in zip(top_labels, top_scores):
st.write(f"โก๏ธ **{label.capitalize()}**: {score:.2%}")
st.success(f"๐ฏ **Most Likely:** *{top_labels[0].capitalize()}*")
else:
st.info("๐ฅ Please upload 1 to 3 animal images to get started.")
# ๐ Footer
st.markdown("---")
st.caption("๐พ Developed by Neena | Powered by TensorFlow & Streamlit")
|