首页 > 解决方案 > 如何使用 PocketSphinx 语音识别码的结果?

问题描述

我正在尝试创建一个程序,它会不断地听语音,如果我说某个字符串,它将调用某个函数,例如 if (string) then (function)。我最初尝试使用 Google Speech to Text API,但无法让它持续收听(我什至发现有人说 Google Speech to Text API 不支持持续收听的线程),所以我恢复到 PocketSphinx。我几乎没有任何代码。我只有你第一次安装和运行 PocketSphinx 时尝试的初始初学者代码(因为我是初学者)。不过,我不想做太多。我创建了自己的 1 个单词的词典,因为我只希望它做一件事,而且它读起来很好。

我遇到的问题实际上是使用它不断侦听的字符串值来运行未来的代码。我不知道我是否必须添加代码以使其在听到一个单词后停止听一会儿,以便它对刚刚听到的单词做某事,或者......另外,我听说过当你比较字符串时,如果我错了,你会使用.equals()而不是,纠正我/详细说明。==我两种方式都试过了。当我使用==时,什么都没有发生,它只是不断地听并打印我说的一个词。当我使用.equals()时,我得到一个AttributeError.

第一种方法:

import os 
from pocketsphinx import LiveSpeech, get_model_path
speech = LiveSpeech(
    sampling_rate = 16000,
    hmm=os.path.join(get_model_path(), 'en-us'),
    lm='A:...(some random directory to a .lm file)',
    dic='A:...(some random directory to a .dic file)')
for code in speech:
    print(code)
    if code == "string":
        print("It works!")

第二种方法:

import os 
from pocketsphinx import LiveSpeech, get_model_path
speech = LiveSpeech(
    sampling_rate = 16000,
    hmm=os.path.join(get_model_path(), 'en-us'),
    lm='A:...(some random directory to a .lm file)',
    dic='A:...(some random directory to a .dic file)')
for code in speech:
    print(code)
    if code.equals(myString): #myString is set to the word I say to it.
        print("It works!")

第二种方法错误:“LiveSpeech”对象没有属性“等于”

标签: pythonvoice-recognitionpocketsphinx

解决方案


您需要像使用迭代器一样使用实时语音(如列表)

例如:

        for phrase in speech:
            text = phrase.hypothesis()

推荐阅读