-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
280 lines (253 loc) · 9.97 KB
/
app.py
File metadata and controls
280 lines (253 loc) · 9.97 KB
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
from asyncio import Task
from threading import Event
from quart import Quart, request, render_template, websocket as ws
from quart.datastructures import FileStorage
from json import dumps, loads
from pathlib import Path
from common import TEMP_FOLDER, BackendError, ensure_directories
from collections import namedtuple
from time import sleep
import os.path
import tempfile
import video_tool
app = Quart(__name__)
ALLOWED_EXTENSIONS = {'.mp4'}
ensure_directories()
VideoInfo = namedtuple('VideoInfo', [
'id', 'name', 'resolution', 'status', 'total_frames', 'extracted_frames',
'labeled_frames', 'excluded_frames'
])
def format_video_info(video_id, raw_info_tuple):
if raw_info_tuple[0]:
return VideoInfo(video_id, raw_info_tuple[1], raw_info_tuple[2],
"PROCESSED", raw_info_tuple[3], raw_info_tuple[3],
raw_info_tuple[4], raw_info_tuple[5])
else:
return VideoInfo(video_id, raw_info_tuple[1], None,
raw_info_tuple[2].name.split(".")[1], raw_info_tuple[3],
raw_info_tuple[4], None, None)
@app.before_serving
async def before_serving():
await video_tool.load_videos()
@app.after_serving
async def after_serving():
await video_tool.save_videos()
# web page routes
@app.route('/')
@app.route('/index')
@app.route('/index.html')
async def index_page():
videos_result = await video_tool.get_all_videos()
if not videos_result.is_success:
return await render_template('index.html.j2', videos=[])
video_infos: dict = videos_result.data
videos = []
for video_id, video_info in video_infos.items():
formatted_info = format_video_info(video_id, video_info)
if formatted_info.status != "PROCESSED":
formatted_info = formatted_info._replace(resolution="-",
labeled_frames="-",
excluded_frames="-")
videos.append(formatted_info)
return await render_template('index.html.j2', videos=videos)
@app.route('/label')
@app.route('/label.html')
async def label_page():
video_id = request.args.get('video_id', None)
if video_id is None:
return await render_template('error.html.j2',
error_code=404,
error_message="No video id provided")
video_result = await video_tool.get_video_info(video_id)
if not video_result.is_success:
return await render_template('error.html.j2',
error_code=404,
error_message="No matching video found")
video_info = video_result.data
if not video_info[0]:
return await render_template('error.html.j2',
error_code=404,
error_message="Video not processed")
return await render_template('label.html.j2',
frame_height=video_info[2][1],
frame_width=video_info[2][0],
video=format_video_info(video_id, video_info))
@app.route('/error')
@app.route('/error.html')
async def error_page():
code = request.args.get('code', 500)
try:
code = int(code)
except ValueError:
code = 500
return await render_template('error.html.j2', error_code=code)
@app.route('/js/<path:path>')
async def send_js(path):
if os.path.exists('static/js/' + path):
return await app.send_static_file('js/' + path)
else:
return await render_template('error.html.j2', error_code=404)
@app.route('/css/<path:path>')
async def send_css(path):
if os.path.exists('static/css/' + path):
return await app.send_static_file('css/' + path)
else:
return await render_template('error.html.j2', error_code=404)
@app.route('/img/<path:path>')
async def send_img(path):
if os.path.exists('static/img/' + path):
return await app.send_static_file('img/' + path)
else:
return await render_template('error.html.j2', error_code=404)
@app.route('/frame/<string:frame_id>', methods=['GET'])
async def get_frame_png(frame_id):
result = video_tool.get_frame_png(frame_id)
if not result.is_success:
return await app.send_static_file('img/placeholder.png')
return await app.send_static_file(result.data)
@app.route('/favicon.ico')
async def send_favicon():
return await app.send_static_file('favicon.ico')
# api routes
@app.route('/api')
async def api_root():
welcome = {"welcome": "Welcome to the FTC Machine Learning API", "status": 0}
return dumps(welcome)
@app.route("/api/videos")
async def api_all_videos():
videos = await video_tool.get_all_videos()
if not videos.is_success:
return dumps({"status": videos.status, "error": videos.message})
return dumps({"status": 0, "videos": videos.data})
@app.route("/api/video/<string:video_id>", methods=['GET'])
async def api_video_info(video_id):
result = await video_tool.get_video_info(video_id)
if not result.is_success:
return dumps({"status": result.status, "error": result.message})
processed, data = result.data[0], result.data[1:]
if processed:
return dumps({
"status": 0,
"video_type": "processed",
"video_resolution": data[0],
"total_frame_count": data[1],
"labeled_frame_count": data[2],
"excluded_frame_count": data[3]
})
return dumps({
"status": 0,
"video_type": "processing",
"video_status": data[0],
"total_frame_count": data[1],
"extracted_frame_count": data[2]
})
@app.route('/api/video/upload', methods=['POST'])
async def api_video_upload():
nfu = BackendError.NO_FILE_UPLOADED
uft = BackendError.UNSUPPORTED_FILE_TYPE
if 'file' not in await request.files:
return dumps({"status": nfu, "error": nfu.error_message})
file: FileStorage = (await request.files)['file']
if not file:
return dumps({"status": nfu, "error": nfu.error_message})
assert isinstance(file, FileStorage)
if not file.filename:
return dumps({"status": nfu, "error": nfu.error_message})
if not '.' in file.filename and Path(
file.filename).suffix in ALLOWED_EXTENSIONS:
return dumps({"status": uft, "error": uft.error_message})
tmp_file_path = tempfile.mkstemp(dir=str(TEMP_FOLDER),
prefix="vid",
suffix='.mp4')[1]
await file.save(tmp_file_path) # type: ignore
file.close()
if 'name' in await request.form:
name = (await request.form)['name']
else:
name = file.filename.rsplit('.')[0]
video_name, video_id = (await video_tool.upload_video(name,
tmp_file_path)).data
return dumps({"status": 0, "video_id": video_id, "video_name": video_name})
@app.route('/api/video/<string:video_id>/cancel', methods=['GET'])
async def api_frame_extract_cancel(video_id):
result = await video_tool.cancel_process(video_id)
if not result.is_success:
return dumps({"status": result.status, "error": result.message})
return dumps({"status": 0})
@app.route('/api/video/<string:video_id>/frames/<int:index>', methods=['GET'])
async def api_read_frame(video_id, index):
result = await video_tool.read_frame(video_id, index)
if not result.is_success:
return dumps({"status": result.status, "error": result.message})
return dumps({
"status": 0,
"frame_id": result.data[0],
"frame_labels": result.data[1]
})
@app.route('/api/video/<string:video_id>/frames/<int:index>/label',
methods=['POST'])
async def api_label_frame(video_id, index):
if 'label' not in await request.json or 'box' not in await request.json:
ir = BackendError.INVALID_REQUEST
return dumps({"status": ir, "error": ir.error_message})
label = (await request.json)['label']
box = (await request.json)['box']
result = await video_tool.label_frame(video_id, index, label, box)
if not result.is_success:
return dumps({"status": result.status, "error": result.message})
return dumps({"status": 0, "label_id": result.data})
@app.route('/api/video/<string:video_id>/frames/<int:index>/unlabel',
methods=['GET'])
async def api_unlabel_frame(video_id, label_id):
result = await video_tool.unlabel_frame(video_id, label_id)
if not result.is_success:
return dumps({"status": result.status, "error": result.message})
return dumps({"status": 0})
@app.route('/api/video/<string:video_id>/frames/<int:index>/exclude',
methods=['GET'])
async def api_exclude_frame(video_id, index):
result = await video_tool.exclude_frame(video_id, index)
if not result.is_success:
return dumps({"status": result.status, "error": result.message})
return dumps({"status": 0})
@app.websocket('/api/video/<string:video_id>/object_tracking')
async def api_object_tracking(video_id: str):
params = await ws.receive()
if not params or (type(params) != str and type(params) != bytes):
await ws.close(1007)
return
params = loads(params)
if 'start' not in params or 'algorithm' not in params:
await ws.close(1007)
return
frame_index = params['start']
algorithm = params['algorithm']
result = await video_tool.start_object_tracking(video_id, frame_index,
algorithm)
if not result.is_success:
if result.status in [
BackendError.VIDEO_NOT_FOUND, BackendError.VIDEO_PROCESSING,
BackendError.FRAME_NOT_FOUND, BackendError.NO_MORE_FRAMES,
BackendError.FRAME_NOT_LABELED
]:
code = 1011
elif result.status == BackendError.UNACCEPTABLE_ALGORITHM:
code = 1008
else:
code = 1006
await ws.close(code, result.message)
task: Task = result.data[0]
continue_event: Event = result.data[1]
while not task.done():
while continue_event.is_set():
sleep(0.1)
frame_index += 1
frame_result = await video_tool.read_frame(video_id, frame_index)
if not frame_result.is_success:
await ws.close(1011, frame_result.message)
task.cancel()
return
frame_id, frame_labels = frame_result.data
await ws.send(dumps({"frame_id": frame_id, "frame_labels": frame_labels}))
continue_event.set()
await ws.close(1000)