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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 5 |
+
import subprocess
|
| 6 |
+
import atexit
|
| 7 |
+
|
| 8 |
+
from flask import Flask, render_template, request, jsonify
|
| 9 |
+
|
| 10 |
+
from satellite_functions import satellite_cnn_predict
|
| 11 |
+
from camera_functions import camera_cnn_predict
|
| 12 |
+
from meteorological_functions import weather_data_predict
|
| 13 |
+
|
| 14 |
+
import sqlite3
|
| 15 |
+
|
| 16 |
+
# Load environment variables from .env file
|
| 17 |
+
load_dotenv()
|
| 18 |
+
MAPBOX_TOKEN = os.getenv("MAPBOX_TOKEN")
|
| 19 |
+
|
| 20 |
+
# Create a database and alerts table if not exists
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def init_db():
|
| 24 |
+
conn = sqlite3.connect("alerts.db")
|
| 25 |
+
cursor = conn.cursor()
|
| 26 |
+
cursor.execute(
|
| 27 |
+
"""
|
| 28 |
+
CREATE TABLE IF NOT EXISTS alerts (
|
| 29 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 30 |
+
email TEXT NOT NULL,
|
| 31 |
+
latitude REAL NOT NULL,
|
| 32 |
+
longitude REAL NOT NULL
|
| 33 |
+
)
|
| 34 |
+
"""
|
| 35 |
+
)
|
| 36 |
+
conn.commit()
|
| 37 |
+
conn.close()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Function that runs email_alert.py script
|
| 41 |
+
def run_alert_script():
|
| 42 |
+
script_path = os.path.join(os.path.dirname(__file__), "email_alert.py")
|
| 43 |
+
try:
|
| 44 |
+
subprocess.run(["python", script_path], check=True)
|
| 45 |
+
print("Processing script executed successfully.")
|
| 46 |
+
except subprocess.CalledProcessError as e:
|
| 47 |
+
print(f"Error running processing script: {e}")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Initialize the scheduler
|
| 51 |
+
scheduler = BackgroundScheduler()
|
| 52 |
+
scheduler.add_job(func=run_alert_script, trigger="interval", hours=1)
|
| 53 |
+
scheduler.start()
|
| 54 |
+
|
| 55 |
+
# Ensure the scheduler is shut down properly on exit
|
| 56 |
+
atexit.register(lambda: scheduler.shutdown())
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
app = Flask(__name__)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.route("/")
|
| 63 |
+
def home():
|
| 64 |
+
return render_template("home.html")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@app.route("/detect/camera")
|
| 68 |
+
def detect_camera():
|
| 69 |
+
return render_template("detect_camera.html")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@app.route("/detect/satellite")
|
| 73 |
+
def detect_satellite():
|
| 74 |
+
return render_template("detect_satellite.html", MAPBOX_TOKEN=MAPBOX_TOKEN)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@app.route("/alert", methods=["GET", "POST"])
|
| 78 |
+
def alert():
|
| 79 |
+
if request.method == "GET":
|
| 80 |
+
return render_template("alert.html", MAPBOX_TOKEN=MAPBOX_TOKEN)
|
| 81 |
+
|
| 82 |
+
if request.method == "POST":
|
| 83 |
+
data = request.json
|
| 84 |
+
email = data.get("email")
|
| 85 |
+
latitude = data.get("latitude")
|
| 86 |
+
longitude = data.get("longitude")
|
| 87 |
+
|
| 88 |
+
# Check for missing or invalid coordinates or email
|
| 89 |
+
if not email or latitude == 0 or longitude == 0:
|
| 90 |
+
return (
|
| 91 |
+
jsonify(
|
| 92 |
+
{
|
| 93 |
+
"success": False,
|
| 94 |
+
"message": "Invalid coordinates or missing information.",
|
| 95 |
+
}
|
| 96 |
+
),
|
| 97 |
+
400,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
conn = sqlite3.connect("alerts.db")
|
| 102 |
+
cursor = conn.cursor()
|
| 103 |
+
|
| 104 |
+
# Check if the email is already in the database
|
| 105 |
+
cursor.execute("SELECT * FROM alerts WHERE email=?", (email,))
|
| 106 |
+
existing_alert = cursor.fetchone()
|
| 107 |
+
if existing_alert:
|
| 108 |
+
return (
|
| 109 |
+
jsonify(
|
| 110 |
+
{
|
| 111 |
+
"success": False,
|
| 112 |
+
"message": "This email is already subscribed to alerts.",
|
| 113 |
+
}
|
| 114 |
+
),
|
| 115 |
+
400,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Insert the new email
|
| 119 |
+
cursor.execute(
|
| 120 |
+
"INSERT INTO alerts (email, latitude, longitude) VALUES (?, ?, ?)",
|
| 121 |
+
(email, latitude, longitude),
|
| 122 |
+
)
|
| 123 |
+
conn.commit()
|
| 124 |
+
conn.close()
|
| 125 |
+
return (
|
| 126 |
+
jsonify(
|
| 127 |
+
{
|
| 128 |
+
"success": True,
|
| 129 |
+
"message": "Alert subscription successful! You will now receive wildfire alerts.",
|
| 130 |
+
}
|
| 131 |
+
),
|
| 132 |
+
200,
|
| 133 |
+
)
|
| 134 |
+
except sqlite3.IntegrityError:
|
| 135 |
+
return (
|
| 136 |
+
jsonify(
|
| 137 |
+
{
|
| 138 |
+
"success": False,
|
| 139 |
+
"message": "An error occurred while processing your request. Please try again later.",
|
| 140 |
+
}
|
| 141 |
+
),
|
| 142 |
+
500,
|
| 143 |
+
)
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return jsonify({"success": False, "message": str(e)}), 500
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# The route for predicting wildfire using satellite data
|
| 149 |
+
@app.route("/satellite_predict", methods=["POST"])
|
| 150 |
+
def satellite_predict():
|
| 151 |
+
data = request.json
|
| 152 |
+
latitude = data["location"][1]
|
| 153 |
+
longitude = data["location"][0]
|
| 154 |
+
zoom = data["zoom"]
|
| 155 |
+
|
| 156 |
+
output_size = (350, 350)
|
| 157 |
+
crop_amount = 35
|
| 158 |
+
save_path = "satellite_image.png"
|
| 159 |
+
|
| 160 |
+
prediction_sattelite = satellite_cnn_predict(
|
| 161 |
+
latitude,
|
| 162 |
+
longitude,
|
| 163 |
+
output_size=output_size,
|
| 164 |
+
zoom_level=zoom,
|
| 165 |
+
crop_amount=crop_amount,
|
| 166 |
+
save_path=save_path,
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
satellite_confidence = round(
|
| 170 |
+
(
|
| 171 |
+
prediction_sattelite
|
| 172 |
+
if prediction_sattelite > 0.5
|
| 173 |
+
else 1 - prediction_sattelite
|
| 174 |
+
)
|
| 175 |
+
* 100
|
| 176 |
+
) # float to percentage
|
| 177 |
+
|
| 178 |
+
satellite_status = 1 if prediction_sattelite > 0.5 else 0
|
| 179 |
+
|
| 180 |
+
prediction_weather = weather_data_predict(latitude, longitude)
|
| 181 |
+
|
| 182 |
+
weather_confidence = round(
|
| 183 |
+
(prediction_weather if prediction_weather > 0.5 else 1 - prediction_weather)
|
| 184 |
+
* 100
|
| 185 |
+
) # float to percentage
|
| 186 |
+
|
| 187 |
+
weather_status = 1 if prediction_weather > 0.5 else 0
|
| 188 |
+
|
| 189 |
+
# Calculate average probability and its corresponding binary status
|
| 190 |
+
prediction_average = (prediction_sattelite + prediction_weather) / 2
|
| 191 |
+
|
| 192 |
+
average_confidence = round(
|
| 193 |
+
(prediction_average if prediction_average > 0.5 else 1 - prediction_average)
|
| 194 |
+
* 100
|
| 195 |
+
) # float to percentage
|
| 196 |
+
|
| 197 |
+
average_status = 1 if prediction_average > 0.5 else 0
|
| 198 |
+
|
| 199 |
+
response = {
|
| 200 |
+
"satellite_probability": satellite_confidence,
|
| 201 |
+
"satellite_status": satellite_status,
|
| 202 |
+
"weather_probability": weather_confidence,
|
| 203 |
+
"weather_status": weather_status,
|
| 204 |
+
"average_probability": average_confidence,
|
| 205 |
+
"average_status": average_status,
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
return jsonify(response), 200
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# The route for predicting wildfire using camera images
|
| 212 |
+
@app.route("/camera_predict", methods=["POST"])
|
| 213 |
+
def camera_predict():
|
| 214 |
+
image_file = request.files["image"]
|
| 215 |
+
prediction = camera_cnn_predict(image_file)
|
| 216 |
+
|
| 217 |
+
confidence = round(
|
| 218 |
+
(prediction if prediction > 0.5 else 1 - prediction) * 100)
|
| 219 |
+
|
| 220 |
+
# Alphabetically *fire* (0) comes before *no fire* (1)
|
| 221 |
+
wildfire_prediction = 1 if prediction < 0.5 else 0
|
| 222 |
+
|
| 223 |
+
response_data = {
|
| 224 |
+
"wildfire_prediction": wildfire_prediction,
|
| 225 |
+
"confidence": confidence,
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
return jsonify(response_data), 200
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
if __name__ == "__main__":
|
| 232 |
+
init_db()
|
| 233 |
+
app.run(debug=True)
|