syedhaider270/Animal_Classification_Testing
0
1import streamlit as st2import numpy as np3from PIL import Image4import joblib5import json6from tensorflow.keras.applications import MobileNetV27from tensorflow.keras.applications.mobilenet_v2 import preprocess_input8from tensorflow.keras.preprocessing.image import img_to_array9 10# Load trained model and class names11knn_model = joblib.load("knn_model.joblib")12with open("class_names.json", "r") as f:13 class_names = json.load(f)14 15# Load MobileNetV2 feature extractor16feature_extractor = MobileNetV2(weights="imagenet", include_top=False, pooling="avg", input_shape=(224, 224, 3))17 18st.set_page_config(page_title="Animal Classifier", layout="centered")19st.title("๐พ Animal Image Classifier")20st.write("Upload an image of an animal to identify its class.")21 22uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])23 24if uploaded_file is not None:25 image = Image.open(uploaded_file).convert("RGB")26 st.image(image, caption="Uploaded Image", use_column_width=True)27 28 # Preprocess the image29 img = image.resize((224, 224))30 img_array = img_to_array(img)31 img_array = preprocess_input(img_array)32 img_array = np.expand_dims(img_array, axis=0)33 34 # Extract features35 features = feature_extractor.predict(img_array, verbose=0)36 37 # Predict38 prediction = knn_model.predict(features)[0]39 predicted_class = class_names[prediction]40 41 st.success(f"โ
Predicted Animal: **{predicted_class}**")42 43 