mask_publisher.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # BSD 3-Clause License
3 
4 # Copyright (c) 2019, Noam C. Golombek
5 # All rights reserved.
6 
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 
10 # 1. Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 
13 # 2. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 
17 # 3. Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 
33 
34 import socket
35 import numpy as np
36 import rospy
37 import rosgraph
38 from std_msgs.msg import Header, Int8MultiArray, MultiArrayDimension
39 from cnn_bridge.msg import Netmask
40 
41 import timeit
42 class MaskPublisher(object):
43  '''A class to publish commands to ROS'''
44 
45  def __init__(self, width, height, topic_name="mask"):
46  '''Initialize ros publisher, ros subscriber'''
47  self.mask_pub = rospy.Publisher(
48  rospy.get_name() + '/' + topic_name, Netmask, queue_size=1)
49 
50  self.mask = Int8MultiArray()
51  self.mask.layout.dim.append(MultiArrayDimension())
52  self.mask.layout.dim.append(MultiArrayDimension())
53  self.mask.layout.dim[0].label = "height"
54  self.mask.layout.dim[1].label = "width"
55  self.mask.layout.dim[0].size = height
56  self.mask.layout.dim[1].size = width
57  self.mask.layout.dim[0].stride = height * width
58  self.mask.layout.dim[1].stride = width
59  self.mask.layout.data_offset = 0
60 
61  def send_mask(self, data, meta_header):
62  """Set up a ROS camera
63  Input:
64  data (str) The data to send to ROS [throttle, steer]
65  Output: None"""
66  try:
67  rosgraph.Master('/rostopic').getPid()
68  except socket.error:
69  raise ROSTopicIOException("Unable to communicate with master!")
70 
71  start_time = timeit.default_timer()
72 
73  mask_data = np.array(data)
74 
75  np_arr_time = timeit.default_timer() - start_time
76 
77  mask_data = np.array(data).flatten()
78  flatten_time = timeit.default_timer() - start_time - np_arr_time
79 
80  mask_data = np.pad(mask_data, (self.mask.layout.data_offset, 0), 'constant')
81  pad_time = timeit.default_timer() - start_time- np_arr_time - flatten_time
82 
83  self.mask.data = mask_data.tolist()
84  list_time = timeit.default_timer() - start_time - np_arr_time - flatten_time - pad_time
85 
86  # rospy.logwarn((np_arr_time*1000, flatten_time*1000, pad_time*1000, list_time*1000))
87 
88  h = Header()
89  h.stamp = rospy.Time.now()
90 
91  msg = Netmask()
92  msg.header = h
93  msg.image_identifier = meta_header
94  msg.mask = self.mask
95 
96  self.mask_pub.publish(msg)
def __init__(self, width, height, topic_name="mask")
def send_mask(self, data, meta_header)


cnn_bridge
Author(s): Noam C. Golombek , Alexander Beringolts
autogenerated on Mon Jun 10 2019 12:53:26