-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
166 lines (134 loc) · 3.44 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
import manager as manager
from flask import Flask, Response, request, render_template
from flask_socketio import SocketIO, send, emit
from engineio.payload import Payload
from threading import Thread
import fps as FPS
app = Flask(__name__,
static_folder="frontend",
template_folder='frontend')
Payload.max_decode_packets = 74
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app) # , logger=True
manager = manager.Manager('color', socketio)
streaming = True
fps = FPS.FPS()
@app.after_request
def add_header(r):
"""
Deshabilita cache
"""
r.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate, public, max-age=0'
r.headers['Pragma'] = 'no-cache'
r.headers['Expires'] = '0'
return r
def stream():
global streaming
while streaming:
stringData = manager.getFrame()
fps.update()
yield(b'--frame\r\n'b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')
def start():
socketio.run(app, host='0.0.0.0')
@app.route('/video')
def video():
return Response(stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/')
def main():
return render_template('./index.html')
@app.route('/config-colores')
def info():
return render_template('./views/config-colores.html')
@app.route('/modo/<modo>', methods=['GET', 'POST'])
def cambiarModo(modo):
global manager
params = request.json
dicParams = {
'showID': params["showID"],
'showTxt': params["showTxt"],
'showCentroid': params["showCentroid"],
'showBoundingRect': params["showBoundingRect"],
'showMask': params["showMask"],
'position': 'center',
'drawContours': params["drawContours"],
'measure': params["showMeasure"],
'countItems': False
}
manager.setVariables(dicParams)
manager.cambioModo(modo)
return modo
@app.route('/start')
def startStream():
global streaming
fps.start()
streaming = True
Thread(target=stream, args=()).start()
return 'OK'
@app.route('/stop')
def stopStream():
global streaming
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
streaming = False
return 'OK'
@app.route('/velocidad/<velocidad>')
def velocidad(velocidad):
global manager
manager.moverCinta(velocidad)
return 'OK'
@app.route('/direccion')
def direccion():
global manager
manager.cambiarDireccionCinta()
return 'OK'
@app.route('/send_picture')
def sendPicture():
global manager
manager.sendPicture()
return 'OK'
@app.route('/params/showID')
def showID():
global manager
manager.toggleShowID()
return 'OK'
@app.route('/params/showCentroid')
def showCentroid():
global manager
manager.toggleShowCentroid()
return 'OK'
@app.route('/params/drawContours')
def drawContours():
global manager
manager.toggleDrawContours()
return 'OK'
@app.route('/params/showTxt')
def showTxt():
global manager
manager.toggleShowTxt()
return 'OK'
@app.route('/params/showBoundingRect')
def showBoundingRect():
global manager
manager.toggleShowBoundingRect()
return 'OK'
@app.route('/params/showMeasure')
def showMeasure():
global manager
manager.toggleShowMeasure()
return 'OK'
@app.route('/params/showMask')
def showMask():
global manager
manager.toggleShowMask()
return 'OK'
@socketio.on('getColores')
def handle_message(msg):
emit('colores', manager.getColorRanges())
@socketio.on('setColores')
def handle_message(colores):
manager.setColorRanges(colores)
def sendDetectedObject(obj):
socketio.emit('addList', obj)
if __name__ == '__main__':
start()