Spaces:
Sleeping
Sleeping
File size: 2,424 Bytes
f030920 2c9c8b0 f030920 2c9c8b0 d7e763d 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 f030920 2c9c8b0 d6191fd |
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 |
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"
)
# ๐ฆ Load model and class labels
@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()
model = load_model_once()
class_indices = load_labels()
class_labels = list(class_indices.keys())
# ๐ท App Header
st.title("๐คSmart Animal Identifier")
st.markdown("Upload up to **3 animal images** and discover what species they are!")
# ๐ค Upload Images
uploaded_files = st.file_uploader(
"๐ธ Upload your animal photos",
type=["jpg", "jpeg", "png"],
accept_multiple_files=True
)
if uploaded_files:
# Limit to 3 images
uploaded_files = uploaded_files[:3]
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
image = Image.open(uploaded_file).convert("RGB")
preview = image.copy()
preview.thumbnail((150, 150))
st.image(preview, caption="Image Preview", use_container_width=True)
# ๐งช Preprocess for prediction
resized = image.resize((128, 128))
img_array = img_to_array(resized) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# ๐ฎ Make prediction
preds = model.predict(img_array)[0]
# โ
Safe check
if len(preds) != len(class_labels):
st.error("โ ๏ธ Mismatch between model outputs and label classes.")
else:
top_indices = preds.argsort()[-3:][::-1]
top_labels = [class_labels[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()}* ")
|