RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
object_recognition.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_OBJECT_RECOGNITION
2 #define RAPP_CLOUD_OBJECT_RECOGNITION
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
22  const std::shared_ptr<rapp::object::picture> image,
23  std::function<void(std::string)> callback,
24  std::string token
25  )
26  : asio_service_http(token), delegate__(callback)
27  {
28  assert(image);
29  std::string boundary = random_boundary();
30  std::string fname = random_boundary()+"."+image->type();
31  boost::property_tree::ptree tree;
32  tree.put("file", fname);
33  std::stringstream ss;
34  boost::property_tree::write_json(ss, tree, false);
35  post_ = "--" + boundary + "\r\n"
36  + "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
37  + ss.str() + "\r\n";
38  post_ += "--"+boundary+"\r\n"
39  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
40  + "Content-Type: image/"+image->type()+"\r\n"
41  + "Content-Transfer-Encoding: binary\r\n\r\n";
42  // Append binary data
43  auto imagebytes = image->bytearray();
44  post_.insert(post_.end(), imagebytes.begin(), imagebytes.end());
45  post_ += "\r\n";
46  post_ += "--"+boundary+"--";
47  header_ = "POST /hop/object_recognition_caffe HTTP/1.1\r\n";
48  header_ += "Content-Type: multipart/form-data; boundary="+boundary+"\r\n\r\n";
49  callback_ = std::bind(&object_recognition::handle_reply, this, std::placeholders::_1);
50  }
51 private:
55  void handle_reply(std::string json)
56  {
57  std::stringstream ss(json);
58  std::string object_class;
59  try {
60  boost::property_tree::ptree tree;
61  boost::property_tree::read_json(ss, tree);
62  for (auto child : tree.get_child("object_class")) {
63  object_class = child.second.get_value<std::string>();
64  }
65  // Check for Errors returned by the platform
66  for (auto child : tree.get_child("error")) {
67  const std::string value = child.second.get_value<std::string>();
68  if (!value.empty()) {
69  std::cerr << "object_recognition JSON error: " << value << std::endl;
70  }
71  }
72  }
73  catch(boost::property_tree::json_parser::json_parser_error & je) {
74  std::cerr << "object_recognition::handle_reply Error parsing: "
75  << je.filename() << " on line: " << je.line() << std::endl;
76  std::cerr << je.message() << std::endl;
77  }
78  delegate__(object_class);
79  }
80 
82  std::function<void(std::string)> delegate__;
83 };
84 }
85 }
86 #endif
object_recognition(const std::shared_ptr< rapp::object::picture > image, std::function< void(std::string)> callback, std::string token)
Constructor.
std::string header_
Header that will be used.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
std::function< void(std::string)> delegate__
The callback called upon completion of receiving the detected faces.
std::string post_
Actual post Data.
void handle_reply(std::string json)
handle the rapp-platform JSON reply
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
base class for asynchronous http websockets used for connecting to cloud services ...
recognize object from an image