依赖导入
<!-- 获取音频信息 -->
<dependency>
<groupId>org</groupId>
<artifactId>jaudiotagger</artifactId>
<version>2.0.3</version>
</dependency>
<!-- 语音识别 -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>com.alphacephei</groupId>
<artifactId>vosk</artifactId>
<version>0.3.32</version>
</dependency>
model下载
我们需要实现离线语音识别,那么就得将模型下载到本地电脑。下载地址为官网的 Models 模块: https://alphacephei.com/vosk/models
我们直接找到 Chinese 分类,这里有 2 个模型
将下载的语言模型包,在下面代码中引入
Model model = new Model("下载模型包路径");//语言模型
代码
import com.example.speechrecognition.utils.CommonUtils;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.wav.WavFileReader;
import org.vosk.LibVosk;
import org.vosk.LogLevel;
import org.vosk.Model;
import org.vosk.Recognizer;
import javax.sound.sampled.AudioSystem;
import java.io.*;
/**
* @Description TODO
* @ClassName VoskAudiToTextTests
* @Author JiangHuanXiang
* @Date 2023/6/3 10:42
* @Version 1.0
**/
public class VoskAudiToTextDemo {
public static void main(String[] args) {
LibVosk.setLogLevel(LogLevel.DEBUG);
try {
String modePath = CommonUtils.getModelFileUrl("voskmodel/model_small_cn_0.22").getPath();
Model model = new Model(modePath);//语言模型
String filepath = CommonUtils.getModelFileUrl("Speech/01.wav").getPath();
File file = new File(filepath);
InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
Recognizer recognizer = new Recognizer(model, getSampleRate(file));
int nbytes;
byte[] b = new byte[4096];
while ((nbytes = ais.read(b)) >= 0) {
recognizer.acceptWaveForm(b, nbytes);
}
System.out.println("结果:"+recognizer.getFinalResult());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取音频文件的采样率
*/
private static Float getSampleRate(File file) throws Exception {
WavFileReader fileReader = new WavFileReader();
AudioFile audioFile = fileReader.read(file);
String sampleRate = audioFile.getAudioHeader().getSampleRate();
return Float.parseFloat(sampleRate);
}
}
CommonUtils
import java.io.File;
import java.net.URL;
/**
* @Description TODO
* @ClassName CommonUtils
* @Author JiangHuanXiang
* @Date 2023/6/3 12:14
* @Version 1.0
**/
public class CommonUtils {
public static File getModelFileUrl(String filePath) {
ClassLoader classLoader = CommonUtils.class.getClassLoader();
URL resourceUrl = classLoader.getResource(filePath);
if (resourceUrl != null) {
return new File(resourceUrl.getFile());
} else {
throw new RuntimeException("Failed to locate model file: " + filePath);
}
}
}
注意:以上代码只支持.wav格式的音频文件
测试
音频文件:文章来源:https://www.toymoban.com/news/detail-743308.html
文章来源地址https://www.toymoban.com/news/detail-743308.html
到了这里,关于Java 离线语音识别实现语音转文字的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!