test03.py
Go to the documentation of this file.
00001 import roslib; roslib.load_manifest('hai_sandbox')
00002 import rospy
00003 import hrl_lib.util as hru
00004 import pylab as pb
00005 import numpy as np
00006 import itertools as it
00007 import hrl_lib.rutils as ru
00008 import sys
00009 from cv_bridge.cv_bridge import CvBridge, CvBridgeError
00010 import scipy.spatial as sp
00011 import cv
00012 
00013 ##
00014 # @return mat, mat, array
00015 def contact_mat(contact_msgs):
00016     #start_time = contact_msgs[0].header.stamp.to_time()
00017     times = np.array([c.header.stamp.to_time() for c in contact_msgs]) #- start_time
00018     left, right = zip(*[[list(c.l_finger_tip), list(c.r_finger_tip)] for c in contact_msgs])
00019     
00020     left = np.matrix(left).T
00021     right = np.matrix(right).T
00022     return left, right, times
00023 
00024 
00025 ##
00026 # @return array, array
00027 def find_contact_times(left_mat, right_mat, times):
00028     left_mat = left_mat - left_mat[:, 0] 
00029     right_mat = right_mat - right_mat[:,0]
00030     
00031     #When/where did contact happen? 
00032     #TODO: we are assuming just one finger of one arm here!
00033     loc_r, time_c = np.where(np.abs(left_mat) > 250)
00034     times_contact = times[time_c.A1]
00035 
00036     return loc_r, times_contact
00037 
00038 def group_by_first_el(a_list):
00039     d = {}
00040     for el in a_list:
00041         if not d.has_key(el[0]):
00042             d[el[0]] = []
00043         d[el[0]].append(el)
00044     return d
00045 
00046 def get_closest_msgs(fname, topics, times):
00047     times_set = set(times)
00048     for top, msg, t in ru.bag_iter(fname, topics):
00049         msg_time = msg.header.stamp.to_time()
00050         if len(times_set.intersection([msg_time])) > 0:
00051             yield msg
00052 
00053 def find_contact_images(bag_name, contact_times, all_times, topic_name):
00054     print 'finding closest images for ', topic_name
00055     times_tree = sp.KDTree(np.matrix(all_times).T)
00056     closest_times = [all_times[times_tree.query([a_time])[1]] for a_time in contact_times]
00057     pdb.set_trace()
00058     
00059     print 'getting & saving images, expecting', len(set(closest_times)), 'images'
00060     bridge = CvBridge()
00061     cleaned_topic_name = topic_name.replace('/', '')
00062     i = 0
00063     for ros_msg in get_closest_msgs(bag_name, [topic_name], closest_times):
00064         i = i + 1
00065         msg_time = ros_msg.header.stamp.to_time() - all_times[0]
00066         cv_image = bridge.imgmsg_to_cv(ros_msg, 'bgr8')
00067         img_name = "%s_%.3f_touched.png" % (cleaned_topic_name, msg_time)
00068         print 'writing', img_name
00069         cv.SaveImage(img_name, cv_image)
00070     print 'got', i, 'images'
00071 
00072 
00073 fname = sys.argv[1]
00074 fname_wide = sys.argv[2]
00075 #fname_cloud = sys.argv[3]
00076 
00077 press_lt = '/pressure/l_gripper_motor'
00078 press_rt = '/pressure/r_gripper_motor'
00079 forearm_cam_l = '/l_forearm_cam/image_rect_color'
00080 ws_l = '/wide_stereo/left/image_rect_color'
00081 ws_r = '/wide_stereo/right/image_rect_color'
00082 cloud_top = '/full_cloud'
00083 
00084 print 'reading pressure messages'
00085 #Get the pressure messages
00086 msgs_dict = ru.bag_sel(fname, [press_lt, press_rt])
00087 
00088 #Get the image times
00089 print 'getting image times'
00090 all_cam_times = group_by_first_el([[top, msg.header.stamp.to_time()] for top, msg, t in ru.bag_iter(fname_wide, [ws_l, ws_r])])
00091 all_cam_times[forearm_cam_l] = [[top, msg.header.stamp.to_time()] for top, msg, t in ru.bag_iter(fname, [forearm_cam_l])]
00092 
00093 [msg.header.stamp.to_time() for top, msg, t in ru.bag_iter(fname, ['/wide_stereo/left/image_raw'])][0:4]
00094 
00095 print 'processing pressure'
00096 press_lmsgs = [msg for top, msg, t in msgs_dict[press_lt]]
00097 press_rmsgs = [msg for top, msg, t in msgs_dict[press_rt]]
00098 
00099 #ll_mat contains (contact_loc, contact_times)
00100 ll_mat, lr_mat, times_l = contact_mat(press_lmsgs)
00101 rl_mat, rr_mat, times_r = contact_mat(press_rmsgs)
00102 contact_loc, times_contact_pressure = find_contact_times(ll_mat, lr_mat, times_l)
00103 print 'contact loc', contact_loc
00104 
00105 #figure out which images are closest in time
00106 #note: each row is an instance in KDTrees, query return ([distance], [indices])
00107 import pdb
00108 pdb.set_trace()
00109 #Maybe just get the range of messages around contact time? +/- a couple of messages? make that a bag?
00110 find_contact_images(fname,      times_contact_pressure.copy(), [t for top, t in all_cam_times[forearm_cam_l]], forearm_cam_l)
00111 find_contact_images(fname_wide, times_contact_pressure.copy(), [t for top, t in all_cam_times[ws_l]], ws_l)
00112 find_contact_images(fname_wide, times_contact_pressure.copy(), [t for top, t in all_cam_times[ws_r]], ws_r)
00113 
00114 print 'plotting'
00115 #Plot readings
00116 pb.figure()
00117 for i in range(ll_mat.shape[0]):
00118     pb.plot(times_l, ll_mat[i,:].T.A1, label=str(i))
00119 pb.legend()
00120 pb.show()
00121 
00122 
00123 


hai_sandbox
Author(s): Hai Nguyen
autogenerated on Wed Nov 27 2013 11:46:56