-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
247 lines (196 loc) · 8.03 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from flask import Flask, render_template, request
from joblib import dump, load
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
import pandas as pd
import os
import xgboost as xgb
from time import sleep
import logging
import numpy as np
import geopandas as gdp
import folium
app = Flask(__name__)
# app.logger.disabled = True
# log = logging.getLogger("werkzeug")
# log.disabled = True
fire_data = pd.read_csv("Data/WildfireData.csv", na_values="NaN")
svm = load('Weights/svm.joblib')
elnt = load('Weights/elnt.joblib')
#model_xgb = xgb.Booster()
# model_xgb.load_model('Weights/xgb.json')
putout_model = load_model('Weights/putout.h5')
cause_model = load_model('Weights/cause.h5')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/detect')
def about():
return render_template('detect.html')
@app.route('/localize', methods=['GET', 'POST'])
def localize():
if(request.method == 'POST'):
output = "/static/assets/img/error.png"
sleep(2)
return render_template('localize.html', output=output)
else:
return render_template('localize.html', output="")
@app.route('/predict', methods=['GET', 'POST'])
def predict():
if(request.method == 'POST'):
output = "Unknown error"
try:
show_map = False
latitude = float(request.form.get("latitude"))
longitude = float(request.form.get("longitude"))
month = int(request.form.get("month"))
remoteness = float(request.form.get("remoteness"))
temperature = float(request.form.get("temperature"))
wind = float(request.form.get("wind"))
humidity = float(request.form.get("humidity"))
precipitation = float(request.form.get("precipitation"))
vegetation = int(request.form.get("vegetation"))
assert (latitude >= -90)
assert (latitude <= 90)
assert (longitude >= -180)
assert (longitude <= 180)
assert (month >= 1)
assert (month <= 12)
assert (remoteness >= 0)
assert (temperature >= -274)
assert (wind >= 0)
assert (humidity >= 0)
assert (humidity <= 100)
assert (precipitation >= 0)
assert (vegetation == 0 or vegetation == 4 or vegetation == 9 or vegetation ==
12 or vegetation == 14 or vegetation == 15 or vegetation == 16)
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
month = months[month - 1]
# Create data
columns = ['latitude', 'longitude', 'discovery_month',
'Vegetation', 'Temp_pre_7', 'Hum_pre_7', 'Prec_pre_7', 'Wind_pre_7']
mag_data = fire_data[columns]
mag_data = mag_data.dropna()
X_data = {
'latitude': [latitude],
'longitude': [longitude],
'discovery_month': [month],
'Vegetation': [vegetation],
'Temp_pre_7': [temperature],
'Hum_pre_7': [humidity],
'Prec_pre_7': [precipitation],
'Wind_pre_7': [wind]
}
X_dataf = pd.DataFrame(data=X_data)
X_dataf = X_dataf.append(mag_data)
# One Hot Encodings
non_dummy_cols = ['latitude', 'longitude',
'Temp_pre_7', 'Hum_pre_7', 'Prec_pre_7', 'Wind_pre_7']
dummy_cols = list(set(X_dataf.columns) - set(non_dummy_cols))
X_dataf = pd.get_dummies(X_dataf, columns=dummy_cols)
X_dataf = X_dataf.iloc[:1]
y_elastic = elnt.predict(X_dataf)
y_elastic = y_elastic[0]
# y_svm = svm.predict(X_dataf)
# print(y_svm[0])
#y_xgb = xgb.predict(X_dataf)
#result = (y_svm + y_xgb + y_elastic)/3
#result = (y_svm + y_elastic) / 2
result = y_elastic
# result is the final answer in acres (fire burn area)
# fire size: 'latitude', 'longitude', 'discovery_month', 'Vegetation', 'Temp_pre_7', 'Hum_pre_7', 'Prec_pre_7', 'Wind_pre_7'
# putout: 'fire_size', 'remoteness', 'discovery_month', 'Vegetation'
result_rounded = round(result)
if(result_rounded > 1000000):
result_rounded = 1000000
if(result_rounded <= 10):
result_rounded = 10
result_float = float(result_rounded)
# Create data
columns1 = ['fire_size', 'discovery_month',
'Vegetation', 'remoteness']
putout_data = fire_data[columns1]
putout_data = putout_data.dropna()
X_data1 = {
'fire_size': [result_float],
'discovery_month': [month],
'Vegetation': [vegetation],
'remoteness': [remoteness]
}
X_dataf1 = pd.DataFrame(data=X_data1)
X_dataf1 = X_dataf1.append(putout_data)
# One Hot Encodings
non_dummy_cols1 = ['fire_size', 'remoteness']
dummy_cols1 = list(set(X_dataf1.columns) - set(non_dummy_cols1))
X_dataf1 = pd.get_dummies(X_dataf1, columns=dummy_cols1)
X_dataf1 = X_dataf1.iloc[:1]
# Predict
result1 = putout_model.predict(X_dataf1)[0][0]
result1_rounded = round(result1)
if(result1_rounded > 365):
result1_rounded = 365
if(result1_rounded <= 0):
result1_rounded = 0
cause_data = {
'fire_size': [result_float],
'remoteness': [remoteness],
'putout_time': [result1]
}
cause_data = pd.DataFrame(cause_data)
result3 = cause_model.predict(cause_data)
final = 0
for i in range(6):
if (result3[0][i] > final):
final = result3[0][i]
# print(final)
final = np.where(result3[0] == final)
final = final[0][0]
print(final)
if (final == 0):
cause = "Debris Burning"
elif (final == 1):
cause = "Arson"
elif (final ==2):
cause = "Lightning"
elif (final == 3):
cause = "Equipment Use"
elif (final == 4):
cause = "Campfire"
else:
cause = "Other"
output = f"Burn Area: {result_rounded} Acres\nPutout Time: {result1_rounded} Days\nCause: {cause}"
m = folium.Map(location = [latitude, longitude],
zoom_start = 12)
map_pred = folium.FeatureGroup(name = 'Visualized Predicted Fire')
folium.CircleMarker(location = [latitude, longitude],
radius = result_rounded/6,
weight = 0,
color = '#fc4e2a',
fill_color = '#fc4e2a',
fill_opacity = 0.7,
fill = True).add_to(map_pred)
map_pred.add_to(m)
folium.LayerControl(collapsed = False).add_to(m)
m.save(r'templates/predicted-map.html')
except Exception as e:
print(e)
output = "Invalid parameters"
return render_template('predict.html', output=output, show_map = True)
else:
return render_template('predict.html', output="", show_map = False)
@app.route('/visualize')
def portfolio():
return render_template('visualize.html')
@app.route('/visualize-map')
def visualize_map():
return render_template('visualize-map.html')
@app.route('/visualize-map2')
def visualize_map2():
return render_template('visualize-map2.html')
@app.route('/predicted_map')
def predicted_map():
return render_template('predicted-map.html')
if __name__ == "__main__":
app.run(debug=True)