vosk 官网
-
可以下载中文模型
-
有训练的方法
java使用vosk
- 引入依赖
<!-- 获取音频信息 -->
<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>
- 使用方法
public static void main(String[] args) {
LibVosk.setLogLevel(LogLevel.DEBUG);
File file = new File("D:\\test.wav");//语音文件仅支持wav
try (Model model = new Model(System.getProperty("user.dir")+ "/src/main/resources/vosk-model-small-cn-0.22");//模型地址
InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
Recognizer recognizer = new Recognizer(model, getSampleRate(file))) {
//recognizer.setWords(true); // 开启词信息的详细输出
int bytes;
byte[] b = new byte[4096];//缓存大小:4k
while ((bytes = ais.read(b)) >= 0) {
recognizer.acceptWaveForm(b, bytes);
}
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();
String channel = audioFile.getAudioHeader().getChannels();
System.out.println("语音的声道:" + channel );
System.out.println("语音的采样率:" + sampleRate);
return Float.parseFloat(sampleRate) * Integer.parseInt(channel);
}