EtherSenseServer.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import pyrealsense2 as rs
3 import sys, getopt
4 import asyncore
5 import numpy as np
6 import pickle
7 import socket
8 import struct
9 import cv2
10 
11 
12 print('Number of arguments:', len(sys.argv), 'arguments.')
13 print('Argument List:', str(sys.argv))
14 mc_ip_address = '224.0.0.1'
15 port = 1024
16 chunk_size = 4096
17 #rs.log_to_console(rs.log_severity.debug)
18 
19 def getDepthAndTimestamp(pipeline, depth_filter):
20  frames = pipeline.wait_for_frames()
21  # take owner ship of the frame for further processing
22  frames.keep()
23  depth = frames.get_depth_frame()
24  if depth:
25  depth2 = depth_filter.process(depth)
26  # take owner ship of the frame for further processing
27  depth2.keep()
28  # represent the frame as a numpy array
29  depthData = depth2.as_frame().get_data()
30  depthMat = np.asanyarray(depthData)
31  ts = frames.get_timestamp()
32  return depthMat, ts
33  else:
34  return None, None
36  cfg = rs.config()
37  cfg.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
38  pipeline = rs.pipeline()
39  pipeline_profile = pipeline.start(cfg)
40  sensor = pipeline_profile.get_device().first_depth_sensor()
41  return pipeline
42 
43 class DevNullHandler(asyncore.dispatcher_with_send):
44 
45  def handle_read(self):
46  print(self.recv(1024))
47 
48  def handle_close(self):
49  self.close()
50 
51 
52 class EtherSenseServer(asyncore.dispatcher):
53  def __init__(self, address):
54  asyncore.dispatcher.__init__(self)
55  print("Launching Realsense Camera Server")
56  try:
58  except:
59  print("Unexpected error: ", sys.exc_info()[1])
60  sys.exit(1)
61  self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
62  print('sending acknowledgement to', address)
63 
64  # reduce the resolution of the depth image using post processing
65  self.decimate_filter = rs.decimation_filter()
66  self.decimate_filter.set_option(rs.option.filter_magnitude, 2)
67  self.frame_data = ''
68  self.connect((address[0], 1024))
69  self.packet_id = 0
70 
71  def handle_connect(self):
72  print("connection received")
73 
74  def writable(self):
75  return True
76 
77  def update_frame(self):
78  depth, timestamp = getDepthAndTimestamp(self.pipeline, self.decimate_filter)
79  if depth is not None:
80  # convert the depth image to a string for broadcast
81  data = pickle.dumps(depth)
82  # capture the lenght of the data portion of the message
83  length = struct.pack('<I', len(data))
84  # include the current timestamp for the frame
85  ts = struct.pack('<d', timestamp)
86  # for the message for transmission
87  self.frame_data = ''.join([length, ts, data])
88 
89  def handle_write(self):
90  # first time the handle_write is called
91  if not hasattr(self, 'frame_data'):
92  self.update_frame()
93  # the frame has been sent in it entirety so get the latest frame
94  if len(self.frame_data) == 0:
95  self.update_frame()
96  else:
97  # send the remainder of the frame_data until there is no data remaining for transmition
98  remaining_size = self.send(self.frame_data)
99  self.frame_data = self.frame_data[remaining_size:]
100 
101 
102  def handle_close(self):
103  self.close()
104 
105 
106 class MulticastServer(asyncore.dispatcher):
107  def __init__(self, host = mc_ip_address, port=1024):
108  asyncore.dispatcher.__init__(self)
109  server_address = ('', port)
110  self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
111  self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
112  self.bind(server_address)
113 
114  def handle_read(self):
115  data, addr = self.socket.recvfrom(42)
116  print('Recived Multicast message %s bytes from %s' % (data, addr))
117  # Once the server recives the multicast signal, open the frame server
118  EtherSenseServer(addr)
119  print(sys.stderr, data)
120 
121  def writable(self):
122  return False # don't want write notifies
123 
124  def handle_close(self):
125  self.close()
126 
127  def handle_accept(self):
128  channel, addr = self.accept()
129  print('received %s bytes from %s' % (data, addr))
130 
131 
132 def main(argv):
133  # initalise the multicast receiver
134  server = MulticastServer()
135  # hand over excicution flow to asyncore
136  asyncore.loop()
137 
138 if __name__ == '__main__':
139  main(sys.argv[1:])
140 
std::string join(const std::string &base, const std::string &path)
Definition: filesystem.h:113
static std::string print(const transformation &tf)
def __init__(self, host=mc_ip_address, port=1024)
def getDepthAndTimestamp(pipeline, depth_filter)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14