00001 print 'REFINE_GRASP_POINT_FINDDD.PY IS DEPRECATED, USE REFINE_GRASP_POINT.PY INSTEAD. Waiting for 10 annoying sec more.'
00002 import time
00003 time.sleep(10)
00004
00005 import numpy as np
00006 import cPickle
00007 import os
00008
00009 import cv2
00010 import pylab
00011 from cv_bridge import CvBridge, CvBridgeError
00012 from shogun.Features import RealFeatures, Labels
00013
00014 from geometry_msgs.msg import Point
00015 from iri_bow_object_detector.srv import RefineGraspPoint
00016 from iri_perception_msgs.msg import ImagePoint
00017
00018
00019
00020
00021 def norm_descs(descs, l=1):
00022 print descs.shape
00023 norms = np.apply_along_axis(np.linalg.norm, 0, descs, l)
00024 print sum(norms==0)
00025 descs/=norms
00026 descs[np.isnan(descs)]=0
00027 descs=np.sqrt(descs)
00028 return descs
00029
00030
00031 def select_grasp_point_finddd(req, svm_file):
00032 orig_descs = req.descriptor_set
00033 P=[(box.point1.x, box.point1.y, box.point2.x, box.point2.y, box.value ) for box in req.posible_solutions]
00034 P.sort(key=lambda x:x[4], reverse=True)
00035 if len(P) < 1:
00036 return Point(0,0,0)
00037
00038 P0 = P[0]
00039
00040 pf=open(svm_file, 'rb')
00041 svm = cPickle.load(pf)
00042 pf.close()
00043
00044 descs = orig_descs.descriptors
00045 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])]
00046 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])]
00047 descs_n = np.asarray(descs_n)
00048
00049
00050
00051 print descs_n.shape
00052
00053 descs_svm=RealFeatures(descs_n.T)
00054 out=svm.apply(descs_svm)
00055 print out.get_labels()
00056 sco=out.get_labels()
00057 maxp3d=None
00058 maxp2d=None
00059 maxpsco=None
00060 shape=(480,640)
00061
00062
00063 bridge = CvBridge()
00064 try:
00065 image = bridge.imgmsg_to_cv(req.image, "bgr8")
00066 except CvBridgeError, e:
00067 print e
00068
00069 image_cv2 = np.asarray(image)
00070 imabw=np.zeros((shape[0],shape[1])).astype('uint8')
00071 imabw=cv2.cvtColor(image_cv2,cv2.COLOR_RGB2GRAY)
00072 imabw2=np.zeros((shape[0]/3,shape[1]/3)).astype('uint8')
00073 imabw=(255*imabw).astype('uint8')
00074 imabw2=cv2.resize(src=imabw,dsize=(shape[1]/3,shape[0]/3),interpolation=cv2.INTER_LINEAR)
00075 edges=cv2.Canny(imabw2,50,60)
00076
00077
00078 print sco
00079 scores = min(sco)*np.ones((shape[0]/3,shape[1]/3))
00080
00081 for s,(u,v) in zip(sco,xys):
00082 if (u > P0[0]) and (u < P0[2]) and (v > P0[1]) and (v < P0[3]):
00083 if maxpsco==None or maxpsco<s:
00084 maxp2d=(u,v)
00085 maxpsco=s
00086 u = u/3
00087 v = v/3
00088 scores[v,u]=s
00089
00090
00091 for d in descs:
00092 u = d.u/3
00093 v = d.v/3
00094
00095 if (edges[v,u]!=0):
00096 scores[v,u]=np.nan
00097
00098
00099
00100
00101 probs = [wi[4] for wi in P]
00102 maxprob = max(probs)
00103 minprob = min(probs)
00104 for ii,wi in enumerate(P):
00105 if minprob==maxprob:
00106 wi_color = 255
00107 else:
00108 wi_color = int(np.round(255*((wi[4]-minprob)/(maxprob-minprob))))
00109 cv2.rectangle(image,(wi[0],wi[1]), (wi[2],wi[3]), (0, 0, wi_color), 3)
00110
00111
00112 cv2.rectangle(image,(maxp2d[0]-5,maxp2d[1]-1), (maxp2d[0]+5, maxp2d[1]+1), (255,255,255), 2)
00113 cv2.rectangle(image,(maxp2d[0]-1,maxp2d[1]-5), (maxp2d[0]+1, maxp2d[1]+5), (255,255,255), 2)
00114 cv2.rectangle(image,(maxp2d[0]-1,maxp2d[1]-1), (maxp2d[0]+1, maxp2d[1]+1), (0,0,0), 1)
00115 cv2.namedWindow("Grasp point")
00116 cv2.imshow("Grasp point", image)
00117 cv2.WaitKey(1000)
00118
00119 home_path = os.getenv("HOME")
00120 basestr_dir=home_path + "/experimentos/cloths/"
00121 os.system('mkdir -p '+basestr_dir)
00122 basestr=basestr_dir+"exp-SIFT_"
00123 ps=os.popen('ls '+basestr+'*.png | cut -d "_" -f2 | cut -d "." -f1 2>/dev/null')
00124 nums=ps.readlines()
00125 if len(nums):
00126 num=max([int(x) for x in nums])
00127 else:
00128 num=-1
00129 cv2.imshow(basestr+"%04d.png"%(num), image)
00130
00131 cmap=pylab.cm.jet
00132 cmap.set_bad('black',1.)
00133 masked_array = np.ma.masked_where(np.isnan(scores), scores)
00134
00135 pylab.ion()
00136 pylab.clf()
00137 pylab.imshow(masked_array, cmap=cmap)
00138 pylab.colorbar()
00139 pylab.draw()
00140
00141
00142
00143 pylab.savefig(basestr+"%04d_scores.png"%(num))
00144
00145
00146 res = ImagePoint(maxp2d[0], maxp2d[1])
00147 return res