7 namespace speech = ::cogrob::cloud::speech;
12 DEFINE_int32(speech_fail_prematurely_retry_cutoff_msec, 300,
13 "Cutoff duration to allow retrying if recognition failed prematurely.");
27 const gcloud_speech_msgs::LinearPcm16Le16000Audio::ConstPtr& msg) {
28 if (msg->data.size() & 1 != 0) {
29 LOG(ERROR) <<
"Size of data in LinearPcm16Le16000Audio is not " 30 <<
"multiple of 2. Discarding sample.";
35 std::unique_ptr<speech::AudioSample> audio_sample(
42 const gcloud_speech_msgs::SpeechToTextGoalConstPtr& goal) {
48 int max_audio_seconds = goal->listen_duration_sec;
49 if (max_audio_seconds == 0) {
50 max_audio_seconds = 14;
52 int max_wait_seconds = goal->max_recognition_duration_sec;
53 if (max_wait_seconds == 0) {
54 max_wait_seconds = max_audio_seconds + 2;
57 bool interim_results = !goal->suppress_interim_results;
60 gcloud_speech_msgs::SpeechToTextResult result_msg;
62 int retry_time_left = 2;
63 std::chrono::system_clock::time_point retry_deadline =
64 std::chrono::system_clock::now() +
65 std::chrono::milliseconds(FLAGS_speech_fail_prematurely_retry_cutoff_msec);
67 while (retry_time_left > 0
68 && std::chrono::system_clock::now() < retry_deadline) {
69 LOG(INFO) <<
"Start recognize.";
71 max_audio_seconds, max_wait_seconds, goal->max_alternatives);
79 LOG(INFO) <<
"Result: " << result.
ValueOrDie().ShortDebugString();
81 if (result.
ValueOrDie().is_final() or interim_results) {
82 gcloud_speech_msgs::SpeechToTextFeedback feedback_msg;
83 for (
const auto& candidate: result.
ValueOrDie().candidates()) {
84 gcloud_speech_msgs::RecognitionHypothesis hypothesis;
85 hypothesis.transcript = candidate.transcript();
86 hypothesis.confidence = candidate.confidence();
87 feedback_msg.hypotheses.push_back(hypothesis);
89 feedback_msg.is_portion_final = result.
ValueOrDie().is_final();
90 feedback_msg.stability = result.
ValueOrDie().stability();
95 if (result.
ValueOrDie().candidates().size() > 0) {
96 result_msg.transcript +=
97 " " + result.
ValueOrDie().candidates()[0].transcript();
115 if (!last_result.
ok()) {
116 result_msg.is_error =
true;
117 result_msg.error_info = last_result.
status().error_message();
virtual util::Status StartRecognize(AudioQueue *audio_queue, util::SimpleThreadSafeQueue< RecognitionResult > *result_queue, const std::vector< std::string > &hints={}, int max_audio_seconds=14, int max_wait_seconds=17, int max_alternatives=10)=0
util::SimpleThreadSafeQueue< cogrob::cloud::speech::RecognitionResult > result_queue_
void publishFeedback(const FeedbackConstPtr &feedback)
void AudioMsgCallback(const gcloud_speech_msgs::LinearPcm16Le16000Audio::ConstPtr &msg)
DEFINE_int32(speech_fail_prematurely_retry_cutoff_msec, 300,"Cutoff duration to allow retrying if recognition failed prematurely.")
actionlib::SimpleActionServer< gcloud_speech_msgs::SpeechToTextAction > * simple_action_server_
virtual util::Status Stop()=0
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
std::atomic_bool is_active_
void ExecuteSpeechToTextAction(const gcloud_speech_msgs::SpeechToTextGoalConstPtr &goal)
void push(Args &&...args)
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
virtual util::StatusOr< RecognitionResult > GetLastResult()=0
cogrob::cloud::speech::AudioQueue audio_queue_
SpeechToTextActionHandler(cogrob::cloud::speech::GoogleSpeechRecognizerInterface *recognizer, actionlib::SimpleActionServer< gcloud_speech_msgs::SpeechToTextAction > *simple_action_server)
cogrob::cloud::speech::GoogleSpeechRecognizerInterface * recognizer_
const Status & status() const
bool isPreemptRequested()
virtual bool IsRunning()=0
std::vector< uint8_t > AudioSample
const T & ValueOrDie() const