From 26013232543eed6045ec60a5b57548a132793918 Mon Sep 17 00:00:00 2001 From: YasinOnm08 Date: Mon, 16 Sep 2024 14:38:45 +0200 Subject: [PATCH] voice_recognition --- py/voice_recognition.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/py/voice_recognition.py b/py/voice_recognition.py index 8b13789..0ee41d0 100644 --- a/py/voice_recognition.py +++ b/py/voice_recognition.py @@ -1 +1,33 @@ +import speech_recognition as sr + + +class Voice: + @staticmethod + def listen(): + recognizer = sr.Recognizer() + + try: + recognizer = sr.Recognizer() + with sr.Microphone() as source: + print("Adjusting for ambient noise...") + recognizer.adjust_for_ambient_noise(source) + print("Listening...") + audio_data = recognizer.listen(source) + print("Audio captured") + try: + text = recognizer.recognize_sphinx(audio_data) # Using Google Web Speech API + except sr.UnknownValueError: + text = "ERROR" + except sr.RequestError as e: + text = "ERROR" + + except sr.RequestError as e: + text = "ERROR" + except sr.UnknownValueError: + text = "ERROR" + except Exception as e: + text = "ERROR" + return text + print(listen()) +