1 #ifndef RAPP_CLOUD_FACEDETECTOR
2 #define RAPP_CLOUD_FACEDETECTOR
23 const std::shared_ptr<rapp::object::picture> image,
25 const std::string token,
26 std::function<
void(std::vector<rapp::object::face>)> callback
33 boost::property_tree::ptree tree;
34 tree.put(
"file", fname);
35 tree.put(
"fast", boost::lexical_cast<std::string>(fast));
37 boost::property_tree::write_json(ss, tree,
false);
39 post_ =
"--" + boundary +
"\r\n"
40 +
"Content-Disposition: form-data; name=\"json\"\r\n\r\n"
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";
47 auto imagebytes = image->bytearray();
48 post_.insert(
post_.end(), imagebytes.begin(), imagebytes.end());
49 post_ +=
"\r\n--" + boundary +
"--";
51 header_ =
"POST /hop/face_detection HTTP/1.1\r\n";
52 +
"Content-Type: multipart/form-data; boundary=" + boundary +
"\r\n\r\n";
63 std::stringstream ss(json);
64 std::vector<rapp::object::face> faces;
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>();
77 else if (it.first ==
"y") {
78 std::get<1>(up_left) = it.second.get_value<
float>();
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>();
87 else if (it.first ==
"y") {
88 std::get<1>(down_right) = it.second.get_value<
float>();
95 std::get<0>(down_right),
96 std::get<1>(down_right)));
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;
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;
115 std::function<void(std::vector<rapp::object::face>)>
delegate_;
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)
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 ...