-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
200 lines (181 loc) · 6.79 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
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
import os
from joblib import dump, load
import tensorflow as tf
import keras
from keras.models import load_model
import pandas as pd
import cv2
UPLOAD_FOLDER = './uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
tsunami_model = load('tsunami_model.joblib')
magnitude_model = load('magnitude_model.joblib')
putout_model = load('putout_model.joblib')
hurricane_model = load_model('hurricane-weights.h5')
wildfire_model = load_model('wildfire-weights.h5')
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html')
@app.route('/fires', methods=['GET', 'POST'])
def fires():
if request.method == 'POST':
if(request.form["type"] == "0"):
output = ""
try:
longitude = float(request.form["longitude"])
latitude = float(request.form["latitude"])
month = int(request.form["month"])
temperature = float(request.form["temperature"])
humidity = float(request.form["humidity"])
precipitation = float(request.form["precipitation"])
wind = float(request.form["wind"])
vegetation = int(request.form["vegetation"])
assert (longitude >= -90)
assert (longitude <= 90)
assert (latitude >= -180)
assert (latitude <= 180)
assert (month >= 1)
assert (month <= 12)
assert (humidity >= 0)
assert (humidity <= 100)
assert (precipitation >= 0)
assert (wind >= 0)
assert (vegetation >= 1)
assert (vegetation <= 28)
putout_data = {'longitude': [longitude], 'latitude': [longitude], 'discovery_month': [month], 'Vegetation': [vegetation], 'Temp_pre_7': [temperature], 'Hum_pre_7': [humidity], 'Prec_pre_7': [precipitation], 'Wind_pre_7': [wind]}
putout_dataf = pd.DataFrame(data=putout_data)
result = putout_model.predict(putout_dataf)[0]
if(result < 0):
result = 0
result = round(result, 2)
result = str(result)
if(result == "0"):
result = "0-1"
output = result + " days to put out the fire"
except Exception as e:
print(e)
output = "Invalid inputs!"
return render_template('fires.html', output=output)
else:
if 'image' not in request.files:
return render_template('fires.html', output="File not found! Please try re-uploading.")
f = request.files["image"]
if f.filename == '':
return render_template('fires.html', output="File not found! Please try re-uploading.")
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(filepath)
image = cv2.imread(filepath, cv2.IMREAD_COLOR)
image = cv2.resize(image, (300, 300))
image_tensor = tf.convert_to_tensor(image, dtype=tf.float32)
image_tensor = tf.expand_dims(image_tensor, 0)
result = wildfire_model.predict(image_tensor)[0][0]
result = result * 100
if(result < 0):
result = 0
if(result > 100):
result = 100
result = round(result, 2)
result = str(result)
output = result + "% chance of a wildfire"
return render_template('fires.html', output=output)
return render_template('fires.html', output="An unknown error occurred!")
else:
return render_template('fires.html', output="")
@app.route('/hurricanes', methods=['GET', 'POST'])
def hurricanes():
if request.method == 'POST':
if(request.form["type"] == "0"):
if 'image' not in request.files:
return render_template('hurricanes.html', output="File not found! Please try re-uploading.")
f = request.files["image"]
if f.filename == '':
return render_template('hurricanes.html', output="File not found! Please try re-uploading.")
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(filepath)
image = cv2.imread(filepath, cv2.IMREAD_COLOR)
image = cv2.resize(image, (300, 300))
image_tensor = tf.convert_to_tensor(image, dtype=tf.float32)
image_tensor = tf.expand_dims(image_tensor, 0)
result = hurricane_model.predict(image_tensor)[0][0]
print(result)
result = result * 100
if(result < 0):
result = 0
if(result > 100):
result = 100
result = round(result, 2)
result = str(result)
output = result + "% chance of a flood damage after a hurricane"
return render_template('hurricanes.html', output=output)
return render_template('hurricanes.html', output="An unknown error occurred!")
else:
return render_template('hurricanes.html', output="Coming soon!")
else:
return render_template('hurricanes.html', output="")
@app.route('/earthquakes', methods=['GET', 'POST'])
def earthquakes():
if request.method == 'POST':
print(request.form)
if(request.form["type"] == "0"):
output = ""
try:
longitude = float(request.form["longitude"])
latitude = float(request.form["latitude"])
assert (longitude >= -90)
assert (longitude <= 90)
assert (latitude >= -180)
assert (latitude <= 180)
mag_data = {'longitude': [longitude], 'latitude': [longitude]}
mag_dataf = pd.DataFrame(data=mag_data)
result = magnitude_model.predict(mag_dataf)[0]
if(result < 0):
result = 0
result = round(result, 2)
result = str(result)
output = result + " on the Richter scale"
except Exception as e:
print(e)
output = "Invalid inputs!"
return render_template('earthquakes.html', output=output)
else:
output = ""
try:
longitude = float(request.form["longitude"])
latitude = float(request.form["latitude"])
month = int(request.form["month"])
day = int(request.form["day"])
deaths = int(request.form["deaths"])
assert (longitude >= -90)
assert (longitude <= 90)
assert (latitude >= -180)
assert (latitude <= 180)
assert (month >= 1)
assert (month <= 12)
assert (day >= 1)
assert (day <= 31)
assert (deaths >= 0)
tsunami_data = {'LONGITUDE': [longitude], 'LATITUDE': [longitude], 'MONTH': [month], 'DAY': [day], 'DEATHS': [deaths]}
tsunami_dataf = pd.DataFrame(data=tsunami_data)
result = tsunami_model.predict(tsunami_dataf)[0] * 100
if(result < 0):
result = 0
if(result > 100):
result = 100
result = round(result, 2)
result = str(result)
output = result + "% chance of a tsunami"
except Exception as e:
print(e)
output = "Invalid inputs!"
return render_template('earthquakes.html', output=output)
else:
return render_template('earthquakes.html', output="")