-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_web.py
497 lines (406 loc) · 19.3 KB
/
main_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
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
# -*- encoding: utf-8 -*-
__author__ = 'Mr.Bemani'
import sys
import os
import glob
import traceback
if getattr(sys, 'frozen', False):
APP_BASE_DIR = os.path.dirname(os.path.abspath(sys.executable))
elif __file__:
APP_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(APP_BASE_DIR)
webserver = None
app_state = "running"
import time
import shutil
import threading
from datetime import datetime
import logging
import multiprocessing as mp
from typing import Union, Callable
import queue
import subprocess as subp
import json
from addict import Dict
import tsutil
import numpy as np
import cv2 as cv
ts_app_logger = logging.getLogger("ts_app_logger")
def restart_program():
"""Restarts the current program, with file objects and descriptors
cleanup
"""
python = sys.executable
os.execl(python, python, * sys.argv)
def exit_program():
"""Restarts the current program, with file objects and descriptors
cleanup
"""
os._exit(0)
############################################################
# set environment variables
os.environ["PC_TEST"] = "0"
############################################################
from flask import Flask, render_template, Response, request, jsonify, send_from_directory, make_response
from werkzeug.serving import make_server
from configure import config, loadConfig, saveConfig
from video import fetch_frame_loop
from magic_happening import process_frame_loop, PF_W, PF_H, frame_update_time
from event_utils import load_event_log
current_frame = None
main_loop_running = False
def main_loop_running_cb():
return main_loop_running
app = Flask(__name__)
frame_queue = queue.Queue(2)
@app.route('/')
def index():
return "<script>window.location.href='/webui/';</script>"
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('css', path)
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('js', path)
@app.route('/assets/<path:path>')
def send_assets(path):
return send_from_directory('assets', path)
@app.route('/sys/solar')
@app.route('/sys/solar/')
def sys_solar():
return send_from_directory('/opt/sys/solar_energy_watcher', 'solar.html')
@app.route('/sys/temperature')
@app.route('/sys/temperature/')
def sys_temperature():
return send_from_directory('/opt/sys/temperature_watcher', 'temperature.html')
@app.route('/video/toggle_motion_map')
def toggle_motion_map():
config.show_motion_map = not config.show_motion_map
return ("1" if config.show_motion_map else "0")
@app.route('/webui/setup', methods=['GET'])
@app.route('/webui/setup/', methods=['GET'])
def webui_setup():
settings_obj = dict(debug=False,
video_src="",
camera_web_url="javascript:void(0);",
preview_quality=80,
preview_width=720,
preview_height=405,
max_detection=500,
output_dir="./outputs",
tracking=dict(det_w=1280,
det_h=720,
dist_thresh=100,
max_object_count=100,
max_skip_frame=2,
max_trace_length=64,
min_rock_pix=24,
max_rock_pix=300,
min_speed_threshold=1.0,
min_y_motion=0.1,
min_y_x_ratio=1.618,
min_trace_length=4,
allow_move_up=False,
move_up_thresh=10),
camera_id="",
location="",
sms=dict(enable=False,
phone="",
sender=""),
vcr_path="/opt/vcr",
roi_mask="./mask.png",
green_max_ratio=0.5,
frame_dist_cm=10.0)
if app_state == "running":
settings_obj = config.to_dict()
return render_template('setup.tpl.html',
video_src=settings_obj['video_src'],
camera_web_url=settings_obj['camera_web_url'],
preview_quality=settings_obj['preview_quality'],
preview_width=settings_obj['preview_width'],
preview_height=settings_obj['preview_height'],
max_detection=settings_obj['max_detection'],
output_dir=settings_obj['output_dir'],
tracking__det_w=settings_obj['tracking']['det_w'],
tracking__det_h=settings_obj['tracking']['det_h'],
tracking__dist_thresh=settings_obj['tracking']['dist_thresh'],
tracking__max_object_count=settings_obj['tracking']['max_object_count'],
tracking__max_skip_frame=settings_obj['tracking']['max_skip_frame'],
tracking__max_trace_length=settings_obj['tracking']['max_trace_length'],
tracking__min_rock_pix=settings_obj['tracking']['min_rock_pix'],
tracking__max_rock_pix=settings_obj['tracking']['max_rock_pix'],
tracking__min_speed_threshold=settings_obj['tracking']['min_speed_threshold'],
tracking__min_y_motion=settings_obj['tracking']['min_y_motion'],
tracking__min_y_x_ratio=settings_obj['tracking']['min_y_x_ratio'],
tracking__allow_move_up=settings_obj['tracking']['allow_move_up'],
tracking__move_up_thresh=settings_obj['tracking']['move_up_thresh'],
sms__enable=settings_obj['sms']['enable'],
sms__phone=settings_obj['sms']['phone'],
sms__sender=settings_obj['sms']['sender'],
camera_id=settings_obj['camera_id'],
description=settings_obj['location'],
vcr_path=settings_obj['vcr_path'],
roi_mask=settings_obj['roi_mask'],
green_max_ratio=settings_obj['green_max_ratio'],
frame_dist_cm=settings_obj['frame_dist_cm'])
@app.route('/api/setup', methods=['POST'])
def api_setup():
global config
if not request.json:
return jsonify({'status': 'error', 'message': 'Invalid request'}), 400
# update config, then saveConfig to settings.yml
try:
config.debug = request.json.get('debug', False)
config.video_src = request.json.get('video_src', '')
config.camera_web_url = request.json.get('camera_web_url', '')
config.preview_quality = request.json.get('preview_quality', 80)
config.preview_width = request.json.get('preview_width', 854)
config.preview_height = request.json.get('preview_height', 480)
config.max_detection = request.json.get('max_detection', 240)
config.output_dir = request.json.get('output_dir', './outputs')
config.tracking.det_w = request.json.get('tracking__det_w', 720)
config.tracking.det_h = request.json.get('tracking__det_h', 720)
config.tracking.dist_thresh = request.json.get('tracking__dist_thresh', 100)
config.tracking.max_object_count = request.json.get('tracking__max_object_count', 30)
config.tracking.max_skip_frame = request.json.get('tracking__max_skip_frame', 3)
config.tracking.max_trace_length = request.json.get('tracking__max_trace_length', 4)
config.tracking.min_rock_pix = request.json.get('tracking__min_rock_pix', 16)
config.tracking.max_rock_pix = request.json.get('tracking__max_rock_pix', 300)
config.tracking.min_speed_threshold = request.json.get('tracking__min_speed_threshold', 1.0)
config.tracking.min_y_motion = request.json.get('tracking__min_y_motion', 0.1)
config.tracking.min_y_x_ratio = request.json.get('tracking__min_y_x_ratio', 1.618)
config.tracking.allow_move_up = True
config.tracking.move_up_thresh = 0
config.camera_id = request.json.get('camera_id', '')
config.location = request.json.get('description', '')
config.sms.enable = request.json.get('sms__enable', False)
config.sms.phone = request.json.get('sms__phone', '')
config.sms.sender = request.json.get('sms__sender', '')
config.vcr_path = request.json.get('vcr_path', '/opt/vcr')
config.roi_mask = request.json.get('roi_mask', './mask.png')
config.green_max_ratio = request.json.get('green_max_ratio', 0.5)
config.frame_dist_cm = request.json.get('frame_dist_cm', 10.0)
ret = saveConfig("settings.yml")
yield "<script>window.location.href='/wait/5';</script>"
time.sleep(2)
webserver.shutdown()
webserver.server_close()
exit_program()
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/webui')
@app.route('/webui/')
def webui():
if app_state == "setup":
return "<script>window.location.href='/webui/setup';</script>"
last_n_alerts = []
try:
last_n_alerts = load_event_log("events.csv", None)[:20]
last_n_alerts = [(datetime.fromtimestamp(int(float(x[5]))),
int(float(x[4])),
round(float(x[1]), 3),
round(float(x[3]), 2),
str(x[7])) for x in last_n_alerts]
except:
# log traceback
ts_app_logger.error(traceback.format_exc())
video_preview_url = "/video_preview"
live_url = config.camera_web_url
location = config.location
camera_id = config.camera_id
target_name = config.target_name
target_tags = config.target_tags
target_desc = config.target_desc
target_range = config.target_range
target_range_unit = config.target_range_unit
return render_template('index.tpl.html',
live_url=live_url,
alerts=last_n_alerts,
video_preview_url=video_preview_url,
location=location, camera_id=camera_id,
target_name=target_name, target_tags=target_tags,
target_desc=target_desc, target_range=target_range,
target_range_unit=target_range_unit)
@app.route('/wait/<int:seconds>')
def web_wait(seconds):
# wait n seconds and href to querystring url
target_url = request.args.get('url', '/')
return render_template('wait.tpl.html', seconds=seconds, target_url=target_url)
@app.route('/api/terminate')
def web_terminate():
yield "<script>window.location.href='/wait/5';</script>"
time.sleep(2)
webserver.shutdown()
webserver.server_close()
exit_program()
@app.route('/api/get_dets')
def api_get_dets():
try:
os.system("rm -rf /tmp/dets.zip")
if os.path.exists("/opt/dets"):
tsutil.zip_dir("/opt/dets/", "/tmp/dets.zip", clear_dir=False)
else:
tsutil.zip_dir("./tmp/", "/tmp/dets.zip", clear_dir=False)
return send_from_directory("/tmp/", "dets.zip", as_attachment=True)
except:
return jsonify({'status': 'error', 'message': 'failed to get dets'}), 500
@app.route('/api/manual_cutoff')
def api_manual_cutoff():
config.manual_cutoff = not config.manual_cutoff
return ("1" if config.manual_cutoff else "0")
@app.route('/api/settings/update')
def webapi_settings_update():
global config
if not request.json:
return jsonify({'status': 'error', 'message': 'Invalid request'}), 400
try:
_up_dict = Dict()
_up_dict.sms = Dict()
_up_dict.sms.enable = request.form.get('sms__enable', False)
_up_dict.sms.phone = request.form.get('sms__phone', '')
_up_dict.sms.sender = request.form.get('sms__sender', '')
_up_dict.tracking = Dict()
_up_dict.tracking.det_w = request.form.get('det_w', 720)
_up_dict.tracking.det_h = request.form.get('det_h', 720)
_up_dict.tracking.dist_thresh = request.form.get('dist_thresh', 5)
_up_dict.tracking.max_object_count = request.form.get('max_object_count', 30)
_up_dict.tracking.max_skip_frame = request.form.get('max_skip_frame', 3)
_up_dict.tracking.max_trace_length = request.form.get('max_trace_length', 5)
_up_dict.tracking.min_rock_pix = request.form.get('min_rock_pix', 16)
_up_dict.tracking.max_rock_pix = request.form.get('max_rock_pix', 300)
_up_dict.tracking.min_speed_threshold = request.form.get('min_speed_threshold', 1.0)
_up_dict.tracking.min_y_motion = request.form.get('min_y_motion', 0.1)
_up_dict.tracking.min_y_x_ratio = request.form.get('min_y_x_ratio', 1.618)
_up_dict.tracking.allow_move_up = request.form.get('allow_move_up', False)
_up_dict.tracking.move_up_thresh = request.form.get('move_up_thresh', 10)
_up_dict.debug = request.form.get('debug', False)
_up_dict.video_src = request.form.get('video_src', '')
_up_dict.camera_web_url = request.form.get('camera_web_url', '')
_up_dict.preview_width = request.form.get('preview_width', 320)
_up_dict.preview_height = request.form.get('preview_height', 180)
_up_dict.max_detection = request.form.get('max_detection', 240)
_up_dict.output_dir = request.form.get('output_dir', './outputs')
_up_dict.vcr_path = request.form.get('vcr_path', '/opt/vcr')
_up_dict.roi_mask = request.form.get('roi_mask', './mask.png')
_up_dict.green_max_ratio = request.form.get('green_max_ratio', 0.5)
_up_dict.frame_dist_cm = request.form.get('frame_dist_cm', 10.0)
_up_dict.camera_id = request.form.get('camera_id', '')
_up_dict.location = request.form.get('location', '')
config.update(_up_dict)
saveConfig("settings.yml")
yield "<script>window.location.href='/wait/5';</script>"
time.sleep(2)
webserver.shutdown()
webserver.server_close()
exit_program()
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/video/get_frame')
def get_frame():
# current frame
encode_param = [int(cv.IMWRITE_JPEG_QUALITY), config.preview_quality]
pw, ph = config.preview_width, config.preview_height
if current_frame is not None:
minifrm = cv.resize(current_frame, (pw, ph), interpolation=cv.INTER_NEAREST)
_, jpgframe = cv.imencode('.jpg', minifrm, encode_param)
return Response(jpgframe.tobytes(), mimetype='image/jpeg')
else:
img = np.zeros((ph, pw, 3), dtype=np.uint8)
cx = pw // 2
cy = ph // 2
## put white color text "No Image" in the center of this black empty frame
cv.putText(img, "No Image", (cx - 100, cy),
cv.FONT_HERSHEY_SIMPLEX, 1.5,
(255, 255, 255), 2)
_, jpgframe = cv.imencode('.jpg', img, encode_param)
return Response(jpgframe.tobytes(), mimetype='image/jpeg')
@app.route('/get_record_video/<record_id>')
def get_record_video(record_id):
record_base = os.path.join(config.output_dir, record_id)
output_mp4 = os.path.join(record_base, "augmented.mkv")
if os.path.exists(output_mp4):
file_resp = send_from_directory(record_base, "augmented.mkv")
resp = make_response(file_resp)
dt = datetime.fromtimestamp(int(record_id)//1000).strftime("%Y%m%d_%H%M%S")
resp.headers["Content-Disposition"] = f"attachment; filename={dt}.mkv"
return resp
elif not os.path.exists(output_mp4):
return jsonify({'status': 'error', 'message': 'Video not ready'}), 404
def run_setup_loop():
pass # to-do
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Rock detection and tracking')
parser.add_argument('--debug', action='store_true', help='Enable debug mode')
parser.add_argument('--cfg', type=str, help='Config file path', default="settings.yml")
parser.add_argument('-i', '--video_src', type=str, help='Video source')
parser.add_argument('-o', '--output_dir', type=str, help='Output video file basepath')
parser.add_argument('-v', '--vcr_dir', type=str, help='VCR directory')
parser.add_argument('--port', type=int, help='Webserver port', default=8080)
args = parser.parse_args()
if args.cfg is None or args.cfg == "":
args.cfg = os.path.join(APP_BASE_DIR, "settings.yml")
# load config
print (args.cfg)
has_config = loadConfig(args.cfg)
print (config)
if not has_config:
app_state = "setup"
#if app_state == "setup":
# run_setup_loop()
# exit_program()
config.show_motion_map = False
if args.debug is True:
config.debug = args.debug
if args.video_src is not None and args.video_src != "":
config.video_src = args.video_src
if args.output_dir is not None and args.output_dir != "":
config.output_dir = args.output_dir
if args.vcr_dir is not None and args.vcr_dir != "":
config.vcr_path = args.vcr_dir
print (config)
if not os.path.exists(config.output_dir):
os.makedirs(config.output_dir)
if not os.path.exists("events.csv"):
with open("events.csv", "w", encoding="utf-8") as f:
f.write("")
f.flush()
pass
# manual cutoff for detection and tracking
config.manual_cutoff = False
current_frame = np.zeros((PF_H, PF_W, 3), dtype=np.uint8)
# clear tmp dir
if not os.path.exists("./tmp"):
#shutil.rmtree("./tmp")
os.mkdir("./tmp")
main_loop_running = True
# start video fetch loop
video_fetch_thread = threading.Thread(target=fetch_frame_loop,
args=(config.video_src, main_loop_running_cb, frame_queue),
daemon=True)
video_fetch_thread.start()
# start video processing loop
extra_info = Dict()
video_process_thread = threading.Thread(target=process_frame_loop,
args=(config, main_loop_running_cb, frame_queue, current_frame, extra_info),
daemon=True)
video_process_thread.start()
############################################################
# start webserver
try:
print ("Start webserver...")
ts_app_logger.info('Starting Webserver...')
#app.run(host="0.0.0.0", port=8080, debug=config.debug, threaded=True)
webserver = make_server("0.0.0.0", args.port, app, threaded=True)
ts_app_logger.info('Webserver started.')
webserver.serve_forever()
except KeyboardInterrupt:
ts_app_logger.info('KeyboardInterrupt, stopping...')
print ("Application is Deinitializing...", end='', flush=True)
main_loop_running = False
time.sleep(0.5)
video_fetch_thread.join(timeout=3)
video_process_thread.join(timeout=3)
print ("OK")
if config.debug:
print ("Server closed.")