00001
00002
00003
00004 import sys
00005 import cv2
00006 import cv_bridge
00007 import jsk_recognition_utils
00008 from jsk_topic_tools import ConnectionBasedTransport
00009 import message_filters
00010 import rospy
00011 from sensor_msgs.msg import Image
00012 import pkg_resources
00013 from distutils.version import StrictVersion
00014 from threading import Lock
00015
00016 class TileImages(ConnectionBasedTransport):
00017 def __init__(self):
00018 super(TileImages, self).__init__()
00019 self.lock = Lock()
00020 self.input_topics = rospy.get_param('~input_topics', [])
00021 if not self.input_topics:
00022 rospy.logerr('need to specify input_topics')
00023 sys.exit(1)
00024 self.cache_img = None
00025 self.draw_topic_name = rospy.get_param('~draw_topic_name', False)
00026 self.approximate_sync = rospy.get_param('~approximate_sync', True)
00027 self.no_sync = rospy.get_param('~no_sync', False)
00028 self.font_scale = rospy.get_param('~font_scale', 4)
00029 if (not self.no_sync and
00030 StrictVersion(pkg_resources.get_distribution('message_filters').version) < StrictVersion('1.11.4') and
00031 self.approximate_sync):
00032 rospy.logerr('hydro message_filters does not support approximate sync. Force to set ~approximate_sync=false')
00033 self.approximate_sync = False
00034 self.pub_img = self.advertise('~output', Image, queue_size=1)
00035
00036 def subscribe(self):
00037 self.sub_img_list = []
00038 if self.no_sync:
00039 self.input_imgs = {}
00040 self.sub_img_list = [rospy.Subscriber(topic, Image, self.simple_callback(topic), queue_size=1) for topic in self.input_topics]
00041 rospy.Timer(rospy.Duration(0.1), self.timer_callback)
00042 else:
00043 for i, input_topic in enumerate(self.input_topics):
00044 sub_img = message_filters.Subscriber(input_topic, Image)
00045 self.sub_img_list.append(sub_img)
00046 if self.approximate_sync:
00047 async = message_filters.ApproximateTimeSynchronizer(
00048 self.sub_img_list, queue_size=10, slop=1)
00049 async.registerCallback(self._apply)
00050 else:
00051 sync = message_filters.TimeSynchronizer(
00052 self.sub_img_list, queue_size=10)
00053 sync.registerCallback(self._apply)
00054 def unsubscribe(self):
00055 for sub in self.sub_img_list:
00056 sub.sub.unregister()
00057 def timer_callback(self, event):
00058 with self.lock:
00059 imgs = [self.input_imgs[topic] for topic in self.input_topics
00060 if topic in self.input_imgs]
00061 self._append_images(imgs)
00062 def simple_callback(self, target_topic):
00063 def callback(msg):
00064 with self.lock:
00065 bridge = cv_bridge.CvBridge()
00066 self.input_imgs[target_topic] = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
00067 if self.draw_topic_name:
00068 cv2.putText(self.input_imgs[target_topic], target_topic, (0, 50), cv2.FONT_HERSHEY_PLAIN, self.font_scale, (0, 255, 0), 4)
00069 return callback
00070 def _append_images(self, imgs):
00071 if not imgs:
00072 return
00073 if self.cache_img is None:
00074 out_bgr = jsk_recognition_utils.get_tile_image(imgs)
00075 self.cache_img = out_bgr
00076 else:
00077 try:
00078 out_bgr = jsk_recognition_utils.get_tile_image(
00079 imgs, tile_shape=None, result_img=self.cache_img)
00080 except ValueError:
00081 out_bgr = jsk_recognition_utils.get_tile_image(imgs)
00082 self.cache_img = out_bgr
00083 bridge = cv_bridge.CvBridge()
00084 imgmsg = bridge.cv2_to_imgmsg(out_bgr, encoding='bgr8')
00085 self.pub_img.publish(imgmsg)
00086 def _apply(self, *msgs):
00087 bridge = cv_bridge.CvBridge()
00088 imgs = []
00089 for msg, topic in zip(msgs, self.input_topics):
00090 img = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
00091 if self.draw_topic_name:
00092 cv2.putText(img, topic, (0, 50), cv2.FONT_HERSHEY_PLAIN, self.font_scale, (0, 255, 0), 4)
00093 imgs.append(img)
00094 self._append_images(imgs)
00095
00096
00097 if __name__ == '__main__':
00098 rospy.init_node('tile_image')
00099 tile_image = TileImages()
00100 rospy.spin()