-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
162 lines (126 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#from crypt import methods
import datetime as dt
import requests
import json
import nautical_calculations
from flask import Flask
from flask import request
from flask_cors import CORS, cross_origin
import json
from nautical_calculations import convert_to_miles, get_distance
with open("data.json", 'r') as my_json:
json_dict = json.load(my_json)
def nearby_resorts(lat, lng, distance_in_miles):
"""
Takes lat, lng coordinates, distance(mi) as parameters, then prints out a list of nearby resorts that are
less than or equal to the passed in distance in miles.
"""
all_resorts = json_dict["skiAreas"]["skiArea"]
closest_four = []
for index in range(len(all_resorts)):
if all_resorts[index].get('georeferencing') == None:
continue
elif all_resorts[index]['georeferencing'].get('@lat') == None:
continue
else:
resort_lat = all_resorts[index]['georeferencing']['@lat']
resort_lng = all_resorts[index]['georeferencing']['@lng']
resort_name = all_resorts[index]['name']
resort_id = all_resorts[index]['@id']
distance_from_resort = convert_to_miles(get_distance(lat, lng, resort_lat, resort_lng))
# if distance from resort is less than or equal to 100 miles, print the resort name
# can change to return resort
if distance_from_resort <= distance_in_miles:
if len(closest_four) < 4:
closest_four.append([resort_name, distance_from_resort, resort_id])
elif len(closest_four) == 4:
for x in closest_four:
if x[1] > distance_from_resort:
closest_four.remove(x)
closest_four.append([resort_name, distance_from_resort, resort_id])
break
# print(f'{resort_name}: {round(distance_from_resort, 2)} miles away')
closest_four_ids = []
for x in (closest_four):
closest_four_ids.append(x[2])
return closest_four_ids
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
API_KEY = "66d7fecb1ff378f6d549b766038d5ff3"
#put API key into text file read from
#CORS(app)
app = Flask(__name__, static_folder='frontend/build',static_url_path='')
CORS(app)
def kelvin_to_fahrenheit(kelvin):
celsius = kelvin -273.15
fahrenheit = celsius * (9/5) + 32
return int(fahrenheit)
@app.route("/data", methods=["GET","POST"], strict_slashes=False)
@cross_origin()
def add_articles():
resort_list = []
resort1 = request.json['resort1']
resort_list.append(resort1)
resort2 = request.json['resort2']
resort_list.append(resort2)
resort3 = request.json['resort3']
resort_list.append(resort3)
resort4 = request.json['resort4']
resort_list.append(resort4)
compiled_fahr = []
compiled_desc = []
compiled_snow = []
for resort in resort_list:
lat = resort[0]
lon = resort[1]
url = f"https://api.openweathermap.org/data/2.5/weather?lat={float(lat)}&lon={float(lon)}&appid={API_KEY}"
data = requests.get(url).json()
temp_kelvin = data['main']['temp']
compiled_fahr.append(kelvin_to_fahrenheit(temp_kelvin))
description = data['weather'][0]['description']
compiled_desc.append(description)
if "snow" in data:
snow = data['snow']['1h']
else:
snow = 0
compiled_snow.append(snow)
big_array = []
for index in range(4):
big_array.append(compiled_desc[index])
big_array.append(compiled_fahr[index])
big_array.append(compiled_snow[index])
print(big_array)
return json.dumps(big_array)
@app.route("/four", methods=["GET","POST"], strict_slashes=False)
@cross_origin()
def closest_four():
lat = request.json["latitude"]
lon = request.json["longitude"]
list_4 = nearby_resorts(float(lat), float(lon), 100)
return json.dumps(list_4)
@app.route("/count", methods=["GET"], strict_slashes=False)
@cross_origin()
def get_count():
import torch
import numpy as np
import json
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
imgs = ['ski1.jpg', 'ski2.jpg', 'ski3.jpg', 'ski4.jpg']
count_list = []
for image in imgs:
results = model(image)
results.xyxy[0]
df_results = results.pandas().xyxy[0]
np.sum(df_results['name'] == 'person')
count = np.sum(df_results['name'] == 'person')
count_list.append(str(count))
# print(df_results)
# print(count)
# results.show()
count = 0
return json.dumps(count_list)
@app.route('/')
@cross_origin()
def serve():
return send_from_directory(app.statuc_folder, 'index.html')
if __name__ == '__main__':
app.run()