EtherSenseClient.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 local_ip_address = '192.168.0.1'
16 port = 1024
17 chunk_size = 4096
18 
19 def main(argv):
20  multi_cast_message(mc_ip_address, port, 'EtherSensePing')
21 
22 
23 #UDP client for each camera server
24 class ImageClient(asyncore.dispatcher):
25  def __init__(self, server, source):
26  asyncore.dispatcher.__init__(self, server)
27  self.address = server.getsockname()[0]
28  self.port = source[1]
29  self.buffer = bytearray()
30  self.windowName = self.port
31  # open cv window which is unique to the port
32  cv2.namedWindow("window"+str(self.windowName))
33  self.remainingBytes = 0
34  self.frame_id = 0
35 
36  def handle_read(self):
37  if self.remainingBytes == 0:
38  # get the expected frame size
39  self.frame_length = struct.unpack('<I', self.recv(4))[0]
40  # get the timestamp of the current frame
41  self.timestamp = struct.unpack('<d', self.recv(8))
42  self.remainingBytes = self.frame_length
43 
44  # request the frame data until the frame is completely in buffer
45  data = self.recv(self.remainingBytes)
46  self.buffer += data
47  self.remainingBytes -= len(data)
48  # once the frame is fully recived, process/display it
49  if len(self.buffer) == self.frame_length:
50  self.handle_frame()
51 
52  def handle_frame(self):
53  # convert the frame from string to numerical data
54  imdata = pickle.loads(self.buffer)
55  bigDepth = cv2.resize(imdata, (0,0), fx=2, fy=2, interpolation=cv2.INTER_NEAREST)
56  cv2.putText(bigDepth, str(self.timestamp), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (65536), 2, cv2.LINE_AA)
57  cv2.imshow("window"+str(self.windowName), bigDepth)
58  cv2.waitKey(1)
59  self.buffer = bytearray()
60  self.frame_id += 1
61  def readable(self):
62  return True
63 
64 
65 class EtherSenseClient(asyncore.dispatcher):
66  def __init__(self):
67  asyncore.dispatcher.__init__(self)
68  self.server_address = ('', 1024)
69  # create a socket for TCP connection between the client and server
70  self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
71  self.socket.settimeout(5)
72 
73  self.bind(self.server_address)
74  self.listen(10)
75 
76  def writable(self):
77  return False # don't want write notifies
78 
79  def readable(self):
80  return True
81 
82  def handle_connect(self):
83  print("connection recvied")
84 
85  def handle_accept(self):
86  pair = self.accept()
87  #print(self.recv(10))
88  if pair is not None:
89  sock, addr = pair
90  print ('Incoming connection from %s' % repr(addr))
91  # when a connection is attempted, delegate image receival to the ImageClient
92  handler = ImageClient(sock, addr)
93 
94 def multi_cast_message(ip_address, port, message):
95  # send the multicast message
96  multicast_group = (ip_address, port)
97  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
98  connections = {}
99  try:
100  # Send data to the multicast group
101  print('sending "%s"' % message + str(multicast_group))
102  sent = sock.sendto(message.encode(), multicast_group)
103 
104  # defer waiting for a response using Asyncore
105  client = EtherSenseClient()
106  asyncore.loop()
107 
108  # Look for responses from all recipients
109 
110  except socket.timeout:
111  print('timed out, no more responses')
112  finally:
113  print(sys.stderr, 'closing socket')
114  sock.close()
115 
116 if __name__ == '__main__':
117  main(sys.argv[1:])
def multi_cast_message(ip_address, port, message)
static std::string print(const transformation &tf)
def __init__(self, server, source)


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