12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import speech_recognition as sr
- from kalliope.core import Utils
- from kalliope.stt.Utils import SpeechRecognition
- class Wit(SpeechRecognition):
- def __init__(self, callback=None, **kwargs):
- """
- Start recording the microphone and analyse audio with Wit.ai api
- :param callback: The callback function to call to send the text
- :param kwargs:
- """
- SpeechRecognition.__init__(self)
- # callback function to call after the translation speech/tex
- self.main_controller_callback = callback
- self.key = kwargs.get('key', None)
- self.show_all = kwargs.get('show_all', False)
- # start listening in the background
- self.set_callback(self.wit_callback)
- self.start_listening()
- def wit_callback(self, recognizer, audio):
- try:
- captured_audio = recognizer.recognize_wit(audio,
- key=self.key,
- show_all=self.show_all)
- Utils.print_success("Wit.ai Speech Recognition thinks you said %s" % captured_audio)
- self._analyse_audio(captured_audio)
- except sr.UnknownValueError:
- Utils.print_warning("Wit.ai Speech Recognition could not understand audio")
- # callback anyway, we need to listen again for a new order
- self._analyse_audio(audio_to_text=None)
- except sr.RequestError as e:
- Utils.print_danger("Could not request results from Wit.ai Speech Recognition service; {0}".format(e))
- # callback anyway, we need to listen again for a new order
- self._analyse_audio(audio_to_text=None)
- # stop listening for an audio
- self.stop_listening()
- def _analyse_audio(self, audio_to_text):
- """
- Confirm the audio exists and run it in a Callback
- :param audio_to_text: the captured audio
- """
- if self.main_controller_callback is not None:
- self.main_controller_callback(audio_to_text)
|