我知道如何使用Python检测语音,但这个问题更具体:如何让Python只监听一个单词,然后如果Python能识别单词则返回True.
我知道,我可以让Python一直听,然后制作类似Pseudocode的东西:
while True: if stt.listen() == "keyword": return True
我已经做到了这一点,并且程序在经过几分钟的总听之后就会挂起(最后见).所以我需要一种只听一个特定单词的方法.
"挂断"是什么意思?该程序没有崩溃但没有响应.它不再听我的声音,当我按下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