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

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

相关推荐
RainSerein5 小时前
Laravel8中调取腾讯云文字识别OCR
ocr·php·腾讯云·laravel
AI生存日记6 小时前
AI 日报:阿里、字节等企业密集发布新技术,覆盖语音、图像与药物研发等领域
人工智能·华为云·语音识别·open ai大模型
静心问道14 小时前
XLSR-Wav2Vec2:用于语音识别的无监督跨语言表示学习
人工智能·学习·语音识别
老胖闲聊10 天前
Python pytesseract【OCR引擎库】 简介
开发语言·python·ocr
沉到海底去吧Go11 天前
【工具教程】PDF指定区域OCR识别重命名工具使用教程和注意事项
pdf·ocr·图片区域识别改名·仓储物流单据识别·物流单据识别改名·pdf区域识别改名·pdf区域识别重命名
heart000_111 天前
通义大模型在文档自动化处理中的高效部署指南(OCR集成与批量处理优化)
运维·自动化·ocr
kevin 111 天前
OCR大模型,破解金融文档处理困境,从文字识别到文字理解
金融·ocr
Just_Paranoid11 天前
华为云Flexus+DeepSeek征文|基于Dify构建智能票据信息识别助手
华为云·ocr·dify·maas·deepseek·flexusx
whoarethenext12 天前
使用 C++/OpenCV 构建中文 OCR 系统:实现账单、发票及 PDF 读取
c++·opencv·ocr
漫游者Nova12 天前
免费音频视频语音识别转文字软件SenseVoice整合包下载,支持批量操作可生成字幕
人工智能·语音识别·语音转文字·语音识别转文本