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 cv2
00042 import numpy as np
00043 from numpy import genfromtxt
00044 import math
00045 import tf
00046 import os
00047 
00048 fake_green = False
00049 
00050 def collect_image_files(image_dir,file_pattern):
00051   images = glob.glob(image_dir + '/' + file_pattern)
00052   images.sort()
00053   #images = images[:1101]
00054   #images = images[1102:2202]
00055   # images = images[2203:]
00056   return images
00057 
00058 def collect_poses(file):
00059   poses = genfromtxt(file, delimiter=',')
00060   return poses
00061 
00062 def playback_images(image_dir,file_pattern,camera_info_file,pose_file,publish_rate):
00063   if camera_info_file != "":
00064     cam_info = camera_info_parser.parse_yaml(camera_info_file)
00065     publish_cam_info = True
00066   else:
00067     publish_cam_info = False
00068   if pose_file != "":
00069     poses = collect_poses(pose_file)
00070     publish_poses = True
00071   else:
00072     publish_poses = False
00073   image_files = collect_image_files(image_dir,file_pattern)
00074   rospy.loginfo('Found %i images.',len(image_files))
00075   bridge = cv_bridge.CvBridge()
00076   rate = rospy.Rate(publish_rate)
00077   image_publisher = rospy.Publisher('camera/image_color', sensor_msgs.msg.Image, queue_size = 5)
00078   if publish_cam_info:
00079     cam_info_publisher = rospy.Publisher('camera/camera_info', sensor_msgs.msg.CameraInfo, queue_size = 5)
00080   if publish_poses:
00081     tf_pose_publisher = tf.TransformBroadcaster()
00082   rospy.loginfo('Starting playback.')
00083   for image_file in image_files:
00084     if rospy.is_shutdown():
00085       break
00086     now = rospy.Time.now()
00087     image = cv2.imread(image_file)
00088     if fake_green:
00089       image[:,:,0] = 0
00090       image[:,:,2] = 0
00091     image_msg = bridge.cv2_to_imgmsg(np.asarray(image[:,:]), encoding='bgr8')
00092     image_msg.header.stamp = now
00093     image_msg.header.frame_id = "/camera"
00094     image_publisher.publish(image_msg)
00095     if publish_cam_info:
00096       cam_info.header.stamp = now
00097       cam_info.header.frame_id = "/camera"
00098       cam_info_publisher.publish(cam_info)
00099     if publish_poses:
00100       img_name = os.path.basename(image_file)
00101       idx = int(''.join(x for x in img_name if x.isdigit())) + 1
00102       tf_pose_publisher.sendTransform((poses[idx, 1], poses[idx, 2], poses[idx, 3]),
00103                      tf.transformations.quaternion_from_euler(math.radians(poses[idx, 4]), math.radians(poses[idx, 5]), math.radians(poses[idx, 6])),
00104                      now,
00105                      'dvl',
00106                      'world')
00107       # print img_name + ' =? ' + str(poses[idx, 0]) + " pose (" + str(poses[idx, 1]) + ", " + str(poses[idx, 2]) + ", " + str(poses[idx, 3]) + ") orientation (" + str(poses[idx, 4]) + ", " + str(poses[idx, 5]) + ", " + str(poses[idx, 6]) + ")"
00108     rate.sleep()
00109   rospy.loginfo('No more images left. Stopping.')
00110 
00111 if __name__ == "__main__":
00112   rospy.init_node('image_sequence_publisher')
00113   try:
00114     image_dir = rospy.get_param("~image_dir")
00115     file_pattern = rospy.get_param("~file_pattern")
00116     camera_info_file = rospy.get_param("~camera_info_file", "")
00117     pose_file = rospy.get_param("~pose_file", "")
00118     frequency = rospy.get_param("~frequency", 10)
00119     fake_green = rospy.get_param("~fake_green", False)
00120     playback_images(image_dir, file_pattern, camera_info_file, pose_file, frequency)
00121   except KeyError as e:
00122     rospy.logerr('Required parameter missing: %s', e)
00123   except Exception, e:
00124     import traceback
00125     traceback.print_exc()


bag_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Sat Jun 8 2019 20:10:13