一、簡介
1.1、PocketSphinx是啥?
PocketSphinx 是一個計算量和 體積都很小的語音識別引擎。是第一個開源的面向嵌入式的中等詞匯量連續(xù)語音識別項目。
1.2、Pocketsphinx on Android
ps:
網(wǎng)上大多的資料都還是停留在02-14年,如果應(yīng)用到Android上面還需要自己去使用NDK去編譯。但是,使用最新的版本,已不需要開發(fā)者自己去編譯了。推薦使用。
二、使用
2.1、資源準(zhǔn)備
-
JAR/AAR
Github 上面只提供了AAR文件,需要JAR包的,點這里。 -
聲學(xué)模型 和 語言模型
官方給的Demo里面只能識別英文,如果想要識別中文,需要下載中文的聲學(xué)模型和語言模型。還是點這里。(不保證為最新版)
2.2、導(dǎo)入依賴包,不說了。
2.3、添加識別相關(guān)資源
包括聲學(xué)模型、語言模型、字典文件、語法文件。
目錄結(jié)構(gòu)參考以下兩張圖片:

源 碼 內(nèi) 資 源 目 錄 結(jié) 構(gòu)

項 目 需 求 資 源 目 錄 結(jié) 構(gòu) 示 例
2.4、修改語法文件和字典文件
-
2.4.1、建立語音模型
本文不進(jìn)行語法的講解,這里給出參考鏈接(英文版):https://cmusphinx.github.io/wiki/tutoriallm/ -
2.4.2、添加字典
按照之前下載的字典文件,一個個和你自己的進(jìn)行對照,沒有其他方法。
2.5、調(diào)試代碼
- 2.5.1、復(fù)制資源文件到手機本地
RecognizerSetupTask recognizerSetupTask = new RecognizerSetupTask(new RecognizerSetupListener() {
@Override
public void onRecognizerAlreadySetup() {
}
@Override
public Exception doInBackGround() {
try {
File assetDir = assets.syncAssets(); //復(fù)制資源文件
setupRecognizer(assetDir); //初始化SpeechRecognizer
} catch (IOException e) {
return e;
}
return null;
}
@Override
public void onRecognizerPrepareError() {
}
@Override
public void onRecognizerPrepareSuccess() {
isInit = true;
}
});
recognizerSetupTask.execute();
- 2.5.2、SpeechRecognizer
private void setupRecognizer(File assetsDir) throws IOException {
SpeechRecognizerSetup setup = SpeechRecognizerSetup.defaultSetup();
if (setup == null) {
Log.e(TAG, "SpeechRecognizerSetup is null");
return;
}
setup.setKeywordThreshold(1e10f)
.setBoolean("-allphone_ci", true)
// .setString("-keyphrase","backward") // forward ;
// setup.setSampleRate(24000);
File file = new File(assetsDir, "zh-ptm");
if (!file.exists()) {
Log.e(TAG, "zh-ptm not found");
return;
}
setup.setAcousticModel(file);
file = new File(assetsDir, "voice.dic");
if (!file.exists()) {
Log.e(TAG, "voice.dic not found");
return;
}
setup.setDictionary(file);
recognizer = setup.getRecognizer();
if (recognizer == null) {
Log.e(TAG, "SpeechRecognizer1 is null");
return;
}
// recognizer.addKeywordSearch();
File menuGrammar = new File(assetsDir, "zh_test.gram");
recognizer.addGrammarSearch(PocketListener.SEARCH, menuGrammar);
}
- 2.5.3、開始錄音
recognizer.startListening("zh_test");
recognizer.addListener(recognitionListener);
RecognitionListener recognitionListener = new RecognitionListener() {
@Override
public void onBeginningOfSpeech() {
Log.e(TAG, "onBeginningOfSpeech()");
}
@Override
public void onEndOfSpeech() {
String searchName = recognizer.getSearchName();
Log.e(TAG, "onEndOfSpeech()" + searchName);
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null) {
Log.e(TAG, "onPartialResult() hypothesis is null ");
return;
}
Log.e(TAG, "onPartialResult()" + hypothesis.getHypstr());
}
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis == null) {
Log.e(TAG, "onResult() hypothesis is null ");
return;
}
Log.e(TAG, "onResult()" + hypothesis.getHypstr());
String string = hypothesis.getHypstr();
}
@Override
public void onError(Exception e) {
Log.e(TAG, "onError()" + e.toString());
}
@Override
public void onTimeout() {
Log.e(TAG, "onTimeout()");
}
};
到這里PocketSphinx語音識別的Android版本,核心的東西都已經(jīng)在這里。后續(xù)會把我自己的Demo 分享到 PocketSphinxDemo項目中。歡迎提出意見和建議。