14 from sensor_msgs.msg
import CompressedImage
15 from sensor_msgs.msg
import Image
23 custom_name = custom_name
or name
25 path = filter(
lambda x: x != os.path.dirname(os.path.abspath(__file__)), sys.path)
26 f, pathname, desc = imp.find_module(name, path)
28 module = imp.load_module(custom_name, f, pathname, desc)
38 this_dir = os.path.dirname(__file__)
42 this_dir_cwd = os.getcwd()
45 sys.path = [a
for a
in sys.path
if a
not in [this_dir, this_dir_cwd]]
47 sys.path = [a
for a
in sys.path
if not a.endswith(
'smach_viewer/lib/smach_viewer')]
52 if sys.version_info[0] >= 3:
59 super(SmachImagePublisher, self).
__init__()
61 duration = rospy.get_param(
'~duration', 0.1)
66 self.
filepath =
'/tmp/smach_image_publisher_{}.png'.format(
70 self.
_pub = rospy.Publisher(
'~image', Image, queue_size=1)
72 '~image/compressed', CompressedImage, queue_size=1)
76 dotcode = copy.copy(self.
dotstr)
77 if sys.version_info[0] < 3
and isinstance(dotcode, unicode):
78 dotcode = dotcode.encode(
'utf8')
86 '-Gsize={},{}\\!'.format(w_scale, h_scale),
90 stdin=subprocess.PIPE,
91 stdout=subprocess.PIPE,
92 stderr=subprocess.PIPE,
94 universal_newlines=
True
96 _, error = p.communicate(dotcode)
98 rospy.logerr(
"ERROR PARSING DOT CODE {}".format(error))
101 if not os.path.exists(self.
filepath):
103 if os.path.getsize(self.
filepath) == 0:
113 img = cv2.copyMakeBorder(
114 img, top_pad, bottom_pad, left_pad, right_pad,
115 cv2.BORDER_CONSTANT, value=(255, 255, 255))
116 img_msg = self.
bridge.cv2_to_imgmsg(img, encoding=
'bgr8')
117 img_msg.header.stamp = rospy.Time.now()
118 self.
_pub.publish(img_msg)
119 compressed_img_msg = CompressedImage()
120 compressed_img_msg.header = img_msg.header
121 compressed_img_msg.format = img_msg.encoding
122 compressed_img_msg.format +=
'; jpeg compressed bgr8'
123 compressed_img_msg.data = np.array(
124 cv2.imencode(
'.jpg', img)[1]).tostring()
129 while os.path.exists(self.
filepath):
131 'Removing file: {}'.format(self.
filepath))
136 if __name__ ==
'__main__':
137 rospy.init_node(
'smach_image_publisher')
141 rospy.logwarn(
'Killing threads...')
145 rospy.on_shutdown(signal_handler)