Python语音识别软件,带有谷歌的API噪音

时间:2014-12-09 05:41:12

标签: python api speech-recognition

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

我一直在使用谷歌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,在任何情况下,我可以说什么,有背景噪音,并且当我说完话后让它停止听?感谢。

1 个答案:

答案 0 :(得分:0)

我刚刚调整了你的代码,它现在运行正常。抱歉没有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