Skip to content

Commit e1615fb

Browse files
k-okadaknorth55
authored andcommitted
[jsk_perception/node_scripts/tile_image.py] process and publish only when subscriber exists
1 parent c518162 commit e1615fb

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

jsk_perception/node_scripts/tile_image.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,24 @@ def _append_images(self, imgs):
130130
out_bgr = jsk_recognition_utils.get_tile_image(
131131
imgs, tile_shape=shape_xy)
132132
self.cache_img = out_bgr
133-
bridge = cv_bridge.CvBridge()
134-
imgmsg = bridge.cv2_to_imgmsg(out_bgr, encoding='bgr8')
135-
self.pub_img.publish(imgmsg)
136-
137-
compressed_msg = CompressedImage()
138-
compressed_msg.header = imgmsg.header
139-
compressed_msg.format = imgmsg.encoding + '; jpeg compressed bgr8'
140-
compressed_msg.data = np.array(
141-
cv2.imencode('.jpg', out_bgr)[1]).tostring()
142-
self.pub_compressed_img.publish(compressed_msg)
133+
if self.pub_img.get_num_connections() > 0:
134+
bridge = cv_bridge.CvBridge()
135+
imgmsg = bridge.cv2_to_imgmsg(out_bgr, encoding='bgr8')
136+
self.pub_img.publish(imgmsg)
137+
138+
if self.pub_compressed_img.get_num_connections() > 0:
139+
compressed_msg = CompressedImage()
140+
compressed_msg.header = imgmsg.header
141+
compressed_msg.format = imgmsg.encoding + '; jpeg compressed bgr8'
142+
compressed_msg.data = np.array(
143+
cv2.imencode('.jpg', out_bgr)[1]).tostring()
144+
self.pub_compressed_img.publish(compressed_msg)
143145

144146
def _apply(self, *msgs):
147+
if self.pub_img.get_num_connections() == 0 and
148+
self.pub_compressed_img.get_num_connections() == 0:
149+
return
150+
145151
bridge = cv_bridge.CvBridge()
146152
imgs = []
147153
for msg, topic in zip(msgs, self.input_topics):

0 commit comments

Comments
 (0)