gpt4 book ai didi

python - 我如何用Python中的语音识别检测一个词

转载 作者:太空狗 更新时间:2023-10-30 00:02:11 24 4
gpt4 key购买 nike

我知道如何用 Python 检测语音,但这个问题更具体:如何让 Python 只监听一个单词,然后在 Python 可以识别该单词的情况下返回 True。

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

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

关于python - 我如何用Python中的语音识别检测一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34596576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com