Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,32 @@ import numpy as np
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# Load the models
|
| 8 |
-
filter_model = load_model("model_checkpoint")
|
| 9 |
-
corn_model = load_model(
|
| 10 |
-
cassava_model = load_model(
|
| 11 |
|
| 12 |
# Function to preprocess and predict with the filter model
|
| 13 |
def is_leaf_image(img, model, threshold=0.5):
|
| 14 |
img_resized = img.resize((128, 128)) # Ensure correct size for the filter model
|
| 15 |
img_array = np.expand_dims(image.img_to_array(img_resized) / 255.0, axis=0) # Normalize
|
| 16 |
prediction = model.predict(img_array)
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return leaf_probability >= threshold, leaf_probability
|
| 19 |
|
| 20 |
-
# Function to predict
|
| 21 |
-
def
|
| 22 |
img = image.img_to_array(img)
|
| 23 |
img = np.expand_dims(img, axis=0)
|
| 24 |
-
img = img / 255.0 # Normalize
|
| 25 |
prediction = model.predict(img)
|
| 26 |
predicted_class = np.argmax(prediction)
|
| 27 |
return predicted_class, class_names[predicted_class]
|
|
@@ -33,6 +42,11 @@ def home_page():
|
|
| 33 |
st.write("""
|
| 34 |
Welcome to the Crop Disease Identification App. This tool aims at helping local farmers
|
| 35 |
identify diseases in maize and cassava crops by analyzing images of leaves.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
""")
|
| 37 |
|
| 38 |
def about_page():
|
|
@@ -40,57 +54,68 @@ def about_page():
|
|
| 40 |
st.image("IMG_20240727_143208_444.jpg", use_column_width=True)
|
| 41 |
st.write("""
|
| 42 |
This application is designed to assist farmers in diagnosing common diseases in maize and cassava crops.
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
""")
|
| 45 |
|
| 46 |
def prediction_page():
|
| 47 |
st.title("Crop Disease Prediction")
|
| 48 |
|
| 49 |
-
#
|
| 50 |
crop_type = st.selectbox("Select the crop:", ["Maize", "Cassava"])
|
| 51 |
|
| 52 |
-
# File uploader
|
| 53 |
uploaded_file = st.file_uploader("Upload an image of the leaf...", type=["jpg", "jpeg", "png"])
|
| 54 |
|
| 55 |
if uploaded_file is not None:
|
| 56 |
-
# Load
|
| 57 |
img = Image.open(uploaded_file)
|
| 58 |
-
st.image(img, caption=
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
if crop_type == "Maize":
|
| 71 |
-
|
| 72 |
-
class_names = [
|
| 73 |
-
|
| 74 |
else:
|
| 75 |
-
|
| 76 |
-
class_names = [
|
| 77 |
-
|
| 78 |
|
| 79 |
-
# Display
|
| 80 |
st.write(f"Predicted Class: {class_name}")
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Main app
|
| 96 |
def main():
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# Load the models
|
| 8 |
+
filter_model = load_model("model_checkpoint")
|
| 9 |
+
corn_model = load_model('corn_model_mobilenetv2.h5')
|
| 10 |
+
cassava_model = load_model('cassava_disease_model.h5')
|
| 11 |
|
| 12 |
# Function to preprocess and predict with the filter model
|
| 13 |
def is_leaf_image(img, model, threshold=0.5):
|
| 14 |
img_resized = img.resize((128, 128)) # Ensure correct size for the filter model
|
| 15 |
img_array = np.expand_dims(image.img_to_array(img_resized) / 255.0, axis=0) # Normalize
|
| 16 |
prediction = model.predict(img_array)
|
| 17 |
+
|
| 18 |
+
# Debugging output
|
| 19 |
+
st.write(f"Filter Model Prediction Output: {prediction}")
|
| 20 |
+
|
| 21 |
+
# Handle single-value or two-class outputs
|
| 22 |
+
if len(prediction[0]) == 1: # Single-class output
|
| 23 |
+
leaf_probability = prediction[0][0] # Output is the probability of "Leaf"
|
| 24 |
+
else: # Two-class output
|
| 25 |
+
leaf_probability = prediction[0][1] # "Leaf" class probability
|
| 26 |
+
|
| 27 |
return leaf_probability >= threshold, leaf_probability
|
| 28 |
|
| 29 |
+
# Function to predict an uploaded image
|
| 30 |
+
def predict_image(img, model, class_names):
|
| 31 |
img = image.img_to_array(img)
|
| 32 |
img = np.expand_dims(img, axis=0)
|
| 33 |
+
img = img / 255.0 # Normalize image
|
| 34 |
prediction = model.predict(img)
|
| 35 |
predicted_class = np.argmax(prediction)
|
| 36 |
return predicted_class, class_names[predicted_class]
|
|
|
|
| 42 |
st.write("""
|
| 43 |
Welcome to the Crop Disease Identification App. This tool aims at helping local farmers
|
| 44 |
identify diseases in maize and cassava crops by analyzing images of leaves.
|
| 45 |
+
|
| 46 |
+
Use the navigation menu to explore the app:
|
| 47 |
+
- **Home:** Overview of the app
|
| 48 |
+
- **About:** Information about the app and its purpose
|
| 49 |
+
- **Prediction:** Upload an image and get a disease diagnosis along with recommendations
|
| 50 |
""")
|
| 51 |
|
| 52 |
def about_page():
|
|
|
|
| 54 |
st.image("IMG_20240727_143208_444.jpg", use_column_width=True)
|
| 55 |
st.write("""
|
| 56 |
This application is designed to assist farmers in diagnosing common diseases in maize and cassava crops.
|
| 57 |
+
By utilizing deep learning models trained on thousands of leaf images, the app can accurately identify
|
| 58 |
+
diseases such as "blight, common rust and gray leaf spot" in maize and "Bacterial blight, green mottle, brown streak and mosaic" in cassava.
|
| 59 |
+
The models were built using Convolutional Neural Networks (CNNs), trained on publicly available datasets and tested on locally obtained images.
|
| 60 |
+
The goal is to provide quick and accurate disease identification to help mitigate crop losses and improve yields.
|
| 61 |
+
|
| 62 |
+
If you have any questions or feedback, please reach out to the developer.
|
| 63 |
+
|
| 64 |
+
E-mail: olorunnisholato7@gmail.com
|
| 65 |
+
|
| 66 |
+
Thank you for using the Crop Disease Identification App!
|
| 67 |
""")
|
| 68 |
|
| 69 |
def prediction_page():
|
| 70 |
st.title("Crop Disease Prediction")
|
| 71 |
|
| 72 |
+
# Model selection
|
| 73 |
crop_type = st.selectbox("Select the crop:", ["Maize", "Cassava"])
|
| 74 |
|
| 75 |
+
# File uploader for image input
|
| 76 |
uploaded_file = st.file_uploader("Upload an image of the leaf...", type=["jpg", "jpeg", "png"])
|
| 77 |
|
| 78 |
if uploaded_file is not None:
|
| 79 |
+
# Load the uploaded image
|
| 80 |
img = Image.open(uploaded_file)
|
| 81 |
+
st.image(img, caption='Uploaded Image', use_column_width=True)
|
| 82 |
+
st.write("")
|
| 83 |
+
|
| 84 |
+
# Check if the image contains a leaf
|
| 85 |
+
is_leaf, leaf_probability = is_leaf_image(img, filter_model)
|
| 86 |
+
|
| 87 |
+
if not is_leaf:
|
| 88 |
+
st.error(f"This image does not appear to contain a leaf (Probability: {leaf_probability:.2f}).")
|
| 89 |
+
else:
|
| 90 |
+
st.success(f"The image contains a leaf (Probability: {leaf_probability:.2f}).")
|
| 91 |
+
|
| 92 |
+
# Set image size based on crop type
|
| 93 |
if crop_type == "Maize":
|
| 94 |
+
img = img.resize((128, 128)) # Resize for Maize
|
| 95 |
+
class_names = ['Blight', 'Common_Rust', 'Gray_Leaf_Spot', 'Healthy']
|
| 96 |
+
predicted_class, class_name = predict_image(img, corn_model, class_names)
|
| 97 |
else:
|
| 98 |
+
img = img.resize((224, 224)) # Resize for Cassava
|
| 99 |
+
class_names = ['Cassava_bacterial_blight','Cassava_brown_streak_disease','Cassava_green_mottle','Cassava_healthy','Cassava_mosaic_disease']
|
| 100 |
+
predicted_class, class_name = predict_image(img, cassava_model, class_names)
|
| 101 |
|
| 102 |
+
# Display prediction
|
| 103 |
st.write(f"Predicted Class: {class_name}")
|
| 104 |
+
|
| 105 |
+
# Recommendations based on prediction
|
| 106 |
+
recommendations = {
|
| 107 |
+
'Blight': "Remove infected plants, rotate crops, apply fungicides.",
|
| 108 |
+
'Common_Rust': "Use resistant varieties, apply fungicides early.",
|
| 109 |
+
'Gray_Leaf_Spot': "Monitor fields, apply fungicides, manage nitrogen.",
|
| 110 |
+
'Cassava_bacterial_blight': "Remove infected plants, sterilize tools.",
|
| 111 |
+
'Cassava_brown_streak_disease': "Plant resistant varieties, manage whiteflies.",
|
| 112 |
+
'Cassava_green_mottle': "Use resistant varieties, control weeds.",
|
| 113 |
+
'Cassava_mosaic_disease': "Plant resistant varieties, ensure hygiene.",
|
| 114 |
+
'Healthy': "The leaf is healthy. No action needed.",
|
| 115 |
+
'Cassava_healthy': "The leaf is healthy. No action needed."
|
| 116 |
+
}
|
| 117 |
+
if class_name in recommendations:
|
| 118 |
+
st.warning(recommendations[class_name])
|
| 119 |
|
| 120 |
# Main app
|
| 121 |
def main():
|