此代码有什么问题?它没有显示任何错误,但是也没有显示任何输出

时间:2019-10-21 20:21:46

标签: python speech-recognition

我正在尝试语音转文本程序。我已经写了这段代码,但是它没有显示任何输出,也没有显示任何错误。它不是在读我的演讲。找不到任何解决方案。请帮忙。

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 个答案:

答案 0 :(得分:3)

反复烦人地向计算机打招呼冒着婚姻风险之后,捕获的音频总是小写:

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

很显然,'Hello'应该是'hello'

切换并重试之后,我的浏览器打开了URL https://www.google.com/hello,该URL无法解析,但是我认为这会使您更进一步。

HTH。