From 40a1c688aeadf315feb25afb0bcc0577b0d38627 Mon Sep 17 00:00:00 2001 From: YasinOnm08 Date: Mon, 16 Sep 2024 15:15:00 +0200 Subject: [PATCH] voice_recognition comments --- py/voice_recognition.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/py/voice_recognition.py b/py/voice_recognition.py index 0ee41d0..8ffc199 100644 --- a/py/voice_recognition.py +++ b/py/voice_recognition.py @@ -1,21 +1,20 @@ import speech_recognition as sr - -class Voice: +class Voice: #create Class @staticmethod - def listen(): + def listen(): #define function listen() recognizer = sr.Recognizer() try: - recognizer = sr.Recognizer() with sr.Microphone() as source: print("Adjusting for ambient noise...") - recognizer.adjust_for_ambient_noise(source) + recognizer.adjust_for_ambient_noise(source, duration=0.5) #listen to surrounding for .5sec to adjust backgroundnoise print("Listening...") - audio_data = recognizer.listen(source) + audio_data = recognizer.listen(source) #listen to user until user stops speaking print("Audio captured") try: - text = recognizer.recognize_sphinx(audio_data) # Using Google Web Speech API + text = recognizer.recognize_sphinx(audio_data) # Using Sphinx convert audio to text (also works offline) + #if any Exceptions or Errors eccur => return ERROR except sr.UnknownValueError: text = "ERROR" except sr.RequestError as e: @@ -28,6 +27,5 @@ class Voice: except Exception as e: text = "ERROR" return text - print(listen())