mirror of
https://github.com/geekwenjie/SmartJavaAI.git
synced 2026-09-12 12:48:57 +00:00
- 新增 语音识别模块,集成 OpenAI 开源的 Whisper 和 Vosk
- 修复 质量评估模型的 Bug - 修复 OCR 模块 recognizeAndDraw 方法的 Bug - 修复 车牌识别在未检测到车牌时的报错问题 - 优化 OCR 表格识别功能,新增导出方式
This commit is contained in:
7
examples/speech-examples/.gitignore
vendored
Normal file
7
examples/speech-examples/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
.idea
|
||||
.idea/
|
||||
target
|
||||
log
|
||||
*.iml
|
||||
/.settings/
|
||||
/logging.file_IS_UNDEFINED/
|
||||
43
examples/speech-examples/README.md
Normal file
43
examples/speech-examples/README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# OCR文字识别示例
|
||||
|
||||
|
||||
我来为你写一个 `speech-examples` 项目的 README.md 文件。
|
||||
|
||||
## 🎤 语音识别示例
|
||||
|
||||
本项目展示了如何使用 SmartJavaAI SDK 进行语音识别,支持 Whisper 和 Vosk 两种语音识别引擎。
|
||||
|
||||
## 📁 项目结构
|
||||
|
||||
```
|
||||
src
|
||||
├── main
|
||||
│ ├── java
|
||||
│ │ └── smartai/examples/speech/asr
|
||||
│ │ └── SpeechRecognizeDemo.java # 语音识别示例
|
||||
│ └── resources
|
||||
│ ├── logback.xml # 日志配置文件
|
||||
│ ├── speech_zh.mp3 # 中文测试音频
|
||||
│ ├── lff_zh.mp3 # 中文测试音频
|
||||
│ └── jfk_en.wav # 英文测试音频
|
||||
└── test
|
||||
```
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
1. 克隆项目到本地:
|
||||
|
||||
2. 导入项目至 IntelliJ IDEA。
|
||||
|
||||
3. 根据需要修改模型路径(见各 demo 中注释)。
|
||||
|
||||
4. 运行对应的 JUnit 测试类方法即可体验各项功能。
|
||||
|
||||
---
|
||||
|
||||
## 📄 文档
|
||||
|
||||
有关完整使用说明,请查阅 SmartJavaAI 官方文档:
|
||||
[http://doc.smartjavaai.cn](http://doc.smartjavaai.cn)
|
||||
|
||||
---
|
||||
214
examples/speech-examples/pom.xml
Normal file
214
examples/speech-examples/pom.xml
Normal file
@@ -0,0 +1,214 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.smartjavaai</groupId>
|
||||
<artifactId>examples-speech</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<smartjavaai.version>1.0.23</smartjavaai.version>
|
||||
<!--如果打包运行,需要替换成你的main-->
|
||||
<exec.mainClass>smartai.examples.speech.asr.common.OcrRecognizeDemo</exec.mainClass>
|
||||
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.smartjavaai</groupId>
|
||||
<artifactId>smartjavaai-bom</artifactId>
|
||||
<version>${smartjavaai.version}</version>
|
||||
<type>pom</type>
|
||||
<!-- 注意这里是import -->
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-cli</groupId>
|
||||
<artifactId>commons-cli</artifactId>
|
||||
<version>1.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.17.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j2-impl</artifactId>
|
||||
<version>2.24.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>7.10.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.30</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.83</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!--语音识别模块-->
|
||||
<dependency>
|
||||
<groupId>cn.smartjavaai</groupId>
|
||||
<artifactId>smartjavaai-speech</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.microsoft.onnxruntime</groupId>
|
||||
<artifactId>onnxruntime</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.openpnp</groupId>
|
||||
<artifactId>opencv</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>ai.djl.huggingface</groupId>
|
||||
<artifactId>tokenizers</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>ai.djl.ml.xgboost</groupId>
|
||||
<artifactId>xgboost</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!--ffmpeg库 引用所有平台库-->
|
||||
<dependency>
|
||||
<groupId>ws.schild</groupId>
|
||||
<artifactId>jave-all-deps</artifactId>
|
||||
<version>3.5.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- windows平台 (保留对应平台的配置,可以减小包大小)-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ws.schild</groupId>-->
|
||||
<!-- <artifactId>jave-nativebin-win64</artifactId>-->
|
||||
<!-- <version>3.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
|
||||
|
||||
<!-- linux x86 平台 (保留对应平台的配置,可以减小包大小)-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ws.schild</groupId>-->
|
||||
<!-- <artifactId>jave-nativebin-linux64</artifactId>-->
|
||||
<!-- <version>3.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- linux arm64 平台 (保留对应平台的配置,可以减小包大小)-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ws.schild</groupId>-->
|
||||
<!-- <artifactId>jave-nativebin-linux-arm64</artifactId>-->
|
||||
<!-- <version>3.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- linux arm32 平台 (保留对应平台的配置,可以减小包大小)-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ws.schild</groupId>-->
|
||||
<!-- <artifactId>jave-nativebin-linux-arm32</artifactId>-->
|
||||
<!-- <version>3.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
|
||||
<!-- macOS osx64 平台 (保留对应平台的配置,可以减小包大小)-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ws.schild</groupId>-->
|
||||
<!-- <artifactId>jave-nativebin-osx64</artifactId>-->
|
||||
<!-- <version>3.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- macOS M系列 平台 (保留对应平台的配置,可以减小包大小)-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>ws.schild</groupId>-->
|
||||
<!-- <artifactId>jave-nativebin-osxm1</artifactId>-->
|
||||
<!-- <version>3.5.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals><goal>shade</goal></goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>${exec.mainClass}</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>aliyunmaven</id>
|
||||
<name>阿里云公共仓库</name>
|
||||
<url>https://maven.aliyun.com/repository/public</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,311 @@
|
||||
package smartai.examples.speech.asr;
|
||||
|
||||
import ai.djl.util.JsonUtils;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.smartjavaai.common.entity.Language;
|
||||
import cn.smartjavaai.common.entity.R;
|
||||
import cn.smartjavaai.speech.asr.config.AsrModelConfig;
|
||||
import cn.smartjavaai.speech.asr.entity.AsrResult;
|
||||
import cn.smartjavaai.speech.asr.entity.VoskParams;
|
||||
import cn.smartjavaai.speech.asr.entity.WhisperParams;
|
||||
import cn.smartjavaai.speech.asr.enums.AsrModelEnum;
|
||||
import cn.smartjavaai.speech.asr.factory.SpeechRecognizerFactory;
|
||||
import cn.smartjavaai.speech.asr.model.SpeechRecognizer;
|
||||
import cn.smartjavaai.speech.asr.model.VoskRecognizer;
|
||||
import cn.smartjavaai.speech.asr.model.WhisperRecognizer;
|
||||
import io.github.givimad.whisperjni.WhisperFullParams;
|
||||
import io.github.givimad.whisperjni.WhisperGrammar;
|
||||
import io.github.givimad.whisperjni.WhisperSamplingStrategy;
|
||||
import io.github.givimad.whisperjni.WhisperState;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Test;
|
||||
import org.vosk.Recognizer;
|
||||
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
*
|
||||
* 语音识别demo
|
||||
* @author dwj
|
||||
* @date 2025/8/6
|
||||
*/
|
||||
@Slf4j
|
||||
public class SpeechRecognizeDemo {
|
||||
|
||||
/**
|
||||
* 获取Whisper模型
|
||||
* 模型下载网盘:通过网盘分享的文件:https://pan.baidu.com/s/1kiMF5MF641R7LTn1GpB2lQ?pwd=1234 提取码: 1234
|
||||
* 更多模型下载地址:https://huggingface.co/ggerganov/whisper.cpp/tree/main
|
||||
* @return
|
||||
*/
|
||||
public SpeechRecognizer getWhisperRecognizer() {
|
||||
AsrModelConfig config = new AsrModelConfig();
|
||||
config.setModelEnum(AsrModelEnum.WHISPER);
|
||||
//模型下载地址:https://huggingface.co/ggerganov/whisper.cpp/tree/main
|
||||
config.setModelPath("/Users/xxx/Documents/develop/model/speech/ggml-medium.bin");
|
||||
return SpeechRecognizerFactory.getInstance().getModel(config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Whisper 语音识别
|
||||
* 多语言模型支持100种语言
|
||||
* 注意事项:
|
||||
* 1、不支持centos7
|
||||
* 2、模型越大越准确
|
||||
* 3、暂不支持GPU使用,如需GPU使用需要自行编译:https://github.com/ggml-org/whisper.cpp/tree/master?tab=readme-ov-file#nvidia-gpu-support
|
||||
*/
|
||||
@Test
|
||||
public void testWhisper() {
|
||||
try {
|
||||
SpeechRecognizer recognizer = getWhisperRecognizer();
|
||||
WhisperParams params = new WhisperParams();
|
||||
//语言:中文
|
||||
params.setLanguage(Language.ZH);
|
||||
R<AsrResult> result = recognizer.recognize("src/main/resources/speech_zh.mp3", params);
|
||||
if (result.isSuccess()){
|
||||
log.info("识别成功:{}", JsonUtils.toJson(result.getData()));
|
||||
}else{
|
||||
log.info("识别失败:{}", result.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whisper 语音识别(使用个性化配置)
|
||||
* 多语言模型支持100种语言
|
||||
* 注意事项:
|
||||
* 1、不支持centos7
|
||||
* 2、模型越大越准确
|
||||
* 3、暂不支持GPU使用,如需GPU使用需要自行编译:https://github.com/ggml-org/whisper.cpp/tree/master?tab=readme-ov-file#nvidia-gpu-support
|
||||
*/
|
||||
@Test
|
||||
public void testWhisperWithCustomConfig() {
|
||||
try {
|
||||
SpeechRecognizer recognizer = getWhisperRecognizer();
|
||||
WhisperParams params = new WhisperParams();
|
||||
//语言:中文
|
||||
params.setLanguage(Language.ZH);
|
||||
/**
|
||||
* 解码搜索策略类型:
|
||||
* GREEDY - 贪婪解码,逐步选择概率最高的结果;
|
||||
* BEAN_SEARCH - Beam 搜索,保留多个候选路径以提高准确性。
|
||||
*/
|
||||
WhisperFullParams fullParams = new WhisperFullParams(WhisperSamplingStrategy.BEAN_SEARCH);
|
||||
//语言
|
||||
fullParams.language = Language.ZH.getCode();
|
||||
//线程数,设为 0 表示使用最大核心数。
|
||||
fullParams.nThreads = 0;
|
||||
//解码器使用的历史文本作为提示的最大 token 数。
|
||||
fullParams.nMaxTextCtx = 16384;
|
||||
//解码起始偏移(毫秒)
|
||||
fullParams.offsetMs = 0;
|
||||
//解码持续时长(毫秒),超过此长度的音频将被截断
|
||||
fullParams.durationMs = 0;
|
||||
//是否翻译为英文
|
||||
fullParams.translate = false;
|
||||
// 初始提示,用于提供上下文或样例,帮助模型更准确地理解语音内容
|
||||
fullParams.initialPrompt = "简体中文";
|
||||
//禁用上下文链接,不使用前一段解码结果作为上下文
|
||||
fullParams.noContext = true;
|
||||
//是否强制仅输出一个段落(适用于短语音)
|
||||
fullParams.singleSegment = false;
|
||||
//是否打印特殊标记
|
||||
fullParams.printSpecial = false;
|
||||
//是否直接从 whisper.cpp 中打印结果(不推荐,建议使用回调方式替代)
|
||||
fullParams.printRealtime = false;
|
||||
//抑制非语音 token输出
|
||||
fullParams.suppressNonSpeechTokens = false;
|
||||
//更多参数请查看官网:https://github.com/GiviMAD/whisper-jni/blob/33854520b1f0b3697106a7932a2fd64e8191bca9/src/main/java/io/github/givimad/whisperjni/WhisperFullParams.java
|
||||
params.setParams(fullParams);
|
||||
//建议上传 WAV 格式音频。其他格式将自动转换为 WAV,可能影响处理速度
|
||||
R<AsrResult> result = recognizer.recognize("src/main/resources/speech_zh.mp3", params);
|
||||
if (result.isSuccess()){
|
||||
log.info("识别成功:{}", JsonUtils.toJson(result.getData()));
|
||||
}else{
|
||||
log.info("识别失败:{}", result.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whisper 语音识别(使用Grammar语法规则)
|
||||
* 多语言模型支持100种语言
|
||||
* 注意事项:
|
||||
* 1、不支持centos7
|
||||
* 2、模型越大越准确
|
||||
* 3、暂不支持GPU使用,如需GPU使用需要自行编译:https://github.com/ggml-org/whisper.cpp/tree/master?tab=readme-ov-file#nvidia-gpu-support
|
||||
*/
|
||||
@Test
|
||||
public void testWhisperWithGrammar() {
|
||||
try {
|
||||
WhisperRecognizer whisperRecognizer = (WhisperRecognizer)getWhisperRecognizer();
|
||||
//语法规则
|
||||
String grammarText = "root ::= \" And so, my fellow American, ask not what your country can do for you, ask what you can do for your country.\"";
|
||||
try (WhisperGrammar grammar = whisperRecognizer.parseGrammar(grammarText)){
|
||||
WhisperParams params = new WhisperParams();
|
||||
WhisperFullParams fullParams = new WhisperFullParams(WhisperSamplingStrategy.BEAN_SEARCH);
|
||||
//语言:英文
|
||||
fullParams.language = Language.EN.getCode();
|
||||
fullParams.grammar = grammar;
|
||||
params.setParams(fullParams);
|
||||
//建议上传 WAV 格式音频。其他格式将自动转换为 WAV,可能影响处理速度
|
||||
R<AsrResult> result = whisperRecognizer.recognize("src/main/resources/jfk_en.wav", params);
|
||||
if (result.isSuccess()){
|
||||
log.info("识别成功:{}", JsonUtils.toJson(result.getData()));
|
||||
}else{
|
||||
log.info("识别失败:{}", result.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Vosk模型(中文)
|
||||
* 模型下载网盘:通过网盘分享的文件:https://pan.baidu.com/s/1kiMF5MF641R7LTn1GpB2lQ?pwd=1234 提取码: 1234
|
||||
* 更多模型下载地址:https://alphacephei.com/vosk/models
|
||||
* @return
|
||||
*/
|
||||
public SpeechRecognizer geVoskRecognizer() {
|
||||
AsrModelConfig config = new AsrModelConfig();
|
||||
config.setModelEnum(AsrModelEnum.VOSK);
|
||||
/**
|
||||
* 每个模型只支持一种语言,请下载对应语音的模型,模型下载地址:https://alphacephei.com/vosk/models
|
||||
* 将模型解压后,将模型目录位置填写到此处
|
||||
*/
|
||||
config.setModelPath("/Users/xxx/Documents/develop/model/speech/vosk-model-cn-0.22");
|
||||
/**
|
||||
* macos m系列芯片需要手动下载依赖库,并指定位置,其他平台不需要
|
||||
* 下载地址:https://pan.baidu.com/s/1LZ_EX1XdTTp_f5ruud82MA?pwd=1234 提取码: 1234
|
||||
*/
|
||||
// config.setLibPath(Paths.get("/Users/xxx/Downloads/vosk-arrch64-dylib-main/libvosk.dylib"));
|
||||
return SpeechRecognizerFactory.getInstance().getModel(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Vosk模型(英文)
|
||||
* 模型下载网盘:通过网盘分享的文件:https://pan.baidu.com/s/1kiMF5MF641R7LTn1GpB2lQ?pwd=1234 提取码: 1234
|
||||
* 更多模型下载地址:https://alphacephei.com/vosk/models
|
||||
* @return
|
||||
*/
|
||||
public SpeechRecognizer geEnVoskRecognizer() {
|
||||
AsrModelConfig config = new AsrModelConfig();
|
||||
config.setModelEnum(AsrModelEnum.VOSK);
|
||||
/**
|
||||
* 每个模型只支持一种语言,请下载对应语音的模型,模型下载地址:https://alphacephei.com/vosk/models
|
||||
* 将模型解压后,将模型目录位置填写到此处
|
||||
*/
|
||||
config.setModelPath("/Users/xxx/Documents/develop/model/speech/vosk-model-small-en-us-0.15");
|
||||
// config.setLibPath(Paths.get("/Users/xxx/Downloads/vosk-arrch64-dylib-main/libvosk.dylib"));
|
||||
return SpeechRecognizerFactory.getInstance().getModel(config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Vosk 语音识别
|
||||
* 支持 20 多种语言和方言——英语、印度英语、德语、法语、西班牙语、葡萄牙语、中文、俄语、土耳其语、越南语、意大利语、荷兰语、加泰罗尼亚语、阿拉伯语、希腊语、波斯语、菲律宾语、乌克兰语、哈萨克语、瑞典语、日语、世界语、印地语、捷克语、波兰语等
|
||||
* 注意事项:
|
||||
* 1、每个模型只支持一种语言,请下载对应语言的模型
|
||||
* 2、如果音频中存在多种语言,不推荐使用vosk,可以使用Whisper
|
||||
* 3、模型越大越准确
|
||||
* 4、暂不支持GPU使用,如需GPU使用需要自行编译:https://alphacephei.com/vosk/install
|
||||
*/
|
||||
@Test
|
||||
public void testVosk() {
|
||||
try {
|
||||
SpeechRecognizer recognizer = geVoskRecognizer();
|
||||
//建议上传 WAV 格式音频。其他格式将自动转换为 WAV,可能影响处理速度
|
||||
R<AsrResult> result = recognizer.recognize("src/main/resources/lff_zh.mp3");
|
||||
if (result.isSuccess()){
|
||||
log.info("识别成功:{}", JsonUtils.toJson(result.getData()));
|
||||
}else{
|
||||
log.info("识别失败:{}", result.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vosk 语音识别(使用Grammar语法规则)
|
||||
* 支持 20 多种语言和方言——英语、印度英语、德语、法语、西班牙语、葡萄牙语、中文、俄语、土耳其语、越南语、意大利语、荷兰语、加泰罗尼亚语、阿拉伯语、希腊语、波斯语、菲律宾语、乌克兰语、哈萨克语、瑞典语、日语、世界语、印地语、捷克语、波兰语等
|
||||
* 注意事项:
|
||||
* 1、每个模型只支持一种语言,请下载对应语言的模型
|
||||
* 2、如果音频中存在多种语言,不推荐使用vosk,可以使用Whisper
|
||||
* 3、模型越大越准确
|
||||
* 4、暂不支持GPU使用,如需GPU使用需要自行编译:https://alphacephei.com/vosk/install
|
||||
*/
|
||||
@Test
|
||||
public void testVoskWithGrammar() {
|
||||
try {
|
||||
//获取英文模型
|
||||
SpeechRecognizer recognizer = geEnVoskRecognizer();
|
||||
VoskParams voskParams = new VoskParams();
|
||||
//英文
|
||||
voskParams.setLanguage(Language.EN);
|
||||
voskParams.setGrammar("[\"one two three four five six seven eight nine zero oh\"]");
|
||||
//建议上传 WAV 格式音频。其他格式将自动转换为 WAV,可能影响处理速度
|
||||
R<AsrResult> result = recognizer.recognize("src/main/resources/test_en.wav",voskParams);
|
||||
if (result.isSuccess()){
|
||||
log.info("识别成功:{}", JsonUtils.toJson(result.getData()));
|
||||
}else{
|
||||
log.info("识别失败:{}", result.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vosk 语音识别(使用Vosk内部识别器)
|
||||
* 支持 20 多种语言和方言——英语、印度英语、德语、法语、西班牙语、葡萄牙语、中文、俄语、土耳其语、越南语、意大利语、荷兰语、加泰罗尼亚语、阿拉伯语、希腊语、波斯语、菲律宾语、乌克兰语、哈萨克语、瑞典语、日语、世界语、印地语、捷克语、波兰语等
|
||||
* 注意事项:
|
||||
* 1、每个模型只支持一种语言,请下载对应语言的模型
|
||||
* 2、如果音频中存在多种语言,不推荐使用vosk,可以使用Whisper
|
||||
* 3、模型越大越准确
|
||||
* 4、暂不支持GPU使用,如需GPU使用需要自行编译:https://alphacephei.com/vosk/install
|
||||
*/
|
||||
@Test
|
||||
public void testVoskAdvanced() {
|
||||
try {
|
||||
VoskRecognizer recognizer = (VoskRecognizer)geVoskRecognizer();
|
||||
//使用vosk内部接口,需要指定识别音频的采样率
|
||||
Recognizer voskRecognizer = recognizer.createAdvancedRecognizer(16000);
|
||||
voskRecognizer.setWords(true);
|
||||
voskRecognizer.setPartialWords(true);
|
||||
// 使用vosk内部接口,只支持wav格式
|
||||
String audioPath = "src/main/resources/lff_zh.wav";
|
||||
InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(audioPath)));
|
||||
int nbytes;
|
||||
byte[] b = new byte[4096];
|
||||
while ((nbytes = ais.read(b)) >= 0) {
|
||||
if (voskRecognizer.acceptWaveForm(b, nbytes)) {
|
||||
log.info(voskRecognizer.getResult());
|
||||
} else {
|
||||
log.info(voskRecognizer.getPartialResult());
|
||||
}
|
||||
}
|
||||
log.info(voskRecognizer.getFinalResult());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: smartai.examples.face.SeetaFace6LinuxDemo
|
||||
|
||||
BIN
examples/speech-examples/src/main/resources/jfk_en.wav
Normal file
BIN
examples/speech-examples/src/main/resources/jfk_en.wav
Normal file
Binary file not shown.
BIN
examples/speech-examples/src/main/resources/lff_zh.mp3
Normal file
BIN
examples/speech-examples/src/main/resources/lff_zh.mp3
Normal file
Binary file not shown.
BIN
examples/speech-examples/src/main/resources/lff_zh.wav
Normal file
BIN
examples/speech-examples/src/main/resources/lff_zh.wav
Normal file
Binary file not shown.
14
examples/speech-examples/src/main/resources/logback.xml
Normal file
14
examples/speech-examples/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- 步骤2: 配置文件 (src/main/resources/logback.xml) -->
|
||||
<configuration scan="true" scanPeriod="30 seconds">
|
||||
<!-- 控制台日志输出 -->
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
</configuration>
|
||||
BIN
examples/speech-examples/src/main/resources/speech_zh.mp3
Normal file
BIN
examples/speech-examples/src/main/resources/speech_zh.mp3
Normal file
Binary file not shown.
BIN
examples/speech-examples/src/main/resources/test_en.wav
Normal file
BIN
examples/speech-examples/src/main/resources/test_en.wav
Normal file
Binary file not shown.
Reference in New Issue
Block a user