I know how to detect speech with Python but this question is more specific: How can I make Python listening for only one word and then returns True if Python could recognize the word.
I know, I could just let Python listen all the Time and then make something like that Pseudocode:
while True:
if stt.listen() == "keyword":
return True
I have already made that and the program is hanging up after some minutes of always listening (See at the end). So I need a way to only listen for one specific word.
What does "hang up" mean? The program is not crashing but not responding. It isn't listening to my voice anymore and when I press STRG + C
it does nothing.
I am searching for something like this:
while True:
if stt.waitFor("keyword"):
return True
Hope you understood, Best regards
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()
For more details see http://cmusphinx.sourceforge.net
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With