RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
human_detection.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_HUMAN_DETECTION
2 #define RAPP_CLOUD_HUMAN_DETECTION
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
23  const std::shared_ptr<rapp::object::picture> image,
24  std::function<void(std::vector<rapp::object::human>)> callback,
25  std::string token
26  )
27  : asio_service_http(token), delegate__(callback)
28  {
29  assert(image);
30  std::string boundary = random_boundary();
31  std::string fname = random_boundary()+"."+image->type();
32  boost::property_tree::ptree tree;
33  tree.put("file", fname);
34  std::stringstream ss;
35  boost::property_tree::write_json(ss, tree, false);
36  post_ = "--" + boundary + "\r\n"
37  + "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
38  + ss.str() + "\r\n";
39  post_ += "--"+boundary+"\r\n"
40  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
41  + "Content-Type: image/"+image->type()+"\r\n"
42  + "Content-Transfer-Encoding: binary\r\n\r\n";
43  // Append binary data
44  auto imagebytes = image->bytearray();
45  post_.insert(post_.end(), imagebytes.begin(), imagebytes.end());
46  post_ += "\r\n";
47  post_ += "--"+boundary+"--";
48  header_ = "POST /hop/human_detection HTTP/1.1\r\n";
49  header_ += "Content-Type: multipart/form-data; boundary="+boundary+"\r\n\r\n";
50  callback_ = std::bind(&human_detection::handle_reply, this, std::placeholders::_1);
51  }
52 private:
56  void handle_reply(std::string json)
57  {
58  std::stringstream ss(json);
59  std::vector<rapp::object::human> humans;
60  try {
61  boost::property_tree::ptree tree;
62  boost::property_tree::read_json(ss, tree);
63  // iterate detected humans (json object `human`)
64  for (auto child : tree.get_child("humans")) {
65  std::pair<float,float> up_left;
66  std::pair<float,float> down_right;
67  // iterate `human` json members
68  for (auto iter = child.second.begin(); iter!= child.second.end(); ++iter) {
69  if (iter->first == "up_left_point") {
70  for (auto it : iter->second) {
71  if (it.first == "x") {
72  std::get<0>(up_left) = it.second.get_value<float>();
73  }
74  else if (it.first == "y") {
75  std::get<1>(up_left) = it.second.get_value<float>();
76  }
77  }
78  }
79  else if (iter->first == "down_right_point") {
80  for (auto it : iter->second) {
81  if ( it.first == "x" ) {
82  std::get<0>(down_right) = it.second.get_value<float>();
83  }
84  else if (it.first == "y") {
85  std::get<1>(down_right) = it.second.get_value<float>();
86  }
87  }
88  }
89  }
90  humans.push_back(rapp::object::human(std::get<0>(up_left),
91  std::get<1>(up_left),
92  std::get<0>(down_right),
93  std::get<1>(down_right)));
94  }
95  // Check for Errors returned by the platform
96  for (auto child : tree.get_child("error")) {
97  const std::string value = child.second.get_value<std::string>();
98  if (!value.empty()) {
99  std::cerr << "human_detection JSON error: " << value << std::endl;
100  }
101  }
102  }
103  catch(boost::property_tree::json_parser::json_parser_error & je) {
104  std::cerr << "human_detection::handle_reply Error parsing: "
105  << je.filename() << " on line: " << je.line() << std::endl;
106  std::cerr << je.message() << std::endl;
107  }
108  delegate__(humans);
109  }
110 
112  std::function<void(std::vector<rapp::object::human>)> delegate__;
113 };
114 }
115 }
116 #endif
std::string header_
Header that will be used.
human_detection(const std::shared_ptr< rapp::object::picture > image, std::function< void(std::vector< rapp::object::human >)> callback, std::string token)
Constructor.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
void handle_reply(std::string json)
handle the rapp-platform JSON reply
describes human coordinates
Definition: human.hpp:13
std::string post_
Actual post Data.
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 ...
detect humans in an image
std::function< void(std::vector< rapp::object::human >)> delegate__
The callback called upon completion of receiving the detected faces.