Python语音识别库是否始终侦听?

2024-04-26 15:02:29 发布

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

我最近一直在使用python中的语音识别库来启动应用程序。我打算最终使用Raspberry Pi GPIO将该库用于语音激活家庭自动化

我有这个功能,它检测我的声音并启动应用程序。问题是,它似乎挂在我说的一个字上(例如,我说的是互联网,它无限次地启动chrome)

从我所看到的while循环来看,这是一种不寻常的行为。我想不出如何阻止它循环。我需要做一些非循环的事情来让它正常工作吗?请参阅下面的代码

http://pastebin.com/auquf1bR

import pyaudio,os
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
        audio = r.listen(source)

def excel():
        os.system("start excel.exe")

def internet():
        os.system("start chrome.exe")

def media():
        os.system("start wmplayer.exe")

def mainfunction():
        user = r.recognize(audio)
        print(user)
        if user == "Excel":
                excel()
        elif user == "Internet":
                internet()
        elif user == "music":
                media()
while 1:
        mainfunction()

Tags: import应用程序sourceosdefas语音chrome
3条回答

以防万一,这里是一个关于如何在pocketsphinx中持续收听关键字的示例,这将比持续向google发送音频容易得多。 你们可以有更灵活的解决方案

import sys, os, pyaudio
from pocketsphinx import *

modeldir = "/usr/local/share/pocketsphinx/model"
# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'hmm/en_US/hub4wsj_sc_8k'))
config.set_string('-dict', os.path.join(modeldir, 'lm/en_US/cmu07a.dic'))
config.set_string('-keyphrase', 'oh mighty computer')
config.set_float('-kws_threshold', 1e-40)

decoder = Decoder(config)
decoder.start_utt('spotting')

stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()        

while True:
    buf = stream.read(1024)
    decoder.process_raw(buf, False, False)
    if decoder.hyp() != None and decoder.hyp().hypstr == 'oh mighty computer':
        print "Detected keyword, restarting search"
        decoder.end_utt()
        decoder.start_utt('spotting')

我在这个问题上花了很多时间

目前,我正在开发一个名为Athena Voice的Python 3开源跨平台虚拟助手程序: https://github.com/athena-voice/athena-voice-client

用户可以像Siri、Cortana或Amazon Echo一样使用它

它还使用了一个非常简单的“模块”系统,用户可以轻松编写自己的模块来增强其功能。让我知道这是否有用

否则,我建议查看Pocketsphinx和Google的Python语音到文本/文本到语音包

在Python3.4上,Pocketsphinx可以安装以下组件:

pip install pocketsphinx

但是,必须单独安装PyAudio依赖项(非官方下载): http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

可以使用以下命令安装两个google软件包:

pip install SpeechRecognition gTTS

谷歌STT:https://pypi.python.org/pypi/SpeechRecognition/

谷歌TTS:https://pypi.python.org/pypi/gTTS/1.0.2

Pocketsphinx应用于离线唤醒单词识别,Google STT应用于主动监听

问题是,在节目开始时,您实际上只听一次语音,然后在保存的音频的相同位上重复调用recognize。将实际侦听语音的代码移动到while循环中:

import pyaudio,os
import speech_recognition as sr


def excel():
        os.system("start excel.exe")

def internet():
        os.system("start chrome.exe")

def media():
        os.system("start wmplayer.exe")

def mainfunction(source):
    audio = r.listen(source)
    user = r.recognize(audio)
    print(user)
    if user == "Excel":
        excel()
    elif user == "Internet":
        internet()
    elif user == "music":
        media()

if __name__ == "__main__":
    r = sr.Recognizer()
    with sr.Microphone() as source:
        while 1:
            mainfunction(source)

相关问题 更多 >