image_publisher.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import os
00003 
00004 import cv2
00005 
00006 import rospy
00007 import cv_bridge
00008 import dynamic_reconfigure.server
00009 
00010 from jsk_perception.cfg import ImagePublisherConfig
00011 from sensor_msgs.msg import Image, CameraInfo
00012 
00013 
00014 def _cb_dyn_reconfig(config, level):
00015     global file_name
00016     file_name = config['file_name']
00017     config['file_name'] = os.path.abspath(file_name)
00018     return config
00019 
00020 
00021 rospy.init_node("image_publisher")
00022 
00023 dynamic_reconfigure.server.Server(ImagePublisherConfig, _cb_dyn_reconfig)
00024 rate = rospy.Rate(rospy.get_param("rate", 1))
00025 if_publish_info = rospy.get_param("~publish_info", True)
00026 pub = rospy.Publisher("~output", Image, queue_size=1)
00027 if if_publish_info:
00028     pub_info = rospy.Publisher("~output/camera_info", CameraInfo, queue_size=1)
00029 bridge = cv_bridge.CvBridge()
00030 while not rospy.is_shutdown():
00031     try:
00032         now = rospy.Time.now()
00033         image = cv2.imread(file_name)
00034         if image is None:
00035             raise IOError('image value is None')
00036         image_message = bridge.cv2_to_imgmsg(image, encoding="bgr8")
00037         image_message.header.stamp = now
00038         image_message.header.frame_id = "camera"
00039         info = CameraInfo()
00040         info.header.stamp = now
00041         info.header.frame_id = "camera"
00042         info.width = image_message.width
00043         info.height = image_message.height
00044         if if_publish_info:
00045             pub_info.publish(info)
00046         pub.publish(image_message)
00047     except IOError, e:
00048         rospy.loginfo("cannot read the image at %s" % file_name)
00049         rospy.loginfo(e.message)
00050     except AttributeError, e:
00051         rospy.logerr("Did you set properly ~file_name param ?")
00052         rospy.loginfo(e.message)
00053     rate.sleep()
00054 


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Wed Sep 16 2015 04:36:15