是否可以一直有一个google云语音recognition/api监听关键字python

Is it possible to always have a google cloud speech recognition/api listening for a keyword python

我刚开始使用 python 和 google 语音 api/speech 识别。我想知道是否有可能总是让语音 api 监听关键字并在听到关键字时处理命令。由于 google 语音 api 可以处理多少免费音频是有限制的,这可能吗?到目前为止,我的代码看起来像这样,但是一旦 api 在一定秒数(我认为是 4 秒)内没有听到任何语音,它就会抛出一个错误。在最终项目中,我想让它在 raspberry pi 3.

上运行
import speech_recognition as sr
import speak
from time import ctime
import time
import sys
r = sr.Recognizer()
lang = 'en'
data = ''
nameCalled = 0
# Enable Microphone and use for audio input

# Speech recognition using Google Speech Recognition

def spk(text, lang):
    speak.tts(text, lang)

def audioRecord():
    try:
        with sr.Microphone() as source:
                #r.energy_threshold = 500
                # Increase for less sensitivity, decrease for more
                print('Listening...')
                audio = r.listen(source)
                #r.adjust_for_ambient_noise(source)
                data = r.recognize_google(audio)
                print('You said ' + data)
                return data

    except sr.UnknownValueError:
        print('Google could not understand audio!')
    except sr.RequestError as e:
       print('Could not request results for GSR')


def brain(data):
    global nameCalled
    #^^Keep track to see if amber was called^^
    global lang

    #If amber was said, than the next command heard can be executed
    if nameCalled == 0:

        if 'Amber' in data:
            nameCalled = 1
            spk('Yes?', lang)
        elif 'nothing' in data:
            spk('Okay', lang)
            sys.exit()     
        else:
            return 'null'
        #Once we hear amber, the next command spoken can be executed,
        # if something goes wrong, just set the nameCalled variable to 0
        #and restart the process
    elif nameCalled == 1:
        if 'what time is it' in data:
            spk(ctime(), lang)
        if 'nothing' in data:
            spk('Okay', lang)
            sys.exit()
        nameCalled = 0
    else:
        nameCalled = 0





# initialization
spk('hello nick, what can I do for you today', lang)
while 1:
    data = audioRecord()
    brain(data)

Kitt.ai 提供了 'Snowboy',一个用于此目的的热词检测引擎。您可以在检测到热词后触发语音识别,它也非常准确,完全适合这种用例。 最重要的是,它 运行 离线。

在被热词触发后,您可以将代码设置为 运行。

查看: https://snowboy.kitt.ai