CoolFace
Apppublic

Abdullah-Basar/Animal_Classification

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py39 linesDownload Raw Back to root
1import streamlit as st2import joblib3import numpy as np4from sklearn.neighbors import KNeighborsClassifier5from tensorflow.keras.preprocessing import image6import os7from PIL import Image8 9# Load the pre-trained KNN model and class names10knn = joblib.load('knn_model.pk1')11class_names = joblib.load('class_names.pk1')12 13# Title of the app14st.title("Animal Classification Using KNN Model")15 16# Description17st.write("Upload an image of an animal and the model will predict which animal it is.")18 19# Upload image20uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])21 22if uploaded_image is not None:23    # Display image24    img = Image.open(uploaded_image)25    st.image(img, caption='Uploaded Image.', use_column_width=True)26 27    # Preprocess the image for prediction28    img = img.resize((64, 64))  # Resize the image to match the model's expected size (adjust if needed)29    img_array = np.array(img)  # Convert the image to numpy array30    img_array = img_array.flatten().reshape(1, -1)  # Flatten the image and reshape it to match the input for KNN model31 32    # Make prediction33    prediction = knn.predict(img_array)34    predicted_class = class_names[prediction[0]]35 36    # Display prediction37    st.write(f"Prediction: {predicted_class}")38 39