Spaces:
Sleeping
Sleeping
| # app.py | |
| import streamlit as st | |
| import numpy as np | |
| from PIL import Image | |
| from tensorflow.keras.applications import MobileNetV2 | |
| from tensorflow.keras.preprocessing import image as keras_image | |
| from tensorflow.keras.applications.mobilenet_v2 import preprocess_input | |
| import joblib | |
| # Load model and class names | |
| model = joblib.load("knn_model.pkl") | |
| class_names = np.load("class_names.npy") | |
| # Load feature extractor | |
| feature_extractor = MobileNetV2(weights='imagenet', include_top=False, pooling='avg') | |
| # Streamlit UI | |
| st.title("🐾 Animal Image Classifier") | |
| st.write("Upload an animal image and get the predicted class.") | |
| uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"]) | |
| if uploaded_file: | |
| img = Image.open(uploaded_file).convert("RGB") | |
| st.image(img, caption="Uploaded Image", use_column_width=True) | |
| # Preprocess image | |
| img_resized = img.resize((224, 224)) | |
| img_array = keras_image.img_to_array(img_resized) | |
| img_array = np.expand_dims(img_array, axis=0) | |
| img_array = preprocess_input(img_array) | |
| # Extract features | |
| features = feature_extractor.predict(img_array) | |
| # Predict | |
| prediction = model.predict(features)[0] | |
| st.success(f"🧠 Predicted Animal: **{prediction}**") | |