-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
350 lines (239 loc) · 8.23 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
Python back-end (flask server) of labelling tool.
Project structure:
- static/ contains `notes_photos/` and `scripts/`
# NOTE:
#
# Remember to set the environment variable:
# $ export FLASK_APP="flask_server.py"
# Then run using:
# $ flask run
#
#
# DEVELOPMENT:
# $ python3 render_js_css_template.py && flask run
#
#
"""
from flask import Flask, jsonify, render_template, request, Response
from functools import wraps
import glob
import os
import inspect
import json
import copy
import util as UTIL
import mongodb_query as MONGO
import pandas as pd
import numpy as np
env_vars = UTIL.load_json_config(os.environ['OPENMTURK_CONFIG'])[1]
app = Flask(__name__)
# app.config.update(TEMPLATES_AUTO_RELOAD=True)
# Dump the database to file every BACKUP_FREQUENCY inserts
BACKUP_FREQUENCY = env_vars['OPENMTURK_BACKUP_FREQUENCY']
BACKUP_FILENAME = env_vars['OPENMTURK_BACKUP_FILENAME']
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == 'admin' and password == 'secret'
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
def get_style_version(dir_path):
considered_files = glob.glob(dir_path)
considered_files = list(filter(lambda x : len(x.split('.'))==3,
considered_files))
version = -1
fn = lambda x: int(x.split('.')[1])
files = sorted(considered_files, key=fn)
for f in files:
first_part = f.split('.')[0].split('/')[-1]
ext = f.split('.')[-1]
if first_part == 'main'\
and ext == 'js':
version = f.split('.')[1]
return int(version)
def get_metrics():
num_labelled = MONGO.count_labels()
images_dir = UTIL.maybe_add_suffix(env_vars['IMG_DIRECTORY'], '/')+'*'
total = len(list(glob.glob(images_dir)))
info_dict = {
'num_labelled_imgs': num_labelled,
'total_num_imgs': total
}
return info_dict
def dump_all_labels(filename):
serializable_labels = []
labels = MONGO.select_all()
for label in labels:
if '_id' in label.keys():
del label['_id']
serializable_labels+=[label]
with open(filename, 'w') as f:
json.dump(serializable_labels, f)
style_version = get_style_version('static/js/*')
# style_version = 64
#
# Server webpages:
#
img_index = 0
@app.route('/get_prev', methods=['POST'])
def get_prev_image():
global img_index
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
images_dir = UTIL.maybe_add_suffix(env_vars['IMG_DIRECTORY'], '/')+'*'
all_img_paths = list(glob.glob(images_dir))
if img_index > 0:
img_index -= 1
img_path = all_img_paths[img_index]
print('img_path => {}'.format(img_path))
return jsonify(dict(img_path=img_path))
except Exception as e:
print('{} - ERROR : {}'.format(log_prefix, e))
return jsonify(result=300)
@app.route('/get_next', methods=['POST'])
def get_next_image():
global img_index
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
images_dir = UTIL.maybe_add_suffix(env_vars['IMG_DIRECTORY'], '/')+'*'
all_img_paths = list(glob.glob(images_dir))
if img_index < len(all_img_paths):
img_index += 1
img_path = all_img_paths[img_index]
print('img_path => {}'.format(img_path))
return jsonify(dict(img_path=img_path))
except Exception as e:
print('{} - ERROR : {}'.format(log_prefix, e))
return jsonify(result=300)
@app.route('/get_random_image', methods=['POST'])
def get_random_image():
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
images_dir = UTIL.maybe_add_suffix(env_vars['IMG_DIRECTORY'], '/')+'*'
all_img_paths = list(glob.glob(images_dir))
labelled_objs = MONGO.select_attr({'is_labelled': True}, {'img_path':1})
labelled_img_paths = [obj['img_path'] for obj in labelled_objs]
labelled_img_paths = list(filter(lambda x: x in all_img_paths, labelled_img_paths))
labelled_df = pd.DataFrame({'paths': labelled_img_paths})
all_df = pd.DataFrame({'paths': all_img_paths})
df = all_df.join(labelled_df, lsuffix="_left", rsuffix="_right")
unlabelled_df = df[df['paths_right'].isnull()]['paths_left']
rand_path = str(unlabelled_df.sample(1).iloc[0])
return jsonify(dict(img_path=rand_path))
except Exception as e:
print('{} - ERROR : {}'.format(log_prefix, e))
return jsonify(result=300)
@app.route('/get_label', methods=['POST'])
def get_label():
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
label = MONGO.select_label(request.json['img_path'])
return jsonify(dict(label))
except Exception as e:
print('{} - ERROR : {}'.format(log_prefix, e))
return jsonify(result=300)
@app.route('/get_dataset_info', methods=['POST'])
def get_dataset_info():
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
ajax_dict = copy.copy(request.json)
db_info = get_metrics()
print('{} - Produced DB info: {}'.format(log_prefix, db_info))
return jsonify(result=db_info)
except Exception as e:
print('{} - ERROR: {}'.format(log_prefix, e))
return jsonify(result=300)
@app.route('/insert_label', methods=['POST'])
def insert_label():
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
global BACKUP_FILENAME
if insert_label.counter == BACKUP_FREQUENCY:
print('{} - dumping database in {}'.format(log_prefix, BACKUP_FILENAME))
dump_all_labels(BACKUP_FILENAME)
insert_label.counter = 0
try:
label = copy.copy(request.json)
MONGO.insert_label(label)
insert_label.counter += 1
print('{} - Received labels of image {} - insert_label.counter = {}'.format(
log_prefix,
label['img_path'], insert_label.counter))
return jsonify(result=200)
except Exception as e:
print('{} - ERROR: {}'.format(log_prefix, e))
return jsonify(result=300)
insert_label.counter = 0
@app.route('/reset', methods=['POST'])
def reset():
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
label = copy.copy(request.json)
MONGO.delete_label(label)
print('{} - Removed labels from record'.format(log_prefix))
return jsonify(result=200)
except Exception as e:
print('ERROR (app.reset): {}'.format(e))
return jsonify(result=300)
@app.route('/get_all_labels', methods=['POST'])
def get_all_labels():
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
try:
all_labels = MONGO.select_all({'is_labelled': True})
for label in all_labels:
del label['_id']
print('{} - Retrieved {} labels from database'.format(
log_prefix,
len(all_labels)))
return jsonify(all_labels)
except Exception as e:
print('{} - ERROR: {}'.format(log_prefix, e))
return jsonify(result=300)
@app.route('/guidelines.html')
@app.route('/guidelines')
def about():
main_js = 'static/js/main.{}.js'.format(style_version)
main_css = 'static/css/style.{}.css'.format(style_version)
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
print(log_prefix)
return render_template('guidelines.html',
main_js=main_js,
main_css=main_css)
@app.route('/documentation.html')
@app.route('/documentation')
def documentation():
main_js = 'static/js/main.{}.js'.format(style_version)
main_css = 'static/css/style.{}.css'.format(style_version)
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
print(log_prefix)
return render_template('documentation.html',
main_js=main_js,
main_css=main_css)
@app.route('/')
@requires_auth
def index():
main_js = 'static/js/main.{}.js'.format(style_version)
main_css = 'static/css/style.{}.css'.format(style_version)
log_prefix = 'Client request - {}'.format(inspect.stack()[0][3])
log_content = 'Using script versions: {}, {}'.format(
os.path.basename(main_css),
os.path.basename(main_js))
print(log_prefix + ' - ' + log_content)
return render_template('index.html',
main_js=main_js,
main_css=main_css)
if __name__ == '__main__':
app.run()