mjolnir1122 commited on
Commit
af5a72a
·
verified ·
1 Parent(s): 5c6056a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -1,8 +1,9 @@
1
- import gradio as gr
2
  import pickle
3
  import cv2
4
  import numpy as np
5
  from skimage.transform import resize
 
6
 
7
  # Load the trained KNN model and class names
8
  with open("knn_model.pkl", "rb") as f:
@@ -11,12 +12,13 @@ with open("knn_model.pkl", "rb") as f:
11
  with open("class_names.pkl", "rb") as f:
12
  class_names = pickle.load(f)
13
 
14
- # Image Preprocessing Function
15
  def preprocess_image(image):
16
  """Resizes and flattens the image for model prediction."""
17
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR for OpenCV
18
- image_resized = resize(image, (224, 224)) # Resize to match training size
19
- image_flattened = image_resized.flatten().reshape(1, -1) # Flatten to 1D
 
20
  return image_flattened
21
 
22
  # Prediction Function
@@ -24,21 +26,19 @@ def predict_animal(image):
24
  """Predicts the class of the uploaded image."""
25
  processed_image = preprocess_image(image)
26
  prediction = knn_model.predict(processed_image)[0]
27
- return f"Predicted Animal: {class_names[prediction]}"
28
-
29
- # Gradio UI
30
- title = "Animal Image Classifier 🐾"
31
- description = "Upload an image of an animal and click 'Identify' to predict the species."
32
-
33
- app = gr.Interface(
34
- fn=predict_animal,
35
- inputs=gr.Image(type="numpy"),
36
- outputs="text",
37
- title=title,
38
- description=description,
39
- theme="huggingface",
40
- )
41
-
42
- # Run the app
43
- if __name__ == "__main__":
44
- app.launch()
 
1
+ import streamlit as st
2
  import pickle
3
  import cv2
4
  import numpy as np
5
  from skimage.transform import resize
6
+ from PIL import Image
7
 
8
  # Load the trained KNN model and class names
9
  with open("knn_model.pkl", "rb") as f:
 
12
  with open("class_names.pkl", "rb") as f:
13
  class_names = pickle.load(f)
14
 
15
+ # Function to preprocess image
16
  def preprocess_image(image):
17
  """Resizes and flattens the image for model prediction."""
18
+ image = np.array(image) # Convert PIL image to numpy array
19
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR
20
+ image_resized = resize(image, (224, 224)) # Resize to 224x224
21
+ image_flattened = image_resized.flatten().reshape(1, -1) # Flatten
22
  return image_flattened
23
 
24
  # Prediction Function
 
26
  """Predicts the class of the uploaded image."""
27
  processed_image = preprocess_image(image)
28
  prediction = knn_model.predict(processed_image)[0]
29
+ return class_names[prediction]
30
+
31
+ # Streamlit UI
32
+ st.title("🐾 Animal Image Classifier")
33
+ st.write("Upload an image of an animal and click **Identify** to predict the species.")
34
+
35
+ # Upload Image
36
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
37
+
38
+ if uploaded_image is not None:
39
+ image = Image.open(uploaded_image)
40
+ st.image(image, caption="Uploaded Image", use_column_width=True)
41
+
42
+ if st.button("Identify"):
43
+ prediction = predict_animal(image)
44
+ st.success(f"**Predicted Animal:** {prediction} 🐾")