Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
import
|
| 2 |
import pickle
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
from skimage.transform import resize
|
|
|
|
| 6 |
|
| 7 |
# Load the trained KNN model and class names
|
| 8 |
with open("knn_model.pkl", "rb") as f:
|
|
@@ -11,12 +12,13 @@ with open("knn_model.pkl", "rb") as f:
|
|
| 11 |
with open("class_names.pkl", "rb") as f:
|
| 12 |
class_names = pickle.load(f)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def preprocess_image(image):
|
| 16 |
"""Resizes and flattens the image for model prediction."""
|
| 17 |
-
image =
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
return image_flattened
|
| 21 |
|
| 22 |
# Prediction Function
|
|
@@ -24,21 +26,19 @@ def predict_animal(image):
|
|
| 24 |
"""Predicts the class of the uploaded image."""
|
| 25 |
processed_image = preprocess_image(image)
|
| 26 |
prediction = knn_model.predict(processed_image)[0]
|
| 27 |
-
return
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
title
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if __name__ == "__main__":
|
| 44 |
-
app.launch()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import pickle
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
from skimage.transform import resize
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
# Load the trained KNN model and class names
|
| 9 |
with open("knn_model.pkl", "rb") as f:
|
|
|
|
| 12 |
with open("class_names.pkl", "rb") as f:
|
| 13 |
class_names = pickle.load(f)
|
| 14 |
|
| 15 |
+
# Function to preprocess image
|
| 16 |
def preprocess_image(image):
|
| 17 |
"""Resizes and flattens the image for model prediction."""
|
| 18 |
+
image = np.array(image) # Convert PIL image to numpy array
|
| 19 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR
|
| 20 |
+
image_resized = resize(image, (224, 224)) # Resize to 224x224
|
| 21 |
+
image_flattened = image_resized.flatten().reshape(1, -1) # Flatten
|
| 22 |
return image_flattened
|
| 23 |
|
| 24 |
# Prediction Function
|
|
|
|
| 26 |
"""Predicts the class of the uploaded image."""
|
| 27 |
processed_image = preprocess_image(image)
|
| 28 |
prediction = knn_model.predict(processed_image)[0]
|
| 29 |
+
return class_names[prediction]
|
| 30 |
+
|
| 31 |
+
# Streamlit UI
|
| 32 |
+
st.title("🐾 Animal Image Classifier")
|
| 33 |
+
st.write("Upload an image of an animal and click **Identify** to predict the species.")
|
| 34 |
+
|
| 35 |
+
# Upload Image
|
| 36 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 37 |
+
|
| 38 |
+
if uploaded_image is not None:
|
| 39 |
+
image = Image.open(uploaded_image)
|
| 40 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 41 |
+
|
| 42 |
+
if st.button("Identify"):
|
| 43 |
+
prediction = predict_animal(image)
|
| 44 |
+
st.success(f"**Predicted Animal:** {prediction} 🐾")
|
|
|
|
|
|