forked from ami-GS/RPi_CAMStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piCameraClient.py
214 lines (177 loc) · 5.71 KB
/
piCameraClient.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
from ws4py.client.tornadoclient import TornadoWebSocketClient
import zlib
from tornado.ioloop import IOLoop, PeriodicCallback
import tornado, tornado.web, tornado.httpserver
from tornado.websocket import WebSocketHandler
import cv2.cv as cv
import cv2
from threading import Thread
import time
import sys
import wsaccel
wsaccel.patch_tornado()
import json
from imageprocess import ImageProcess
INTERVAL = 10#100
FPS = 30
status = True
FRAME = ""
class ReceiveWebSocket(TornadoWebSocketClient):
def __init__(self, url, protocols=None, Show=None, extensions=None,
io_loop=None, ssl_options=None, headers=None):
TornadoWebSocketClient.__init__(self, url, protocols=protocols, extensions=extensions,
io_loop=io_loop, ssl_options=ssl_options, headers=headers)
self.connecting = False
self.Show = Show
self.url = url
self.protocols = protocols
def opened(self):
print("connected to root server")
self.connecting = True
def received_message(self, m):
try:
global FRAME
FRAME = str(m)
m = zlib.decompress(str(m))
self.Show.loop(m)
except Exception as e:
m = json.loads(str(m))
if m[0] == "EXIT":
print "This client IP is already connecting"
self.closed()
#============For tree p2p================
#
self._p2p_proto(m)
#
#=============================
def _p2p_proto(self, m):
openPort = m[3]
t = Thread(target=startWSServer, args=(str(openPort), self.Show),)
t.setDaemon(True)
t.start()
if m[0] == "REDIRECT":
host, port= m[1], m[2]
print "Redirected to "+host+":"+port
#IOLoop.instance().stop()
con(host, port, self.Show)
elif m[0] == "KEEP":
print "Keep connection"
def closed(self, code, reason=None):
print "connection closed by:", reason
self.Show._finish()
class WSHandler(WebSocketHandler):
def initialize(self, Show, ioloop):
self.Show = Show
self.period = 0.1
self.leafClient = False
self.ioloop = ioloop
import threading
t = Thread(target=self.ioloop.start)
t.setDaemon(True)
t.start()
print threading.active_count()
def open(self):
#self.callback = PeriodicCallback(self._send_image, self.period)
#self.callback.start()
print self.request.remote_ip, "connected here"
self.haveLeafClient = True
t = Thread(target=self._send_image)
t.setDaemon(True)
t.start()
def _send_image(self):
while True:
self.write_message(FRAME, binary=True)
# frame = self.Frames[0]
# m = zlib.compress(frame)
#self.write_message("aiueo", binary=True)
def on_message(self):
pass
def on_close(self):
print self.request.remote_ip, "connection closed from here"
print self._reason
pass
class ShowPicture():
def __init__(self):
import numpy as np
self.np = np
cv2.namedWindow("RPiCAM", 1)
self.Frames = []
def run(self):
while status:
if len(self.Frames):
decimg = self._decode_image(self.Frames.pop(0))
self._show_image(decimg)
if cv.WaitKey(INTERVAL) == 27:
self._finish()
break
def loop(self, img):
if cv.WaitKey(1000/FPS) == 27:
self._finish()
decimg = self._decode_image(img)
self._show_image(decimg)
def _decode_image(self, img):
narray = self.np.fromstring(img, dtype="uint8")
decimg = cv2.imdecode(narray, 1)
return decimg
def _show_image(self, img):
cv2.imshow('RPiCAM', img)
def set_image(self, img):
self.Frames.append(img)
def _finish(self):
global status
status = False
IOLoop.instance().stop()
cv2.destroyAllWindows()
def getTreeP2PHost(host, port):
request = tornado.httpclient.HTTPRequest(url="http://"+host+":"+port+"/", method="GET")
client = tornado.httpclient.HTTPClient()
try:
response = client.fetch(request)
if response.body:
host = response.body
except Exception as e:
print e
return host
def startWSServer(port, Show):
ioloop = IOLoop.current()
app = tornado.web.Application([
(r"/camera", WSHandler, dict(Show=Show, ioloop=ioloop)),
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(port)
print "start server"
def con(host, port, Show):
trial = 0
ws = ReceiveWebSocket("ws://"+host+":"+port+"/camera",
protocols=["http-only", "chat"], Show=Show)
while (not ws.connecting) and trial < 20:
print trial ,
trial += 1
time.sleep(0.5)
ws = ReceiveWebSocket("ws://"+host+":"+port+"/camera",
protocols=["http-only", "chat"], Show=Show)
ws.connect()
IOLoop.current().start()
if trial >= 20:
print "Connection timeout"
elif ws.connecting:
pass
def main(host="localhost", port="8080"):
def initThread(thread):
thread.setDaemon(True)
return thread
#host = getTreeP2PHost(host, port)
Show = ShowPicture()
con(host, port, Show)
HELP = "Usage : piCameraClient.py [host] [port]"
if __name__ == "__main__":
args = sys.argv
if len(args) == 1:
print HELP
main()
elif len(args) == 2:
main(args[1])
elif len(args) == 3:
main(args[1], args[2])
else:
print HELP