mjolnir1122 commited on
Commit
19f3b33
·
verified ·
1 Parent(s): e5828a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,40 +1,44 @@
1
  import streamlit as st
2
  import numpy as np
3
  import joblib
 
 
 
 
4
  import cv2
5
- from tensorflow.keras.applications import VGG16
6
- from tensorflow.keras.applications.vgg16 import preprocess_input
7
 
8
- # Load model and class labels
9
  knn_model = joblib.load("knn_model.pkl")
10
  class_labels = joblib.load("class_labels.pkl")
11
 
12
- # Load VGG16 feature extractor (same as used in training)
13
- feature_extractor = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
 
14
 
15
  def extract_features(image):
16
- """Manually reshape the image into the expected feature vector size."""
17
- image = cv2.resize(image, (64, 32)) # Resize to the shape used in training
18
- features = image.flatten().reshape(1, -1) # Flatten to match model input size
19
- return features
 
 
 
 
20
 
21
  def predict_animal(image):
22
- """Preprocess the image and make a prediction."""
23
  processed_image = extract_features(image) # Extract features
24
- prediction = knn_model.predict(processed_image)[0] # Get predicted index
25
- predicted_label = list(class_labels.keys())[list(class_labels.values()).index(prediction)]
26
- return predicted_label
27
-
28
- st.title("Animal Image Classifier")
29
-
30
- uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
31
-
32
- if uploaded_file is not None:
33
- image = np.array(cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv2.IMREAD_COLOR))
34
-
35
- # Display the image
36
- st.image(image, caption="Uploaded Image", use_container_width=True)
37
-
38
- if st.button("Identify"):
39
  prediction = predict_animal(image)
40
- st.success(f"Predicted Animal: **{prediction}**")
 
1
  import streamlit as st
2
  import numpy as np
3
  import joblib
4
+ import tensorflow as tf
5
+ from tensorflow.keras.applications import ResNet50
6
+ from tensorflow.keras.applications.resnet50 import preprocess_input
7
+ from tensorflow.keras.preprocessing.image import img_to_array
8
  import cv2
 
 
9
 
10
+ # Load KNN model and class labels
11
  knn_model = joblib.load("knn_model.pkl")
12
  class_labels = joblib.load("class_labels.pkl")
13
 
14
+ # Load ResNet50 model (same as used in training)
15
+ resnet_model = ResNet50(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
16
+ resnet_model = tf.keras.Model(inputs=resnet_model.input, outputs=tf.keras.layers.GlobalAveragePooling2D()(resnet_model.output))
17
 
18
  def extract_features(image):
19
+ """Extract features from image using ResNet50 (same as training)."""
20
+ image = cv2.resize(image, (224, 224)) # Resize image
21
+ image = img_to_array(image)
22
+ image = np.expand_dims(image, axis=0)
23
+ image = preprocess_input(image)
24
+
25
+ features = resnet_model.predict(image) # Extract features
26
+ return features.reshape(1, -1) # Ensure shape matches KNN input
27
 
28
  def predict_animal(image):
29
+ """Predict the class of the input image."""
30
  processed_image = extract_features(image) # Extract features
31
+ prediction = knn_model.predict(processed_image)[0] # Predict class index
32
+ return list(class_labels.keys())[list(class_labels.values()).index(prediction)] # Convert index to label
33
+
34
+ # Streamlit UI
35
+ st.title("🐾 Animal Image Classifier")
36
+ uploaded_file = st.file_uploader("📤 Upload an image", type=["jpg", "png", "jpeg"])
37
+
38
+ if uploaded_file:
39
+ image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
40
+ st.image(image, caption="📷 Uploaded Image", use_container_width=True)
41
+
42
+ if st.button("🔍 Identify"):
 
 
 
43
  prediction = predict_animal(image)
44
+ st.success(f"🎯 Predicted Animal: {prediction}")