Senasu commited on
Commit
93786b4
·
verified ·
1 Parent(s): 272c73b

Upload 3 Files

Browse files
Files changed (3) hide show
  1. app.py +82 -0
  2. cnn_model.h5 +3 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ model = load_model('cnn_model.h5')
9
+
10
+ def process_image(img):
11
+ img = img.convert('RGB')
12
+ img = img.resize((224, 224))
13
+ img = np.array(img)
14
+ img = img / 255.0
15
+ img = np.expand_dims(img, axis=0)
16
+ return img
17
+
18
+ st.title('Rice Classification :rice_scene:')
19
+ st.write('Upload a rice image and the model will detect type of rice.')
20
+
21
+ file = st.file_uploader('Select an image', type=['jpg', 'jpeg', 'png'])
22
+
23
+ if file is not None:
24
+ # Display the uploaded image
25
+ img = Image.open(file)
26
+ st.image(img, caption='Uploaded Image')
27
+
28
+ # Preprocess the image
29
+ image = process_image(img)
30
+
31
+ # Model prediction
32
+ with st.spinner('Classifying the image...'):
33
+ predictions = model.predict(image)
34
+ predicted_class = np.argmax(predictions)
35
+ predicted_prob = predictions[0][predicted_class]
36
+
37
+ # Class names for prediction
38
+ class_names = ['Arborio',
39
+ 'Basmati',
40
+ 'Ipsala',
41
+ 'Jasmine',
42
+ 'Karacadag']
43
+
44
+ # Display the prediction
45
+ st.subheader(f"Prediction: {class_names[predicted_class]}")
46
+ st.write(f"Confidence: {predicted_prob * 100:.2f}%")
47
+
48
+ # Display prediction probabilities
49
+ st.write("Prediction Probabilities for Each Class:")
50
+ probabilities = predictions[0]
51
+ prob_dict = {class_names[i]: probabilities[i] for i in range(len(class_names))}
52
+
53
+ # Plot settings
54
+ sns.set(style="white") # Clean style with no grid background
55
+
56
+ # Create the figure
57
+ fig, ax = plt.subplots(figsize=(12, 8)) # Adjust figure size for better readability
58
+
59
+ # Plot the bar chart
60
+ ax.bar(list(prob_dict.keys()), list(prob_dict.values()), color='#F29F05', edgecolor='black')
61
+ ax.set_ylabel('Probability', fontsize=14)
62
+ ax.set_title('Prediction Probabilities for Each Class', fontsize=20)
63
+
64
+ # Rotate x-axis labels for better readability
65
+ plt.xticks(rotation=90, ha='right', fontsize=15)
66
+
67
+ # Annotate bars with percentage values
68
+ for index, value in enumerate(prob_dict.values()):
69
+ ax.text(index, value, f'{value * 100:.0f}%', va='bottom', ha='center', fontsize=10)
70
+
71
+ # Remove background grid and spines
72
+ ax.spines['top'].set_visible(False)
73
+ ax.spines['right'].set_visible(False)
74
+ ax.spines['left'].set_visible(False)
75
+ ax.spines['bottom'].set_visible(False)
76
+ ax.grid(False)
77
+
78
+ # Adjust layout to prevent clipping
79
+ fig.tight_layout()
80
+
81
+ # Display the plot in Streamlit
82
+ st.pyplot(fig)
cnn_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be305d2411b6476f8c10c9a5c6a65adb5d8607de6035db9cd7bef8b7866a78e1
3
+ size 367292344
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ tensorflow
3
+ matplotlib
4
+ seaborn