代码之家  ›  专栏  ›  技术社区  ›  Luc Angevare

当说了些什么时,如何开始录音?

  •  1
  • Luc Angevare  · 技术社区  · 7 年前

    我正在尝试制作一个使用语音识别的程序。现在我遇到了一个问题,这就是你必须按下一个按钮或回车来启动语音识别。有没有一种方法可以让你说出一个短语(有点像谷歌),它就可以开始识别Python3中的语音?
    这是我的代码:

    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 回复  |  直到 5 年前
        1
  •  7
  •   nicocappa    7 年前

    是的,基本上你必须把你的识别分为两部分:关键词识别(只听一个关键词)和主识别(识别用户在关键词后说的话)。要知道这意味着你的节目将一直在收听。

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

    由于收听关键字需要您的程序不断收听和识别,因此您不希望使用任何需要互联网连接的语音识别API(Bing、Google、Watson、Houndify等)。这是因为所有这些都有每月的API限制,您将很容易通过这些限制。您希望保存这些API以供实际识别。我相信你唯一的离线选择就是使用 recognize_sphinx 或者雪人热词检测。我从未真正使用过Snowboy(虽然我听说它很不错),因为它在Windows上不起作用(或者至少在我编写程序时不起作用),但Sphinx有一个类似的关键字检测工具。

    基本上,您传递sphinx_识别器关键字,以及通过元组拾取这些关键字的敏感度,它将尝试在语音中查找这些关键字。要注意的是,关键词越敏感,误报率就越高。

    以下是一个例子:

    import speech_recognition as sr
    import time
    
    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), ]
    
    source = sr.Microphone()
    
    
    def callback(recognizer, audio):  # this is called from the background thread
    
        try:
            speech_as_text = recognizer.recognize_sphinx(audio, keyword_entries=keywords)
            print(speech_as_text)
    
            # Look for your "Ok Google" keyword in speech_as_text
            if "google" in speech_as_text or "hey google":
                recognize_main()
    
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
    
    
    def recognize_main():
        print("Recognizing Main...")
        audio_data = r.listen(source)
        # interpret the user's words however you normally interpret them
    
    
    def start_recognizer():
        r.listen_in_background(source, callback)
        time.sleep(1000000)
    
    
    start_recognizer()
    

    使用语音识别库时,此链接非常有用:

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

        2
  •  0
  •   user13496328    6 年前

    from pocketsphinx import LiveSpeech
    import os
    for i in LiveSpeech():
        print(i)
        if "hey Google" in str(i):
           os.startfile("Your File.your_format")
    #works Perfectly and if you want to hide python console then save as youfile.pyw