利用Sphinx4在windows上搭建一个小的应用程序
利用Sphinx4在windows上搭建一个小的应用程序
Sphinx4
Sphinx4是CMUSphinx开源代码中的另一个语音识别开发包,与PocketSphinx不同的是,它是基于Java来编写的,所以习惯基于Java开发的小伙伴可以利用Sphinx4来搭建自己的语音识别小程序。
要使用Sphinx4库,有两种方法,这也是Java引用外来包的两种方法,一种是下载jars,直接在工程中引用,一种是利用现在build工具gradle或apache maven,而Sphinx4支持maven包的引用方式。
编程环境
利用java开发,我使用的是Eclipse作为Java开发的iDE。
- 安装java jre(版本要>1.7.0) 安装路径
- Eclipse (2019-03)
Jar包方式
代码
最终的项目结构如下:
代码如下:
package com.exmaple;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
public class TranscriberDemo {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
InputStream stream = new FileInputStream(new File("whatstheweatherlike.wav"));
recognizer.startRecognition(stream);
SpeechResult result;
while ((result = recognizer.getResult()) != null) {
System.out.format("Hypothesis: %s\n", result.getHypothesis());
}
recognizer.stopRecognition();
/*
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
SpeechResult result = recognizer.getResult();
while ((result = recognizer.getResult()) != null) {
System.out.format("Hypothesis: %s\n", result.getHypothesis());
}
recognizer.stopRecognition();*/
}
}
开发步骤
下载jar包
- 访问Sphinx4 jar包链接
- 在搜索框中输入sphinx4
- 选中上图中画出的group是edu.cmu.sphinx,version是5prealpha-SNAPSHOT的两个包,分别是sphinx4-data和sphinx4-core
- 下载下图中的红圈标出的jar包,并点击绿圈标出的标签页,点击黄圈标出的下载既可下载,对于另一个jar包同理:
创建工程
按照之前的代码结构和代码创建工程,并添加一个音频文件到根目录,音频文件的来源可以参考我之前的文章,或者自己提供。
如何引用jar包
- 右击项目名称,选择properties
- 点击add jars将下载的两个jar包加入到libraries中
调试
右击代码文件,按下图标出的进行调试:
Gradle
什么是gradle
Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,build脚本使用Groovy编写。以上是从百度百科摘抄来的,看完了之后实话说我是一头雾水。首先我们需要知道什么是构建工具(buiild tool),构建工具就是指用某一种语言指明该工程生成最终可执行程序的所需要的步骤,是多个需要执行的任务的集合,而Gradle就是这样一种构建工具,它使用的是Groovy语言。
代码
工程结构如下图:
java文件代码如下:
package Sphinx4ExampleWithGradle;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
public class MainApp {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
InputStream stream = new FileInputStream(new File("whatstheweatherlike.wav"));
recognizer.startRecognition(stream);
SpeechResult result;
while ((result = recognizer.getResult()) != null) {
System.out.format("Hypothesis: %s\n", result.getHypothesis());
}
recognizer.stopRecognition();
/*
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
SpeechResult result = recognizer.getResult();
while ((result = recognizer.getResult()) != null) {
System.out.format("Hypothesis: %s\n", result.getHypothesis());
}
recognizer.stopRecognition();
*/
}
}
buildgradle文件如下:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* user guide available at https://docs.gradle.org/5.0/userguide/java_library_plugin.html
*/
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
//mainClassName = 'Sphinx4ExampleWithGradle'
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
mavenLocal()
mavenCentral()
maven {url "https://oss.sonatype.org/content/repositories/snapshots"}
}
dependencies {
compile group: 'edu.cmu.sphinx', name: 'sphinx4-core', version:'5prealpha-SNAPSHOT'
compile group: 'edu.cmu.sphinx', name: 'sphinx4-data', version:'5prealpha-SNAPSHOT'
}
jar {
manifest {
attributes(
'Main-Class': 'Sphinx4ExampleWithGradle.MainApp')
}
from{
configuration.compile.collect{ it.isDirectory() ? it : zipTree(it)}
}
}
task stage {
dependsOn 'build'
dependsOn 'clean'
build.mustRunAfter clean
}
开发流程
时间不够了,下次分解。