无网不进  
软硬件开发

http://blog.csdn.net/mwlwlm/article/details/52735700

目前国内语音识别主要是使用科大讯飞的在线语音识别,而且准确度也非常高,这主要得益于其强大的语音库,甚至方言也可以识别。但有很多时候需要离线的环境,这就需要离线识别,另外本人在这个平台上使用开源的技术,所以这里使用Sphinx4语音识别平台

1.下载Sphinx4开发包 
Sphinx有c语言,和Java版本,java版本的开发包技术Sphinx4, 
下载:sphinx4-core-5prealpha-20160628.232526-10.jar ,API jar包 
下载:sphinx4-data-5prealpha-20160628.232535-10.jar 因为的语音识别库 
可以到 Sonatype OSS repositoryhttps://oss.sonatype.org)下载开发包,不过访问非常慢,要有点耐性,也可以到百度盘下载https://pan.baidu.com/s/1jHD0aj0

2.将jar包添加到项目引用中 
这里写图片描述

3.开发语音识别对话dialog

package com.diego.speech.sphinx4;

import com.diego.camera.Neck;
import com.diego.serialcomm.DriverCMD;
import com.diego.serialcomm.ServoCMD;
import com.diego.speech.freetts.FreeTTSEN;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;

public class SphinxEN {

    private static final String ACOUSTIC_MODEL = "resource:/edu/cmu/sphinx/models/en-us/en-us";//设定语音识别模型,使用系统的英文语音模型。
    private static final String DICTIONARY_PATH = System.getProperty("user.dir")+"/res/9070.dic";//设定自定义语料库
    private static final String LANGUAGE_MODEL = System.getProperty("user.dir")+"/res/9070.lm";//设定统计语言模型



    private DriverCMD drivercmd;
    private FreeTTSEN freettsen;
    private Neck neck;


    public SphinxEN(DriverCMD drivercmd, FreeTTSEN freettsen, Neck neck) {
        super();
        this.drivercmd = drivercmd;
        this.freettsen = freettsen;
        this.neck = neck;
    }


    //////////FreeTTS


    public void dialog() throws Exception {
        Configuration configuration = new Configuration();
        configuration.setAcousticModelPath(ACOUSTIC_MODEL);
        configuration.setDictionaryPath(DICTIONARY_PATH);

        configuration.setUseGrammar(false);
        configuration.setLanguageModelPath(LANGUAGE_MODEL);
        LiveSpeechRecognizer lmRecognizer = new LiveSpeechRecognizer(configuration);


        lmRecognizer.startRecognition(true);
        System.out.println("******Start reconition");//开始识别
        while (true) {
            String utterance = lmRecognizer.getResult().getHypothesis();
            if (utterance.equals("quit"))
                break;
            else{
                System.out.println(utterance);//在控制台输出识别的语句
                freettsen.speakEN(utterance);//使用freeTTS朗读识别的语句
                drivercmd.mycmd(utterance);//根据识别的语句控制小车的动作
                neck.mycmd(utterance);// 根据识别的语句控制小车摄像头的转动方向
            }

        }
        lmRecognizer.stopRecognition();

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

4.自定义语料库的制造,在这里我们只是控制小车 
a.首先编辑一个需要识别的语句文本文件,这里的文件名为robot.txt

forward
backward
right
left
stop
exit
automate
manual
up
down
offside
contrary
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

b.在线生成语料库,和统计语言模型 
在线工具的地址如下: 
http://www.speech.cs.cmu.edu/tools/lmtool-new.html 
这里写图片描述
这里写图片描述 
生成后下载.lm和.dic两个文件,并放到程序中设定的访问目录即可。

5.调用语音识别模块,示例代码如下

System.out.println("****** begin the sphinx dialog");       
        SphinxEN sphinx=new SphinxEN(driver,freett,neck);
        try {
            sphinx.dialog();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.exit(0);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

执行如下: 
这里写图片描述
可以根据语音识别控制小车的前进后退,转弯,及摄像头的转动

6.识别准确率的问题

经过测试官方的语音包应该是安静的环境下制作的标准发音语音包,所以如果是在安静的环境下,发音准确点,识别率还是挺高的,能达到80%以上的准确率,但是如果在有噪音的环境下,识别率会非常低,查找官方文档,Sphinx是针对特定环境的识别,所以如果是噪音环境,需要制作噪音环境的语音模型。但如果要制作适合各种环境的语音模型,那需要大量的模拟环境,大量的语料才可以,而且即使制作成了,那也是非常大的语料库,一般的设备根本运行不起来。 
基于这样的结果Sphinx可以制作针对特定语音环境的语音识别,建立特定的语言模型

官方有大规模语音包的下载,据官方介绍识别率非常准确,但由于对内存资源要求太大,在4g内存的mac电脑上也是跑不起来的,目前还不知道需要多大的内存才能跑起来

官方也提供的中文普通话语音包的下载,目前还没有测试过

版权声明:本文为博主原创文章,未经博主允许不得转载。
posted on 2018-01-10 18:24  无网不进  阅读(254)  评论(0编辑  收藏  举报