249 | def recog(self, utterance_id): |
250 | context = rospy.get_param(self.node_name + '/speech_context', []) |
251 | path = self.utterance_file(utterance_id) |
252 | |
253 | with io.open(path, 'rb') as audio_file: |
254 | content = audio_file.read() |
255 | |
256 | audio = types.RecognitionAudio(content=content) |
257 | config = types.RecognitionConfig( |
258 | encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, |
259 | sample_rate_hertz=self.sample_rate, |
260 | language_code='en-US', |
261 | enable_automatic_punctuation=True) |
262 | |
263 | |
264 | if self.async: |
265 | operation = self.speech_client.long_running_recognize(config, audio) |
266 | op_result = operation.result() |
267 | for result in op_result.results: |
268 | for alternative in result.alternatives: |
269 | print(alternative) |
270 | return alternative.transcript, alternative.confidence |
271 | else: |
272 | try: |
273 | |
274 | response = self.speech_client.recognize(config, audio) |
275 | if response.results is not None: |
276 | for result in response.results: |
277 | |
278 | |
279 | |
280 | if result.alternatives[0].transcript is not None: |
281 | print(result.alternatives[0]) |
282 | return result.alternatives[0].transcript, result.alternatives[0].confidence |
283 | else: |
284 | print("Try speaking again") |
285 | except Exception as e: |
286 | rospy.logerr("Some kind of error: {}".format(e)) |