refine_grasp_point.py
Go to the documentation of this file.
00001 import numpy as np
00002 import cPickle
00003 from os.path import exists
00004 import cv2
00005 
00006 from geometry_msgs.msg import Point
00007 from iri_bow_object_detector.srv import RefineGraspPoint
00008 from iri_perception_msgs.msg import ImagePoint
00009 
00010 from bow_detector_utils import ros_ima_to_numpy, save_data_for_log, draw_boxes
00011 
00012 # SHOULD NOT BE NECESSARY, CONSIDER DELETE
00013 # def norm_descs(descs, l=1):
00014 #     print descs.shape
00015 #     norms = np.apply_along_axis(np.linalg.norm, 0, descs, l) #leave bias alone
00016 #     print sum(norms==0)
00017 #     descs/=norms
00018 #     descs[np.isnan(descs)]=0
00019 #     descs=np.sqrt(descs)
00020 #     return descs
00021 
00022 def cv_select_point(event, x, y, flags, param):
00023     # param is list where point will be added
00024     if event and cv2.EVENT_RBUTTONDOWN:
00025         param.append((x,y))
00026 
00027 def user_select_point(cvimage):
00028     """Select point based on user input."""
00029     cv2.namedWindow('select_point')
00030     cv2.imshow('select_point', cvimage)
00031     selected_point=[]
00032     cv2.setMouseCallback('select_point', cv_select_point, selected_point)
00033     while len(selected_point)==0:
00034         cv2.waitKey(100)
00035     cv2.destroyWindow('select_point')
00036     return selected_point
00037 
00038 
00039 def select_grasp_point(req, ref_method='finddd', svm_file=None, verb=1):
00040     P=[(box.point1.x, box.point1.y, box.point2.x, box.point2.y, box.value ) for box in req.posible_solutions]
00041     P.sort(key=lambda x:x[4], reverse=True)
00042     if len(P) < 1:
00043         return Point(0,0,0)
00044     P0 = P[0]
00045     image = ros_ima_to_numpy(req.image)
00046 
00047     scores=None #default result
00048 
00049     if ref_method == 'finddd':
00050         assert svm_file!=None and exists(svm_file)
00051         from shogun.Features import RealFeatures, Labels
00052         pf=open(svm_file, 'rb')
00053         svm = cPickle.load(pf)
00054         pf.close()
00055 
00056         descs = req.descriptor_set.descriptors
00057         descs_n = [desc.descriptor for desc in descs if (desc.u > P0[0]) and (desc.u < P0[2]) and (desc.v > P0[1]) and (desc.v < P0[3])]
00058         xys = [(desc.u, desc.v) for desc in descs if (desc.u > P0[0]) and (desc.u < P0[2]) and (desc.v > P0[1]) and (desc.v < P0[3])]
00059         descs_n = np.asarray(descs_n) 
00060         #descriptors should be already normalized! TODO write an assert to check this
00061 
00062         descs_svm=RealFeatures(descs_n.T)
00063         out=svm.apply(descs_svm)
00064         if verb>1: print out.get_labels()
00065         sco=out.get_labels()
00066         maxp3d=maxp2d=maxpsco=None
00067         shape=(480,640) #wtf
00068         #get canny for overlay
00069         imabw=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
00070         #imabw=(255*imabw).astype('uint8') #wtf!!!
00071         imabw2=cv2.resize(imabw,(shape[1]/3,shape[0]/3),interpolation=cv2.INTER_LINEAR)
00072         edges=cv2.Canny(imabw2,50,60)
00073         scores = min(sco)*np.ones((shape[0]/3,shape[1]/3))
00074 
00075         for s,(u,v) in zip(sco,xys):
00076                 if ((u > P0[0]) and (u < P0[2]) and (v > P0[1]) 
00077                     and (v < P0[3]) and (maxpsco==None or maxpsco<s)):
00078                     maxp2d=(u,v)
00079                     maxpsco=s
00080                 u, v = (u/3, v/3)
00081                 scores[v,u]=s
00082         for d in descs:
00083                 u, v = (d.u/3, d.v/3)
00084                 if (edges[v,u]!=0): scores[v,u]=np.nan
00085     elif ref_method == 'middle':
00086         maxp2du = P0[0] + (P0[2] - P0[0])/2
00087         maxp2dv = P0[1] + (P0[3] - P0[1])/2
00088         maxp2d = (maxp2du, maxp2dv)
00089     elif ref_method == 'manual':
00090         image_select = image.copy()
00091         draw_boxes(image_select, P)
00092         selected_point = user_select_point(image_select)
00093         maxp2d = (selected_point[0][0], selected_point[0][1])
00094 
00095     #draw boxes, save log and show:
00096     print 'Res 2D: (' + str(maxp2d[0]) + ',' + str(maxp2d[1]) + ')'
00097     draw_boxes(image, P, maxp2d)
00098     save_data_for_log(bbox_ima=image, finddd_scores=scores, incr_num=False, fignum=2)
00099 
00100     #return
00101     res = ImagePoint(maxp2d[0], maxp2d[1])
00102     return res


iri_bow_object_detector
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 22:45:46