ros1_classifier_server.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012, 2019 Scott Niekum, Joshua Whitley
2 // All rights reserved.
3 //
4 // Software License Agreement (BSD License 2.0)
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of {copyright_holder} nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 #include <pluginlib/class_loader.h>
34 
35 #include <string>
36 #include <map>
37 
38 #include "ros/ros.h"
39 
42 #include "ml_classifiers/CreateClassifier.h"
43 #include "ml_classifiers/AddClassData.h"
44 #include "ml_classifiers/TrainClassifier.h"
45 #include "ml_classifiers/ClearClassifier.h"
46 #include "ml_classifiers/SaveClassifier.h"
47 #include "ml_classifiers/LoadClassifier.h"
48 #include "ml_classifiers/ClassifyData.h"
49 
50 using namespace ml_classifiers; // NOLINT
51 using std::string;
52 using std::cout;
53 using std::endl;
54 
55 std::map<string, boost::shared_ptr<Classifier>> classifier_list;
56 pluginlib::ClassLoader<Classifier> c_loader("ml_classifiers", "ml_classifiers::Classifier");
57 
58 bool createHelper(string class_type, boost::shared_ptr<Classifier> & c)
59 {
60  try {
61  c = c_loader.createInstance(class_type);
62  } catch (pluginlib::PluginlibException & ex) {
63  ROS_ERROR("Classifer plugin failed to load! Error: %s", ex.what());
64  }
65 
66  return true;
67 }
68 
70  CreateClassifier::Request & req,
71  CreateClassifier::Response & res)
72 {
73  string id = req.identifier;
75 
76  if (!createHelper(req.class_type, c)) {
77  res.success = false;
78  return false;
79  }
80 
81  if (classifier_list.find(id) != classifier_list.end()) {
82  cout << "WARNING: ID already exists, overwriting: " << req.identifier << endl;
83  classifier_list.erase(id);
84  }
85 
86  classifier_list[id] = c;
87 
88  res.success = true;
89  return true;
90 }
91 
93  AddClassData::Request & req,
94  AddClassData::Response & res)
95 {
96  string id = req.identifier;
97 
98  if (classifier_list.find(id) == classifier_list.end()) {
99  res.success = false;
100  return false;
101  }
102 
103  for (size_t i = 0; i < req.data.size(); i++) {
104  classifier_list[id]->addTrainingPoint(req.data[i].target_class, req.data[i].point);
105  }
106 
107  res.success = true;
108  return true;
109 }
110 
112  TrainClassifier::Request & req,
113  TrainClassifier::Response & res)
114 {
115  string id = req.identifier;
116 
117  if (classifier_list.find(id) == classifier_list.end()) {
118  res.success = false;
119  return false;
120  }
121 
122  cout << "Training " << id << endl;
123 
124  classifier_list[id]->train();
125  res.success = true;
126  return true;
127 }
128 
130  ClearClassifier::Request & req,
131  ClearClassifier::Response & res)
132 {
133  string id = req.identifier;
134 
135  if (classifier_list.find(id) == classifier_list.end()) {
136  res.success = false;
137  return false;
138  }
139 
140  classifier_list[id]->clear();
141  res.success = true;
142  return true;
143 }
144 
145 
147  SaveClassifier::Request & req,
148  SaveClassifier::Response & res)
149 {
150  string id = req.identifier;
151 
152  if (classifier_list.find(id) == classifier_list.end()) {
153  res.success = false;
154  return false;
155  }
156 
157  classifier_list[id]->save(req.filename);
158  res.success = true;
159  return true;
160 }
161 
162 
164  LoadClassifier::Request & req,
165  LoadClassifier::Response & res)
166 {
167  string id = req.identifier;
169 
170  if (!createHelper(req.class_type, c)) {
171  res.success = false;
172  return false;
173  }
174 
175  if (!c->load(req.filename)) {
176  res.success = false;
177  return false;
178  }
179 
180  if (classifier_list.find(id) != classifier_list.end()) {
181  cout << "WARNING: ID already exists, overwriting: " << req.identifier << endl;
182  classifier_list.erase(id);
183  }
184 
185  classifier_list[id] = c;
186 
187  res.success = true;
188  return true;
189 }
190 
192  ClassifyData::Request & req,
193  ClassifyData::Response & res)
194 {
195  string id = req.identifier;
196 
197  for (size_t i = 0; i < req.data.size(); i++) {
198  string class_num = classifier_list[id]->classifyPoint(req.data[i].point);
199  res.classifications.push_back(class_num);
200  }
201 
202  return true;
203 }
204 
205 int main(int argc, char ** argv)
206 {
207  ros::init(argc, argv, "classifier_server");
208  ros::NodeHandle n;
209 
211  "/ml_classifiers/create_classifier", createCallback);
213  "/ml_classifiers/add_class_data", addCallback);
215  "/ml_classifiers/train_classifier", trainCallback);
217  "/ml_classifiers/clear_classifier", clearCallback);
219  "/ml_classifiers/save_classifier", saveCallback);
221  "/ml_classifiers/load_classifier", loadCallback);
223  "/ml_classifiers/classify_data", classifyCallback);
224 
225  ROS_INFO("Classifier services now ready");
226  ros::spin();
227 
228  return 0;
229 }
bool createHelper(string class_type, boost::shared_ptr< Classifier > &c)
pluginlib::ClassLoader< Classifier > c_loader("ml_classifiers","ml_classifiers::Classifier")
bool createCallback(CreateClassifier::Request &req, CreateClassifier::Response &res)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::map< string, boost::shared_ptr< Classifier > > classifier_list
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool clearCallback(ClearClassifier::Request &req, ClearClassifier::Response &res)
bool saveCallback(SaveClassifier::Request &req, SaveClassifier::Response &res)
#define ROS_INFO(...)
bool loadCallback(LoadClassifier::Request &req, LoadClassifier::Response &res)
ROSCPP_DECL void spin()
bool trainCallback(TrainClassifier::Request &req, TrainClassifier::Response &res)
c
Definition: easy.py:61
bool classifyCallback(ClassifyData::Request &req, ClassifyData::Response &res)
#define ROS_ERROR(...)
int main(int argc, char **argv)
bool addCallback(AddClassData::Request &req, AddClassData::Response &res)


ml_classifiers
Author(s): Scott Niekum , Joshua Whitley
autogenerated on Sun Dec 15 2019 03:53:50