shah1zil commited on
Commit
380eb1f
·
verified ·
1 Parent(s): 874b7c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
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
+ # Load pre-trained ResNet50 model for feature extraction
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
22
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
23
+
24
+ if uploaded_file is not None:
25
+ # Process the image
26
+ img = load_img(uploaded_file, target_size=(32, 32))
27
+ img = img_to_array(img)
28
+ img = np.expand_dims(img, axis=0)
29
+ img = preprocess_input(img)
30
+
31
+ # Extract features
32
+ features = resnet_model.predict(img)
33
+
34
+ # Make prediction
35
+ prediction = model.predict(features)
36
+ predicted_class = class_names[prediction[0]]
37
+
38
+ # Display result
39
+ st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
40
+ st.write(f"Predicted Class: {predicted_class}")