Go to the documentation of this file.00001 import rosbag
00002 import rospy
00003 import sys
00004 import os
00005
00006 from generate_statistics import data_path, ground_truth_bags, infer_experiment_name
00007
00008 from sensor_msgs.msg import CameraInfo, PointCloud2, Image
00009
00010 import cv2
00011 from cv_bridge import CvBridge, CvBridgeError
00012 bridge = CvBridge()
00013
00014
00015
00016
00017
00018
00019
00020 topics=['/camera/rgb/image_rect_color', 'camera/rgb/image_rect_color']
00021
00022
00023
00024 if __name__ == "__main__":
00025 if len(sys.argv) < 2:
00026 print "Usage: extract_images <bag> [format=jpg] "
00027 sys.exit()
00028
00029 rospy.init_node("generate_images")
00030
00031
00032 out_format = "ppm"
00033 if len(sys.argv) >= 3:
00034 out_format = sys.argv[2]
00035 print "Output format: %s" % out_format
00036
00037
00038 bag_name = sys.argv[1]
00039 if not os.path.exists(bag_name):
00040 print "ERROR Bag does not exist: " + bag_name
00041
00042 experiment_name = infer_experiment_name(bag_name)
00043 if experiment_name is None:
00044 print "WARN: could not infer experiment name"
00045 experiment_name = os.path.basename(bag_name)
00046 print "Experiment: %s" % experiment_name
00047
00048 print "Opening bag %s" % bag_name
00049 orig_bag = rosbag.Bag(bag_name, 'r')
00050
00051
00052 dirname = os.path.dirname(sys.argv[1])
00053 out_dir = os.path.join(dirname, out_format, experiment_name)
00054 if not os.path.exists(out_dir):
00055 print "Creating output directory %s" % out_dir
00056 os.makedirs(out_dir)
00057
00058 img_files = []
00059 timestamps = []
00060
00061
00062
00063 i = 1
00064 for topic, msg, t in orig_bag.read_messages(topics=topics):
00065 try:
00066 cv_image = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
00067 except CvBridgeError, e:
00068 print e
00069 continue
00070
00071 cv2.imshow("Image window", cv_image)
00072 cv2.waitKey(10)
00073
00074
00075 img_file = experiment_name+"_" + "%05d" % i + "."+out_format
00076 img_files.append(img_file)
00077 path = os.path.join(out_dir, img_file)
00078 print " writing %s" % path
00079 cv2.imwrite(path, cv_image)
00080
00081 timestamps.append(t)
00082
00083 i += 1
00084
00085
00086
00087
00088 orig_bag.close()
00089
00090 print "Writing BMF file (required for Ochs segmentation)"
00091 with open(os.path.join(out_dir, experiment_name+".bmf"), "w") as f:
00092 f.write("%d 1\n" % len(img_files) )
00093 for imf in img_files:
00094 f.write(imf + "\n")
00095
00096
00097 print "Writing time file (required for evaluating segmentation)"
00098 with open(os.path.join(out_dir, experiment_name+".time.txt"), "w") as f:
00099 f.write("#time stamps\n")
00100 for t in timestamps:
00101 f.write(str(t)+"\n")