-
Notifications
You must be signed in to change notification settings - Fork 4
/
arcore.py
278 lines (207 loc) · 8.12 KB
/
arcore.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
import zmq
import msgpack
import numpy as np
import threading
import logging
import time
TIMEOUT = 1
logging.basicConfig(level=logging.CRITICAL) # CRITICAL for production DEBUG for debug
log = logging.getLogger(__name__)
class Camera():
""" ARCore camera
"""
def __init__(self,intrinsics=[0,0], vm=None):
self.intrinsics = intrinsics
if vm:
self.view_matrix = np.matrix([[vm[0],vm[1],vm[2],vm[3]],
[vm[4],vm[5],vm[6],vm[7]],
[vm[8],vm[9],vm[10],vm[11]],
[vm[12],vm[13],vm[14],vm[15]]])
else:
self.view_matrix = np.matrix(np.zeros((4, 4)))
class Node():
"""Sceneform Node
"""
def __init__(self, wm = None):
if wm:
self.world_matrix = np.matrix([[wm[0],wm[1],wm[2],wm[3]],
[wm[4],wm[5],wm[6],wm[7]],
[wm[8],wm[9],wm[10],wm[11]],
[wm[12],wm[13],wm[14],wm[15]]])
else:
self.world_matrix = np.matrix(np.zeros((4, 4)))
class Frame():
""" ARCore frame
"""
def __init__(self, camera = Camera(),root = Node()):
self.camera = camera
self.root = root
self.mode = 'CAMERA'
@classmethod
def recv(cls,socket):
frame_buffer = socket.recv_multipart()
frame = Frame()
while len(frame_buffer)>0:
header = frame_buffer.pop(0)
if header == b"STATE":
frame.mode = frame_buffer.pop(0).decode()
elif header == b"CAMERA":
arCamera = Camera(
intrinsics=msgpack.unpackb(frame_buffer.pop(0)),
vm=msgpack.unpackb(frame_buffer.pop(0))
)
frame.camera = arCamera
elif header == b"NODE":
arNode = Node(
wm=msgpack.unpackb(frame_buffer.pop(0))
)
frame.root = arNode
return frame
class ArEventHandler():
""" Application Event handler
Used to link Android Application event with
the local application(ex: blender).
"""
def __init__(self):
self._frameListeners = []
self._getScene = None
self._record = None
def bindOnFrameReceived(self,f):
self._frameListeners.append(f)
def bindGetScene(self, f):
self._getScene = f
def bindRecord(self, f):
self._record = f
def OnFrameReceived(self, frame):
for f in self._frameListeners:
f(frame)
def OnRecord(self, status):
if self._record:
return self._record(status)
else:
log.info("Record not implemented")
return None
def OnGetScene(self,offset,chunk_size):
return self._getScene(0,0)
class ArCoreInterface(object):
""" Application Ar interface
Interface in charge to manage the network bridge
between local application and the remote app.
"""
def __init__(self, handler, port=63150):
self._net_link = AppLink(handler, port=port)
self.handler = handler
def start(self):
self._net_link.start()
def is_running(self):
return self._net_link.is_alive()
def stop(self):
self._net_link.stop()
class AppLink(threading.Thread):
""" Application network link
Thread use to listen android remote application.
TODO: complete this area
"""
def __init__(
self,
handler,
port=63150,
context=zmq.Context.instance(),
name="Applink"):
threading.Thread.__init__(self)
self.name = name
self.status = 0 # 0: idle, 1: Connecting, 2: Connected
self.context = context
# The command socket handle asynchonous commands
# the from android application
self.command_socket = self.context.socket(zmq.ROUTER)
self.command_socket.setsockopt(zmq.IDENTITY, b'SERVER')
self.command_socket.setsockopt(zmq.RCVHWM, 0)
self.command_socket.bind("tcp://*:{}".format(port+2))
# Data socket is used load each frame ARCore state from
# the android application
self.data_socket = self.context.socket(zmq.PULL)
self.data_socket.bind("tcp://*:{}".format(port+1))
self.data_socket.linger = 0
# TTL socket is in charge of the connexion monitoring
self.ttl_socket = self.context.socket(zmq.ROUTER)
self.ttl_socket.setsockopt(zmq.IDENTITY, b'SERVERTTL')
self.ttl_socket.setsockopt(zmq.RCVHWM, 0)
self.ttl_socket.linger = 0
self.ttl_socket.bind("tcp://*:{}".format(port))
self.client_addr = None
self.exit_event = threading.Event()
self.handler = handler
def run(self):
log.debug("Running App link")
poller = zmq.Poller()
poller.register(self.data_socket, zmq.POLLIN)
poller.register(self.ttl_socket, zmq.POLLIN)
poller.register(self.command_socket, zmq.POLLIN)
while 1: #not self.exit_event.is_set():
items = dict(poller.poll(TIMEOUT))
# Handle arcord data replication
if self.data_socket in items:
try:
self.handler.OnFrameReceived(Frame.recv(self.data_socket))
except:
log.info("error")
# Handle live link heartbeat
if self.ttl_socket in items:
request = self.ttl_socket.recv_multipart()
identity = request[0]
if self.client_addr is None:
self.client_addr = request[2].decode()
self.ttl_socket.send(identity, zmq.SNDMORE)
self.ttl_socket.send_multipart([b"pong"])
# Handle remote command
if self.command_socket in items:
command = self.command_socket.recv_multipart()
identity = command[0]
if command[1] == b"SCENE":
log.debug("Try to get scene")
scene_data_chunk = self.handler.OnGetScene(int(command[2]),int(command[3]))
self.command_socket.send(identity, zmq.SNDMORE)
self.command_socket.send_multipart([b"SCENE",scene_data_chunk])
elif command[1] == b"RECORD":
result = self.handler.OnRecord(command[2].decode())
response = [b"RECORD"]
if result:
response.append(result.encode())
else:
response.append(b"STOPPED")
self.command_socket.send(identity, zmq.SNDMORE)
self.command_socket.send_multipart(response)
else:
log.error("Wrong request")
log.debug("Exiting App link")
self.data_socket.close()
self.ttl_socket.close()
self.command_socket.close()
def stop(self):
self.exit_event.set()
if __name__ == "__main__":
ar_handler = ArEventHandler()
def test(frame):
log.debug(frame.camera.translation)
ar_handler.bindOnFrameReceived(test)
ar = ArCoreInterface(ar_handler)
ar.start()
time.sleep(5)
ar.stop()