speech_to_text_action_handler.cc
Go to the documentation of this file.
2 
3 #include "third_party/gflags.h"
4 #include "third_party/glog.h"
5 #include "util/statusor.h"
6 
7 namespace speech = ::cogrob::cloud::speech;
8 
11 
12 DEFINE_int32(speech_fail_prematurely_retry_cutoff_msec, 300,
13  "Cutoff duration to allow retrying if recognition failed prematurely.");
14 
15 namespace gcloud_speech {
16 
19  SpeechToTextSimpleActionServer* simple_action_server) {
20  recognizer_ = recognizer;
21  simple_action_server_ = simple_action_server;
22  is_active_.store(false);
23 }
24 
25 
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.";
31  DCHECK(false);
32  return;
33  }
34  if (is_active_.load()) {
35  std::unique_ptr<speech::AudioSample> audio_sample(
36  new speech::AudioSample(msg->data));
37  audio_queue_.push(std::move(audio_sample));
38  }
39 }
40 
42  const gcloud_speech_msgs::SpeechToTextGoalConstPtr& goal) {
45  recognizer_->Stop();
46  is_active_.store(true);
47 
48  int max_audio_seconds = goal->listen_duration_sec;
49  if (max_audio_seconds == 0) {
50  max_audio_seconds = 14;
51  }
52  int max_wait_seconds = goal->max_recognition_duration_sec;
53  if (max_wait_seconds == 0) {
54  max_wait_seconds = max_audio_seconds + 2;
55  }
56 
57  bool interim_results = !goal->suppress_interim_results;
58 
59  // This is the result we will publish.
60  gcloud_speech_msgs::SpeechToTextResult result_msg;
61 
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);
66 
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);
72 
73  while (recognizer_->IsRunning() &&
77  if (result.ok()) {
78  // Processes the result and post some feedback.
79  LOG(INFO) << "Result: " << result.ValueOrDie().ShortDebugString();
80 
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);
88  }
89  feedback_msg.is_portion_final = result.ValueOrDie().is_final();
90  feedback_msg.stability = result.ValueOrDie().stability();
92  }
93 
94  if (result.ValueOrDie().is_final()) {
95  if (result.ValueOrDie().candidates().size() > 0) {
96  result_msg.transcript +=
97  " " + result.ValueOrDie().candidates()[0].transcript();
98  }
99  }
100  }
101  }
102 
103  recognizer_->Stop();
104 
105  if (recognizer_->GetLastResult().ok()) {
106  // If there is no error, we can quit retrying.
107  break;
108  }
109  // Decreate retry counter so we don't retry too many times.
110  --retry_time_left;
111  }
112 
115  if (!last_result.ok()) {
116  result_msg.is_error = true;
117  result_msg.error_info = last_result.status().error_message();
118  }
119 
121  simple_action_server_->setPreempted(result_msg);
122  } else {
123  simple_action_server_->setSucceeded(result_msg);
124  }
125  is_active_.store(false);
126 }
127 
128 } // namespace gcloud_speech
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.")
bool ok() const
Definition: statusor.h:277
actionlib::SimpleActionServer< gcloud_speech_msgs::SpeechToTextAction > * simple_action_server_
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
void ExecuteSpeechToTextAction(const gcloud_speech_msgs::SpeechToTextGoalConstPtr &goal)
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
virtual util::StatusOr< RecognitionResult > GetLastResult()=0
SpeechToTextActionHandler(cogrob::cloud::speech::GoogleSpeechRecognizerInterface *recognizer, actionlib::SimpleActionServer< gcloud_speech_msgs::SpeechToTextAction > *simple_action_server)
cogrob::cloud::speech::GoogleSpeechRecognizerInterface * recognizer_
const Status & status() const
Definition: statusor.h:272
std::vector< uint8_t > AudioSample
Definition: audio_sample.h:43
const T & ValueOrDie() const
Definition: statusor.h:282


gcloud_speech
Author(s):
autogenerated on Mon Jun 10 2019 13:20:53