Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
-
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 6 |
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 7 |
|
| 8 |
# Load the trained KNN model and class names
|
|
@@ -11,9 +11,11 @@ with open('class_names.txt', 'r') as f:
|
|
| 11 |
class_names = f.readlines()
|
| 12 |
class_names = [x.strip() for x in class_names]
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# Streamlit app
|
| 15 |
st.title('Animal Image Classifier')
|
| 16 |
-
|
| 17 |
st.write('Upload an image to classify it.')
|
| 18 |
|
| 19 |
# Upload Image
|
|
@@ -23,11 +25,13 @@ if uploaded_file is not None:
|
|
| 23 |
# Process the image
|
| 24 |
img = load_img(uploaded_file, target_size=(224, 224)) # Resize image to match model input
|
| 25 |
img = img_to_array(img) # Convert to array
|
|
|
|
| 26 |
img = preprocess_input(img) # Preprocess image for ResNet50
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
prediction = model.predict(features) # Get prediction
|
| 32 |
|
| 33 |
# Show the result
|
|
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
+
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
|
| 6 |
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 7 |
|
| 8 |
# Load the trained KNN model and class names
|
|
|
|
| 11 |
class_names = f.readlines()
|
| 12 |
class_names = [x.strip() for x in class_names]
|
| 13 |
|
| 14 |
+
# Load pre-trained ResNet50 model for feature extraction (without the top layer)
|
| 15 |
+
resnet_model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
|
| 16 |
+
|
| 17 |
# Streamlit app
|
| 18 |
st.title('Animal Image Classifier')
|
|
|
|
| 19 |
st.write('Upload an image to classify it.')
|
| 20 |
|
| 21 |
# Upload Image
|
|
|
|
| 25 |
# Process the image
|
| 26 |
img = load_img(uploaded_file, target_size=(224, 224)) # Resize image to match model input
|
| 27 |
img = img_to_array(img) # Convert to array
|
| 28 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
| 29 |
img = preprocess_input(img) # Preprocess image for ResNet50
|
| 30 |
|
| 31 |
+
# Extract features using ResNet50
|
| 32 |
+
features = resnet_model.predict(img) # Extract features using ResNet50
|
| 33 |
+
|
| 34 |
+
# Make prediction with the KNN model
|
| 35 |
prediction = model.predict(features) # Get prediction
|
| 36 |
|
| 37 |
# Show the result
|