Spaces:
Sleeping
Sleeping
Upload main files
Browse files- app.py +77 -0
- cnn_model.h5 +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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('Wheat Classification :ear_of_rice:')
|
| 19 |
+
st.write('Upload a wheat image and the model will detect type of wheat.')
|
| 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 = ['Vitreous','Starchy','Foreign','Mixture (Labeled)','Mixture']
|
| 39 |
+
# Display the prediction
|
| 40 |
+
st.subheader(f"Prediction: {class_names[predicted_class]}")
|
| 41 |
+
st.write(f"Confidence: {predicted_prob * 100:.2f}%")
|
| 42 |
+
|
| 43 |
+
# Display prediction probabilities
|
| 44 |
+
st.write("Prediction Probabilities for Each Class:")
|
| 45 |
+
probabilities = predictions[0]
|
| 46 |
+
prob_dict = {class_names[i]: probabilities[i] for i in range(len(class_names))}
|
| 47 |
+
|
| 48 |
+
# Plot settings
|
| 49 |
+
sns.set(style="white") # Clean style with no grid background
|
| 50 |
+
|
| 51 |
+
# Create the figure
|
| 52 |
+
fig, ax = plt.subplots(figsize=(12, 8)) # Adjust figure size for better readability
|
| 53 |
+
|
| 54 |
+
# Plot the bar chart
|
| 55 |
+
ax.bar(list(prob_dict.keys()), list(prob_dict.values()), color='#A65A17', edgecolor='black')
|
| 56 |
+
ax.set_ylabel('Probability', fontsize=14)
|
| 57 |
+
ax.set_title('Prediction Probabilities for Each Class', fontsize=20)
|
| 58 |
+
|
| 59 |
+
# Rotate x-axis labels for better readability
|
| 60 |
+
plt.xticks(rotation=90, ha='right', fontsize=15)
|
| 61 |
+
|
| 62 |
+
# Annotate bars with percentage values
|
| 63 |
+
for index, value in enumerate(prob_dict.values()):
|
| 64 |
+
ax.text(index, value, f'{value * 100:.0f}%', va='bottom', ha='center', fontsize=10)
|
| 65 |
+
|
| 66 |
+
# Remove background grid and spines
|
| 67 |
+
ax.spines['top'].set_visible(False)
|
| 68 |
+
ax.spines['right'].set_visible(False)
|
| 69 |
+
ax.spines['left'].set_visible(False)
|
| 70 |
+
ax.spines['bottom'].set_visible(False)
|
| 71 |
+
ax.grid(False)
|
| 72 |
+
|
| 73 |
+
# Adjust layout to prevent clipping
|
| 74 |
+
fig.tight_layout()
|
| 75 |
+
|
| 76 |
+
# Display the plot in Streamlit
|
| 77 |
+
st.pyplot(fig)
|
cnn_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3ec3ebac4d847bf8c0785fea6bf387031b58ca0806d03549c0fbd3583134958f
|
| 3 |
+
size 367292376
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
matplotlib
|
| 4 |
+
seaborn
|