forked from GeorgeTownsendd/SeaSentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
127 lines (97 loc) · 4.08 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
from flask import Flask, render_template
from keras.models import load_model
import cv2
import os
import json
# email
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
sys.path.append(os.path.abspath("/Users/tomsmail/Documents/Hackathons/OilDetector.nosync/FinancialData"))
from ApplyLeakageToPrice import *
# Constants
image_size_width = 255
image_size_height = 255
sender_email = 'sea.sentry.com@gmail.com'
sender_password = os.environ.get('PASSWORD')
point_off_coast_of_scotland_1 = (57, -1)
point_off_coast_of_scotland_2 = (56, -2)
TESTING_NO_EMAIL = True
TESTING_NO_MODEL = True
TESTING_NO_PRICE_PREDICTION = False
if not TESTING_NO_MODEL:
model = load_model('best_model.h5')
else:
model = ""
# Create a Flask app
app = Flask(__name__)
locations_of_oil_spills = [(11, -60)]
# Request satellite data from sentinel (we define where we want to look for oil spills).
# 5km between each point, this is about a difference of 0.05 in latitude and longitude.
def generate_lat_long_points(range_upper_left = (90, 180), range_bottom_right = (-90, -180), buffer=0.05):
# Generate a list of lat and lon points to search for oil spills.
(x1, x2) = range_upper_left
(y1, y2) = range_bottom_right
lat_points = [i for i in range(x1 * 20, x2 * 20, int(buffer * 20))]
lon_points = [i for i in range(y1 * 20, y2 * 20, int(buffer * 20))]
return [(lat / 20, lon / 20) for lat in lat_points for lon in lon_points]
# Run trained model on image and if it is an oil spill, add it to the list of oil spills.
def predict_oil_spill(image_url, lat, lon):
# Run the model on the image and return the result.
image = cv2.imread(image_url)
image = cv2.resize(image, (image_size_width, image_size_height))
bool_ret = False
if model.predict(image) == 1:
bool_ret = True
return bool_ret
# Notify investor of oil any oil spills, using email client.
def send_email(receiver_email = "thomas.c.smail@gmail.com", subject = "Oil Spill Alert", body = "There is an oil spill. You should buy now, before the price spikes and sell when the oil price is high."):
# Send an email to the investor if there are any oil spills.
# Create a multipart message
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# Add body to email
message.attach(MIMEText(body, "plain"))
# Connect to the SMTP server
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
# Login to the email account
server.login(sender_email, sender_password)
# Send email
server.sendmail(sender_email, receiver_email, message.as_string())
# Close the SMTP server connection
server.quit()
# Return the map to the user.
@app.route('/')
def homepage_func():
# Check if there is an oil spill
is_spill = False
for lat, lon in generate_lat_long_points(point_off_coast_of_scotland_1, point_off_coast_of_scotland_2):
if is_spill:
break
# make requests to the sentinel API to get the image
if TESTING_NO_MODEL:
is_spill = True
else:
image = "GET_IMAGE_FROM_SENTINEL_API"
is_spill = predict_oil_spill(image, lat, lon, "2021-01-01")
locations_of_oil_spills.append((lat, lon))
json_data = json.dumps(locations_of_oil_spills)
# Save json data to file
with open('static/data.json', 'w') as f:
f.write(json_data)
if TESTING_NO_PRICE_PREDICTION:
sell_date = "2024-03-06 00:00:00"
else:
sell_date = GenerateData("")
if TESTING_NO_EMAIL:
print(f"There is an oil spill at {11}, {-60} you should buy now, before the price spikes and sell on {sell_date}.")
else:
print(f"There is an oil spill at {11}, {-60} you should buy now, before the price spikes and sell on {sell_date}.")
send_email(body=f"There is an oil spill at {11}, {-60} you should buy now, before the price spikes and sell on {sell_date}.")
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)