-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivecv.py
111 lines (96 loc) · 2.82 KB
/
livecv.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
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import struct
import sys
from StringIO import StringIO
import threading
import cv2
import Queue
import signal
import numpy as np
class Common:
"""Common Handler"""
def __init__(self):
self.last = None
self.lasthandler = None
self.queue = Queue.Queue(1)
def newcode(self,x,h):
"""New code x from the handler h. Store and push in queue"""
self.last = x
self.lasthandler = h
if not self.queue.empty():
self.queue.get()
self.queue.put(x)
def lastcode(self):
"""Last without queue"""
r = self.last
self.last = None
return r
def lastcodewait(self):
"""Last using queue and wait"""
return self.queue.get()
def sendback(self,msg):
"""Send back to the websocket"""
self.lasthandler.write_message(msg,binary=False)
common = Common()
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
self.set_nodelay(True)
self.write_message("The server says: 'Hello'. Connection was accepted.")
def select_subprotocol(self,protocols):
print "protocols",len(protocols),protocols
if len(protocols) > 0:
return protocols[0]
else:
return
def on_message(self, message):
common.newcode(message,self)
#self.write_message("Ok " + buffer.getvalue(),binary=False)
def check_origin(self,origin):
print "check origin",origin
return True
def on_close(self):
print 'connection closed...'
application = tornado.web.Application([
(r'/x', WSHandler),
(r"/", tornado.web.RedirectHandler, {"url": "/index.html"}),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": "."}),
])
#WebSocket connection to 'ws://127.0.0.1:8000/' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
#https://github.com/gdi2290/angular-websocket/issues/13
def _server_runner(app):
app.listen(8000)
tornado.ioloop.IOLoop.instance().start()
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
def imshow(x):
cv2.imshow("live",x)
def main():
th = threading.Thread(target = _server_runner, args = [application])
th.daemon = True
th.start()
signal.signal(signal.SIGINT, signal_handler)
cv2.namedWindow("live")
data = {} # for holding stuff safely
while True:
cc = common.lastcode()
# Passive wait on Queue: 1) no SIGINT, 2) hourglass over window
#cc = common.lastcodewait()
if cc is not None:
buffer = StringIO()
sys.stdout = buffer
good = True
try:
exec cc
except Exception ,e:
common.sendback("Exception " + str(e))
good = False
if good:
common.sendback("Ok " + buffer.getvalue())
sys.stdout = sys.__stdout__
cv2.waitKey(1)
if __name__ == "__main__":
main()