Abdullah-Basar's picture
Update app.py
4fdc63e verified
import streamlit as st
import joblib
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from tensorflow.keras.preprocessing import image
import os
from PIL import Image
# Load the pre-trained KNN model and class names
knn = joblib.load('knn_model.pk1')
class_names = joblib.load('class_names.pk1')
# Title of the app
st.title("Animal Classification Using KNN Model")
# Description
st.write("Upload an image of an animal and the model will predict which animal it is.")
# Upload image
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
# Display image
img = Image.open(uploaded_image)
st.image(img, caption='Uploaded Image.', use_column_width=True)
# Preprocess the image for prediction
img = img.resize((64, 64)) # Resize the image to match the model's expected size (adjust if needed)
img_array = np.array(img) # Convert the image to numpy array
img_array = img_array.flatten().reshape(1, -1) # Flatten the image and reshape it to match the input for KNN model
# Make prediction
prediction = knn.predict(img_array)
predicted_class = class_names[prediction[0]]
# Display prediction
st.write(f"Prediction: {predicted_class}")