pocketsphinx android,PocketSphinx On Android 语音识别

一、简介

1.1、PocketSphinx是啥?

PocketSphinx 是一个计算量和 体积都很小的语音识别引擎。是第一个开源的面向嵌入式的中等词汇量连续语音识别项目。

1.2、Pocketsphinx on Android

AndroidStudio 版Demo:点我。

官方介绍:点这里。

ps:

网上大多的资料都还是停留在02-14年,如果应用到Android上面还需要自己去使用NDK去编译。但是,使用最新的版本,已不需要开发者自己去编译了。推荐使用。

二、使用

2.1、资源准备

JAR/AAR

Github 上面只提供了AAR文件,需要JAR包的,点这里。

声学模型 和 语言模型

官方给的Demo里面只能识别英文,如果想要识别中文,需要下载中文的声学模型和语言模型。还是点这里。(不保证为最新版)

2.2、导入依赖包,不说了。

2.3、添加识别相关资源

包括声学模型、语言模型、字典文件、语法文件。

目录结构参考以下两张图片:

82ffb90cc463?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

源 码 内 资 源 目 录 结 构

82ffb90cc463?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

项 目 需 求 资 源 目 录 结 构 示 例

2.4、修改语法文件和字典文件

2.4.2、添加字典

按照之前下载的字典文件,一个个和你自己的进行对照,没有其他方法。

2.5、调试代码

2.5.1、复制资源文件到手机本地

RecognizerSetupTask recognizerSetupTask = new RecognizerSetupTask(new RecognizerSetupListener() {

@Override

public void onRecognizerAlreadySetup() {

}

@Override

public Exception doInBackGround() {

try {

File assetDir = assets.syncAssets(); //复制资源文件

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版本,核心的东西都已经在这里。后续会把我自己的Demo 分享到 PocketSphinxDemo项目中。欢迎提出意见和建议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值