11

I have tried it on python shell 3.5.8

import speech_recognition as sr
import webbrowser as wb

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe 
%s'


# obtain audio from the microphone  
r = sr.Recognizer()  
with sr.Microphone() as source:  
   print("Please wait. Calibrating microphone...")  
   # listen for 5 seconds and create the ambient noise energy level  
   r.adjust_for_ambient_noise(source, duration=5)  
   print("Say something!")  
   audio = r.listen(source)  

# recognize speech 
try:
    text = r.recognize_google(audio)
    print("I thinks you said '" + r.recognize_google(audio) + "'")

    f_text='https://www.google.co.in/search?q=' + text
    wb.get(chrome_path).open(f_text)
#except sr.UnknownValueError:  
#  print("I could not understand audio")  
except sr.RequestError as e:  
   print("error; {0}".format(e))

except Exception as e:
   print (e)
File "C:\Users\manoj\AppData\Local\Programs\Python\Python35\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
7
  • Check the documentation. The problem is with recognize_google usage. I'd say, first try to run it only once, not twice. :P Commented May 29, 2019 at 11:15
  • ok thank you, will try to move recognize_google(audio) out of the try block and will try print the audio once, if it works will let you know on this, And if possible can you provide me the link for documentation to check details. Commented May 29, 2019 at 11:28
  • What I meant is that you have text = r.recognize_google(audio) and then another recognize in the print statement. You haven't included the stacktrace, so this is only a guess Commented May 29, 2019 at 14:33
  • i did removed r.recognize_google(audio) from the print statement with the variable name(text), but getting the same error. Commented May 29, 2019 at 16:59
  • I think I got the issue if I ran the python -m speech_recognition in cmd and if I speak it is not transcribing my speech, so this might be the issue, please do find this for your reference and kindly help me on the same to fix the issue PS C:\WINDOWS\system32> python -m speech_recognition A moment of silence, please... Set minimum energy threshold to 171.88727223022704 Say something! Commented May 29, 2019 at 20:03

3 Answers 3

7

try this out

try:
    text = r.recognize_google(audio, language = 'en-IN', show_all = True )
    print("I thinks you said '" + r.recognize_google(audio) + "'")

    f_text='https://www.google.co.in/search?q=' + text
    wb.get(chrome_path).open(f_text)

I think this is happen because of noise interrupts algorithm. due to noise it gives UnknownValueError

Sign up to request clarification or add additional context in comments.

Comments

1

Try below code.

import speech_recognition as sr
from os import walk

r = sr.Recognizer()
#optional
#r.energy_threshold = 300

def startConvertion(path = 'file.wav', lang = 'en-IN'): 
    with sr.AudioFile(path) as source:
        #print('Fetching File')
        audio_file = r.record(source)
        print(r.recognize_google(audio_file, language=lang))
startConvertion()

Comments

0

Here's an excerpt from an example in SpeechRecognition repo:

except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.