Merge branch 'main' of interstellardevelopment.org:sageTheDm/ai-virtual-assistant

This commit is contained in:
sageTheDM 2024-09-16 16:08:18 +02:00
commit 68f2b744fc

View file

@ -1,21 +1,20 @@
import speech_recognition as sr import speech_recognition as sr
class Voice: #create Class
class Voice:
@staticmethod @staticmethod
def listen(): def listen(): #define function listen()
recognizer = sr.Recognizer() recognizer = sr.Recognizer()
try: try:
recognizer = sr.Recognizer()
with sr.Microphone() as source: with sr.Microphone() as source:
print("Adjusting for ambient noise...") 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...") print("Listening...")
audio_data = recognizer.listen(source) audio_data = recognizer.listen(source) #listen to user until user stops speaking
print("Audio captured") print("Audio captured")
try: 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: except sr.UnknownValueError:
text = "ERROR" text = "ERROR"
except sr.RequestError as e: except sr.RequestError as e: