有没有可能总是让google云语音识别/api监听关键字python

2024-04-29 23:09:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用python和googlespeechapi/语音识别。我想知道是否可以一直让语音api监听关键字,当它听到关键字来处理命令。既然googlespeechapi可以处理多少免费音频是有限制的,这是可能的吗?到目前为止,我有这样的代码,但是一旦api在一定时间内(我认为是4秒)没有听到任何语音,它就会抛出一个错误。在最后一个项目中,我想让这个在树莓皮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)

Tags: theinimportlangfordataiftime
1条回答
网友
1楼 · 发布于 2024-04-29 23:09:22

Kitt.ai提供了“Snowboy”,一个用于此目的的热词检测引擎。你可以在检测到hotword之后触发语音识别,它也非常准确,它正好适合这个用例。 最棒的是,它离线运行。在

您可以设置代码在被hotword触发后运行。在

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

相关问题 更多 >