|
@@ -2,9 +2,10 @@ import speech_recognition as sr
|
|
|
|
|
|
from kalliope.core import Utils
|
|
|
from kalliope.core.OrderListener import OrderListener
|
|
|
+from kalliope.stt.Utils import SpeechRecognition
|
|
|
|
|
|
|
|
|
-class Apiai(OrderListener):
|
|
|
+class Apiai(SpeechRecognition):
|
|
|
|
|
|
def __init__(self, callback=None, **kwargs):
|
|
|
"""
|
|
@@ -12,27 +13,31 @@ class Apiai(OrderListener):
|
|
|
:param callback: The callback function to call to send the text
|
|
|
:param kwargs:
|
|
|
"""
|
|
|
- OrderListener.__init__(self)
|
|
|
+ SpeechRecognition.__init__(self)
|
|
|
|
|
|
# callback function to call after the translation speech/tex
|
|
|
self.callback = callback
|
|
|
- # obtain audio from the microphone
|
|
|
- r = sr.Recognizer()
|
|
|
- with sr.Microphone() as source:
|
|
|
- # listen for 1 second to calibrate the energy threshold for ambient noise levels
|
|
|
- r.adjust_for_ambient_noise(source)
|
|
|
- Utils.print_info("Say something!")
|
|
|
- audio = r.listen(source)
|
|
|
-
|
|
|
- # recognize speech using Apiai Speech Recognition
|
|
|
- try:
|
|
|
+ self.key = kwargs.get('key', None)
|
|
|
+ self.language = kwargs.get('language', "en")
|
|
|
+ self.session_id = kwargs.get('session_id', None)
|
|
|
+ self.show_all = kwargs.get('show_all', False)
|
|
|
|
|
|
- key = kwargs.get('key', None)
|
|
|
- language = kwargs.get('language', "en")
|
|
|
- session_id= kwargs.get('session_id', None)
|
|
|
- show_all = kwargs.get('show_all', False)
|
|
|
+ # start listening in the background
|
|
|
+ self.stop_listening = self.start_listening(self.apiai_callback)
|
|
|
|
|
|
- captured_audio = r.recognize_api(audio, client_access_token=key, language=language, session_id=session_id, show_all=show_all)
|
|
|
+ def apiai_callback(self, recognizer, audio):
|
|
|
+ """
|
|
|
+ called from the background thread
|
|
|
+ :param recognizer:
|
|
|
+ :param audio:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ captured_audio = recognizer.recognize_api(audio,
|
|
|
+ client_access_token=self.key,
|
|
|
+ language=self.language,
|
|
|
+ session_id=self.session_id,
|
|
|
+ show_all=self.show_all)
|
|
|
Utils.print_success("Apiai Speech Recognition thinks you said %s" % captured_audio)
|
|
|
self._analyse_audio(captured_audio)
|
|
|
|
|
@@ -47,10 +52,8 @@ class Apiai(OrderListener):
|
|
|
|
|
|
def _analyse_audio(self, audio):
|
|
|
"""
|
|
|
- Confirm the audio exists annd run it in a Callback
|
|
|
+ Confirm the audio exists and run it in a Callback
|
|
|
:param audio: the captured audio
|
|
|
"""
|
|
|
- # if self.main_controller is not None:
|
|
|
- # self.main_controller.analyse_order(audio)
|
|
|
if self.callback is not None:
|
|
|
self.callback(audio)
|