recognizer_prediction_psm_server.cpp
Go to the documentation of this file.
1 
18 #include "ros/ros.h"
19 #include "asr_recognizer_prediction_psm/recognizer_prediction_psm.h"
20 #include <sensor_msgs/PointCloud2.h>
21 #include <asr_msgs/AsrAttributedPointCloud.h>
22 #include <asr_msgs/AsrObject.h>
24 #include "AttributedPoint.h"
25 #include "asrVisualizer.h"
26 
27 
28 
43 bool generate_hyps(asr_recognizer_prediction_psm::recognizer_prediction_psm::Request &req,
44  asr_recognizer_prediction_psm::recognizer_prediction_psm::Response &res)
45 {
47  bool visualize_hypothesis = true;
48 
49  if(!n.getParam("/recognizer_prediction_psm/visualize_hypothesis", visualize_hypothesis))
50  visualize_hypothesis = true;
51 
52  // Load all data
53  std::string path = req.pathToXML;
54  std::vector<std::string> scene_list = req.scenes;
55  std::vector<float> scene_probabilities = req.scene_probabilities;
56  std::vector<int> votes_per_scene;
57  uint32_t num_votes = req.num_votes;
58  std::vector<asr_msgs::AsrObject> objects = req.objects;
59  std::vector<boost::shared_ptr<asr_msgs::AsrObject>> object_pointers;
60 
61  // Some sanity checks
62  {
63  if(scene_list.size() != scene_probabilities.size())
64  {
65  ROS_ERROR("The number of scenes has to be equal to the number of scene probabilities!");
66  return false;
67  }
68 
69  if(objects.size() < 1)
70  {
71  ROS_ERROR("There has to be at least one observed object!");
72  return false;
73  }
74 
75  ROS_INFO("Overall number of hypothesis that should be generated: %i", num_votes);
76 
77  for(unsigned int i=0;i<scene_list.size();i++)
78  {
79  votes_per_scene.push_back((int) (scene_probabilities.at(i) * num_votes));
80 
81  ROS_INFO("Loaded scene %s with probability %.2f. Number of hypothesis for this scene %i",
82  scene_list.at(i).c_str(),
83  scene_probabilities.at(i),
84  votes_per_scene.at(i));
85  }
86 
87  // Convert the objects msgs in the correct format
88  for(asr_msgs::AsrObject o : objects)
89  {
90 
91  if(!o.sampledPoses.size()){
92  std::cerr << "Got a AsrObject without poses." << std::endl;
93  std::exit(1);
94  }
95 
97  p->type = o.type;
98  p->identifier = o.identifier;
99  p->header.frame_id = o.header.frame_id;
100  p->sampledPoses.push_back(o.sampledPoses.at(0));
101  object_pointers.push_back(p);
102 
103  }
104  }
105  // / Sanity checks
106 
107 
108 
109 
110 
111  // Now start the pose prediction engine with the given data
112  std::vector<ASR::AttributedPoint> pose_hypotheses;
113  std::vector<ASR::AttributedPoint> found_objects;
114  bool all_scene_objects_found = true;
115 
116  printf("\n\n");
117 
118  for(unsigned int i=0;i<scene_list.size();i++)
119  {
120  ROS_INFO("\nPath: %s\nCurrent Scene: %s\n", path.c_str(), scene_list.at(i).c_str());
121 
127  scene_list.at(i),
128  object_pointers,
129  votes_per_scene.at(i),
130  req.base_frame_id)
131  );
132 
136  engine->calcHypotheses();
137 
138 
142  engine->collectPoseHypothesesRecursive(pose_hypotheses, found_objects);
143 
144 
149  if(engine->getNumberOfMissingObjects() > 0 && scene_list.at(i).compare("background") != 0)
150  all_scene_objects_found = false;
151  }
152 
153 
154 
155 
156 
157 
161  asr_msgs::AsrAttributedPointCloud msg;
162 
163  for(unsigned int i=0; i<pose_hypotheses.size();i++)
164  {
165  asr_msgs::AsrAttributedPoint element;
166 
167  element.type = pose_hypotheses.at(i).objectType;
168  element.pose = pose_hypotheses.at(i).pose;
169 
170  msg.elements.push_back(element);
171  }
172  res.pose_hypothesis = msg;
173  res.all_scene_objects_found = all_scene_objects_found;
174 
175  printf("\n\n");
176 
177 
181  if(visualize_hypothesis)
182  {
184  vis->generateMarkerArray(pose_hypotheses, found_objects );
185 
186 
187  for(int i=0;i<5;i++) {
188  ROS_INFO("Publishing MarkerArray");
189  vis->publishMarkerArray();
190  sleep(1);
191  }
192  }
193 
194  return true;
195 }
196 
197 int main(int argc, char **argv)
198 {
199  ros::init(argc, argv, "generate_hypothesis_server");
200  ros::NodeHandle n;
201 
202  ros::ServiceServer service = n.advertiseService("recognizer_prediction_psm", generate_hyps);
203  ROS_INFO("Ready to generate hypothesis\n");
204 
205 
206  ros::spin();
207 
208  return 0;
209 }
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ROSCPP_DECL void spin(Spinner &spinner)
#define ROS_INFO(...)
bool generate_hyps(asr_recognizer_prediction_psm::recognizer_prediction_psm::Request &req, asr_recognizer_prediction_psm::recognizer_prediction_psm::Response &res)
generate_hyps
int main(int argc, char **argv)
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR(...)


asr_recognizer_prediction_psm
Author(s): Braun Kai, Meißner Pascal
autogenerated on Wed Feb 19 2020 03:31:30