-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
190 lines (149 loc) · 5.59 KB
/
web.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
import argparse
import datetime
import logging
import pathlib
import random
import flask
import sqlite3
import database
import util
db_path = None
nib_key_path = None
tile_path = None
feature_name = None
app = flask.Flask(__name__, static_url_path='')
@app.route("/")
def send_index():
return flask.send_from_directory('web', 'index.html')
@app.route('/static/script.js')
def send_script():
return flask.send_from_directory('web/static', 'script.js')
@app.route('/api/review/next_tile')
def get_next_tile_for_review():
db = database.Database(db_path)
tiles = db.tiles_for_review(feature_name, limit=1)
if not tiles:
return '', 204
tile_hash, z, x, y, score, model_version = list(tiles)[0]
top, left = util.tile2deg(x, y, z)
bottom, right = util.tile2deg(x + 1, y + 1, z)
return {
'tile_hash': tile_hash,
'z': z,
'x': x,
'y': y,
'score': score,
'model_version': model_version,
'top': top,
'left': left,
'bottom': bottom,
'right': right,
}
def score_neighbours(cursor, own_hash):
now = datetime.datetime.now().isoformat()
z, own_x, own_y = database.get_tile_pos(cursor, own_hash)
for neighbour_x in [own_x - 1, own_x, own_x + 1]:
for neighbour_y in [own_y - 1, own_y, own_y + 1]:
for neighbour_hash in database.get_tile_hash(cursor,
z,
neighbour_x,
neighbour_y):
database.write_score(cursor,
neighbour_hash,
feature_name,
1.0,
'neighbour',
now)
def _write_ground_truth(tile_hash, feature_name, response, cursor):
if response == 'accept':
predicted_score = database.get_score(cursor, tile_hash, feature_name)
database.set_true_score(cursor,
tile_hash,
feature_name,
predicted_score)
elif response == 'true':
database.set_has_feature(cursor, tile_hash, feature_name, True)
score_neighbours(cursor, tile_hash)
elif response == 'false':
if feature_name == 'solar_area':
database.set_true_score(cursor, tile_hash, feature_name, 0.0)
else:
database.set_has_feature(cursor, tile_hash, feature_name, False)
elif response == 'skip':
database.remove_score(cursor, feature_name, tile_hash)
else:
raise ValueError
@app.route('/api/review/response', methods=['POST'])
def accept_tile_response():
body = flask.request.json
tile_hash = body['tile_hash']
response = body['response']
if response not in ['accept', 'false', 'skip', 'true']:
return 'Bad "response" value', 400
if feature_name == 'solar_area' and response == 'true':
return 'Can\'t use "true" with solar_area', 400
db = database.Database(db_path)
while True:
try:
with db.transaction('write_ground_truth') as c:
_write_ground_truth(tile_hash, feature_name, response, c)
break
except sqlite3.IntegrityError as e:
print(e)
break
except sqlite3.OperationalError as e:
print(e)
continue
return {}
@app.route('/api/review/surrounding', methods=['POST'])
def review_surrounding_tiles():
body = flask.request.json
tile_hash = body['tile_hash']
db = database.Database(db_path)
while True:
try:
with db.transaction('tag_neighbours_for_scoring') as c:
score_neighbours(c, tile_hash)
break
except sqlite3.OperationalError:
continue
return {}
@app.route('/api/tiles/by-hash/<tile_hash>.jpeg')
def get_tile_by_hash(tile_hash):
dir_name = tile_hash[:2]
file_name = tile_hash[2:]
return flask.send_from_directory(
tile_path,
'{}/{}.jpeg'.format(dir_name, file_name))
@app.route('/api/tiles/by-pos/<z>/<x>/<y>.jpeg')
def get_nib_tile(z, x, y):
z = int(z)
x = int(x)
y = int(y)
db = database.Database(db_path)
with db.transaction('get_tile_hashes_from_position') as cursor:
tiles = database.get_tile_hash(cursor, z, x, y)
if tiles:
tile_hash = random.choice(tiles)
else:
print(f'No tiles found for {z}/{x}/{y}, trying to download')
nib_api_key = util.load_key(nib_key_path)
written, tile_hash = util.download_single_tile(
tile_path, nib_api_key, z, x, y)
if written:
with db.transaction('add_downloaded_tile_web_by_pos') as cursor:
database.add_tile_hash(cursor, z, x, y, tile_hash)
return flask.redirect(f'/api/tiles/by-hash/{tile_hash}.jpeg', code=307)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--database', default='data/tiles.db')
parser.add_argument('--NiB-key', type=str, default='secret/NiB_key.json')
parser.add_argument('--tile-path', type=str, default='data/images')
parser.add_argument('--feature', type=str, required=True)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
db_path = pathlib.Path(args.database)
nib_key_path = pathlib.Path(args.NiB_key)
tile_path = pathlib.Path(args.tile_path)
feature_name = args.feature
app.run('0.0.0.0', 5000)