Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import joblib
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.neighbors import KNeighborsClassifier
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
import os
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Load the pre-trained KNN model and class names
|
10 |
+
knn = joblib.load('knn_model.pk1')
|
11 |
+
class_names = joblib.load('class_names.pk1')
|
12 |
+
|
13 |
+
# Title of the app
|
14 |
+
st.title("Animal Classification Using KNN Model")
|
15 |
+
|
16 |
+
# Description
|
17 |
+
st.write("Upload an image of an animal and the model will predict which animal it is.")
|
18 |
+
|
19 |
+
# Upload image
|
20 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
21 |
+
|
22 |
+
if uploaded_image is not None:
|
23 |
+
# Display image
|
24 |
+
img = Image.open(uploaded_image)
|
25 |
+
st.image(img, caption='Uploaded Image.', use_column_width=True)
|
26 |
+
|
27 |
+
# Preprocess the image for prediction
|
28 |
+
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 array
|
30 |
+
img_array = img_array.flatten().reshape(1, -1) # Flatten the image and reshape it to match the input for KNN model
|
31 |
+
|
32 |
+
# Make prediction
|
33 |
+
prediction = knn.predict(img_array)
|
34 |
+
predicted_class = class_names[prediction[0]]
|
35 |
+
|
36 |
+
# Display prediction
|
37 |
+
st.write(f"Prediction: {predicted_class}")
|
38 |
+
|