java使用CMU sphinx语音识别

java使用CMU sphinx语音识别

一、pom依赖

1、依赖dependency

xml 复制代码
        <!-- 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

xml 复制代码
        <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不同版本的源码,源码中默认只包含英文资源包。

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


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

解压后的文件

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

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

java 复制代码
    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 model
        recognizer.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()这行总是会报溢出错误,也有可能是输入的设备不支持,各位可以自行尝试。有结果可以评论学习一下,感谢。

java 复制代码
    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 测试端需要有语音输入设备

设置-系统-声音-输入输入 配置中需要有输入设备,测试麦克风 可以查看此设备是否可用

相关推荐
沉到海底去吧Go41 分钟前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
木子单片机5 小时前
基于STM32语音识别柔光台灯
stm32·单片机·嵌入式硬件·proteus·语音识别·keil
撞南墙者6 小时前
如何让AI自己检查全文?使用OCR和LLM实现自动“全文校订”(可DIY校订规则)
人工智能·ocr
Blossom.1181 天前
量子通信:从科幻走向现实的未来通信技术
人工智能·深度学习·目标检测·机器学习·计算机视觉·语音识别·量子计算
沉到海底去吧Go2 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
TextIn智能文档云平台2 天前
从OCR到Document Parsing,AI时代的非结构化数据处理发生了什么改变?
人工智能·自然语言处理·ocr·pdf解析·textin·复杂文档解析
CV-杨帆2 天前
Paraformer分角色语音识别-中文-通用 FunASR
人工智能·语音识别
mulannanlu3 天前
免费开源Umi-OCR,离线使用,批量精准!
ocr
凌康ACG3 天前
易语言使用OCR
c++·yolo·c#·ocr·易语言
Blossom.1183 天前
人工智能在智能教育中的创新应用与未来趋势
java·人工智能·深度学习·目标检测·机器学习·计算机视觉·语音识别