首页 文章

如果有人说Python的话怎么开始录音?

提问于
浏览
1

我正在尝试制作一个使用语音识别的程序 . 现在我遇到了一个我遇到的问题,这就是你必须按一个按钮或Enter才能启动语音识别 . 有没有一种方法可以让你说一个短语(有点像Hey Google)它开始在Python 3中识别语音?
这是我的代码:

录制音频代码:

r = sr.Recognizer()

with sr.Microphone() as source:
    audio = r.listen(source)
x = r.recognize_google(audio)

print("I'm listening!")

try:
    print("You said: " + r.recognize_google(audio))
except speech_recognition.UnknownValueError:
    print("I am sorry but I couldn't understand you, try again.")
except speech_recognition.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))

提前致谢!

1 回答

  • 2

    是的,基本上你必须将你的认识细分为两部分:关键字识别(仅监听关键字)和主要识别(识别用户在关键字后所说的内容) . 知道这意味着你的程序将一直在倾听 .

    对于关键字识别,您可以使用 Recognizer()listen_in_background 方法并在您提供的任何回调中扫描关键字 . 如果找到关键字,则调用 Recognizer().listen(source) .

    由于收听关键字会要求您的程序不断收听和识别,因此您不希望使用任何需要互联网连接的语音识别API(Bing,Google,Watson,Houndify等) . 这是因为所有这些都有每月API限制,您将很容易燃烧 . 您希望保存这些API以进行实际识别 . 我相信您唯一的离线选项是使用 recognize_sphinx 或snowboy热门词检测 . 我_48441非常好),因为它在编写我的程序时没有't work on Windows (or at least it didn',但是Sphinx有一个关键字检测工具 .

    基本上,您传递了sphinx_recognizer关键字以及通过元组获取这些关键字的敏感度,并且它将尝试专注于在语音中查找这些关键字 . 请注意,关键字越敏感,您获得的误报就越多 .

    这是一个例子:

    import speech_recognition as sr
    
    r = sr.Recognizer()
    
    # Words that sphinx should listen closely for. 0-1 is the sensitivity  
    # of the wake word.
    keywords =  [("google", 1), ("hey google", 1),]
    
    
    def callback(recognizer, audio): # this is called from the background thread
    
        try:
            speech_as_text = recognizer.recognize_sphinx(audio, keyword_entries=keywords)  
    
            # Look for your "Ok Google" keyword in speech_as_text
            if "my word" in speech_as_text:
                recognize_main()
    
        except LookupError:
            print("Oops! Didn't catch that")
    
    def recognize_main():
        audio_data = r.listen(source)
    
        # interpret the user's words however you normally interpret them
    
    def start_recognizer():    
        r.listen_in_background(sr.Microphone(), callback)
    
    
    start_recognizer()
    

    使用speech_recognition库时,此链接非常有用:

    https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst

相关问题