articulation_eval_full.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('articulation_structure')
00004 import rospy
00005 import sys
00006 from visualization_msgs.msg import Marker, MarkerArray
00007 from geometry_msgs.msg import Vector3, Point, Pose, PoseArray
00008 from std_msgs.msg import ColorRGBA
00009 from sensor_msgs.msg import ChannelFloat32,CameraInfo,CompressedImage
00010 from articulation_msgs.msg import *
00011 from articulation_msgs.srv import *
00012 from articulation_models.track_utils import *
00013 import logging
00014 import getopt
00015 import colorsys
00016 import numpy
00017 import copy
00018 import rosbag
00019 
00020 def all_detected(pose_array):
00021   for p in pose_array.poses:
00022     r = [p.orientation.x,p.orientation.y,p.orientation.z,p.orientation.w]
00023     if( numpy.dot(r,r) == 0 ):
00024       return False
00025   return True
00026 
00027 def main():
00028   rospy.init_node('articulation_eval')
00029   
00030   info_pub = rospy.Publisher('/camera/camera_info',CameraInfo)
00031   image_pub = rospy.Publisher('/camera/image_raw/compressed',CompressedImage)
00032   posearray_pub = rospy.Publisher('/camera/pose_array',PoseArray)
00033   model_pub = rospy.Publisher('/model', ModelMsg)
00034   track_pub = rospy.Publisher('/track', TrackMsg)
00035   
00036   fit_models = rospy.ServiceProxy('fit_models', ArticulatedObjectSrv)
00037   get_spanning_tree = rospy.ServiceProxy('get_spanning_tree', ArticulatedObjectSrv)
00038   get_fast_graph = rospy.ServiceProxy('get_fast_graph', ArticulatedObjectSrv)
00039   get_graph = rospy.ServiceProxy('get_graph', ArticulatedObjectSrv)
00040   get_graph_all = rospy.ServiceProxy('get_graph_all', ArticulatedObjectSrv)
00041   visualize_graph = rospy.ServiceProxy('visualize_graph', ArticulatedObjectSrv)
00042   
00043   downsample = rospy.get_param("~downsample",100)
00044   steps = rospy.get_param("~steps",100)
00045   sigma_position = rospy.get_param("~sigma_position",0.005)
00046   sigma_orientation = rospy.get_param("~sigma_orientation",0.4)
00047   reduce_dofs = rospy.get_param("~reduce_dofs",1)
00048   mode = rospy.get_param("~mode","spanningtree")
00049   inbag_filename = rospy.get_param('~bagfile','/home/sturm/sturm-data/sturm10tro/cabinet-pose.bag')
00050   outbag_filename = rospy.get_param('~outfile','/home/sturm/sturm-data/sturm10tro/cabinet-eval.bag')
00051   
00052   # read whole bag file, group by timestamp in header
00053   bag = {}
00054   for topic, msg, t in rosbag.Bag(inbag_filename).read_messages():
00055     timestamp = t
00056     if not timestamp in bag:
00057       bag[timestamp] = {}
00058     bag[timestamp][topic] = msg
00059   print "read bagfile %s with %d different header timestamps"%( inbag_filename, len(bag) )
00060   topics = max([len(msgs.keys()) for (timestamp,msgs) in bag.iteritems()])
00061   for (timestamp) in bag.keys():
00062     if len( bag[timestamp] ) != topics or not all_detected(bag[timestamp]['/camera/pose_array']):
00063       del bag[timestamp]
00064   timestamps = bag.keys()
00065   timestamps.sort()
00066 
00067   outbag = rosbag.Bag(outbag_filename,"w")
00068   request = ArticulatedObjectSrvRequest()
00069   for step in range(1,steps+1):
00070     # downsample sequence      
00071     sequence = [ bag[timestamp]['/camera/pose_array'] for timestamp in timestamps ]
00072     parts = len( sequence[0].poses )
00073     print "we have %d observations of %d object parts"%(len(bag),parts)
00074     # cutoff
00075     cutoff = int(max(1,step * len(sequence) / float(steps))) 
00076     sequence = sequence[0:cutoff]
00077     print "selected the first %d observations"%len(sequence)
00078     d = max(1,int(downsample * step / float(steps)))
00079     downsampled = [ i for i in range(len(sequence)) if (i % (max(1,len(sequence) / d)) == 0 )]
00080     #print downsampled
00081     downsampled = downsampled[len(downsampled) - d:]
00082     sequence_downsampled = [ sequence[i] for i in downsampled ]
00083     print "--> downsampled to %d pose observations (downsample=%d)"%(len(sequence_downsampled),downsample)
00084     
00085     # debugging:
00086     #parts = 3
00087     
00088     # build service request message  
00089     request.object.header = sequence_downsampled[-1].header
00090     request.object.parts = [ 
00091       TrackMsg(
00092                id=i,
00093 
00094                header=request.object.header,
00095                pose=[p.poses[i] for p in sequence_downsampled]
00096       ) 
00097       for i in range(parts)
00098     ]
00099     set_param(request.object, "sigma_position", sigma_position, ParamMsg.PRIOR)
00100     set_param(request.object, "sigma_orientation", sigma_orientation, ParamMsg.PRIOR)
00101     set_param(request.object, "reduce_dofs", reduce_dofs, ParamMsg.PRIOR)
00102     
00103   #  b=rosbag.Bag("eval.bag","w")
00104   #  b.write("/object",request.object,rospy.Time(0))
00105   #  b.close()
00106     
00107     print "fitting models" 
00108     response = fit_models(request)
00109     print '\n'.join(
00110        ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f'%(
00111             model.name[0:2],
00112             model.id/parts,
00113             model.id%parts,
00114             get_param(model,'bic'),
00115             get_param(model,'avg_error_position'),
00116             get_param(model,'avg_error_orientation')
00117         ) for model in response.object.models])
00118     
00119     if mode=="fastgraph":
00120       print "running fastgraph" 
00121       response = get_fast_graph(request)
00122     elif mode=="graph":
00123       print "running graph" 
00124       response = get_graph(request)
00125     elif mode=="spanningtree":
00126       print "running spanningtree" 
00127       response = get_spanning_tree(request)
00128     elif mode=="all":
00129       print "running all structure selection methods" 
00130       response = get_graph_all(request)
00131     
00132     request.object = copy.deepcopy(response.object)
00133     response = visualize_graph(request)  
00134     
00135     rospy.loginfo('received articulation model: '+(' '.join(
00136        ['(%s:%d,%d)'%(model.name[0:2],model.id/parts,model.id%parts) for model in response.object.models])))
00137     
00138     print '\n'.join(
00139        ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f'%(
00140             model.name[0:2],
00141             model.id/parts,
00142             model.id%parts,
00143             get_param(model,'bic'),
00144             get_param(model,'avg_error_position'),
00145             get_param(model,'avg_error_orientation')
00146         ) for model in response.object.models])
00147     print "dofs=%d, nominal dofs=%d, bic=%f pos_err=%f rot_err=%f"%(
00148             get_param(response.object,'dof'),
00149             get_param(response.object,'dof.nominal'),
00150             get_param(response.object,'bic'),
00151             get_param(response.object,'avg_error_position'),
00152             get_param(response.object,'avg_error_orientation')
00153             )
00154     
00155    # print response.object.params
00156     outbag.write("/object",response.object,rospy.Time(step))
00157     #print response.object.models
00158     
00159     # send image and info for last pose
00160     last_timestamp = timestamps[ downsampled[-1] ]
00161     info_pub.publish( bag[last_timestamp]['/camera/camera_info'] )
00162     image_pub.publish( bag[last_timestamp]['/camera/image_raw/compressed'] )
00163     posearray_pub.publish( bag[last_timestamp]['/camera/pose_array'] )
00164   outbag.close()
00165   
00166 if __name__ == '__main__':
00167   main()
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


articulation_structure
Author(s): sturm
autogenerated on Wed Dec 26 2012 15:37:59