00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 import rospy
00029 import rospkg
00030
00031 import numpy as np
00032 import matplotlib.pylab as plt
00033 import matplotlib
00034 matplotlib.rcParams['ps.useafm'] = True
00035 matplotlib.rcParams['pdf.use14corefonts'] = True
00036 matplotlib.rcParams['text.usetex'] = True
00037
00038 import sys
00039 import os
00040 import os.path
00041 import shutil
00042
00043 import re
00044 from plot_statistics import plot_statistics
00045 from compare_statistics import compare_statistics
00046
00047 from subprocess import check_call, CalledProcessError
00048
00049 rp = rospkg.RosPack()
00050 data_path = os.path.join(rp.get_path("shape_reconstruction"), "../../../data")
00051
00052 ground_truth_bags = {
00053 'bodies9': ["ground_truth/bodies9_gt_metalcase.bag", "ground_truth/bodies9_gt_statue.bag",],
00054 'box': "ground_truth/box_gt_2-17-14-13.bag",
00055 'doorOpenClose': "ground_truth/doorOpenClose_full_2-19_20-53-28_gt.bag",
00056 'drawer_pc': "ground_truth/drawer_pc_imgs_2-19_20-6-22_gt.bag",
00057 'globe_full': "ground_truth/globe_full_2-19_21-29-15_gt.bag",
00058 'robotDrawerOpen': "ground_truth/robotDrawerOpen_full_gt.bag",
00059 'head1_full': "ground_truth/head1_full_2015-08-10-11-57-55_gt.bag",
00060 'redthing': -1,
00061 'ks_drawer': ["ground_truth/ks_drawer_full_gt_drawer.bag", "ground_truth/ks_drawer_full_gt_base.bag"],
00062 'ks_laptop': ["ground_truth/ks_laptop_full_gt_lid.bag", "ground_truth/ks_laptop_full_gt_base.bag"],
00063 }
00064
00065 ground_truth_bags_different_for_w_wo_tracker = {
00066 'redthing': { "with": "ground_truth/redthing5_full_gt_shapetracker.bag",
00067 "wo": "ground_truth/redthing5_full_gt_WOshapetracker.bag"},
00068 }
00069
00070 ochs_result_folders = {}
00071 ochs_result_root = os.path.join(data_path, "ochs_results")
00072 for k,v in ground_truth_bags.items():
00073 ochs_result_folders[k] = os.path.join(ochs_result_root, k, "DenseSegmentation")
00074
00075 print ochs_result_folders
00076
00077 def infer_ground_truth_bag(result_bag):
00078 for k, v in ground_truth_bags.iteritems():
00079 if result_bag.find(k) != -1:
00080 print ("Infering ground truth bag: %s" % v)
00081 ground_truth_bag = v
00082 break
00083
00084 return ground_truth_bag
00085
00086 def infer_experiment_name(bag_name):
00087 for k in ground_truth_bags.keys():
00088 if bag_name.find(k) != -1:
00089 print ("Inferred experiment name: %s" % k)
00090 return k
00091
00092 return None
00093
00094
00095
00096 def create_statistic_files_and_plot(result_bag, ground_truth_bag, ochs_folder=""):
00097 cmd = "rosrun shape_reconstruction shape_reconstruction_statistics %s %s %s" % \
00098 (result_bag, os.path.join(data_path, ground_truth_bag), ochs_folder)
00099 print (cmd)
00100 check_call(cmd, shell=True)
00101
00102 print "----------------------"
00103 print ("Starting plot statistics")
00104
00105 folders = result_bag.split("/")
00106
00107 folder_path = os.path.join("/".join(folders[:-1]), folders[-1].split(".")[0])
00108 print ("Looking for stats in folder %s" % folder_path)
00109
00110 if not os.path.isdir(folder_path):
00111 print "ERROR: Stats dir was not generated"
00112 return
00113
00114 plot_statistics(folder_path)
00115
00116 return folder_path
00117
00118 def keep_best_rb_stat(stats_folder):
00119 rbs = {}
00120 for f in os.listdir(stats_folder):
00121 res = re.match("segment_ext_d_and_c_rb([0-9]+)\.txt", f)
00122 if res is not None:
00123 data = np.genfromtxt(os.path.join(stats_folder, f), dtype=float, delimiter=' ', names=True)
00124 seg_acc = data['tp'] / (data['tp']+data['fp']+data['fn'])
00125
00126
00127 seg_acc_mean = np.arange(1., seg_acc.shape[0]+1).dot(seg_acc)
00128 rb = int(res.group(1))
00129 print " rigid body %d has %.5f" % (rb, seg_acc_mean)
00130 rbs[rb] = (f, seg_acc_mean)
00131 continue
00132
00133 rb = max(rbs.items(), key=lambda x: x[1][1])
00134 print rb[0]
00135
00136 print "Best matching rigid body: %d" % rb[0]
00137
00138 moveto = os.path.join(stats_folder, "not_matching")
00139 try:
00140 os.makedirs(moveto)
00141 print "Creating %s" % moveto
00142 except:
00143 print "Directory %s exists" % moveto
00144
00145 ptn = re.compile("[a-zA-Z_]+_rb%d\.[a-zA-Z]{3}" % rb[0])
00146 ptn_ochs = re.compile("ochs")
00147
00148 for f in os.listdir(stats_folder):
00149 if ptn.match(f) is None and ptn_ochs.match(f) is None and f != "not_matching":
00150 print "Moving %s" % f
00151 fp = os.path.join(stats_folder, f)
00152
00153 fdes = os.path.join(moveto, f)
00154 if os.path.exists(fdes):
00155 try:
00156 os.remove(fdes)
00157 except:
00158 print "Cannot move %s because file exists and cannot be deleted" % fdes
00159 continue
00160
00161 try:
00162 shutil.move(fp , moveto)
00163 except Exception, e:
00164 print e
00165 print "Unknown error moving file %s" % fp
00166 continue
00167
00168 return rbs
00169
00170 def generate_statistics(result_bag, sys):
00171 print ("Starting statistics generation for %s " % result_bag)
00172
00173 ground_truth_bag = None
00174 if len(sys.argv) == 3:
00175 ground_truth_bag = sys.argv[2]
00176
00177 if ground_truth_bag is None:
00178 ground_truth_bag = infer_ground_truth_bag(result_bag)
00179 if ground_truth_bag is None:
00180 print ("Unable to infer ground truth bag - aborting")
00181 import sys
00182 sys.exit()
00183
00184 if type(ground_truth_bag) == list:
00185 print " Several ground truth bags available:\n%s " % "\n".join(map (lambda x: " %d: %s" % (x[0], x[1]), enumerate(ground_truth_bag)))
00186 elif ground_truth_bag == -1:
00187 experiment_name = infer_experiment_name(result_bag)
00188 assert ( experiment_name in ground_truth_bags_different_for_w_wo_tracker)
00189 print " %s gets special treatment - we have different ground truths for with and w/o tracker" % experiment_name
00190 elems = os.path.split(result_bag)
00191 elems2 = os.path.split(elems[0])
00192 if "wo" in ("%s/%s" % (elems2[-1], elems[-1])).lower():
00193 ground_truth_bag = ground_truth_bags_different_for_w_wo_tracker[experiment_name]["wo"]
00194 else:
00195 ground_truth_bag = ground_truth_bags_different_for_w_wo_tracker[experiment_name]["with"]
00196 print " Selected %s" % ground_truth_bag
00197 ground_truth_bag = [ground_truth_bag]
00198
00199 else:
00200 ground_truth_bag = [ground_truth_bag]
00201
00202 for gt_bag in ground_truth_bag:
00203 experiment_name = infer_experiment_name(gt_bag)
00204 print "Bag: %s" % gt_bag
00205 print "Experiment name: %s" % experiment_name
00206
00207 ochs_folder = ""
00208 try:
00209 ochs_folder = ochs_result_folders[experiment_name]
00210 except:
00211 print "ERROR could not find ochs folder for experiment %s" % experiment_name
00212 raw_input("(hit enter to ignore)")
00213
00214 stats_folder = create_statistic_files_and_plot(result_bag, gt_bag, ochs_folder)
00215
00216 if len(ground_truth_bag) > 1:
00217 keep_best_rb_stat(stats_folder)
00218
00219 import re
00220 new_folder_name = stats_folder + "_" + re.split("\.|/", gt_bag)[-2]
00221 os.rename(stats_folder, new_folder_name)
00222
00223 if __name__ == "__main__":
00224 if len(sys.argv) < 2:
00225 print "Usage: generate_statistics.py <result bag file/folder containing bag files>[ <ground_truth>]"
00226 sys.exit()
00227
00228 path = sys.argv[1]
00229
00230 if os.path.isdir(path):
00231 print "Processing all bags in directory"
00232
00233 if len(sys.argv) > 2:
00234 print "You cannot pass ground truth if you process an entire directory"
00235 sys.exit()
00236
00237 for f in os.listdir(path):
00238 if f[-4:] != "yaml":
00239 continue
00240
00241 bag_file = f[:-4]+"bag"
00242 generate_statistics(os.path.join(path, bag_file), sys)
00243
00244
00245
00246 else:
00247 generate_statistics(path, sys)
00248 plt.show()
00249
00250
00251
00252