Ten7 commited on
Commit
1a93018
·
verified ·
1 Parent(s): 8290e98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -47
app.py CHANGED
@@ -5,27 +5,9 @@ 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('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)
@@ -38,33 +20,38 @@ def predict_image(img, model, class_names):
38
  # Define pages
39
  def home_page():
40
  st.title("Maize and Cassava Crop Disease Identification")
 
41
  st.image("1093908406.jpg", use_column_width=True)
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():
53
  st.title("About")
 
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")
@@ -78,43 +65,46 @@ def prediction_page():
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
@@ -130,4 +120,4 @@ def main():
130
  prediction_page()
131
 
132
  if __name__ == "__main__":
133
- main()
 
5
  from PIL import Image
6
 
7
  # Load the models
 
8
  corn_model = load_model('corn_model_mobilenetv2.h5')
9
  cassava_model = load_model('cassava_disease_model.h5')
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Function to predict an uploaded image
12
  def predict_image(img, model, class_names):
13
  img = image.img_to_array(img)
 
20
  # Define pages
21
  def home_page():
22
  st.title("Maize and Cassava Crop Disease Identification")
23
+ # Add an image to the home page
24
  st.image("1093908406.jpg", use_column_width=True)
25
  st.write("""
26
  Welcome to the Crop Disease Identification App. This tool aims at helping local farmers
27
  identify diseases in maize and cassava crops by analyzing images of leaves.
28
+
29
  Use the navigation menu to explore the app:
30
  - **Home:** Overview of the app
31
  - **About:** Information about the app and its purpose
32
  - **Prediction:** Upload an image and get a disease diagnosis along with recommendations
33
  """)
34
+
35
 
36
  def about_page():
37
  st.title("About")
38
+ # Add an image to the about page
39
  st.image("IMG_20240727_143208_444.jpg", use_column_width=True)
40
  st.write("""
41
  This application is designed to assist farmers in diagnosing common diseases in maize and cassava crops.
42
  By utilizing deep learning models trained on thousands of leaf images, the app can accurately identify
43
  diseases such as "blight, common rust and gray leaf spot" in maize and "Bacterial blight, green mottle, brown streak and mosaic" in cassava.
44
+ The models were built using Convolutional Neural Networks (CNNs), trained on publicly available datasets and tested on locally obtained images..
45
  The goal is to provide quick and accurate disease identification to help mitigate crop losses and improve yields.
46
+
47
  If you have any questions or feedback, please reach out to the developer.
48
+
49
+
50
  E-mail: olorunnisholato7@gmail.com
51
+
52
  Thank you for using the Crop Disease Identification App!
53
  """)
54
+
55
 
56
  def prediction_page():
57
  st.title("Crop Disease Prediction")
 
65
  if uploaded_file is not None:
66
  # Load the uploaded image
67
  img = Image.open(uploaded_file)
68
+ # Set image size based on crop type
69
+ if crop_type == "Maize":
70
+ img = img.resize((128, 128)) # Resize for Maize
71
+ target_size = (128, 128)
72
+ else:
73
+ img = img.resize((224, 224)) # Resize for Cassava
74
+ target_size = (224, 224)
75
+
76
+ # Display the uploaded image
77
  st.image(img, caption='Uploaded Image', use_column_width=True)
78
  st.write("")
79
 
80
+ # Predict button
81
+ if st.button('Predict'):
82
+ st.write("Classifying...")
83
 
84
+ # Predict based on the selected crop type
 
 
 
 
 
85
  if crop_type == "Maize":
 
86
  class_names = ['Blight', 'Common_Rust', 'Gray_Leaf_Spot', 'Healthy']
87
  predicted_class, class_name = predict_image(img, corn_model, class_names)
88
  else:
 
89
  class_names = ['Cassava_bacterial_blight','Cassava_brown_streak_disease','Cassava_green_mottle','Cassava_healthy','Cassava_mosaic_disease']
90
  predicted_class, class_name = predict_image(img, cassava_model, class_names)
91
 
92
  # Display prediction
93
  st.write(f"Predicted Class: {class_name}")
94
 
95
+ # Recommendation based on the predicted class
96
+ if class_name == 'Healthy' or class_name == 'Cassava_healthy':
97
+ st.success("The leaf is healthy. No action needed.")
98
+ else:
99
+ recommendations = {
100
+ 'Blight': "Recommendation: Immediately remove and destroy infected plants to prevent the spread of the disease. Implement a rotation plan that avoids planting corn for at least two years in affected fields. Apply a fungicide that is effective against blight, such as those containing chlorothalonil or mancozeb, at the early signs of disease.Consider planting blight-resistant corn varieties in future planting seasons.",
101
+ 'Common_Rust': "Recommendation: Use resistant varieties and apply fungicides if necessary. If rust is detected early, apply a fungicide like azoxystrobin or tebuconazole to control the spread. Clear out any crop debris from previous seasons, as rust spores can survive in old plant material. Avoid overhead irrigation to reduce leaf wetness, which can promote rust development.",
102
+ 'Gray_Leaf_Spot': "Recommendation: Ensure proper crop rotation and apply fungicides. Regularly monitor fields during humid conditions, as Gray Leaf Spot thrives in such environments. Use fungicides like pyraclostrobin or propiconazole at the onset of symptoms. Ensure balanced nitrogen levels; excessive nitrogen can exacerbate Gray Leaf Spot. Consider tillage practices that bury crop residues, reducing the amount of fungal spores in the soil.",
103
+ 'Cassava_bacterial_blight': "Recommendation: Use disease-free planting material and remove affected plants. Remove and destroy infected plants to prevent the spread. Avoid excessive irrigation and ensure proper drainage to reduce waterlogging, which can exacerbate CBB. Sterilize tools and equipment to avoid spreading bacteria from infected plants.",
104
+ 'Cassava_brown_streak_disease': "Recommendation: Plant CBSD-resistant cassava varieties, such as those developed through breeding programs. Consider harvesting earlier than usual to reduce yield loss if CBSD symptoms are present. Manage whiteflies, the primary vector for CBSD, by using insecticides or planting barrier crops. Isolate infected fields from healthy ones to prevent the spread of the virus",
105
+ 'Cassava_green_mottle': "Recommendation: Ensure good field sanitation and use resistant varieties. Select varieties known for resistance to CGM. Control weeds that may serve as hosts for the CGM virus.",
106
+ 'Cassava_mosaic_disease': "Recommendation: Use resistant varieties and practice good field hygiene."
107
+ }
108
  st.warning(recommendations[class_name])
109
 
110
  # Main app
 
120
  prediction_page()
121
 
122
  if __name__ == "__main__":
123
+ main()