python语音库,Python语音识别库-总是听吗?

I've recently been working on using a speech recognition library in python in order to launch applications. I Intend to ultimately use the library for voice activated home automation using the Raspberry Pi GPIO.

I have this working, it detects my voice and launches application. The problem is that it seems to hang on the one word I say (for example, I say internet and it launches chrome an infinite number of times)

This is unusual behavior from what I have seen of while loops. I cant figure out how to stop it looping. Do I need to do something out of the loop to make it work properly? Please see the code below.

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()

解决方案

Just in case, here is the example on how to listen continuously for keyword in pocketsphinx, this is going to be way easier than to send audio to google continuously.

And you could have way more flexible solution.

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值