Google.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import speech_recognition as sr
  2. from core.OrderListener import OrderListener
  3. class GoogleSpeechRecognition(OrderListener):
  4. def __init__(self, main_controller=None):
  5. OrderListener.__init__(self, main_controller)
  6. self.listen()
  7. def listen(self):
  8. """
  9. Start recording the microphone
  10. :return:
  11. """
  12. # obtain audio from the microphone
  13. r = sr.Recognizer()
  14. with sr.Microphone() as source:
  15. # listen for 1 second to calibrate the energy threshold for ambient noise levels
  16. r.adjust_for_ambient_noise(source)
  17. print("Say something!")
  18. audio = r.listen(source)
  19. # recognize speech using Google Speech Recognition
  20. try:
  21. # for testing purposes, we're just using the default API key
  22. # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
  23. # instead of `r.recognize_google(audio)`
  24. captured_audio = r.recognize_google(audio, key=None, language="fr-FR")
  25. print "Google Speech Recognition thinks you said %s" % captured_audio
  26. self._analyse_audio(captured_audio)
  27. except sr.UnknownValueError:
  28. print("Google Speech Recognition could not understand audio")
  29. except sr.RequestError as e:
  30. print("Could not request results from Google Speech Recognition service; {0}".format(e))
  31. def _analyse_audio(self, audio):
  32. self.main_controller.analyse_order(audio)