File size: 1,244 Bytes
2d15245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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}**")