Search code examples
python-3.xspeech-recognitionpyttsx3

speech_recognition.UnknownValueError - Can someone explain this error to me


I wasn't very sure on what to title this question so I'm sorry if it's not understandable. This project that I've been working on is a Jarvis-related project (Python) recently and this is my error

Traceback (most recent call last):
File "C:\Users\Admin\Rohan_Python\Virtual Assistant.py", line 13, in <module>
text = recog.recognize_google(sound)
File "D:\Rohan_Python\lib\site-packages\speech_recognition\__init__.py", line 858, in 
recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise 
UnknownValueError()
speech_recognition.UnknownValueError

My code...

             import pyttsx3 as py
             import speech_recognition as sr
             # initializing speech_recognition
             engine = py.init()
             engine.say("Hey there! I'm Trevor. A virtual assistant for 
             programmers.")
             engine.runAndWait()
             recog = sr.Recognizer()
             with sr.Microphone() as Sound_source:
             recog.energy_threshold = 10000
             recog.adjust_for_ambient_noise(Sound_source, 1.2)
             print("Listening...")
             sound = recog.listen(Sound_source)
             text = recog.recognize_google(sound)
             print(text)

I think this might be a simple error but could someone explain what the error means and what my error is? Also do give me the solution. Please and Thanks!


Solution

  • Recognition failed. You should catch that error:

    try:
        text = recog.recognize_google(sound)
        print(text)
    except sr.UnknownValueError:
        print("Could not understand")