analyze_playpen_cloud.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
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 * #this is for Train and Check
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 #        rospy.Subscriber("playpen_segment_object", PointCloud2, self.callback)
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 #        self.get_cloud_stats()
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             # print "d = ", d
00073             # print "v = ", v
00074         #made a simple change so that I only look if table is empty or not
00075         #comparison is made from the user selected region with a base model
00076         #of mean covariance of the empty tabletop
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         # if req.exp_state == 'empty':
00086         #     if np.max(f_np_array_cloud.shape)<200:
00087         #         result = "success"
00088         #     elif mahal_dist<5*self.nom_dist: 
00089         #         result = "success"
00090         #     else:
00091         #         result = "fail"
00092         # elif req.exp_state == 'object':
00093         #     if np.max(f_np_array_cloud.shape)<200:
00094         #         result = "fail"
00095         #     elif mahal_dist<5*self.nom_dist:
00096         #         result = "success"
00097         #     else:
00098         #         result = "fail"
00099         # elif req.exp_state == 'objects':
00100         #     print "multiple objects is not yet supported"
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         # for i in xrange(10):
00154         #     buf_mean = buf_mean + self.mean_ls[i]
00155         #     buf_cov = buf_cov + self.cov_ls[i]  #this is not exactly correct if populations
00156                                                     #have different # of points, but it should be approximately right
00157         if req.expected == 'table':
00158             self.table_mean = mean_arr[5]
00159             self.table_cov = cov_arr[5]
00160             # self.table_mean = buf_mean*1/10.0
00161             # self.table_cov = buf_cov*1/10.0
00162         elif req.expected == 'object':
00163             self.object_mean = mean_arr[5]
00164             self.object_cov = cov_arr[5]
00165             # self.object_mean = buf_mean*1/10.0
00166             # self.object_cov = buf_cov*1/10.0
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         # print "mean :\n", mean_3d
00201         # print "cov matrix :\n", cov_3d
00202         # print "eig of cov :\n", v
00203         # print d
00204         # print "max direction of var :\n", max_var
00205         # hs.draw.pub_body((0, 0, 0), (0, 0, 0, 1), 
00206     #                  (0.2, 0.2, 0.2), (1, 0,0,1), 1000000, hs.draw.Marker.SPHERE)
00207 #        self.draw.pub_body((mean_3d[2], -1*mean_3d[0], -1*mean_3d[1]), (0, 0, 0, 1), 
00208 #                     (0.2, 0.2, 0.2), (0, 1,0,1), 1000001, self.draw.Marker.SPHERE)
00209 
00210 if __name__ == '__main__':
00211     ra = ResultsAnalyzer()
00212     ra.run()
00213 
00214 
00215 
00216 # import roslib
00217 # roslib.load_manifest('my_package')
00218 # import sys
00219 # import rospy
00220 # import cv
00221 # from std_msgs.msg import String
00222 # from sensor_msgs.msg import Image
00223 # from cv_bridge import CvBridge, CvBridgeError
00224 
00225 # class image_converter:
00226 
00227 #   def __init__(self):
00228 #     self.image_pub = rospy.Publisher("image_topic_2",Image)
00229 
00230 #     cv.NamedWindow("Image window", 1)
00231 #     self.bridge = CvBridge()
00232 #     self.image_sub = rospy.Subscriber("image_topic",Image,self.callback)
00233 
00234 #   def callback(self,data):
00235 #     try:
00236 #       cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
00237 #     except CvBridgeError, e:
00238 #       print e
00239 
00240 #     (cols,rows) = cv.GetSize(cv_image)
00241 #     if cols > 60 and rows > 60 :
00242 #       cv.Circle(cv_image, (50,50), 10, 255)
00243 
00244 #     cv.ShowImage("Image window", cv_image)
00245 #     cv.WaitKey(3)
00246 
00247 #     try:
00248 #       self.image_pub.publish(self.bridge.cv_to_imgmsg(cv_image, "bgr8"))
00249 #     except CvBridgeError, e:
00250 #       print e
00251 
00252 # def main(args):
00253 #   ic = image_converter()
00254 #   rospy.init_node('image_converter', anonymous=True)
00255 #   try:
00256 #     rospy.spin()
00257 #   except KeyboardInterrupt:
00258 #     print "Shutting down"
00259 #   cv.DestroyAllWindows()
00260 
00261 # if __name__ == '__main__':
00262 #     main(sys.argv)


pr2_playpen
Author(s): Marc Killpack / mkillpack3@gatech.edu, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 12:18:32