sunbal7 commited on
Commit
abcd1c5
Β·
verified Β·
1 Parent(s): f373eda

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ # Treatment database (expand with your own data)
8
+ TREATMENTS = {
9
+ "Tomato_Early_Blight": {
10
+ "summary": "Early Blight (Alternaria solani)",
11
+ "treatment": [
12
+ "Remove infected leaves immediately",
13
+ "Apply chlorothalonil (1 tbsp/gallon) weekly",
14
+ "Improve air circulation around plants"
15
+ ]
16
+ },
17
+ "Potato_Late_Blight": {
18
+ "summary": "Late Blight (Phytophthora infestans)",
19
+ "treatment": [
20
+ "Destroy infected plants",
21
+ "Apply copper-based fungicides every 7 days",
22
+ "Avoid overhead watering"
23
+ ]
24
+ }
25
+ }
26
+
27
+ # Load pre-trained model (using open-source model from TF Hub)
28
+ @st.cache_resource
29
+ def load_model():
30
+ try:
31
+ model = tf.keras.models.load_model('model/plant_disease_model.h5')
32
+ except:
33
+ # Fallback to pre-trained EfficientNet
34
+ model = tf.keras.applications.EfficientNetB3(
35
+ input_shape=(256,256,3),
36
+ weights='imagenet',
37
+ include_top=False
38
+ )
39
+ model.trainable = False
40
+ model = tf.keras.Sequential([
41
+ model,
42
+ tf.keras.layers.GlobalAveragePooling2D(),
43
+ tf.keras.layers.Dense(38, activation='softmax')
44
+ ])
45
+ return model
46
+
47
+ def preprocess_image(image):
48
+ img = Image.open(image).convert('RGB')
49
+ img = img.resize((256, 256))
50
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
51
+ img_array = tf.expand_dims(img_array, 0) / 255.0
52
+ return img_array
53
+
54
+ def main():
55
+ st.title("🌱 Plant Disease Detection & Treatment System")
56
+ st.markdown("Upload a leaf image for disease diagnosis and treatment recommendations")
57
+
58
+ uploaded_file = st.file_uploader("Choose a leaf image...", type=["jpg", "jpeg", "png"])
59
+
60
+ if uploaded_file is not None:
61
+ # Display image
62
+ st.image(uploaded_file, caption='Uploaded Leaf Image', width=300)
63
+
64
+ # Preprocess and predict
65
+ img_array = preprocess_image(uploaded_file)
66
+ model = load_model()
67
+ predictions = model.predict(img_array)
68
+ predicted_class = np.argmax(predictions[0])
69
+
70
+ # Get class name (replace with your actual class names)
71
+ class_names = ["Tomato_Early_Blight", "Potato_Late_Blight"] # Add all 38 classes
72
+
73
+ # Display results
74
+ st.subheader("πŸ” Diagnosis Summary")
75
+ disease = class_names[predicted_class]
76
+ st.success(f"Detected Disease: **{disease.replace('_', ' ')}**")
77
+ st.write(f"Confidence: {predictions[0][predicted_class]*100:.2f}%")
78
+
79
+ # Show treatment
80
+ st.subheader("πŸ’Š Recommended Treatment")
81
+ if disease in TREATMENTS:
82
+ st.markdown(f"**{TREATMENTS[disease]['summary']}**")
83
+ for step in TREATMENTS[disease]["treatment"]:
84
+ st.markdown(f"- {step}")
85
+ else:
86
+ st.warning("Treatment information not found for this disease")
87
+
88
+ if __name__ == "__main__":
89
+ main()