forked from ConSurv/ThEmoBe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
381 lines (299 loc) · 14.8 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
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
import asyncio
import os
import time
import uuid
import ast
import threading
from flask import Flask, send_file
from flask import request, jsonify
from flask_api import status
from flask_migrate import Migrate
from flask_celery import make_celery
from sqlalchemy.ext.automap import automap_base
from sqlalchemy import Table, create_engine
from sqlalchemy import create_engine, MetaData, Table, Column
from annotation_pipeline import annotateVideo
from config import Config
from flask_sqlalchemy import SQLAlchemy
from pollingManager import handlePolling
# from model.load_behaviour_model_from_checkpoint_2 import *
# from model.loading_emotion_model_7 import *
import sys
def sizeof_fmt(num, suffix='B'):
''' by Fred Cirera, https://stackoverflow.com/a/1094933/1870254, modified'''
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix)
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
class Tasks(db.Model):
__tablename__ = 'tasks_table'
id = db.Column(db.String(128), primary_key=True)
themobe_id = db.Column(db.String(128),index=True, unique=True)
expires_in = db.Column(db.BigInteger)
interval= db.Column(db.BigInteger)
last_polled_time = db.Column(db.BigInteger)
download_allocation_time = db.Column(db.BigInteger)
download_req_id = db.Column(db.String(128),index=True, unique=True)
task_status = db.Column(db.String(100))
download_count = db.Column(db.Integer)
persistent_status = db.Column(db.Boolean)
def __repr__(self):
# return '<Tasks {}>'.format(self.themobe_id)
return "<Tasks(theombe_id='%s', interval='%s', task_status='%s')>" % (self.themobe_id, self.interval, self.task_status)
# return '<Tasks %r>' % self.themobe_id'
def getExpiresin(self):
return self.expires_in
db.create_all()
migrate = Migrate(app, db)
# db.init_app(app)
# db.create_all()
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
celery = make_celery(app)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
DEFAULT_EXPIRES_IN = 900 # In sec
DEFAULT_POLLING_INTERVAL = 2 # In sec
DEFAULT_PERSISTENT_STATUS = True
# behaviour_model, emotion_model = None, None
# print("dgssdhfffdjdjjdjjjdfjdsj")
# @app.before_first_request
# def do_something_only_once():
# global behaviour_model, emotion_model
# print("111111111111111111111111111111111111111111111111111111111111")
# behaviour_model = create_behaviour_model_from_checkpoint()
# emotion_model = create_emotion_model_from_checkpoint()
# print("222222222222222222222222222222222222222222222222222222222222")
# print("3333333333333333333 Initialized models 3333333333333333333333333333333333333333333")
print("=============== Memory usage ====================")
for name, size in sorted(((name, sys.getsizeof(value)) for name, value in locals().items()),
key= lambda x: -x[1])[:10]:
print("{:>30}: {:>8}".format(name, sizeof_fmt(size)))
print("=================================================")
@app.route("/annotate", methods=["POST"])
def annotate():
target = "/".join([APP_ROOT, "toAnnotate"])
if not os.path.isdir(target):
os.mkdir(target)
# Obtain filename
file = request.files['video']
filename = file.filename
# Check for video formats
if not (".mp4" in file.filename):
response = {"error_id": "Bad Request", "error_message": "video file missing"}
return jsonify(response), status.HTTP_400_BAD_REQUEST
id = uuid.uuid1()
# Save file
if file:
# Adding to datastore/database- temporarily
video_file = target + '/' + str(id) + '.mp4'
print("dest:" + target)
file.save(video_file)
print("request form ", request.form)
# Annotate video for emotions
if 'emo' in request.form:
emo_annotation = ast.literal_eval(request.form['emo'])
else:
response = {"error_id": "Bad Request", "error_message": "parameters missing : 'emo' "}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if 'behav' in request.form:
behav_annotation = ast.literal_eval(request.form['behav'])
else:
response = {"error_id": "Bad Request", "error_message": "parameters missing : 'behav' "}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if 'threat' in request.form:
threat_annotation = ast.literal_eval(request.form['threat'])
else:
response = {"error_id": "Bad Request", "error_message": "parameters missing : 'emo' "}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if 'expires_in' in request.form:
requested_expiry = request.form['expires_in']
if (int(requested_expiry) > DEFAULT_EXPIRES_IN):
# Cannot allow a expires_in more than default
requested_expiry = DEFAULT_EXPIRES_IN
else:
requested_expiry = DEFAULT_EXPIRES_IN
if 'persistent_status' in request.form:
persistent_status = request.form['persistent_status']
persistent_status = persistent_status
else:
persistent_status = DEFAULT_PERSISTENT_STATUS
# Saving task details to database and call annotation
last_polled_time = int(round(time.time() * 1000))
hashed_id = str(uuid.uuid4())
task = Tasks(id=hashed_id, themobe_id=str(id), expires_in=requested_expiry,
interval=DEFAULT_POLLING_INTERVAL,last_polled_time=last_polled_time,
task_status="PROCESSING",download_count=0,persistent_status=persistent_status)
db.session.add(task)
db.session.commit()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# asyncio.ensure_future(annotateAsync(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation,str(id)))
# annotateAsync(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation, str(id))
# result=asyncio.ensure_future(annotateAsync(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation,str(id)))
# Need to be asyncr
celery_annotate.delay(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation, id)
print("Job sent to celery")
response = {"themobe_id": id, "video": filename, "video_status": "processing", "expires_in": requested_expiry,
"interval": DEFAULT_POLLING_INTERVAL}
return jsonify(response)
@celery.task(name='app.celery_annotate')
def celery_annotate(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation, id):
annotateVideo(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation, id)
print("++++++++++++++++ celery got the work ++++++++++++++++")
# modify database
try:
task = db.session.query(Tasks)
task = task.filter(Tasks.download_req_id == id)
record = task.one()
current_time = int(round(time.time() * 1000))
record.task_status = "ANNOTATED"
record.download_allocation_time = current_time
record.download_req_id = str(uuid.uuid4())
db.session.commit()
print("Database annotated for completeion")
except:
print("Error in updating annotation completion")
# async def annotateAsync(APP_ROOT, video_file, emo_annotation, behav_annotation, threat_annotation,id):
# # await asyncio.sleep(20)
# print("commited changes")
# task = db.session.query(Tasks)
# task = task.filter(Tasks.themobe_id == id)
# record = task.one()
# current_time = int(round(time.time() * 1000))
# record.task_status = "ANNOTATED"
# record.download_allocation_time = current_time
# record.download_req_id = str(uuid.uuid4())
# db.session.commit()
# print("commited changes")
# return "sucessfull"
@app.route("/poll")
def polling():
if 'themobe_id' in request.args:
id = request.args.get('themobe_id')
if (db.session.query(Tasks).filter_by(themobe_id=id).scalar()) is None:
response = {"error_id": "Unauthorized Request", "error_message": "'themobe_id' does not exist"}
return jsonify(response), status.HTTP_401_UNAUTHORIZED
else:
# task = models.Tasks.query().filter(models.Tasks.themobe_id == id).first()
task = db.session.query(Tasks)
task = task.filter(Tasks.themobe_id == id)
record = task.one()
current_time = int(round(time.time() * 1000))
interval = record.interval
last_polled_time = record.last_polled_time
task_status = record.task_status
download_req_id = record.download_req_id
# Update interval when polling is heavy
if (last_polled_time + interval * 1000 > current_time):
record.interval = interval + 3
db.session.commit()
response = {"error_id": "slow down", "error_message": "heavy polling adding load to endpoint"}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if (task_status == "DOWNLOADED" or task_status == "EXPIRED"):
# update polling
record.last_polled_time = current_time
db.session.commit()
response = {"error_id": "task completed", "error_message": "task already downlaoded or expired."}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if (task_status == "PROCESSING"):
# update polling
record.last_polled_time = current_time
db.session.commit()
response = {"error_id": "task not completed", "error_message": "annotation engine is still processing the video"}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if (task_status == "ANNOTATED"):
# update polling
record.last_polled_time = current_time
db.session.commit()
response = {"download_req_id":download_req_id,"task status": "Annotated","download status": "Downloadable"}
return jsonify(response), status.HTTP_400_BAD_REQUEST
else:
response = {"error_id": "Bad Request", "error_message": "'themobe_id' is missing"}
return jsonify(response), status.HTTP_400_BAD_REQUEST
# async def annotateVideoAsync(APP_ROOT, video_path, emo_annotation, behav_annotation, threat_annotation, video_id):
# await asyncio.sleep(10)
# task = db.session.query(models.Tasks)
# task = task.filter(models.Tasks.themobe_id == video_id)
# record = task.one()
# record.task_status = "ANNOTATED"
# print("commited change")
# db.session.commit()
# # return jsonify({"result": result})
# return 1
#
def manageDownload(id, APP_ROOT):
path = "/".join([APP_ROOT, "output"])
# print(path)
video = path + '/' + id + '.mp4'
return send_file(video, as_attachment=True)
@app.route("/download")
def downloadFile():
if 'download_req_id' in request.args:
download_req_id = request.args.get('download_req_id')
if (db.session.query(Tasks).filter_by(download_req_id=download_req_id).scalar()) is None:
response = {"error_id": "Unauthorized Request", "error_message": "'download_req_id' does not exist"}
return jsonify(response), status.HTTP_401_UNAUTHORIZED
else:
# task = models.Tasks.query().filter(models.Tasks.themobe_id == id).first()
task = db.session.query(Tasks)
task = task.filter(Tasks.download_req_id == download_req_id)
record = task.one()
task_status = record.task_status
download_count=record.download_count
persistent_status=record.persistent_status
download_allocation_time = record.download_allocation_time
expires_in = record.expires_in
current_time = int(round(time.time() * 1000))
themobe_id = record.themobe_id
# check for the authenticity of id
if (download_allocation_time + expires_in*1000 < current_time):
response = {"error_id": "access denied", "error_message": "'download_req_id' expired."}
record.task_status = "EXPIRED"
db.session.commit()
return jsonify(response), status.HTTP_400_BAD_REQUEST
if (task_status == "PROCESSING"):
response = {"error_id": "Bad Request", "error_message": "wrong endpoint. Task is still being processed."}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if (task_status == "EXPIRED"):
response = {"error_id": "Bad Request", "error_message": "Task expired. Try again."}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if (task_status == "DOWNLOADED"):
if(download_count>5):
response = {"error_id": "Bad Request", "error_message": "maximum downloads reached."}
return jsonify(response), status.HTTP_400_BAD_REQUEST
else:
record.download_count = download_count + 1
db.session.commit()
if (persistent_status == 0):
path = "/".join([APP_ROOT, "output"])
annotated_video = path + '/' + themobe_id + '.mp4'
original_path = "/".join([APP_ROOT, "toAnnotate"])
original_video = original_path + '/' + themobe_id + '.mp4'
if os.path.exists(original_video):
os.remove(original_video)
if os.path.exists(annotated_video):
os.remove(annotated_video)
return manageDownload(themobe_id, APP_ROOT)
if(task_status == "ANNOTATED"):
record.task_status = "DOWNLOADED"
record.download_count=download_count+1
db.session.commit()
if (persistent_status == False):
path = "/".join([APP_ROOT, "output"])
annotated_video = path + '/' + themobe_id + '.mp4'
original_path = "/".join([APP_ROOT, "toAnnotate"])
original_video = original_path + '/' + themobe_id + '.mp4'
if os.path.exists(original_video):
os.remove(original_video)
if os.path.exists(annotated_video):
os.remove(annotated_video)
return manageDownload(themobe_id, APP_ROOT)
else:
response = {"error_id": "Bad Request", "error_message": "'themobe_id' is missing"}
return jsonify(response), status.HTTP_400_BAD_REQUEST
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)