sairaarif89 commited on
Commit
03760d9
·
verified ·
1 Parent(s): 1f7ccf5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
9
+ model = joblib.load('knn_model.joblib')
10
+ 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
20
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
21
+
22
+ 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
+ # Make prediction
29
+ img = np.expand_dims(img, axis=0) # Add batch dimension
30
+ features = model.predict(img) # Extract features using the model
31
+ prediction = model.predict(features) # Get prediction
32
+
33
+ # Show the result
34
+ predicted_class = class_names[prediction[0]] # Get the class name
35
+ st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
36
+ st.write(f"Predicted Class: {predicted_class}")