| import streamlit as st |
| import joblib |
| import numpy as np |
| from PIL import Image |
| from tensorflow.keras.preprocessing.image import load_img, img_to_array |
| from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input |
|
|
| |
| knn = joblib.load('knn_model.pkl') |
| class_names = joblib.load('class_names.pkl') |
|
|
| |
| def extract_features(img): |
| |
| model = ResNet50(weights='imagenet', include_top=False, pooling='avg') |
| |
| features = model.predict(img) |
| return features |
|
|
| |
| st.title("Animal Classification App") |
|
|
| |
| st.write("This app classifies animals based on uploaded images using a trained KNN model.") |
|
|
| |
| uploaded_file = st.file_uploader("Upload an image of an animal", type=["jpg", "jpeg", "png"]) |
|
|
| |
| if uploaded_file is not None: |
| |
| image = Image.open(uploaded_file) |
| st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
| |
| image = image.resize((224, 224)) |
| img_array = img_to_array(image) |
| img_array = np.expand_dims(img_array, axis=0) |
| img_array = preprocess_input(img_array) |
|
|
| |
| features = extract_features(img_array) |
|
|
| |
| prediction = knn.predict(features) |
|
|
| |
| st.write(f"Prediction: {class_names[prediction[0]]}") |
|
|