【问题标题】:Keyword is not detected using pocketsphinx on android在 android 上使用 pocketsphinx 未检测到关键字
【发布时间】:2026-01-21 07:35:01
【问题描述】:

谁能解释我如何使用pocketsphinx 将我的语音转换为文本?我试试这个:

import com.example.speechtutor.SpeechRecognizerRecorder;
import com.example.speechtutor.SpeechRecognizerRecorderSetup;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import static edu.cmu.pocketsphinx.Assets.syncAssets;

public class SpeakActivity extends Activity implements RecognitionListener {


SpeechRecognizerRecorder recognizer;

private File appDir;

 String filePath;

 private static final String KWS_SEARCH_NAME = "wakeup";
 private static final String FORECAST_SEARCH = "forecast";
 private static final String DIGITS_SEARCH = "digits";
 private static final String MENU_SEARCH = "menu";
 private static final String KEYPHRASE = "hello";


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_speak);

try {
    Log.d("Tag","before trying to sync assets");
    appDir = syncAssets(getApplicationContext());
} catch (IOException e) {
    throw new RuntimeException("failed to synchronize assets", e);
}

Log.d("TAG","before recognizer instantiaiton");
recognizer = SpeechRecognizerRecorderSetup.defaultSetup()
           .setAcousticModel(new File(appDir, "models/hmm/en-us-semi"))
           .setDictionary(new File(appDir, "models/lm/cmu07a.dic"))
           .setRawLogDir(appDir)
           .setKeywordThreshold(200)
           .setAudioStorageDirectory("SpeechTutor")
           .getRecognizer();




filePath = recognizer.getAudioStorageFilePath();

    recognizer.addListener(this);
    // Create keyword-activation search.
    File fillers = new File(appDir, "models/grammar/menu.gram");
    recognizer.addKeywordSearch(KWS_SEARCH_NAME, fillers.getPath());
    // Create grammar-based searches.
    //File menuGrammar = new File(appDir, "models/grammar/menu.gram");
    //recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
    File digitsGrammar = new File(appDir, "models/grammar/digits.gram");
    recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
    // Create language model search.
    //digitsGrammar.File languageModel = new File(appDir, "models/lm/weather.dmp");
    //recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);

    recognizer.startListening(KEYPHRASE);


}

    @Override
public void onPartialResult(Hypothesis arg0) {
       String text = results.getHypstr();

    Log.d("Spoken text",text);  
   }

    @Override
public void onBeginningOfSpeech() {
    }

}

此代码正常工作,但当我说“你好”时会调用onPartialResult。我的应用程序必须将每个语音转换为文本。请给我一个样品。

【问题讨论】:

  • 我很满意找到做袖珍狮身人面像的伊朗开发者。 shoma ham be donble rahi baraye tarif "hello" budin。 mishe lotfan farayandesho be manam 开始? kheyli search kardam o rahhaye zyadi emtehan kardam vali javab nadadan。 *.com/q/37629636/3671748

标签: android speech-recognition pocketsphinx-android


【解决方案1】:

您的代码包含多个问题。尝试将关键字阈值设置为 1e-60、1e-40、1e-20、1e-10,当然不是此行中的 200:

       .setKeywordThreshold(200)

如果只查找关键字,则不需要这行语法:

File digitsGrammar = new File(appDir, "models/grammar/digits.gram");
recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);

这部分看起来也不合理。关键字搜索需要一个单词列表,每行搜索一个,而不是 menu.gram 文件

File fillers = new File(appDir, "models/grammar/menu.gram");
recognizer.addKeywordSearch(KWS_SEARCH_NAME, fillers.getPath());

如果您只搜索单个关键字,则无需添加关键字搜索,您只需为该短语添加关键字搜索

 recognizer.addKeyphraseSearch(KWS_SEARCH_NAME, "hello");

要开始命名搜索,请指向它的名称,而不是关键字 itselsf:

 recognizer.startListening(KWS_SEARCH_NAME);

正确的代码应该是这样的:

import com.example.speechtutor.SpeechRecognizerRecorder;
import com.example.speechtutor.SpeechRecognizerRecorderSetup;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import static edu.cmu.pocketsphinx.Assets.syncAssets;

public class SpeakActivity extends Activity implements RecognitionListener {

 SpeechRecognizerRecorder recognizer;

 private File appDir;

 private static final String KWS_SEARCH_NAME = "wakeup";
 private static final String KEYPHRASE = "hello";


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_speak);

    try {
       Log.d("Tag","before trying to sync assets");
         appDir = syncAssets(getApplicationContext());
    } catch (IOException e) {
         throw new RuntimeException("failed to synchronize assets", e);
    }

Log.d("TAG","before recognizer instantiaiton");
recognizer = SpeechRecognizerRecorderSetup.defaultSetup()
           .setAcousticModel(new File(appDir, "models/hmm/en-us-semi"))
           .setDictionary(new File(appDir, "models/lm/cmu07a.dic"))
           .setRawLogDir(appDir)
           .setKeywordThreshold(1e-40)
           .setAudioStorageDirectory("SpeechTutor")
           .getRecognizer();


    recognizer.addListener(this);
    recognizer.addKeyphraseSearch(KWS_SEARCH_NAME, KEYPHRASE);
    recognizer.startListening(KWS_SEARCH_NAME);
}

    @Override
    public void onPartialResult(Hypothesis hyp) {
        if (hyp == null)
             return;
        // Restart the recognition if keyword is found
        String text = hyp.getHypstr();
        Log.d("Spoken text",text);  
        recognizer.cancel();
        recognizer.startSearch(KWS_SEARCH_NAME);
   }
}

【讨论】:

  • 感谢您的回答。但是除了“你好”之外,日志中仍然没有出现任何内容。你能给我一个完整的例子吗?或 Eclipse 项目。
  • 看,google api 的 RecognizerListener 监听每一个声音,但这个应用只监听“你好”这个词。这是我的问题。看到你是这个问题的专家。
  • 不,google API 不会监听所有内容。他们的模型经过网络查询训练。
  • 我得到了答案。谢谢
  • 你是如何解决 Akbar 的问题的?我有一个类似的问题。我看不到如何添加多个单词...