ros_detector_reem.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import roslib; roslib.load_manifest('iri_bow_object_detector')
00003 
00004 import rospy
00005 
00006 from cv_bridge import CvBridge, CvBridgeError
00007 
00008 import numpy as np
00009 from os.path import exists
00010 from os import system
00011 
00012 from iri_bow_object_detector.srv import *
00013 from iri_bow_object_detector.msg import *
00014 from iri_perception_msgs.msg import ImagePoint
00015 from geometry_msgs.msg import Point
00016 
00017 from iri_bow_object_detector import config_feats
00018 from iri_bow_object_detector.pcl2numpy import get_auto
00019 from iri_bow_object_detector.refine_grasp_point_finddd import select_grasp_point_finddd
00020 from iri_bow_object_detector.refine_grasp_point_middle import select_grasp_point_middle
00021 from iri_bow_object_detector.refine_grasp_point_manual import select_grasp_point_manual
00022 
00023 #from iri_bow_object_detector.run_detector import gen_windows
00024 from iri_bow_object_detector.run_detector import detect_boxes
00025 #import ipdb
00026 import cv
00027 import pylab
00028 
00029 
00030 def read_vws(geo_vw_sets):
00031         vws=[]
00032         for geo_vw_set in geo_vw_sets:
00033                 vws.append([[geo_vw.x, geo_vw.y, geo_vw.word] for geo_vw in geo_vw_set.geo_vws])
00034         return vws
00035 
00036 
00037 def handle_detector_cb(req):
00038         vws = read_vws(req.geo_vw_sets)
00039 
00040         bridge = CvBridge()
00041         try:
00042                 cv_image = bridge.imgmsg_to_cv(req.image, "bgr8")
00043                 cv_mask  = bridge.imgmsg_to_cv(req.mask, "bgr8")
00044         except CvBridgeError, e:
00045                 print e
00046 
00047         cv_mask_gray = cv.CreateMat(cv_mask.rows, cv_mask.cols, cv.CV_8U)
00048 
00049         cv.CvtColor(cv_mask, cv_mask_gray, cv.CV_BGR2GRAY)
00050         #aux = np.ones((480, 640))
00051         #mask = cv.fromarray(aux)
00052         #cv_mask_gray = mask
00053 
00054         conf = config_feats.conf
00055         w  = np.load(rospy.get_param("~linear_svm_file"))
00056 
00057         if not exists(conf.det_results_dir):
00058                 system('mkdir '+conf.det_results_dir)
00059 
00060         print "Processing the algorithm."
00061         (win, prob) = detect_boxes(w, rospy.get_param("~non_linear_svm_file"), cv_image, cv_mask_gray, conf, vws)
00062 
00063         print "Finished"
00064         rospy.loginfo("Detection box: " + str(win) + " probs: " + str(prob));
00065 
00066         res = GeoVwDetectionResponse()
00067 
00068         for idx, sol in enumerate(win):
00069                 point1 = ImagePoint()
00070                 point1.x = sol[0]
00071                 point1.y = sol[1]
00072 
00073                 point2 = ImagePoint()
00074                 point2.x = sol[2]
00075                 point2.y = sol[3]
00076 
00077                 res.posible_solutions.append(ObjectBox(point1, point2, prob[idx]))
00078 
00079         return res
00080 
00081 
00082 def get_auto(data, wants):
00083         resolution = (data.height, data.width)
00084         img = np.fromstring(data.data, np.float32)
00085         step=img.shape[0]/(data.height*data.width)
00086         ret = []
00087         for w, p in wants:
00088                 if w=="pos":
00089                         x = img[p::step].reshape(resolution)
00090                         y = img[p+1::step].reshape(resolution)
00091                         z = img[p+2::step].reshape(resolution)
00092                         x    = np.flipud(x)
00093                         y    = np.flipud(y)
00094                         z    = np.flipud(z)
00095                         ret.extend([x, y, z])
00096                 elif w=="rgb":
00097                         rgb = img[p::step]
00098                         r = np.zeros(data.height*data.width)
00099                         g = np.zeros(data.height*data.width)
00100                         b = np.zeros(data.height*data.width)
00101                         for i in range(rgb.shape[0]):
00102                                 int_rgb = struct.unpack('i', struct.pack('f', rgb[i]))[0]
00103                                 r[i] = (int_rgb & 0xff0000) >> 16
00104                                 g[i] = (int_rgb & 0xff00) >> 8
00105                                 b[i] = (int_rgb & 0xff)
00106                         r = np.flipud(r.reshape(resolution))
00107                         g = np.flipud(g.reshape(resolution))
00108                         b = np.flipud(b.reshape(resolution))
00109                         gray = (0.30*r+0.59*g+0.11*b).astype('uint8')
00110                         ret.extend([r,g,b,gray])
00111                 elif w=="norm":
00112                         n_x = np.flipud(img[p::step].reshape(resolution))
00113                         n_y = np.flipud(img[p+1::step].reshape(resolution))
00114                         n_z = np.flipud(img[p+2::step].reshape(resolution))
00115                         ret.extend([n_x, n_y, n_z])
00116                 elif w=="curv":
00117                         curv = np.flipud(img[p::step].reshape(resolution))
00118                         ret.extend([curv])
00119         return ret
00120 
00121 
00122 
00123 def select_grasp_point_cb(req):
00124         #return select_grasp_point_wrinkled_map(req)
00125         #return select_grasp_point_finddd(req, rospy.get_param("~finddd_svm_file"))
00126         return select_grasp_point_middle(req)
00127         #return select_grasp_point_manual(req)
00128 
00129 
00130 
00131 def add_detector_server():
00132         rospy.init_node('detector')
00133         s1 = rospy.Service('geo_vw_detector', GeoVwDetection, handle_detector_cb)
00134         s2 = rospy.Service('refine_grasp_point', RefineGraspPoint, select_grasp_point_cb)
00135 
00136         # check params
00137         if (not rospy.has_param("~linear_svm_file")):
00138                 rospy.logerr("Node has no param linear_svm_file. Please set it up on launch file")
00139                 exit
00140         if (not rospy.has_param("~non_linear_svm_file")):
00141                 rospy.logerr("Node has no param non_linear_svm_file. Please set it up on launch file")
00142                 exit
00143         if (not rospy.has_param("~finddd_svm_file")):
00144                 rospy.logerr("Node has no param non_linear_svm_file. Please set it up on launch file")
00145                 exit
00146         print "Detecting regions."
00147         rospy.spin()
00148 
00149 if __name__ == "__main__":
00150         add_detector_server()
00151 
00152 
00153 # DEPRECATED Now using finddd
00154 #def select_grasp_point_wrinkled_map(req):
00155         #(XX, YY, ZZ, W) = get_auto(req.wrinkled_map, [("pos", 0), ("curv", 4)])
00156         #P=[(box.point1.x, box.point1.y, box.point2.x, box.point2.y, box.value ) for box in req.posible_solutions]
00157 
00158         #P.sort(key=lambda x:x[4], reverse=True)
00159         #print P
00160 
00161         #if len(P) < 1:
00162                 #return Point(0,0,0)
00163 
00164         #xc=(P[0][0]+P[0][2])/2
00165         #yc=(P[0][1]+P[0][3])/2
00166 
00167         #offset=30
00168         #print xc,yc, W.shape
00169         #WW=W[yc-30:yc+30,xc-30:xc+30]
00170         #print W[yc-30:yc+30,xc-30:xc+30].max()
00171         #y,x=np.unravel_index(np.argmax(WW),WW.shape)
00172         #print np.argmax(WW), WW[y,x]
00173         #x=xc-30+x
00174         #y=yc-30+y
00175 
00176         #print "Solution is " + str(y) + "," + str(x)
00177 
00178         ## show result
00179         #bridge = CvBridge()
00180         #try:
00181                 #image = bridge.imgmsg_to_cv(req.image, "bgr8")
00182         #except CvBridgeError, e:
00183                 #print e
00184         #cv.Rectangle(image,(x-1,y-1), (x+1, y+1), (255,255,255))
00185         #cv.NamedWindow("Grasp point")
00186         #cv.ShowImage("Grasp point", image)
00187         #cv.WaitKey(10)
00188 
00189         #return Point(XX[y,x], YY[y,x], ZZ[y,x])


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