【问题标题】:How can i detect one word with speech recognition in Python如何在 Python 中通过语音识别检测一个单词
【发布时间】:2017-06-07 14:43:59
【问题描述】:

我知道如何使用 Python 检测语音,但这个问题更具体: 如何让 Python 只听一个单词,如果 Python 可以识别该单词,则返回 True。

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

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

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

“挂断”是什么意思?程序没有崩溃但没有响应。它不再听我的声音,当我按STRG + C 时它什么也没做。

我正在寻找这样的东西:

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

希望你能理解,最好的问候

【问题讨论】:

  • Python 2.7 中的 Speech_recognition 包。原谅我忘记了一些技术细节。 pypi.python.org/pypi/SpeechRecognition
  • 实时还是来自 wav 文件?
  • 用麦克风实时处理
  • @NoahFisher 你可以edit你的问题

标签: python speech-recognition


【解决方案1】:
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

【讨论】:

  • 哇,谢谢,会尽快尝试的。
  • 抱歉,我无法安装 Pocketsphinx、Sphinxbase 和依赖项。你能帮帮我吗?
  • 当然,请提供您尝试过的和无效的。 pip install pocketsphinx 应该可以工作。
  • 谢谢,我试过pip install pocketsphinx 但它说swig.exe 不见了。我下载了 swig,但现在我不知道将文件放在哪里。我也希望这也适用于 Linux。
  • 您可以将 swig.exe 放入当前文件夹
猜你喜欢
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多