forked from ShreyaGarg01/SmartGram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
146 lines (120 loc) · 4.7 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
from flask import Flask, render_template, request, Markup
import numpy as np
import requests
import pickle
import io
from PIL import ImageOps
from PIL import Image
# from werkzeug.datastructure import FileStorage
from werkzeug.utils import secure_filename
from keras.models import load_model
import h5py
import cv2
from io import BytesIO
import base64
# Trained Models loaded
crop_recommendation_model_path = 'models/RandomForest.pkl'
crop_recommendation_model = pickle.load(
open(crop_recommendation_model_path, 'rb'))
# Trained Models loaded
plant_pathology_model_path = 'models/PlantPathology.hdf5'
plant_pathology_model = load_model(plant_pathology_model_path)
# Trained Models loaded
crop_yield_model_path = 'models/crop_yield.pkl'
crop_yield_model = pickle.load(
open(crop_yield_model_path, 'rb'))
# FLASK APP
app = Flask(__name__)
# render home page
@ app.route('/')
def home():
title = 'Home'
return render_template('index.html', title=title)
@ app.route('/home')
def start():
title = 'Home'
return render_template('index.html', title=title)
# render crop recommendation form page
@ app.route('/crop-recommend')
def crop_recommend():
title = 'Crop Recommendation'
return render_template('crop_recommendation.html', title=title)
# render crop recommendation form page
@ app.route('/crop-yield')
def crop_yield():
title = 'Crop Yield'
return render_template('crop_yield.html', title=title)
# render crop recommendation form page
@ app.route('/plant-pathology')
def plant_pathology():
title = 'Plant Pathology'
return render_template('plant_pathology.html', title=title)
# render crop recommendation form page
@ app.route('/weather-forecast')
def weather_forecast():
title = 'Weather Forecast'
# return render_template('weather_predication.html', title=title)
return render_template('weather_forecast.html', title=title)
# RENDER PREDICTION PAGES
@ app.route('/crop-predict', methods=['POST'])
def crop_prediction():
title = 'Crop Recommendation'
if request.method == 'POST':
N = int(request.form['nitrogen'])
P = int(request.form['phosphorous'])
K = int(request.form['pottasium'])
ph = float(request.form['ph'])
rainfall = float(request.form['rainfall'])
temperature = float(request.form['temperature'])
humidity = float(request.form['humidity'])
data = np.array([[N, P, K, temperature, humidity, ph, rainfall]])
my_prediction = crop_recommendation_model.predict(data)
final_prediction = my_prediction[0]
return render_template('crop_prediction.html', prediction=final_prediction, title=title)
@ app.route('/crop-yield', methods=['GET', 'POST'])
def crop_yield_prediction():
title = 'Crop Yield'
if request.method == 'POST':
Year = int(request.form['Year'])
average_rain_fall_mm_per_year = int(request.form['average_rain_fall_mm_per_year'])
pesticides_tonnes = int(request.form['pesticides_tonnes'])
temperature = float(request.form['temperature'])
data = np.array([[Year, average_rain_fall_mm_per_year, pesticides_tonnes, temperature]])
my_prediction = crop_yield_model.predict(data)
final_prediction = my_prediction[0]
return render_template('crop_yield.html', prediction=final_prediction, title=title)
# main API code
@app.route('/plant-pathology', methods=['GET', 'POST'])
def pathology():
title = 'Plant Pathology'
if request.method == 'POST':
file = request.files['file']
filename = secure_filename(file.filename)
print(filename)
img = Image.open(file.stream)
# with BytesIO() as buf:
# img.save(buf, 'jpeg')
# image_bytes = buf.getvalue()
# encoded_string = base64.b64encode(image_bytes).decode()
image_data = img
size = (128, 128)
image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
image = np.asarray(image)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img_resize = (cv2.resize(img, dsize=(128, 128), interpolation=cv2.INTER_CUBIC))/255.
data = img_resize[np.newaxis,...]
my_prediction = plant_pathology_model.predict(data)
final_prediction = ""
max_idx = np.argmax(my_prediction)
if max_idx == 0:
final_prediction = "is Healthy!"
elif max_idx == 1:
final_prediction = "has Multiple Diseases!"
elif max_idx == 2:
final_prediction = "has Rust!"
else:
final_prediction = "has Scab!"
print(my_prediction)
return render_template('plant_pathology.html', prediction=final_prediction, title=title)
if __name__ == '__main__':
app.run(debug = True)