Senasu commited on
Commit
e06027d
·
verified ·
1 Parent(s): 72719de

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +93 -0
  2. cnn_model.h5 +3 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from tensorflow.keras.models import load_model
3
+ from PIL import Image
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ import seaborn as sns
7
+
8
+ # Load the trained model
9
+ model = load_model('cnn_model.h5')
10
+
11
+ # Function to process the uploaded image
12
+ def process_image(img):
13
+ img = img.convert('RGB')
14
+ img = img.resize((32, 32))
15
+ img = np.array(img)
16
+ img = img / 255.0
17
+ img = np.expand_dims(img, axis=0)
18
+ return img
19
+
20
+ # Frontend design
21
+ st.set_page_config(page_title="Dog vs Cat Detection", page_icon="🐶🐱", layout="centered")
22
+ st.title("Dog vs Cat Image Classification 🐶🐱")
23
+
24
+ # Description
25
+ st.markdown("""
26
+ This is a simple Dog vs Cat image classifier. Upload an image of either a dog or a cat, and
27
+ the model will predict the class along with the confidence level.
28
+ """)
29
+
30
+ # Image upload
31
+ file = st.file_uploader('Select an image', type=['jpg', 'jpeg', 'png'])
32
+
33
+ if file is not None:
34
+ img = Image.open(file)
35
+
36
+ # Display the uploaded image with a border and centered
37
+ st.image(img, caption='Uploaded Image', use_column_width=True,
38
+ output_format="PNG", width=400)
39
+
40
+ # Preprocess the image
41
+ image = process_image(img)
42
+
43
+ # Model prediction
44
+ with st.spinner('Classifying the image...'):
45
+ predictions = model.predict(image)
46
+ predicted_class = np.argmax(predictions)
47
+ predicted_prob = predictions[0][predicted_class]
48
+
49
+ # Class names for prediction
50
+ class_names = ['Cat','Dog']
51
+
52
+ # Display the prediction result
53
+ st.subheader(f"Prediction: {class_names[predicted_class]}")
54
+ st.write(f"Confidence: {predicted_prob * 100:.2f}%")
55
+
56
+ # Display prediction probabilities
57
+ st.write("Prediction Probabilities for Each Class:")
58
+
59
+ # Prepare probabilities for visualization
60
+ probabilities = predictions[0]
61
+ prob_dict = {class_names[i]: probabilities[i] for i in range(len(class_names))}
62
+
63
+ # Plot settings
64
+ sns.set(style="whitegrid") # Use a grid style for the plot
65
+
66
+ # Create the figure for the bar chart
67
+ fig, ax = plt.subplots(figsize=(10, 6)) # Adjust figure size for better readability
68
+
69
+ # Plot the bar chart with a brighter color palette
70
+ ax.bar(list(prob_dict.keys()), list(prob_dict.values()), color='#f5a623', edgecolor='black')
71
+ ax.set_ylabel('Probability', fontsize=14, color='black')
72
+ ax.set_title('Prediction Probabilities for Each Class', fontsize=18, color='black')
73
+
74
+ # Rotate x-axis labels for better readability
75
+ plt.xticks(rotation=45, ha='right', fontsize=12)
76
+
77
+ # Annotate bars with percentage values
78
+ for index, value in enumerate(prob_dict.values()):
79
+ ax.text(index, value, f'{value * 100:.0f}%', va='bottom', ha='center', fontsize=10)
80
+
81
+ # Style improvements: Remove background grid and spines
82
+ ax.spines['top'].set_visible(False)
83
+ ax.spines['right'].set_visible(False)
84
+ ax.spines['left'].set_visible(False)
85
+ ax.spines['bottom'].set_visible(False)
86
+ ax.grid(False)
87
+
88
+ # Adjust layout to prevent clipping
89
+ fig.tight_layout()
90
+
91
+ # Display the plot in Streamlit
92
+ st.pyplot(fig)
93
+
cnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9761bd08070d88b40efbfa051cd064d4682aef744d71d9be9e5c6b67fa569a2b
3
+ size 96309296
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ tensorflow
3
+ Pillow
4
+ matplotlib
5
+ seaborn