当前位置: 首页 > news >正文

java使用CMU sphinx语音识别

java使用CMU sphinx语音识别

    • 一、pom依赖
      • 1、依赖dependency
      • 2、配置仓库repository
    • 二、下载中文资源包
      • 1、下载中文资源包(需要其他语言的选择对应的文件夹即可),中文选择Mandarin
      • 2、将下载后的文件放到项目中
      • 3、代码-识别wav语音文件
      • 4、代码-识别实时输入(本地pc未成功)
        • 4.1 测试端需要有语音输入设备

一、pom依赖

1、依赖dependency

        <!-- CMUSphinx Core Library --><dependency><groupId>edu.cmu.sphinx</groupId><artifactId>sphinx4-core</artifactId><version>5prealpha-SNAPSHOT</version></dependency><!-- CMUSphinx Data Library --><dependency><groupId>edu.cmu.sphinx</groupId><artifactId>sphinx4-data</artifactId><version>5prealpha-SNAPSHOT</version></dependency>

2、配置仓库repository

        <repository><id>snapshots-repo</id><url>https://oss.sonatype.org/content/repositories/snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository>

二、下载中文资源包

进入sourceforge网站下载
点击菜单Files,其中Acoustic and Language Models是语言资源包文件夹,下面还有sphinx不同版本的源码,源码中默认只包含英文资源包。
sphinx的files列表
在这里插入图片描述

1、下载中文资源包(需要其他语言的选择对应的文件夹即可),中文选择Mandarin

在这里插入图片描述
在这里插入图片描述

2、将下载后的文件放到项目中

解压后的文件
在这里插入图片描述
在这里插入图片描述

3、代码-识别wav语音文件

好像只能识别wav格式的文件,m4a试了不行,可以自行尝试看下结果

    public static void speechToTxt2() throws Exception {// 1、配置Configuration conf = new Configuration();conf.setAcousticModelPath("resource:/sphinx/zh/zh_cn.cd_cont_5000");conf.setDictionaryPath("resource:/sphinx/zh/zh_cn.dic");conf.setLanguageModelPath("resource:/sphinx/zh/zh_cn.lm.bin");System.out.println("Loading models...");//        conf.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
//        conf.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");Context context = new Context(conf);context.setLocalProperty("decoder->searchManager", "allphoneSearchManager");Recognizer recognizer = context.getInstance(Recognizer.class);InputStream stream = ParseUtil.class.getResourceAsStream("/sphinx/wav/2.wav");stream.skip(44);// Simple recognition with generic modelrecognizer.allocate();context.setSpeechSource(stream, TimeFrame.INFINITE);Result result;while ((result = recognizer.recognize()) != null) {SpeechResult speechResult = new SpeechResult(result);System.out.format("Hypothesis: %s\n", speechResult.getHypothesis());System.out.println("List of recognized words and their times:");for (WordResult r : speechResult.getWords()) {System.out.println(r);}//            System.out.println("Lattice contains "
//                    + speechResult.getLattice().getNodes().size() + " nodes");}recognizer.deallocate();}

输出结果如图
其中 Hypothesis: SIL uu o2 c j ie1 r ou3 s r ou3 x r ou1 o2 SIL 就是需要训练的内容。
我们下载的资源文件zh_cn.dic中有已经简单训练的结果
在这里插入图片描述
在这里插入图片描述

4、代码-识别实时输入(本地pc未成功)

调用时,系统能检测到在使用麦克风。但在recognizer.getResult()这行总是会报溢出错误,也有可能是输入的设备不支持,各位可以自行尝试。有结果可以评论学习一下,感谢。

    public static void speechToTxt() throws Exception {// 1、配置Configuration conf = new Configuration();conf.setAcousticModelPath("resource:/sphinx/zh/zh_cn.cd_cont_5000");conf.setDictionaryPath("resource:/sphinx/zh/zh_cn.dic");conf.setLanguageModelPath("resource:/sphinx/zh/zh_cn.lm.bin");// 2、语音识别器LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(conf);// 2.1 开始识别recognizer.startRecognition(true);// 2.2 识别结果SpeechResult result;while ((result = recognizer.getResult()) != null) {System.out.println(result.getHypothesis());}// 2.3 停止识别recognizer.stopRecognition();}
4.1 测试端需要有语音输入设备

设置-系统-声音-输入输入配置中需要有输入设备,测试麦克风可以查看此设备是否可用
在这里插入图片描述
在这里插入图片描述

相关文章:

  • Java代理讲解
  • 多层级的对象如何修改、或json格式
  • 回溯算法理论基础
  • Verilog 语法 (二)
  • 小刚说C语言刷题——1565成绩(score)
  • element-ui tabs 组件源码分享
  • 品融电商:以全域增长方法论,解码2025情绪消费新机遇
  • Coze高阶玩法 | 使用Coze制作思维认知提升视频,效率提升300%!(附保姆级教程)
  • OpenHarmony之电源管理子系统公共事件定义
  • Vue选项式 API 与组合式 API
  • jdk-8u202-linux-x64.tar.gz官方下载地址
  • 统计服务器CPU、内存、磁盘、网络IO、队列、数据库占用空间等等信息
  • 大学IP广播系统解决方案:构建数字化智慧化大学校园IP广播平台
  • 创建型设计模式之:简单工厂模式、工厂方法模式、抽象工厂模式、建造者模式和原型模式
  • Oracle Recovery Tools修复ORA-00742、ORA-600 ktbair2: illegal inheritance故障
  • 路由器的基础配置全解析:静态动态路由 + 华为 ENSP 命令大全
  • 3D模型文件格式之《STL格式介绍》
  • 知识蒸馏和迁移学习的区别
  • Cannot read properties of null (reading ‘classList‘)
  • A2A与MCP之间的简单理解
  • 外交部回应涉长江和记出售巴拿马运河港口交易:望有关各方审慎行事,充分沟通
  • 伊朗爆炸港口已恢复货物进出口工作
  • 上海超万套保租房供应高校毕业生,各项目免押、打折等优惠频出
  • 没有雷军的车展:老外扎堆,萌车、机器狗谁更抢镜?| 湃客Talk
  • 来论|这无非就是一只“纸老虎”:评特朗普政府“关税战”
  • 中国人民对外友好协会代表团访问美国