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