-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
607 lines (524 loc) · 21.7 KB
/
server.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import os
import json
import logging
from sqlalchemy import create_engine, select, Table, MetaData, and_, func, update, insert
import base64
import hashlib
import math
import pymysql
from flask import Flask, render_template, jsonify, abort, request, redirect, send_file, make_response, current_app
from flask_login import LoginManager, login_user, logout_user, login_required, current_user
from models import User, users
import time
app = Flask(__name__, instance_path="/{project_folder_abs_path}/instance")
secret_key = os.getenv('FLASK_SECRET_KEY')
app.config['SECRET_KEY'] = secret_key
login_manager = LoginManager(app)
login_manager.login_view = "/login"
def open_DB_connection(rqst, variables, db_name):
#Database connection
DB_USER = os.getenv('HUMAINT_ANNOTATOR_DB_USER')
DB_PWD = os.getenv('HUMAINT_ANNOTATOR_DB_PWD')
engine = create_engine('mysql+pymysql://'+DB_USER+':'+DB_PWD+'@localhost/humaint_annotator?unix_socket=/var/run/mysqld/mysqld.sock')
metadata = MetaData(bind=None)
### DATABASE TABLES
imgs_info = Table(
'imgs_info',
metadata,
autoload=True,
autoload_with=engine
)
img_annotator_relation = Table(
'img_annotator_relation',
metadata,
autoload=True,
autoload_with=engine
)
user_info = Table(
'user_info',
metadata,
autoload=True,
autoload_with=engine
)
### CONNECTION TO DATABASE
connection = engine.connect()
result = ()
is_update = False
if rqst == "login":
stmt = select(
user_info.columns.user_id,
user_info.columns.username,
user_info.columns.pwd,
user_info.columns.role
).where(
user_info.columns.user_email == variables[0]
)
elif rqst == "get_img":
inter_agreement = int(get_inter_agreement())
if variables[1] == 'persons':
ds_type_annotated = imgs_info.columns.persons_annotated
discarded_by_user = imgs_info.columns.discarded_by_user_persons
auto_discarded = imgs_info.columns.auto_discarded_persons
else:
ds_type_annotated = imgs_info.columns.vehicles_annotated
discarded_by_user = imgs_info.columns.discarded_by_user_vehicles
auto_discarded = imgs_info.columns.auto_discarded_vehicles
try:
stmt = select(
img_annotator_relation.columns.img_name
).where(and_(
img_annotator_relation.columns.user_name == variables[3],
img_annotator_relation.columns.ds_type == variables[1]
))
imgs_to_avoid_tuple = connection.execute(stmt).fetchall()
except Exception as e:
print(e)
list_of_images_to_avoid = []
for tuple in imgs_to_avoid_tuple:
list_of_images_to_avoid.append(tuple[0])
# try:
# stmt = select([func.sum(imgs_info.columns.num_annotated_agents)]).select_from(
# imgs_info
# ).where(and_(
# imgs_info.columns.dataset == variables[0],
# imgs_info.columns.img_distribution == variables[2],
# ds_type_annotated >= inter_agreement
# ))
# result = connection.execute(stmt).fetchall()
# except Exception as e:
# print(e)
#
# inter_agreement_quota_acquired = is_inter_agreement_quota_acquired(result[0][0], variables[0], variables[1], variables[2])
# if len(result) == 0 or not inter_agreement_quota_acquired:
# try:
# aux_inter_agreement = inter_agreement - 1
# while(aux_inter_agreement >= 0):
# stmt = select(
# imgs_info.columns.file_name
# ).where(and_(
# imgs_info.columns.dataset == variables[0],
# imgs_info.columns.img_distribution == variables[2],
# imgs_info.columns.file_name.not_in(list_of_images_to_avoid),
# discarded_by_user != True,
# auto_discarded != True,
# imgs_info.columns.is_key_frame == 1,
# ds_type_annotated == aux_inter_agreement
# )).order_by(ds_type_annotated.desc()).limit(1)
# result = connection.execute(stmt).fetchall()
# aux_inter_agreement -= 1
# if(len(result) != 0):
# break
# except Exception as e:
# print(e)
# else:
# result = ()
# change_distribution = True
#if variables[4]:#inter-agreement quota acquired
stmt = select(
imgs_info.columns.file_name
).where(and_(
imgs_info.columns.dataset == variables[0],
imgs_info.columns.img_distribution == variables[2],
imgs_info.columns.file_name.not_in(list_of_images_to_avoid),
discarded_by_user != True,
auto_discarded != True,
imgs_info.columns.is_key_frame == 1,
ds_type_annotated == 0
)).order_by(ds_type_annotated.desc()).limit(1)
result = connection.execute(stmt).fetchall()
elif rqst == "get_json":
edit_db_entry = variables[2]
if edit_db_entry:
if variables[1] == 'persons':
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
persons_annotated=imgs_info.columns.persons_annotated + 1
)
elif variables[1] == 'vehicles':
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
vehicles_annotated=imgs_info.columns.vehicles_annotated + 1
)
connection.execute(stmt)
is_update = True
stmt = select(
imgs_info.columns.associated_json
).where(
imgs_info.columns.file_name == variables[0]
)
result = connection.execute(stmt).fetchall()
elif rqst == "discard_img":
if(variables[1] == "discarded-by-user"):
if(variables[2] == "persons"):
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
discarded_by_user_persons=1
)
else:
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
discarded_by_user_vehicles=1
)
else:
if (variables[2] == "persons"):
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
auto_discarded_persons=1
)
else:
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
auto_discarded_vehicles=1
)
connection.execute(stmt)
is_update = True
elif rqst == "get_num_annotated_agents":
if(variables[0] == "persons"):
stmt = select(
imgs_info.columns.num_annotated_agents
).where(
imgs_info.columns.dataset == variables[1],
imgs_info.columns.persons_annotated != 0,
imgs_info.columns.img_distribution == variables[2]
)
else:
stmt = select(
imgs_info.columns.num_annotated_agents
).where(
imgs_info.columns.dataset == variables[1],
imgs_info.columns.vehicles_annotated != 0,
imgs_info.columns.img_distribution == variables[2]
)
elif rqst == "new_annotation_entry":
stmt = insert(
img_annotator_relation
).values(
img_name=variables[0],
user_name=variables[1],
ds_type=variables[2]
)
connection.execute(stmt)
is_update = True
elif rqst == "get_sweeps_jsons":
stmt = select(
imgs_info.columns.associated_json
).where(
imgs_info.columns.key_frame_name == variables[0],
imgs_info.columns.is_key_frame == 0
)
elif rqst == "update_sweeps":
if variables[1] == "persons":
stmt = update(
imgs_info
).where(
imgs_info.columns.key_frame_name == variables[0],
imgs_info.columns.is_key_frame == 0
).values(
persons_annotated=imgs_info.columns.persons_annotated+1
)
elif variables[1] == "vehicles":
stmt = update(
imgs_info
).where(
imgs_info.columns.key_frame_name == variables[0],
imgs_info.columns.is_key_frame == 0
).values(
vehicles_annotated=imgs_info.columns.vehicles_annotated + 1
)
connection.execute(stmt)
is_update = True
elif rqst == "update_annotated_agents":
stmt = update(
imgs_info
).where(
imgs_info.columns.file_name == variables[0]
).values(
num_annotated_agents=imgs_info.columns.num_annotated_agents+variables[1]
)
connection.execute(stmt)
is_update = True
if len(result) == 0 and not is_update:
result = connection.execute(stmt).fetchall()
return result
def create_new_annotation_entry(img_name, ds_type):
variables = [img_name, current_user.name, ds_type]
result = open_DB_connection('new_annotation_entry', variables, 'img_annotator_relation')
def get_inter_agreement():
with open('config.json') as config_file:
config = json.load(config_file)
inter_agreement = config["inter_agreement"]
return inter_agreement
def is_inter_agreement_quota_acquired(query_result, dataset, ds_type, distribution):
with open('config.json') as config_file:
config = json.load(config_file)
inter_agreement_quota = config['num_imgs_several_annotators'][ds_type][dataset.lower()][distribution]
if query_result != None and query_result >= inter_agreement_quota:
return True
else:
return False
def get_img(dataset, dataset_type, user_name):
with open('config.json') as config_file:
config = json.load(config_file)
inter_agreement_quota_acquired = False
for dist in config['agents_to_annotate'][dataset_type][dataset]:
variables = [dataset, dataset_type, dist, user_name, inter_agreement_quota_acquired]
images = open_DB_connection("get_img", variables, 'img_info')
if len(images) != 0:
break
if len(images) == 0:
inter_agreement_quota_acquired = True
for dist in config['agents_to_annotate'][dataset_type][dataset]:
variables = [dataset, dataset_type, dist, user_name, inter_agreement_quota_acquired]
images = open_DB_connection("get_img", variables, 'img_info')
if len(images) != 0:
break
img_file_name = images[0][0]
img = {
"file_name": img_file_name
}
return img
def walklevel(some_dir, level=1):
some_dir = some_dir.rstrip(os.path.sep)
assert os.path.isdir(some_dir)
num_sep = some_dir.count(os.path.sep)
for root, dirs, files in os.walk(some_dir):
yield root, dirs, files
num_sep_this = root.count(os.path.sep)
if num_sep + level <= num_sep_this:
del dirs[:]
@app.route('/img_url/<dataset>/<dataset_type>', methods=['GET'])
def get_img_from_storage(dataset, dataset_type):
try:
print("Getting image for user " + current_user.name)
if(dataset_type == "vehicles"):
img = get_img_for_user(dataset, current_user.name)
else:
img = get_img(dataset, dataset_type, current_user.name)
imgs_path = os.getenv('HUMAINT_IMGS_PATH')
complete_img_path = ""
depth_search = 0
if dataset == "kitti" or dataset == "eurocity":
depth_search = 1
for root, dirs, files in walklevel(imgs_path, level=depth_search):
find = False
for d in dirs:
if os.path.exists(str(root) +'/'+ str(d) + '/' + img["file_name"]):
complete_img_path = str(root) +'/'+ str(d) + '/' + img["file_name"]
find = True
break
if find:
break
img_in_base64 = {}
with open(complete_img_path, "rb") as f:
image_binary = f.read()
img_in_base64 = {'img': str(base64.b64encode(image_binary).decode('ascii')), 'img_name': img['file_name']}
except Exception as e:
logging.error(e)
return None
return jsonify(img_in_base64)
def get_img_for_user(dataset, user):
user_json_path = "left_imgs_non_inter_agreement/left_imgs_" + user + ".json"
with open(user_json_path) as f:
left_imgs_parsed_json = json.load(f)
imgs_for_dataset = left_imgs_parsed_json[dataset]
img = {}
if(len(imgs_for_dataset) != 0):
img["file_name"] = imgs_for_dataset[0]
else:
print("No more images for user " + user + " on dataset " + dataset)
return img
def remove_img_from_user_list(dataset, img_name, user):
print("Removing image from pending list")
user_json_path = "left_imgs_non_inter_agreement/left_imgs_" + user + ".json"
with open(user_json_path) as f:
left_imgs_parsed_json = json.load(f)
imgs_for_dataset = left_imgs_parsed_json[dataset]
if(imgs_for_dataset[0] == img_name):
del imgs_for_dataset[0]
left_imgs_parsed_json[dataset] = imgs_for_dataset
with open(user_json_path, 'w', encoding='utf-8') as f2:
json.dump(left_imgs_parsed_json, f2, ensure_ascii=False, indent=4)
@app.route('/img_json/<dataset>/<file_name>', methods=['GET'])
def get_img_json(dataset, file_name):
edit_db_entry = False
variables = [file_name, "", edit_db_entry]
json_file = str(open_DB_connection("get_json", variables, 'img_info')[0][0])
json_data = search_json_in_datasets(json_file, dataset)
return json_data
def search_json_in_datasets(json_file, dataset):
depth_search=0
if dataset == "kitti" or dataset == "eurocity" or dataset == "citypersons":
depth_search=1
jsons_path = os.getenv('HUMAINT_JSONS_PATH')
if dataset == "nuscenes":
jsons_path = jsons_path + "/" + dataset + "/jsons/nuscenes"
else:
jsons_path = jsons_path + "/" + dataset + "/jsons"
for root, dirs, files in walklevel(jsons_path, level=depth_search):
find = False
for d in dirs:
if os.path.exists(root + '/' + d + '/' + json_file):
complete_json_path = root + '/' + d + '/' + json_file
find = True
break
if find:
break
if find:
with open(complete_json_path) as f:
json_data = json.load(f)
else:
abort(404)
return json_data
@app.route('/save_edited_json/<img_name>/<dataset_type>/<annotator>/<selected_dataset>', methods=['POST'])
def save_edited_json(img_name, dataset_type, annotator, selected_dataset):
if (dataset_type == "vehicles"):
remove_img_from_user_list(selected_dataset, img_name, annotator)
# POST request
edited_json = request.get_json()
dict_of_agents = create_edited_agents(edited_json)
edit_db_entry = True
variables = [img_name, dataset_type, edit_db_entry]
create_new_annotation_entry(img_name, dataset_type)
json_file = str(open_DB_connection("get_json", variables, 'img_info')[0][0])
list_of_sweeps_jsons = get_list_of_sweeps_jsons(img_name)
edit_json_files(json_file, edited_json["json"], dict_of_agents, list_of_sweeps_jsons, annotator, selected_dataset, dataset_type)
update_sweeps_in_db(img_name, dataset_type)
return 'OK', 200
def update_sweeps_in_db(key_frame_name, ds_type):
variables = [key_frame_name, ds_type]
updated = open_DB_connection("update_sweeps", variables, "imgs_info")
def edit_json_files(json_file, edited_json, dict_of_agents, list_of_sweeps_jsons, annotator, selected_dataset, dataset_type):
#First edit the key frame json
base_path = "edited_jsons/" + selected_dataset + "/" + dataset_type
if not os.path.exists(base_path):
os.makedirs(base_path)
key_frame_json_path = base_path + "/" + json_file.replace('.json', '_' + annotator + '.json')
with open(key_frame_json_path, 'w', encoding='utf-8') as f:
json.dump(edited_json, f, ensure_ascii=False, indent=4)
#Then edit sweeps' jsons
for sweep in list_of_sweeps_jsons:
sweep_json = search_json_in_datasets(sweep[0], selected_dataset)
edited_sweep_json_path = base_path + "/" + sweep[0].replace('.json', '_' + annotator + '.json')
for agent in dict_of_agents:
k = 0
while k < len(sweep_json["agents"]):
if sweep_json["agents"][k]['uuid'] == agent:
sweep_json["agents"][k] = dict_of_agents[agent]
break
k += 1
with open(edited_sweep_json_path, 'w', encoding='utf-8') as ef:
json.dump(sweep_json, ef, ensure_ascii=False, indent=4)
def create_edited_agents(edited_json):
dict_of_agents = {}
for agent in edited_json["json"]["agents"]:
dict_of_agents[agent["uuid"]] = agent
return dict_of_agents
def get_list_of_sweeps_jsons(key_frame_name):
variables = [key_frame_name]
list_of_sweeps_jsons = open_DB_connection("get_sweeps_jsons", variables, 'imgs_info')
return list_of_sweeps_jsons
@app.route('/user_credentials/<user_email>/<user_pwd>/<remember_user>', methods=['GET'])
def login(user_email, user_pwd, remember_user):
variables = [user_email]
db_result = open_DB_connection("login", variables, 'users')
db_pwd = ""
if db_result:
db_pwd = db_result[0][2]
encoded_pwd = user_pwd.encode()
hashed_pwd = hashlib.sha256(encoded_pwd)
user = User(db_result[0][0], db_result[0][1], user_email, db_pwd, db_result[0][3] == 'admin')
users.append(user)
if remember_user == "true":
remember_me = True
else:
remember_me = False
if hashed_pwd.hexdigest() == db_pwd:
login_user(user, remember=remember_me)
print("USER LOGGED IN:" + user.name)
return "OK", 200
else:
return "KO", 403
@login_manager.user_loader
def load_user(user_id):
for user in users:
if user.id == int(user_id):
return user
return None
@app.route('/logout')
def logout():
logout_user()
return redirect('index.html')
@app.route('/discard-img/<discard_author>/<dataset>/<ds_type>/<img_name>', methods=['GET'])
def discard_img(img_name, discard_author, ds_type, dataset):
variables = [img_name, discard_author, ds_type]
remove_img_from_user_list(dataset, img_name, current_user.name)
db_result = open_DB_connection("discard_img", variables, 'img_info')
return 'OK', 200
@app.route('/get_annotation_percentages', methods=['GET'])
def get_annotation_percentages():
annotation_ptgs = {}
annotation_ptgs['persons'] = {}
annotation_ptgs['vehicles'] = {}
with open('config.json') as config_file:
config = json.load(config_file)
for agents_type in config['agents_to_annotate']:
for ds in config['agents_to_annotate'][agents_type]:
sum_agents_per_dataset = 0
num_annotated_agents = 0
for distribution in config['agents_to_annotate'][agents_type][ds]:
variables = [agents_type, ds, distribution]
sum_agents_per_dataset += config['agents_to_annotate'][agents_type][ds][distribution]
annotated_agents_in_imgs = open_DB_connection("get_num_annotated_agents", variables, 'img_info')
for img in annotated_agents_in_imgs:
num_annotated_agents += img[0]
annotation_ptgs[agents_type][ds] = math.trunc(num_annotated_agents/sum_agents_per_dataset * 100)
return jsonify(annotation_ptgs)
@app.route('/update_annotated_agents/<img_name>/<num_agents>', methods=['GET'])
def update_annotated_agents(img_name, num_agents):
variables = [img_name, num_agents]
result = open_DB_connection("update_annotated_agents", variables, 'imgs_info')
return 'OK', 200
@app.route('/ia_stats_json', methods=['GET'])
def get_IA_stats():
with open('ia_stats.json') as ia_stats_file:
ia_stats = json.load(ia_stats_file)
return ia_stats
@app.route('/get_user_name', methods=["GET"])
def get_user_name():
return jsonify(current_user.name)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<page>')
def render_page(page):
return render_template(page)
@app.route('/login', methods=['GET'])
def render_login():
next_page = request.args.get('next', None)#Pending solution of redirection to next_page
return render_template("login.html")
@app.route('/annotation.html')
@login_required
def render_annotator():
return render_template("annotation.html")
def walk_error_handler(exception_instance):
print("The specified path is incorrect or permission is needed")
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port='5000')