Go to the documentation of this file.00001
00002 import roslib
00003 roslib.load_manifest('pr2_playpen')
00004 import rospy
00005 from sensor_msgs.msg import PointCloud2
00006 import point_cloud_python as pc2py
00007 import numpy as np
00008 from matplotlib import pylab as pl
00009 import draw_scene as ds
00010 import math
00011 from pr2_playpen.srv import *
00012 import threading
00013
00014 class ResultsAnalyzer:
00015
00016 def __init__(self):
00017 rospy.init_node('playpen_results')
00018 self.draw = ds.SceneDraw()
00019 self.cloud = None
00020 rospy.Subscriber("pr2_segment_region", PointCloud2, self.callback)
00021
00022 self.check = rospy.Service("playpen_check_success", Check, self.serv_success)
00023 self.train_empty = rospy.Service("playpen_train", Train, self.serv_train)
00024 self.table_mean = None
00025 self.table_cov = None
00026 self.object_mean = None
00027 self.object_cov = None
00028 self.mean_ls =[]
00029 self.cov_ls = []
00030
00031
00032
00033 self.start_time = rospy.get_time()
00034 self.lock = threading.RLock()
00035 self.new_cloud = False
00036 self.start = 0
00037
00038
00039 def callback(self, data):
00040 self.lock.acquire()
00041 self.cloud = list(pc2py.points(data, ['x', 'y', 'z']))
00042 self.new_cloud = True
00043 print rospy.get_time()-self.start, "time in between call backs"
00044 self.start = rospy.get_time()
00045 self.lock.release()
00046
00047
00048 def serv_success(self, req):
00049 result = "none"
00050 while self.new_cloud == False:
00051 rospy.sleep(0.01)
00052 self.new_cloud = False
00053 self.lock.acquire()
00054 if self.table_mean == None or self.object_mean == None:
00055 print "you haven't initialized yet!!"
00056 return CheckResponse(result)
00057
00058 np_array_cloud = np.array(self.cloud)
00059 f_ind = np.array(~np.isnan(np_array_cloud).any(1)).flatten()
00060 f_np_array_cloud = np_array_cloud[f_ind, :]
00061 if np.max(f_np_array_cloud.shape)>200:
00062 mean_3d = np.mean(f_np_array_cloud, axis = 0)
00063 cov_3d = np.cov(f_np_array_cloud.T)
00064 v, d = np.linalg.eig(cov_3d)
00065 max_var = d[:, v == np.max(v)]
00066 mean_dist_table = (np.matrix(mean_3d).reshape(3,1)-self.table_mean)
00067 mean_dist_object = (np.matrix(mean_3d).reshape(3,1)-self.object_mean)
00068 mahal_dist_table = mean_dist_table.T*0.5*np.matrix(np.linalg.inv(cov_3d)+np.linalg.inv(self.table_cov))*mean_dist_table
00069 mahal_dist_object = mean_dist_object.T*0.5*np.matrix(np.linalg.inv(cov_3d)+np.linalg.inv(self.object_cov))*mean_dist_object
00070 print "table distance = ", mahal_dist_table
00071 print "object distance = ", mahal_dist_object
00072
00073
00074
00075
00076
00077
00078 if np.max(f_np_array_cloud.shape)<200:
00079 result = "no_cloud"
00080 elif mahal_dist_table<mahal_dist_object:
00081 result = "table"
00082 else:
00083 result = "object"
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 self.lock.release()
00103 return CheckResponse(result)
00104
00105 def serv_train(self, req):
00106 num_samples = 0
00107 if req.expected == 'table':
00108 self.table_mean = None
00109 self.table_cov = None
00110 print "training for empty table top"
00111 elif req.expected == 'object':
00112 self.object_mean = None
00113 self.object_cov = None
00114 print "training for object on table top"
00115
00116 self.mean_ls = []
00117 self.cov_ls = []
00118
00119 while num_samples < 11:
00120 start_time = rospy.get_time()
00121 while self.new_cloud == False:
00122 rospy.sleep(0.01)
00123 self.lock.acquire()
00124 np_array_cloud = np.array(self.cloud)
00125 f_ind = np.array(~np.isnan(np_array_cloud).any(1)).flatten()
00126 f_np_array_cloud = np_array_cloud[f_ind, :]
00127
00128 if np.max(f_np_array_cloud.shape)>200:
00129 mean_3d = np.mean(f_np_array_cloud, axis = 0)
00130 cov_3d = np.cov(f_np_array_cloud.T)
00131 v, d = np.linalg.eig(cov_3d)
00132 max_var = d[:, v == np.max(v)]
00133 self.mean_ls.append(np.matrix(mean_3d).reshape(3,1))
00134 self.cov_ls.append(np.matrix(cov_3d))
00135 num_samples = num_samples + 1
00136
00137 self.new_cloud = False
00138 print "still initializing"
00139 self.lock.release()
00140 buf_mean = np.matrix(np.zeros((3,1)))
00141 buf_cov = np.matrix(np.zeros((3,3)))
00142
00143 print "here is the mean list :", self.mean_ls
00144 mean_arr = np.array(self.mean_ls)
00145 mean_arr.sort(axis=0)
00146 print "here is th sorted mean array :", mean_arr
00147
00148 print "here is the mean cov :\n", self.cov_ls
00149 cov_arr = np.array(self.cov_ls)
00150 cov_arr.sort(axis=0)
00151 print "here is the sorted cov :\n", cov_arr
00152
00153
00154
00155
00156
00157 if req.expected == 'table':
00158 self.table_mean = mean_arr[5]
00159 self.table_cov = cov_arr[5]
00160
00161
00162 elif req.expected == 'object':
00163 self.object_mean = mean_arr[5]
00164 self.object_cov = cov_arr[5]
00165
00166
00167
00168
00169
00170 return TrainResponse(num_samples)
00171
00172 def run(self):
00173 print 'Ready to calculate results'
00174 rospy.spin()
00175
00176 def get_cloud_stats(self):
00177 np_array_cloud = np.array(self.cloud)
00178 f_ind = np.array(~np.isnan(np_array_cloud).any(1)).flatten()
00179 f_np_array_cloud = np_array_cloud[f_ind, :]
00180
00181 print 'size of remaining point cloud :', np.max(f_np_array_cloud.shape)
00182 if np.max(f_np_array_cloud.shape)>200:
00183 mean_3d = np.mean(f_np_array_cloud, axis = 0)
00184 cov_3d = np.cov(f_np_array_cloud.T)
00185 v, d = np.linalg.eig(cov_3d)
00186 max_var = d[:, v == np.max(v)]
00187 mean_dist = (np.matrix(mean_3d).reshape(3,1)-self.nom_mean)
00188
00189 mahal_dist = mean_dist.T*0.5*np.matrix(np.linalg.inv(cov_3d)+np.linalg.inv(self.nom_cov))*mean_dist
00190 print "distance = ", mahal_dist
00191
00192 if rospy.get_time()-self.start_time < 10:
00193 self.nom_mean = np.matrix(mean_3d).reshape(3,1)
00194 self.nom_cov = np.matrix(cov_3d)
00195 print "still initializing"
00196 else:
00197 print "real distance now"
00198 else:
00199 print "not doing anything since point cloud is too small"
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 if __name__ == '__main__':
00211 ra = ResultsAnalyzer()
00212 ra.run()
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262