refine_grasp_point_manual.py
Go to the documentation of this file.
00001 print 'REFINE_GRASP_POINT_MANUAL.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 os
00006 import numpy as np
00007 
00008 from iri_bow_object_detector.pcl2numpy import get_auto
00009 from geometry_msgs.msg import Point
00010 from iri_perception_msgs.msg import ImagePoint
00011 from iri_bow_object_detector.srv import RefineGraspPoint
00012 
00013 import cv
00014 from cv_bridge import CvBridge, CvBridgeError
00015 
00016 #import ipdb
00017 
00018 
00019 def cv_select_point(event, x, y, flags, param):
00020     # param is list where point will be added
00021     if event and cv.CV_EVENT_RBUTTONDOWN:
00022         param.append((x,y))
00023 
00024 def user_select_point(cvimage):
00025     """Select point based on user input."""
00026     cv.NamedWindow('select_point')
00027     cv.ShowImage('select_point', cvimage)
00028     selected_point=[]
00029     cv.SetMouseCallback('select_point', cv_select_point, selected_point)
00030     while len(selected_point)==0:
00031         cv.WaitKey(100)
00032     cv.DestroyWindow('select_point')
00033 
00034     return selected_point
00035 
00036 
00037 def select_grasp_point_manual(req):
00038         P=[(box.point1.x, box.point1.y, box.point2.x, box.point2.y, box.value ) for box in req.posible_solutions]
00039         P.sort(key=lambda x:x[4], reverse=True)
00040         #if len(P) < 1:
00041                 #return Point(0,0,0)
00042 
00043         #maxp2du = P[0][0] + (P[0][2] - P[0][0])/2
00044         #maxp2dv = P[0][1] + (P[0][3] - P[0][1])/2
00045         #maxp2d = (maxp2du, maxp2dv)
00046 
00047         #(XX, YY, ZZ, W) = get_auto(req.pointcloud, [("pos", 0), ("curv", 4)])
00048         #res = Point(XX[maxp2dv, maxp2du], YY[maxp2dv, maxp2du], ZZ[maxp2dv, maxp2du])
00049 
00050         # Draw boxes
00051         ## show result
00052         bridge = CvBridge()
00053         try:
00054                 image = bridge.imgmsg_to_cv(req.image, "bgr8")
00055         except CvBridgeError, e:
00056                 print e
00057 
00058         if len(P) > 0:
00059                 probs = [wi[4] for wi in P]
00060                 maxprob = max(probs)
00061                 minprob = min(probs)
00062                 for ii,wi in enumerate(P):
00063                         if minprob==maxprob:
00064                                 wi_color = 255
00065                         else:
00066                                 wi_color = int(np.round(255*((wi[4]-minprob)/(maxprob-minprob))))
00067                         cv.Rectangle(image,(wi[0],wi[1]), (wi[2],wi[3]), (0, 0, wi_color), 3)
00068 
00069         selected_point = user_select_point(image)
00070         maxp2du = selected_point[0][0]
00071         maxp2dv = selected_point[0][1]
00072         maxp2d = (maxp2du, maxp2dv)
00073 
00074 
00075         cv.Rectangle(image,(maxp2d[0]-5,maxp2d[1]-1), (maxp2d[0]+5, maxp2d[1]+1), (255,255,255), 2)
00076         cv.Rectangle(image,(maxp2d[0]-1,maxp2d[1]-5), (maxp2d[0]+1, maxp2d[1]+5), (255,255,255), 2)
00077         cv.Rectangle(image,(maxp2d[0]-1,maxp2d[1]-1), (maxp2d[0]+1, maxp2d[1]+1), (0,0,0), 1)
00078         cv.NamedWindow("Grasp point")
00079         cv.ShowImage("Grasp point", image)
00080         cv.WaitKey(1000)
00081 
00082         basestr="/home/dmartinez/experimentos/ijrr13/exp-SIFT_"
00083         ps=os.popen('ls '+basestr+'*.png | cut -d "_" -f2 | cut -d "." -f1 2>/dev/null')
00084         nums=ps.readlines()
00085         if len(nums):
00086                 num=max([int(x) for x in nums])
00087         else:
00088                 num=-1
00089         cv.SaveImage(basestr+"%04d.png"%(num+1), image)
00090 
00091         # prints
00092         #print res
00093         print 'Res 2D: (' + str(maxp2du) + ',' + str(maxp2dv) + ')'
00094         res = ImagePoint(maxp2du, maxp2dv)
00095 
00096         return res
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 


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