void udp_client::handle_receive(const boost::system::error_code& error, std::size_t bytes_transferred) { if (!error || error == boost::asio::error::message_size) { // Receive next data receive(); // receive_buffer has also old data. New data must be trimmed by checking the data between { and } brackets std::string trimmedData = trim_data(receive_buffer.data()); // Parse the received json string to get data const char * jsonString = trimmedData.c_str(); vector<vector<double>> handVector = getHandData(jsonString); // If predictionMode is active and received both the hand data, // predict the classlabel for every sample and send it via socket if(brain_->isPredictionModeActive() && !handVector.empty() && !handVector[0].empty() && !handVector[1].empty()){ // Predict and get classLabel and maximum likelihood vector<double> predictionResults = brain_->predict(handVector[0], handVector[1]); // Parse the same received json rapidjson::Document outputJson; outputJson.Parse(jsonString); // Add an array OUTPUT with unprocessed predicted class and maximum likelihood rapidjson::Value predictionArray(rapidjson::kArrayType); predictionArray.PushBack(predictionResults[1], outputJson.GetAllocator()); predictionArray.PushBack(predictionResults[2], outputJson.GetAllocator()); outputJson.AddMember("OUTPUT", predictionArray, outputJson.GetAllocator()); // Write the document to the buffer rapidjson::StringBuffer outputJsonBuffer; rapidjson::Writer<rapidjson::StringBuffer> outputJsonWriter(outputJsonBuffer); outputJson.Accept(outputJsonWriter); //Send it via websocket with prediction results ws_socket.send(outputJsonBuffer.GetString(), true, 1); // Post-processed predicted class if(predictionResults[0] > 0){ std::string signName = getSignName<std::string>(std::to_string((int)predictionResults[0])); std::stringstream gestureBuffer; gestureBuffer << "{\"GESTURE\":\"" << signName << "\"}"; ws_socket.send(gestureBuffer.str().c_str(), true, 3); } } // If trainingMode is active and received both the hand data, // train every input sample and simultaneously send the data via socket // but Don't log it in order not to block the user input else if(brain_->isTrainingModeActive() && !handVector.empty() && !handVector[0].empty() && !handVector[1].empty()){ brain_->train(handVector[0], handVector[1], &ws_socket); ws_socket.send(jsonString, false, 1); } // If trainingMode is waiting for user input to train another class // send the data via socket but don't log it in order not to block the user input else if(brain_->isTrainingModeWaitingForInput){ ws_socket.send(jsonString, false, 1); } // If handViewer is selected and both hands or single hand is present // Forward all via socket and log else{ if(ws_socket.isClientConnected()) { ws_socket.send(jsonString, true, 1); } else { BOOST_LOG_TRIVIAL(debug) << "Received : " << trimmedData; } } } else { BOOST_LOG_TRIVIAL(error) << error; } }
/*! Removes bytes from the end of the buffer. */ static status_t remove_trailer(net_buffer *buffer, size_t bytes) { return trim_data(buffer, buffer->size - bytes); }