Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from tensorflow.keras.applications import MobileNetV2
|
| 6 |
+
from tensorflow.keras.preprocessing import image as keras_image
|
| 7 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
| 8 |
+
import joblib
|
| 9 |
+
|
| 10 |
+
# Load model and class names
|
| 11 |
+
model = joblib.load("knn_model.pkl")
|
| 12 |
+
class_names = np.load("class_names.npy")
|
| 13 |
+
|
| 14 |
+
# Load feature extractor
|
| 15 |
+
feature_extractor = MobileNetV2(weights='imagenet', include_top=False, pooling='avg')
|
| 16 |
+
|
| 17 |
+
# Streamlit UI
|
| 18 |
+
st.title("🐾 Animal Image Classifier")
|
| 19 |
+
st.write("Upload an animal image and get the predicted class.")
|
| 20 |
+
|
| 21 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
|
| 22 |
+
|
| 23 |
+
if uploaded_file:
|
| 24 |
+
img = Image.open(uploaded_file).convert("RGB")
|
| 25 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 26 |
+
|
| 27 |
+
# Preprocess image
|
| 28 |
+
img_resized = img.resize((224, 224))
|
| 29 |
+
img_array = keras_image.img_to_array(img_resized)
|
| 30 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 31 |
+
img_array = preprocess_input(img_array)
|
| 32 |
+
|
| 33 |
+
# Extract features
|
| 34 |
+
features = feature_extractor.predict(img_array)
|
| 35 |
+
|
| 36 |
+
# Predict
|
| 37 |
+
prediction = model.predict(features)[0]
|
| 38 |
+
st.success(f"🧠 Predicted Animal: **{prediction}**")
|