forked from JackJJCodes/AgriGenie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
173 lines (140 loc) · 6.24 KB
/
app.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from flask import Flask, request, render_template, Markup
import pickle
import numpy as np
import pandas as pd
from utils.disease import disease_dic
from utils.model import ResNet9
from PIL import Image
import io
import torch
from torchvision import transforms
# Initializing our flask application:
app = Flask(__name__)
# Loading our disease classification model:
disease_classes = ['Apple___Apple_scab',
'Apple___Black_rot',
'Apple___Cedar_apple_rust',
'Apple___healthy',
'Blueberry___healthy',
'Cherry_(including_sour)___Powdery_mildew',
'Cherry_(including_sour)___healthy',
'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
'Corn_(maize)___Common_rust_',
'Corn_(maize)___Northern_Leaf_Blight',
'Corn_(maize)___healthy',
'Grape___Black_rot',
'Grape___Esca_(Black_Measles)',
'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
'Grape___healthy',
'Orange___Haunglongbing_(Citrus_greening)',
'Peach___Bacterial_spot',
'Peach___healthy',
'Pepper,_bell___Bacterial_spot',
'Pepper,_bell___healthy',
'Potato___Early_blight',
'Potato___Late_blight',
'Potato___healthy',
'Raspberry___healthy',
'Soybean___healthy',
'Squash___Powdery_mildew',
'Strawberry___Leaf_scorch',
'Strawberry___healthy',
'Tomato___Bacterial_spot',
'Tomato___Early_blight',
'Tomato___Late_blight',
'Tomato___Leaf_Mold',
'Tomato___Septoria_leaf_spot',
'Tomato___Spider_mites Two-spotted_spider_mite',
'Tomato___Target_Spot',
'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
'Tomato___Tomato_mosaic_virus',
'Tomato___healthy']
disease_model_path = 'Models/plant_disease_model.pth'
disease_model = ResNet9(3, len(disease_classes))
disease_model.load_state_dict(torch.load(
disease_model_path, map_location=torch.device('cpu')))
disease_model.eval()
# Loading our model:
model = pickle.load(open("Models/RFmodel.pkl", "rb"))
# Creating a function to predict image:
def predict_image(img, model = disease_model):
"""
:params: image
:return: prediction
"""
transform = transforms.Compose([
transforms.Resize(256),
transforms.ToTensor(),
])
image = Image.open(io.BytesIO(img))
img_t = transform(image)
img_u = torch.unsqueeze(img_t, 0)
# Get predictions from model
yb = model(img_u)
# Pick index with highest probability
_, preds = torch.max(yb, dim=1)
prediction = disease_classes[preds[0].item()]
# Retrieve the class label
return prediction
@app.route("/")
def home():
return render_template("home.html")
@app.route("/crop-recommend")
def crop_recommend():
return render_template('crop-recommend.html')
@app.route("/crop-predict", methods=["GET", "POST"])
def crop_prediction():
if request.method == "POST":
# Nitrogen
nitrogen = float(request.form["nitrogen"])
# Phosphorus
phosphorus = float(request.form["phosphorus"])
# Potassium
potassium = float(request.form["potassium"])
# Temperature
temperature = float(request.form["temperature"])
# Humidity Level
humidity = float(request.form["humidity"])
# PH level
phLevel = float(request.form["ph-level"])
# Rainfall
rainfall = float(request.form["rainfall"])
# Making predictions from the values:
predictions = model.predict([[nitrogen, phosphorus, potassium, temperature, humidity, phLevel, rainfall]])
output = predictions[0]
finalOutput = output.capitalize()
if (output == "rice" or output == "blackgram" or output == "pomegranate" or output == "papaya"
or output == "cotton" or output == "orange" or output == "coffee" or output == "chickpea"
or output == "mothbeans" or output == "pigeonpeas" or output == "jute" or output == "mungbeans"
or output == "lentil" or output == "maize" or output == "apple"):
cropStatement = finalOutput + " should be harvested. It's a Kharif crop, so it must be sown at the beginning of the rainy season e.g between April and May."
elif (
output == "muskmelon" or output == "kidneybeans" or output == "coconut" or output == "grapes" or output == "banana"):
cropStatement = finalOutput + "should be harvested. It's a Rabi crop, so it must be sown at the end of " \
"monsoon and beginning of winter season e.g between September and October. "
elif output == "watermelon":
cropStatement = finalOutput + "should be harvested. It's a Zaid Crop, so it must be sown between the " \
"Kharif and rabi season i.e between March and June. "
elif (output == "mango"):
cropStatement = finalOutput + "should be harvested. It's a cash crop and also perennial. So you can grow " \
"it anytime. "
return render_template('cropResult.html', prediction_text=cropStatement)
@app.route("/disease-predict", methods = ['GET', 'POST'])
def disease_predict():
title = 'Harvestify - Disease Detection'
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files.get('file')
if not file:
return render_template('disease.html', title=title)
try:
img = file.read()
prediction = predict_image(img)
prediction = Markup(str(disease_dic[prediction]))
return render_template('disease-result.html', prediction=prediction, title=title)
except:
pass
return render_template('disease.html', title=title)
if __name__ == '__main__':
app.run(debug=True)