-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
125 lines (92 loc) · 3.05 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
from flask import (
Flask,
jsonify,
render_template,
request,
Response,
send_from_directory,
)
from flask_socketio import SocketIO, emit
import time
from threading import Thread, Event
from utils.misc import encode
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
socketio = SocketIO(app, cors_allowed_origins="*", async_mode=None, logger=False, engineio_logger=False)
input = []
num_cams = 0
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
return "Dude...Implement Basic Auth!"
if request.method == "GET":
return render_template("index.html")
@app.route("/login", methods=["POST"])
def login():
if request.method == "POST":
return render_template("get_num_cam_info.html")
@app.route("/logout", methods=["POST"])
def logout():
if request.method == "POST":
print("User logged out")
return render_template("index.html")
@app.route("/info/1", methods=["GET", "POST"])
def get_num_cams():
global input, num_cams
if request.method == "POST":
num_cams = int(request.form.get("num_cams"))
ans = request.form.get("ques")
if ans == "yes":
input = [i for i in range(num_cams)]
return render_template("dashboard.html", num_cams=num_cams)
return render_template("get_cam_info.html", num_cams=num_cams)
if request.method == "GET":
return render_template("get_num_cam_info.html")
@app.route("/info/2", methods=["POST"])
def get_input_urls():
global input
if request.method == "POST":
data = dict(request.form)
input = [data["cam_" + str(i+1)] for i in range(num_cams)]
return render_template("dashboard.html", num_cams=num_cams)
if request.method == "GET":
return render_template("dashboard.html")
# socket routes below
thread = Thread()
thread_stop_event = Event()
def getupdatedinfo():
global frames, input
import main
from main import main
callback = main(input)
print("Starting stream")
while not thread_stop_event.isSet():
t1 = time.time()
print("Takes {} time to get 1 batch of frames".format(time.time()-t1))
resp = next(callback)
frames = [ encode(frame[0]) for frame in resp ]
frame_counts = [ frame[1] for frame in resp ]
total_count = sum(frame_counts)
data = {
'total_count': total_count,
'frame_counts': frame_counts,
'frames': frames
}
socketio.emit('newdata', data, namespace='/test')
# time.sleep(1)
# socketio.sleep(2)
@socketio.on('connect', namespace='/test')
def test_connect():
global thread
print("Client Connected")
if not thread.isAlive():
print("Starting Thread")
thread = socketio.start_background_task(getupdatedinfo)
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
global thread
thread_stop_event.set()
print('Client disconnected')
if __name__ == "__main__":
socketio.run(app, debug=True)