/**
     * @brief Constructor
     * @param image is the input image @see rapp::object::picture
     * @param image_format is the image format
     * @param callback is the function that will receive a vector of the detected face(s) coordinates
     */
    faceDetector (
                    const std::shared_ptr<rapp::object::picture> image,
                    const std::string image_format,
                    std::function< void ( std::vector< rapp::object::face > ) > callback
                 )
    : rapp::services::asio_service_http (), delegate_ ( callback )
    {
        assert( image );

        // Create a new random boundary
        std::string boundary = randomBoundary();

        // Create a random file name + extension
        std::string fname = randomBoundary() + "." + image_format;

        // Create the Multi-form POST field
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"file_uri\"; filename=\"" + fname  + "\"\r\n";
        post_ += "Content-Type: image/" + image_format + "\r\n";
        post_ += "Content-Transfer-Encoding: binary\r\n\r\n";

        // Append binary data
        auto imagebytes = image->bytearray();
        post_.insert( post_.end(), imagebytes.begin(), imagebytes.end() );
        post_ += "\r\n";
        post_ += "--" + boundary + "--";

        // Count Data size
        auto size = post_.size() * sizeof( std::string::value_type );

        // Form the Header
        header_ =  "POST /hop/face_detection HTTP/1.1\r\n";
        header_ += "Host: " + std::string( rapp::cloud::address ) + "\r\n";
        header_ += "Connection: close\r\n";
        header_ += "Content-Length: " + boost::lexical_cast<std::string>( size ) + "\r\n";
        header_ += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n\r\n";

        // bind the base class callback, to our handle_reply
        callback_ = std::bind ( &faceDetector::handle_reply, this, std::placeholders::_1 );
    }
Beispiel #2
0
    /**
    * @brief Constructor
    * @param image is a picture object pointer
    * @param callback is the function that will receive a vector of detected qr(s)
    * @param image_format must be defined, e.g.: jpeg, png, gif, etc.
    */
    qrDetector (
                 const std::shared_ptr<rapp::object::picture> image,
                 const std::string image_format,
                 std::function< void ( std::vector< rapp::object::qrCode> ) > callback
               )
    : rapp::services::asio_service_http (), delegate__ ( callback )
    {
        if ( !image )
            throw std::runtime_error ( "qrDetector::qrDetector param image null ptr" );

        // Create a new random boundary
        std::string boundary = randomBoundary();

        // Create the name for the image (just a textfield request)
        post_ = "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"fileName\"\r\n\r\n";
        post_ += "image." + image_format + "\r\n";

        // Create the Multi-form POST field
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"fileContents\"; filename=\"image." + image_format + "\"\r\n";
        post_ += "Content-Type: image/" + image_format + "\r\n";
        post_ += "Content-Transfer-Encoding: binary\r\n\r\n";

        // Append binary data
        auto imagebytes = image->bytearray();
        post_.insert( post_.end(), imagebytes.begin(), imagebytes.end() );
        post_ += "\r\n";
        post_ += "--" + boundary + "--";

        // Count Data size
        auto size = post_.size() * sizeof( std::string::value_type );

        // Form the Header
        header_ =  "POST /hop/qr_detect HTTP/1.1\r\n";
        header_ += "Host: " + std::string( hostname ) + "\r\n";
        header_ += "Connection: close\r\n";
        header_ += "Content-Length: " + boost::lexical_cast<std::string>( size ) + "\r\n";
        header_ += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n\r\n";

        // bind the asio_service_http::callback, to our handle_reply
        callback_ = std::bind ( &qrDetector::handle_reply, this, std::placeholders::_1 );   
    }
    /**
     * @brief Contrusct a speechToText handler
     * @param audio is the actual binary sound file
     * @param language is the language used for speech to text
     * @param grammar is the Grammars used in Spc2Txt
     * @param user is the user token
     * @param words will be searched for in the audio
     * @param sentences will be under consideration
     * @param callback will be executed once the rapp cloud has responded
     */
    speechToText (
                    const std::shared_ptr<rapp::object::audio> file,
                    const std::string language,
                    const std::string user,
                    const std::string audio_source,
                    const std::vector<std::string> grammar,
                    const std::vector<std::string> words,
                    const std::vector<std::string> sentences,
                    std::function< void( std::vector<std::string> words ) > callback
                 )
    : rapp::services::asio_service_http (), delegate_ ( callback )
    {
        assert( file );

        // Create a new random boundary
        std::string boundary = randomBoundary();
        std::string fname =  randomBoundary(); // || "audio.ogg" || "audio.wav"
            
        // Boundary start and 1st POST
        post_  = "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"language\"\r\n\r\n";
        post_ += language + "\r\n";

        // User
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"user\"\r\n\r\n";
        post_ += user + "\r\n";

        // Audio Source (Audio Type)
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"audio_source\"\r\n\r\n";
        post_ += audio_source + "\r\n";

        // Grammar[]
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"grammar\"\r\n\r\n";
        post_ += "[ ";
        for ( const auto gram : grammar ) post_ += "\"" + gram + "\",";
        post_.pop_back();
        post_ += "]\r\n";

        // Words[]
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"words\"\r\n\r\n";
        post_ += "[ ";
        // WARNING - escape double quotes!!!
        for ( const auto word : words ) post_ += "\"" + word + "\",";
        post_.pop_back();
        post_ += "]\r\n";

        // Sentences[]
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"sentences\"\r\n\r\n";
        post_ += "[ ";
        // WARNING - escape double quotes!!!
        for ( const auto sent : sentences ) post_ += "\"" + sent + "\",";
        post_.pop_back();
        post_ += "]\r\n";

        // file_uri - NOTE Assume WAV extension
        post_ += "--" + boundary + "\r\n";
        post_ += "Content-Disposition: form-data; name=\"file_uri\"; filename=\"" + fname + "\"\r\n";
        post_ += "Content-Transfer-Encoding: binary\r\n\r\n";

        //std::cout << post_;

        // Append binary data
        auto bytes = file->bytearray();
        post_.insert( post_.end(), bytes.begin(), bytes.end() );
        post_ += "\r\n";
        post_ += "--" + boundary + "--";
       
        // Count Data size
        auto size = post_.size() * sizeof( std::string::value_type );
        
        // Form the Header
        header_ =  "POST /hop/speech_detection_sphinx4 HTTP/1.1\r\n";
        header_ += "Host: " + std::string( rapp::cloud::address ) + "\r\n";
        header_ += "Connection: close\r\n";
        header_ += "Content-Length: " + boost::lexical_cast<std::string>( size ) + "\r\n";
        header_ += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n\r\n";
        
        //std::cout << header_;

        // bind the base class callback, to our handle_reply
        callback_ = std::bind ( &speechToText::handle_reply, this, std::placeholders::_1 );
    }