ros_detector.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 import numpy as np
00007 from os.path import exists
00008 from os import system, getenv
00009 from sys import exit
00010 
00011 from iri_bow_object_detector.srv import *
00012 from iri_bow_object_detector.msg import *
00013 from iri_perception_msgs.msg import ImagePoint
00014 from geometry_msgs.msg import Point
00015 
00016 from iri_bow_object_detector import config_feats
00017 #from iri_bow_object_detector.refine_grasp_point_finddd import select_grasp_point_finddd
00018 #from iri_bow_object_detector.refine_grasp_point_middle import select_grasp_point_middle
00019 #from iri_bow_object_detector.refine_grasp_point_manual import select_grasp_point_manual
00020 from iri_bow_object_detector.refine_grasp_point import select_grasp_point
00021 
00022 from iri_bow_object_detector.bow_detector_utils import ros_ima_to_numpy, save_extra_training_data
00023 
00024 #from iri_bow_object_detector.run_detector import gen_windows
00025 from iri_bow_object_detector.run_detector import detect_boxes
00026 #import ipdb
00027 import cv, cv2
00028 import pylab
00029 
00030 
00031 
00032 
00033 def read_vws(geo_vw_sets):
00034         vws=[]
00035         for geo_vw_set in geo_vw_sets:
00036                 vws.append([[geo_vw.x, geo_vw.y, geo_vw.word] for geo_vw in geo_vw_set.geo_vws])
00037         return vws
00038 
00039 
00040 def handle_detector_cb(req):
00041         vws = read_vws(req.geo_vw_sets)
00042 
00043         cv_image = ros_ima_to_numpy(req.image)
00044         cv_mask  = ros_ima_to_numpy(req.mask)
00045 
00046         cv_mask_gray = cv2.cvtColor(cv_mask, cv2.COLOR_RGB2GRAY)
00047 
00048         #Save data for future training
00049         save_extra_training_data(cv_image, cv_mask_gray)
00050 
00051         conf = config_feats.conf
00052         linear_svm_file_path = rospy.get_param("~linear_svm_file")
00053         if not exists(linear_svm_file_path):
00054             print "File not found:", non_linear_svm_file
00055             exit(-1)
00056         w  = np.load(linear_svm_file_path)
00057 
00058         if not exists(conf.det_results_dir):
00059                 system('mkdir '+conf.det_results_dir)
00060 
00061         print "Processing the algorithm."
00062         (win, prob) = detect_boxes(w, rospy.get_param("~non_linear_svm_file"), cv_image.copy(), cv_mask_gray, conf, vws)
00063 
00064         print "Finished"
00065         rospy.loginfo("Detection box: " + str(win) + " probs: " + str(prob));
00066 
00067         res = GeoVwDetectionResponse()
00068 
00069         for idx, sol in enumerate(win):
00070                 point1 = ImagePoint()
00071                 point1.x = sol[0]
00072                 point1.y = sol[1]
00073 
00074                 point2 = ImagePoint()
00075                 point2.x = sol[2]
00076                 point2.y = sol[3]
00077 
00078                 res.posible_solutions.append(ObjectBox(point1, point2, prob[idx]))
00079 
00080         return res
00081 
00082 
00083 
00084 def select_grasp_point_cb(req):
00085         #TO DELETE THIS BLOCK
00086         #return select_grasp_point_wrinkled_map(req)
00087         #return select_grasp_point_finddd(req, rospy.get_param("~finddd_svm_file"))
00088         #return select_grasp_point_middle(req)
00089         #return select_grasp_point_manual(req)
00090         #END OF TO DELETE THIS BLOCK
00091 
00092         ref_method = 'finddd'
00093         #ref_method = 'manual'
00094         #ref_method = 'middle'
00095         svm_filename = None
00096         if ref_method == 'finddd':
00097                 svm_filename = rospy.get_param("~finddd_svm_file")
00098 
00099         return select_grasp_point(req, ref_method, svm_filename)
00100 
00101 
00102 def add_detector_server():
00103         rospy.init_node('detector')
00104         s1 = rospy.Service('geo_vw_detector', GeoVwDetection, handle_detector_cb)
00105         s2 = rospy.Service('refine_grasp_point', RefineGraspPoint, select_grasp_point_cb)
00106 
00107         # check params
00108         if (not rospy.has_param("~linear_svm_file")):
00109                 rospy.logerr("Node has no param linear_svm_file. Please set it up on launch file")
00110                 exit
00111         if (not rospy.has_param("~non_linear_svm_file")):
00112                 rospy.logerr("Node has no param non_linear_svm_file. Please set it up on launch file")
00113                 exit
00114         if (not rospy.has_param("~finddd_svm_file")):
00115                 rospy.logerr("Node has no param non_linear_svm_file. Please set it up on launch file")
00116                 exit
00117         print "Detecting regions."
00118         rospy.spin()
00119 
00120 if __name__ == "__main__":
00121         add_detector_server()
00122 
00123 
00124 # DEPRECATED Now using finddd
00125 #def select_grasp_point_wrinkled_map(req):
00126         #(XX, YY, ZZ, W) = get_auto(req.wrinkled_map, [("pos", 0), ("curv", 4)])
00127         #P=[(box.point1.x, box.point1.y, box.point2.x, box.point2.y, box.value ) for box in req.posible_solutions]
00128 
00129         #P.sort(key=lambda x:x[4], reverse=True)
00130         #print P
00131 
00132         #if len(P) < 1:
00133                 #return Point(0,0,0)
00134 
00135         #xc=(P[0][0]+P[0][2])/2
00136         #yc=(P[0][1]+P[0][3])/2
00137 
00138         #offset=30
00139         #print xc,yc, W.shape
00140         #WW=W[yc-30:yc+30,xc-30:xc+30]
00141         #print W[yc-30:yc+30,xc-30:xc+30].max()
00142         #y,x=np.unravel_index(np.argmax(WW),WW.shape)
00143         #print np.argmax(WW), WW[y,x]
00144         #x=xc-30+x
00145         #y=yc-30+y
00146 
00147         #print "Solution is " + str(y) + "," + str(x)
00148 
00149         ## show result
00150         #bridge = CvBridge()
00151         #try:
00152                 #image = bridge.imgmsg_to_cv(req.image, "bgr8")
00153         #except CvBridgeError, e:
00154                 #print e
00155         #cv.Rectangle(image,(x-1,y-1), (x+1, y+1), (255,255,255))
00156         #cv.NamedWindow("Grasp point")
00157         #cv.ShowImage("Grasp point", image)
00158         #cv.WaitKey(10)
00159 
00160         #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