【问题标题】:Python speech recognition software with Google's API in noise带有谷歌 API 的 Python 语音识别软件
【发布时间】:2015-09-07 19:46:25
【问题描述】:

我最近用 Python 为我设置了一个项目,创建一个语音控制的脚本。

我一直在使用 googles API 来执行此操作,但在使用此 API 时,我注意到它永远不会停止监听,除非它完全安静,这会阻止你在甚至有些嘈杂的区域或与周围的人交谈时运行命令。

这是我所拥有的:

import speech_recognition as sr
import pyttsx
r = sr.Recognizer()
engine = pyttsx.init()
listen = False

# Define methods

while True:
while listen == False:
    with sr.Microphone() as source:
        audio = r.listen(source)

    try:
        if (r.recognize(audio) == "start") :
            listen = True
            print("Listening...")
            break
        else:
            continue
    except LookupError:
        continue

while listen == True:
    with sr.Microphone() as source:
        audio = r.listen(source)
    print("1")
    try:
        print("2")
        if (r.recognize(audio) == "stop") :
            listen = False
            print("Listening stopped. Goodnight")
            break
        else:
            print("You said " + r.recognize(audio))
    except LookupError:
        engine.say('Audio cannot be read!')
        engine.runAndWait()
        print("Could not understand audio")

导入 PYTTSX 和 SpeechRecognition(使用 googles API)。现在我怎样才能让它运行得更像 Google Now,在任何情况下,无论有没有背景噪音,我都可以说任何话,并且在我说完后让它停止收听?谢谢。

【问题讨论】:

  • 就像谷歌现在听噪音一样,您需要在上述问题的答案中提供关键字发现解决方案。它可以在噪音中工作,它只会对特定的关键词做出反应。
  • 好吧,这似乎是我要找的东西,但我找不到适用于 Windows 7 的 PocketSphinx。
  • @Mikolay 我在 Sourceforge 上给你发了消息。
  • 你可以在这里下载:pypi.python.org/pypi/PyPocketSphinx/12608

标签: python api speech-recognition


【解决方案1】:

我刚刚对您的代码进行了一段时间的调整,现在可以正常工作了。对不起,我不是 pythonic 我是 python 新手: 这里的“pinger”是一个保存音频的临时变量 并且你应该使用 String.lower() 将从谷歌获得的字符串转换为小字符,以便它与测试字符串匹配。

import speech_recognition as sr
import pyttsx
r = sr.Recognizer()
engine = pyttsx.init()
listen = False

# Define methods

while True:
    while listen == False:
        try:
            with sr.Microphone() as source:
                r.adjust_for_ambient_noise(source)
                audio = r.listen(source)
                pinger=r.recognize_google(audio)
            try:
                if (pinger.lower() == "start") :
                    listen = True
                    print("Listening...")
                    break
                else:
                    continue
            except LookupError:
                continue
        except sr.UnknownValueError:
            continue

    while listen == True:
        print "2nd part"
        try:
            with sr.Microphone() as source:
                r.adjust_for_ambient_noise(source)
                audio = r.listen(source)
                pinger=r.recognize_google(audio)
            print("1")

            print("2")
            if (pinger.lower() == "stop") :
                listen = False
                print("Listening stopped. Goodnight")
                break
            else:
                print("You said " + pinger)
        except LookupError:
            engine.say('Audio cannot be read!')
            engine.runAndWait()
            print("Could not understand audio")
        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")
            continue
        except sr.RequestError:
            print("Could not request results from Google Speech Recognition service")
            continue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多