3 from google.cloud
import speech
4 from google.cloud.speech
import enums
5 from google.cloud.speech
import types
9 from std_msgs.msg
import String
15 FORMAT = pyaudio.paInt16
20 self.
stream = self.audio.open(format=FORMAT, channels=CHANNELS,
21 rate=RATE, input=
True,
22 frames_per_buffer=self.
CHUNK,
28 text_topic = rospy.get_param(
'/text_topic',
'/dialogflow_text')
29 self.
text_pub = rospy.Publisher(text_topic, String, queue_size=10)
31 def _get_data(self, in_data, frame_count, time_info, status):
32 """Daemon thread to continuously get audio data from the server and put 36 self._buff.put(in_data)
37 return None, pyaudio.paContinue
40 """Generator function that continuously yields audio chunks from the buffer. 41 Used to stream data to the Google Speech API Asynchronously. 45 chunk = self._buff.get()
53 chunk = self._buff.get(block=
False)
63 """Iterates through server responses and prints them. 64 The responses passed is a generator that will block until a response 65 is provided by the server. 66 Each response may contain multiple results, and each result may contain 67 multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we 68 print only the transcription for the top alternative of the top result. 70 for response
in responses:
72 if not response.results:
78 result = response.results[0]
79 if not result.alternatives:
83 transcript = result.alternatives[0].transcript
87 rospy.loginfo(
"Google Speech result: {}".format(result))
89 transcript = transcript.encode(
'utf-8')
91 if transcript.startswith(
' '):
92 transcript = transcript[1:]
94 if transcript.lower() ==
'exit':
97 self.text_pub.publish(transcript)
100 """Creates the Google Speech API client, configures it, and sends/gets 101 audio/text data for parsing. 103 language_code =
'en-US' 104 client = speech.SpeechClient()
105 config = types.RecognitionConfig(
106 encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
107 sample_rate_hertz=16000,
108 language_code=language_code)
109 streaming_config = types.StreamingRecognitionConfig(
111 interim_results=
True)
113 requests = (types.StreamingRecognizeRequest(audio_content=content)
for content
in self.
_generator())
114 responses = client.streaming_recognize(streaming_config, requests)
118 """Shut down as cleanly as possible""" 119 rospy.loginfo(
"Shutting down")
123 self.audio.terminate()
127 """Entry function to start the client""" 129 rospy.loginfo(
"Starting Google speech mic client")
131 except KeyboardInterrupt:
135 if __name__ ==
'__main__':
136 rospy.init_node(
'dialogflow_mic_client')
def _get_data(self, in_data, frame_count, time_info, status)
def _listen_print_loop(self, responses)