image_sequence_publisher.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 """
00003 Copyright (c) 2012,
00004 Systems, Robotics and Vision Group
00005 University of the Balearican Islands
00006 All rights reserved.
00007 
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010     * Redistributions of source code must retain the above copyright
00011       notice, this list of conditions and the following disclaimer.
00012     * Redistributions in binary form must reproduce the above copyright
00013       notice, this list of conditions and the following disclaimer in the
00014       documentation and/or other materials provided with the distribution.
00015     * Neither the name of Systems, Robotics and Vision Group, University of 
00016       the Balearican Islands nor the names of its contributors may be used to 
00017       endorse or promote products derived from this software without specific 
00018       prior written permission.
00019 
00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00024 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 """
00031 
00032 
00033 PKG = 'bag_tools' # this package name
00034 
00035 import roslib; roslib.load_manifest(PKG)
00036 import rospy
00037 import sensor_msgs.msg
00038 import cv_bridge
00039 import camera_info_parser
00040 import glob
00041 import cv
00042 
00043 def collect_image_files(image_dir,file_pattern):
00044   images = glob.glob(image_dir + '/' + file_pattern)
00045   images.sort()
00046   return images
00047 
00048 def playback_images(image_dir,file_pattern,camera_info_file,publish_rate):
00049   if camera_info_file != "":
00050     cam_info = camera_info_parser.parse_yaml(camera_info_file)
00051     publish_cam_info = True
00052   else:
00053     publish_cam_info = False
00054   image_files = collect_image_files(image_dir,file_pattern)
00055   rospy.loginfo('Found %i images.',len(image_files))
00056   bridge = cv_bridge.CvBridge()
00057   rate = rospy.Rate(publish_rate)
00058   image_publisher = rospy.Publisher('camera/image', sensor_msgs.msg.Image)
00059   if publish_cam_info:
00060       cam_info_publisher = rospy.Publisher('camera/camera_info', sensor_msgs.msg.CameraInfo)
00061   rospy.loginfo('Starting playback.')
00062   for image_file in image_files:
00063     if rospy.is_shutdown():
00064       break
00065     now = rospy.Time.now()
00066     image = cv.LoadImage(image_file)
00067     image_msg = bridge.cv_to_imgmsg(image, encoding='rgb8')
00068     image_msg.header.stamp = now
00069     image_msg.header.frame_id = "/camera"
00070     image_publisher.publish(image_msg)
00071     if publish_cam_info:
00072       cam_info.header.stamp = now
00073       cam_info.header.frame_id = "/camera"
00074       cam_info_publisher.publish(cam_info)
00075     rate.sleep()
00076   rospy.loginfo('No more images left. Stopping.')
00077 
00078 if __name__ == "__main__":
00079   rospy.init_node('image_sequence_publisher')
00080   try:
00081     image_dir = rospy.get_param("~image_dir")
00082     file_pattern = rospy.get_param("~file_pattern")
00083     camera_info_file = rospy.get_param("~camera_info_file", "")
00084     frequency = rospy.get_param("~frequency", 10)
00085     playback_images(image_dir, file_pattern, camera_info_file, frequency)
00086   except KeyError as e:
00087     print 'Required parameter missing:', e
00088   except Exception, e:
00089     import traceback
00090     traceback.print_exc()


bag_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Fri Aug 28 2015 13:15:13