如何在Python中使用语音识别来检测一个单词 [英] How can i detect one word with speech recognition in Python

查看:21
本文介绍了如何在Python中使用语音识别来检测一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用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中使用语音识别来检测一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆