What is wrong with this code ? It is not showing any error but It is also not showing any output

Viewed 107

I am trying a speech-to-text program . I have written this code but it is not showing any output and also not showing any error. It is not reading my speech. Can't find any solution to it. Please help.

import speech_recognition as sr
import webbrowser as wb

r1 = sr.Recognizer()
r2 = sr.Recognizer()

with sr.Microphone() as source:
 print('Say Hello')
 print('Speak')
 audio = r2.listen(source)

if 'Hello' in r1.recognize_google(audio):
 r1 = sr.Recognizer()
 url = 'https://www.google.com/'
 with sr.Microphone() as source:
    print('search')
    audio = r1.listen(source)

    try:
        get = r1.recognize_google(audio)
        print(get)
        wb.get().open_new(url + get)
    except sr.UnknownValueError:
        print('Not recognised')
    except sr.RequestError as e:
        print('try again'.format(e))
1 Answers

After risking my marriage by repeatedly and annoyingly shouting hello at my computer, what it looks like is the captured audio is always lower case:

Say Hello
Speak
> /Users/matt/repos/stackoverflow/test.py(16)<module>()
-> if 'Hello' in audio_result:
(Pdb) l
 11     
 12     audio_result = r1.recognize_google(audio)
 13     import pdb; pdb.set_trace()
 14     
 15     
 16  -> if 'Hello' in audio_result:
 17     
 18         r1 = sr.Recognizer()
 19         url = 'https://www.google.com/'
 20         with sr.Microphone() as source:
 21             print('search')
(Pdb) audio_result
'hello hello hello hello hello hello hello hello hello hello hello hello hello hello'
(Pdb) 'Hello' in audio_result
False
(Pdb) 'hello' in audio_result
True

So clearly 'Hello' should be 'hello'.

After the switch and a retry, my browser opened to the url https://www.google.com/hello, which didn't resolve, but I think this will get you further.

HTH.

Related