Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,44 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import joblib
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import cv2
|
| 5 |
-
from tensorflow.keras.applications import VGG16
|
| 6 |
-
from tensorflow.keras.applications.vgg16 import preprocess_input
|
| 7 |
|
| 8 |
-
# Load model and class labels
|
| 9 |
knn_model = joblib.load("knn_model.pkl")
|
| 10 |
class_labels = joblib.load("class_labels.pkl")
|
| 11 |
|
| 12 |
-
# Load
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
def extract_features(image):
|
| 16 |
-
"""
|
| 17 |
-
image = cv2.resize(image, (
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def predict_animal(image):
|
| 22 |
-
"""
|
| 23 |
processed_image = extract_features(image) # Extract features
|
| 24 |
-
prediction = knn_model.predict(processed_image)[0] #
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
st.title("Animal Image Classifier")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
image =
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 37 |
-
|
| 38 |
-
if st.button("Identify"):
|
| 39 |
prediction = predict_animal(image)
|
| 40 |
-
st.success(f"Predicted Animal:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import joblib
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.applications import ResNet50
|
| 6 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 7 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 8 |
import cv2
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load KNN model and class labels
|
| 11 |
knn_model = joblib.load("knn_model.pkl")
|
| 12 |
class_labels = joblib.load("class_labels.pkl")
|
| 13 |
|
| 14 |
+
# Load ResNet50 model (same as used in training)
|
| 15 |
+
resnet_model = ResNet50(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
|
| 16 |
+
resnet_model = tf.keras.Model(inputs=resnet_model.input, outputs=tf.keras.layers.GlobalAveragePooling2D()(resnet_model.output))
|
| 17 |
|
| 18 |
def extract_features(image):
|
| 19 |
+
"""Extract features from image using ResNet50 (same as training)."""
|
| 20 |
+
image = cv2.resize(image, (224, 224)) # Resize image
|
| 21 |
+
image = img_to_array(image)
|
| 22 |
+
image = np.expand_dims(image, axis=0)
|
| 23 |
+
image = preprocess_input(image)
|
| 24 |
+
|
| 25 |
+
features = resnet_model.predict(image) # Extract features
|
| 26 |
+
return features.reshape(1, -1) # Ensure shape matches KNN input
|
| 27 |
|
| 28 |
def predict_animal(image):
|
| 29 |
+
"""Predict the class of the input image."""
|
| 30 |
processed_image = extract_features(image) # Extract features
|
| 31 |
+
prediction = knn_model.predict(processed_image)[0] # Predict class index
|
| 32 |
+
return list(class_labels.keys())[list(class_labels.values()).index(prediction)] # Convert index to label
|
| 33 |
+
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
st.title("🐾 Animal Image Classifier")
|
| 36 |
+
uploaded_file = st.file_uploader("📤 Upload an image", type=["jpg", "png", "jpeg"])
|
| 37 |
+
|
| 38 |
+
if uploaded_file:
|
| 39 |
+
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
|
| 40 |
+
st.image(image, caption="📷 Uploaded Image", use_container_width=True)
|
| 41 |
+
|
| 42 |
+
if st.button("🔍 Identify"):
|
|
|
|
|
|
|
|
|
|
| 43 |
prediction = predict_animal(image)
|
| 44 |
+
st.success(f"🎯 Predicted Animal: {prediction}")
|