-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
159 lines (148 loc) · 4.68 KB
/
app.py
File metadata and controls
159 lines (148 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""---------------- REMEDIES ----------------"""
REMEDIES = {
"Potato___healthy": {
"description": "Healthy potato plant",
"remedies": [
"No action needed",
"Maintain good growing conditions"
]
},
"Potato___Early_blight": {
"description": "Fungal disease causing dark spots on leaves",
"remedies": [
"Remove and destroy infected leaves",
"Apply copper-based fungicides",
"Practice crop rotation"
]
},
"Tomato_Septoria_leaf_spot": {
"description": "Fungal disease causing small dark spots with light centers",
"remedies": [
"Remove affected leaves",
"Use fungicides like chlorothalonil",
"Avoid overhead watering"
]
},
"Tomato__Tomato_mosaic_virus": {
"description": "Viral mosaic-pattern disease",
"remedies": [
"Destroy infected plants",
"Control aphids",
"Use resistant varieties"
]
},
"Pepper__bell___healthy": {
"description": "Healthy bell pepper plant",
"remedies": ["No action needed"]
},
"Tomato_Spider_mites_Two_spotted_spider_mite": {
"description": "Spider mites causing yellowing and webbing",
"remedies": [
"Use neem oil",
"Increase humidity",
"Introduce predatory mites"
]
},
"Tomato__Target_Spot": {
"description": "Brown spots with yellow halos",
"remedies": [
"Remove infected leaves",
"Apply copper fungicides",
"Improve ventilation"
]
},
"Tomato_healthy": {
"description": "Healthy tomato plant",
"remedies": ["No action needed"]
},
"Pepper__bell___Bacterial_spot": {
"description": "Bacterial disease with water-soaked spots",
"remedies": [
"Remove infected leaves",
"Use copper sprays",
"Choose resistant varieties"
]
},
"Tomato_Late_blight": {
"description": "Severe fungal disease with brown lesions",
"remedies": [
"Destroy infected plants",
"Spray chlorothalonil fungicide",
"Keep area dry"
]
},
"Potato___Late_blight": {
"description": "Late blight affecting potato leaves and tubers",
"remedies": [
"Use resistant varieties",
"Apply fungicides",
"Avoid excess moisture"
]
},
"Tomato_Early_blight": {
"description": "Dark concentric rings on leaves",
"remedies": [
"Use copper fungicides",
"Remove affected leaves",
"Mulch to reduce soil splash"
]
},
"Tomato__Tomato_YellowLeaf__Curl_Virus": {
"description": "Virus causing curling and yellowing",
"remedies": [
"Destroy infected plants",
"Control whiteflies",
"Use resistant varieties"
]
},
"Tomato_Bacterial_spot": {
"description": "Water-soaked bacterial spots",
"remedies": [
"Remove infected plants",
"Apply copper spray",
"Use clean seeds"
]
},
"Tomato_Leaf_Mold": {
"description": "Yellowing + mold on leaves",
"remedies": [
"Improve ventilation",
"Apply copper spray",
"Avoid overhead watering"
]
}
}
"""--------------- PREDICTION ---------------"""
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
# Load the trained model
model = load_model("/content/drive/MyDrive/best_model.h5")
# Load class indices for mapping
class_indices = np.load('class_indices.npy', allow_pickle=True).item()
idx_to_class = {v: k for k, v in class_indices.items()}
def predict_image(img_path):
# Preprocess
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Predict
predictions = model.predict(img_array)
class_id = np.argmax(predictions)
confidence = np.max(predictions)
class_name = idx_to_class[class_id]
# Remedies fallback
info = REMEDIES.get(class_name, {
"description": "No description available",
"remedies": ["Consult an agricultural specialist"]
})
# Show image + prediction
plt.imshow(img)
plt.title(f"{class_name} ({confidence:.2%})")
plt.axis("off")
plt.show()
print("\nDisease:", class_name)
print("Description:", info["description"])
print("\nRecommended Remedies:")
for i, r in enumerate(info["remedies"], 1):
print(f"{i}. {r}")