我已经安装了 PocketSphinx 演示,它在 Ubuntu 和 Eclipse 下运行良好,但尽管尝试了,但我无法弄清楚如何添加对多个单词的识别。
我想要的只是让代码识别单个单词,然后我可以在代码中 switch()
,例如“上下左右”。我不想识别句子,只识别单个单词。
对此的任何帮助将不胜感激。我发现其他用户也有类似的问题,但到目前为止没有人知道答案。
令我困惑的一件事是为什么我们需要使用“唤醒”常量?
private static final String KWS_SEARCH = "wakeup";
private static final String KEYPHRASE = "oh mighty computer";
.
.
.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
wakeup
与什么有什么关系?
我取得了一些进展(?):使用 addGrammarSearch
我可以使用 .gram
文件列出我的单词,例如up,down,left,right,forwards,backwards
,如果我说的只是那些特定的词,这似乎很好用。但是,任何其他词都会导致系统将所说的内容与所说的“最接近”的词相匹配。理想情况下,如果说出的单词不在 .gram
文件中,我不希望发生识别...
最佳答案
感谢 Nikolay 的提示(请参阅上面的答案),我开发了以下代码,该代码运行良好,并且除非它们在列表中,否则无法识别单词。您可以将其直接复制并粘贴到 PocketSphinxDemo 代码中的主类上:
public class PocketSphinxActivity extends Activity implements RecognitionListener
{
private static final String DIGITS_SEARCH = "digits";
private SpeechRecognizer recognizer;
@Override
public void onCreate(Bundle state)
{
super.onCreate(state);
setContentView(R.layout.main);
((TextView) findViewById(R.id.caption_text)).setText("Preparing the recognizer");
try
{
Assets assets = new Assets(PocketSphinxActivity.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
}
catch (IOException e)
{
// oops
}
((TextView) findViewById(R.id.caption_text)).setText("Say up, down, left, right, forwards, backwards");
reset();
}
@Override
public void onPartialResult(Hypothesis hypothesis)
{
}
@Override
public void onResult(Hypothesis hypothesis)
{
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null)
{
String text = hypothesis.getHypstr();
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBeginningOfSpeech()
{
}
@Override
public void onEndOfSpeech()
{
reset();
}
private void setupRecognizer(File assetsDir)
{
File modelsDir = new File(assetsDir, "models");
recognizer = defaultSetup().setAcousticModel(new File(modelsDir, "hmm/en-us-semi"))
.setDictionary(new File(modelsDir, "dict/cmu07a.dic"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)
.getRecognizer();
recognizer.addListener(this);
File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
}
private void reset()
{
recognizer.stop();
recognizer.startListening(DIGITS_SEARCH);
}
}
您的 digits.gram
文件应该类似于:
up /1e-1/
down /1e-1/
left /1e-1/
right /1e-1/
forwards /1e-1/
backwards /1e-1/
您应该尝试使用双斜杠 //
中的阈值来提高性能,其中 1e-1
代表 0.1(我认为)。我认为最大值是1.0
。
现在是下午 5.30,所以我现在可以停止工作了。结果。
关于android - 使用 PocketSphinx 识别多个关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25748113/