我如何在 Python 中通过语音识别检测一个词

How can i detect one word with speech recognition in Python

我知道如何用 Python 检测语音,但这个问题更具体: 我怎样才能让 Python 只听一个词,然后 returns 如果 Python 可以识别这个词则为真。

我知道,我可以让 Python 一直听,然后做类似的东西 伪代码:

while True:
    if stt.listen() == "keyword":
        return True

我已经做到了,程序在一直收听几分钟后就挂了(见最后)。所以我需要一种方法来只听一个特定的词。

"hang up"是什么意思?程序不是崩溃而是没有响应。它不再听我的声音,当我按下 STRG + C 时它什么也不做。

我正在搜索这样的东西:

while True:
    if stt.waitFor("keyword"):
        return True

希望您理解,此致

import sys, os
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
import pyaudio

modeldir = "../../../model"
datadir = "../../../test/data"

# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
config.set_string('-keyphrase', 'forward')
config.set_float('-kws_threshold', 1e+20)


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

# Process audio chunk by chunk. On keyword detected perform action and restart search
decoder = Decoder(config)
decoder.start_utt()
while True:
    buf = stream.read(1024)
    if buf:
         decoder.process_raw(buf, False, False)
    else:
         break
    if decoder.hyp() != None:
        print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
        print ("Detected keyword, restarting search")
        decoder.end_utt()
        decoder.start_utt()

有关详细信息,请参阅 http://cmusphinx.sourceforge.net