Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,24 @@ from tensorflow.keras.preprocessing import image
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
cassava_model = load_model('cassava_disease_model.h5')
|
| 23 |
-
|
| 24 |
-
# Function to predict an uploaded image
|
| 25 |
-
def predict_image(img, model, class_names):
|
| 26 |
img = image.img_to_array(img)
|
| 27 |
img = np.expand_dims(img, axis=0)
|
| 28 |
-
img = img / 255.0 # Normalize
|
| 29 |
prediction = model.predict(img)
|
| 30 |
predicted_class = np.argmax(prediction)
|
| 31 |
return predicted_class, class_names[predicted_class]
|
|
@@ -35,84 +31,66 @@ def home_page():
|
|
| 35 |
st.title("Maize and Cassava Crop Disease Identification")
|
| 36 |
st.image("1093908406.jpg", use_column_width=True)
|
| 37 |
st.write("""
|
| 38 |
-
Welcome to the Crop Disease Identification App. This tool
|
| 39 |
-
|
| 40 |
-
Use the navigation menu to explore the app:
|
| 41 |
-
- **Home:** Overview of the app
|
| 42 |
-
- **About:** Information about the app and its purpose
|
| 43 |
-
- **Prediction:** Upload an image and get a disease diagnosis along with recommendations.
|
| 44 |
""")
|
| 45 |
|
| 46 |
def about_page():
|
| 47 |
st.title("About")
|
| 48 |
st.image("IMG_20240727_143208_444.jpg", use_column_width=True)
|
| 49 |
st.write("""
|
| 50 |
-
This application
|
| 51 |
-
|
| 52 |
-
Diseases include:
|
| 53 |
-
- Maize: Blight, Common Rust, Gray Leaf Spot
|
| 54 |
-
- Cassava: Bacterial Blight, Green Mottle, Brown Streak, Mosaic
|
| 55 |
-
|
| 56 |
-
For questions or feedback, email: olorunnisholato7@gmail.com
|
| 57 |
""")
|
| 58 |
|
| 59 |
def prediction_page():
|
| 60 |
st.title("Crop Disease Prediction")
|
|
|
|
|
|
|
| 61 |
crop_type = st.selectbox("Select the crop:", ["Maize", "Cassava"])
|
|
|
|
|
|
|
| 62 |
uploaded_file = st.file_uploader("Upload an image of the leaf...", type=["jpg", "jpeg", "png"])
|
| 63 |
|
| 64 |
if uploaded_file is not None:
|
|
|
|
| 65 |
img = Image.open(uploaded_file)
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
img_array = np.expand_dims(image.img_to_array(img_resized) / 255.0, axis=0)
|
| 70 |
-
|
| 71 |
-
# Apply the filter model
|
| 72 |
-
filter_prediction = filter_model.predict(img_array)
|
| 73 |
-
filter_class = np.argmax(filter_prediction)
|
| 74 |
-
if filter_class == 0: # Assuming 0 is "Non-Leaf"
|
| 75 |
-
st.warning("The uploaded image does not appear to contain a leaf. Please try again.")
|
| 76 |
-
return
|
| 77 |
-
|
| 78 |
-
# Resize for crop-specific models
|
| 79 |
-
target_size = (128, 128) if crop_type == "Maize" else (224, 224)
|
| 80 |
-
img = img.resize(target_size)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
st.write("Classifying...")
|
| 87 |
if crop_type == "Maize":
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
else:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
'Cassava_green_mottle',
|
| 95 |
-
'Cassava_healthy',
|
| 96 |
-
'Cassava_mosaic_disease',
|
| 97 |
-
]
|
| 98 |
-
predicted_class, class_name = predict_image(img, cassava_model, class_names)
|
| 99 |
|
|
|
|
| 100 |
st.write(f"Predicted Class: {class_name}")
|
| 101 |
-
|
| 102 |
-
# Recommendations based on predictions
|
| 103 |
-
if class_name in ['Healthy', 'Cassava_healthy']:
|
| 104 |
st.success("The leaf is healthy. No action needed.")
|
| 105 |
else:
|
| 106 |
recommendations = {
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
}
|
| 115 |
-
st.warning(recommendations
|
| 116 |
|
| 117 |
# Main app
|
| 118 |
def main():
|
|
@@ -128,4 +106,3 @@ def main():
|
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
main()
|
| 131 |
-
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load the models
|
| 8 |
+
filter_model = load_model("model_checkpoint") # Replace with actual directory path
|
| 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 |
+
leaf_probability = prediction[0][1] # Assuming "Leaf" class is at index 1
|
| 18 |
+
return leaf_probability >= threshold, leaf_probability
|
| 19 |
+
|
| 20 |
+
# Function to predict crop disease
|
| 21 |
+
def predict_disease(img, model, class_names):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]
|
|
|
|
| 31 |
st.title("Maize and Cassava Crop Disease Identification")
|
| 32 |
st.image("1093908406.jpg", use_column_width=True)
|
| 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():
|
| 39 |
st.title("About")
|
| 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 |
+
The models were built using Convolutional Neural Networks (CNNs), trained on publicly available datasets.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
""")
|
| 45 |
|
| 46 |
def prediction_page():
|
| 47 |
st.title("Crop Disease Prediction")
|
| 48 |
+
|
| 49 |
+
# Select crop type
|
| 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 and display the uploaded image
|
| 57 |
img = Image.open(uploaded_file)
|
| 58 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 59 |
|
| 60 |
+
if st.button("Classify"):
|
| 61 |
+
st.write("Classifying...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Filter model prediction
|
| 64 |
+
is_leaf, leaf_probability = is_leaf_image(img, filter_model)
|
| 65 |
+
if not is_leaf:
|
| 66 |
+
st.warning(f"The uploaded image does not appear to contain a leaf. (Confidence: {leaf_probability:.2f})")
|
| 67 |
+
return
|
| 68 |
|
| 69 |
+
# Crop-specific model prediction
|
|
|
|
| 70 |
if crop_type == "Maize":
|
| 71 |
+
img_resized = img.resize((128, 128))
|
| 72 |
+
class_names = ["Blight", "Common_Rust", "Gray_Leaf_Spot", "Healthy"]
|
| 73 |
+
_, class_name = predict_disease(img_resized, corn_model, class_names)
|
| 74 |
else:
|
| 75 |
+
img_resized = img.resize((224, 224))
|
| 76 |
+
class_names = ["Cassava_bacterial_blight", "Cassava_brown_streak_disease", "Cassava_green_mottle", "Cassava_healthy", "Cassava_mosaic_disease"]
|
| 77 |
+
_, class_name = predict_disease(img_resized, cassava_model, class_names)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Display result
|
| 80 |
st.write(f"Predicted Class: {class_name}")
|
| 81 |
+
if "Healthy" in class_name:
|
|
|
|
|
|
|
| 82 |
st.success("The leaf is healthy. No action needed.")
|
| 83 |
else:
|
| 84 |
recommendations = {
|
| 85 |
+
"Blight": "Recommendation: Remove infected plants and apply fungicides.",
|
| 86 |
+
"Common_Rust": "Recommendation: Use resistant varieties and apply fungicides if necessary.",
|
| 87 |
+
"Gray_Leaf_Spot": "Recommendation: Ensure proper crop rotation and apply fungicides.",
|
| 88 |
+
"Cassava_bacterial_blight": "Recommendation: Remove infected plants and use disease-free planting material.",
|
| 89 |
+
"Cassava_brown_streak_disease": "Recommendation: Plant resistant cassava varieties.",
|
| 90 |
+
"Cassava_green_mottle": "Recommendation: Ensure good field sanitation.",
|
| 91 |
+
"Cassava_mosaic_disease": "Recommendation: Use resistant varieties and practice good hygiene."
|
| 92 |
}
|
| 93 |
+
st.warning(recommendations.get(class_name, "No specific recommendation available."))
|
| 94 |
|
| 95 |
# Main app
|
| 96 |
def main():
|
|
|
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
main()
|
|
|