Go to the documentation of this file.00001
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
00018
00019
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
00025 from iri_bow_object_detector.run_detector import detect_boxes
00026
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
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
00086
00087
00088
00089
00090
00091
00092 ref_method = 'finddd'
00093
00094
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
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
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160