Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用语音识别检测一个单词_Python_Speech Recognition - Fatal编程技术网

如何在Python中使用语音识别检测一个单词

如何在Python中使用语音识别检测一个单词,python,speech-recognition,Python,Speech Recognition,我知道如何使用Python检测语音,但这个问题更具体: 如何使Python只侦听一个单词,然后在Python能够识别该单词的情况下返回True 我知道,我可以让Python一直听,然后做类似的事情 伪代码: while True: if stt.listen() == "keyword": return True 我已经做过了,在听了几分钟后节目就挂了(见结尾)。所以我需要一种只听一个特定单词的方法 挂断”是什么意思?程序没有崩溃,但没有响应。它不再听我的声音,当我按下

我知道如何使用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()

有关更多详细信息,请参见Python 2.7中的语音识别包。对不起,我忘了一些技术细节。实时还是从wav文件?它是使用microphone@NoahFisher你可以回答你的问题哇,谢谢,我会尽快尝试。对不起,我无法安装Pocketsphinx、Sphinxbase和Dependencies。你能帮我吗?当然,请提供你尝试过的和不起作用的
pip安装pocketsphinx
应该可以工作。谢谢,我尝试了
pip安装pocketsphinx
,但它说缺少
swig.exe
。我下载了swig,但现在我不知道把文件放在哪里。我也希望这在Linux上也能起作用。您可以将swig.exe放入当前文件夹