From 4aa35afa778539cc32c5e67ca2cf2db242a4de38 Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Mon, 9 Dec 2024 21:21:11 -0800 Subject: [PATCH] publish compressed tile image --- jsk_perception/node_scripts/tile_image.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/jsk_perception/node_scripts/tile_image.py b/jsk_perception/node_scripts/tile_image.py index c662dd45ed..955ef0cd3a 100755 --- a/jsk_perception/node_scripts/tile_image.py +++ b/jsk_perception/node_scripts/tile_image.py @@ -8,7 +8,9 @@ import jsk_recognition_utils from jsk_topic_tools import ConnectionBasedTransport import message_filters +import numpy as np import rospy +from sensor_msgs.msg import CompressedImage from sensor_msgs.msg import Image import itertools, pkg_resources from distutils.version import StrictVersion @@ -66,6 +68,7 @@ def __init__(self): rospy.logerr('hydro message_filters does not support approximate sync. Force to set ~approximate_sync=false') self.approximate_sync = False self.pub_img = self.advertise('~output', Image, queue_size=1) + self.pub_compressed_img = self.advertise('~output/compressed', CompressedImage, queue_size=1) def subscribe(self): self.sub_img_list = [] @@ -87,14 +90,17 @@ def subscribe(self): sync = message_filters.TimeSynchronizer( self.sub_img_list, queue_size=queue_size) sync.registerCallback(self._apply) + def unsubscribe(self): for sub in self.sub_img_list: sub.sub.unregister() + def timer_callback(self, event): with self.lock: imgs = [self.input_imgs[topic] for topic in self.input_topics if topic in self.input_imgs] self._append_images(imgs) + def simple_callback(self, target_topic): def callback(msg): with self.lock: @@ -105,6 +111,7 @@ def callback(msg): draw_text_box(img, rospy.resolve_name(target_topic), font_scale=self.font_scale) return callback + def _append_images(self, imgs): if not imgs: return @@ -126,6 +133,14 @@ def _append_images(self, imgs): bridge = cv_bridge.CvBridge() imgmsg = bridge.cv2_to_imgmsg(out_bgr, encoding='bgr8') self.pub_img.publish(imgmsg) + + compressed_msg = CompressedImage() + compressed_msg.header = imgmsg.header + compressed_msg.format = imgmsg.encoding + '; jpeg compressed bgr8' + compressed_msg.data = np.array( + cv2.imencode('.jpg', out_bgr)[1]).tostring() + self.pub_compressed_img.publish(compressed_msg) + def _apply(self, *msgs): bridge = cv_bridge.CvBridge() imgs = []