CMUsphinx现场演讲卡住了

时间:2017-09-21 18:01:02

标签: java api speech-recognition cmusphinx

我是狮身人面像的新手。我想创建一个将实时语音转换为没有语法文件的文本的类。我创建了一个java类,它使用CMUSphinx API来收听实时语音并转换为文本并打印出来。我的班级输出卡住了,无法识别语音。

我的代码:

import java.io.IOException;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;

public class SphinxTest {

public static void main(String args[]){
      final String ACOUSTIC_MODEL =
            "resource:/edu/cmu/sphinx/models/en-us/en-us";
         final String DICTIONARY_PATH =
            "resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict";
         final String GRAMMAR_PATH =
            "resource:/edu/cmu/sphinx/demo/dialog/";
         final String LANGUAGE_MODEL =
            "resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin";

         LiveSpeechRecognizer jsgfRecognizer = null;

         Configuration configuration = new Configuration();
            configuration.setAcousticModelPath(ACOUSTIC_MODEL);
            configuration.setDictionaryPath(DICTIONARY_PATH);
            configuration.setLanguageModelPath(LANGUAGE_MODEL);
            configuration.setUseGrammar(false);
            try {
                 jsgfRecognizer =
                        new LiveSpeechRecognizer(configuration);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Cannot Fetch Microphone....");
            }
            jsgfRecognizer.startRecognition(true);
            while (true) {
                System.out.println("------------------------------------");
                System.out.println("Speech Recognization Started.");
                System.out.println("-------------------------------------");
                System.out.println("Speak any sentence and exit to quit");

                String utterance = 
  jsgfRecognizer.getResult().getHypothesis();

                if (utterance.startsWith("exit"))
                    {
                    System.out.println("----Bye Bye----");
                    break;
                    }

                if (utterance.endsWith("go")) {
                    jsgfRecognizer.stopRecognition();
                    System.out.println(utterance);
                    jsgfRecognizer.startRecognition(true);
                }


            }
            jsgfRecognizer.stopRecognition();
}

}

输出结束片段:

23:13:53.240 INFO lexTreeLinguist      Max CI Units 43
23:13:53.241 INFO lexTreeLinguist      Unit table size 79507
23:13:53.246 INFO speedTracker         # -----------------------------     Timers----------------------------------------
23:13:53.246 INFO speedTracker         # Name               Count   CurTime       MinTime   MaxTime   AvgTime   TotTime   
23:13:53.248 INFO speedTracker         Load AM              1       6.0770s       6.0770s   6.0770s   6.0770s   6.0770s   
23:13:53.248 INFO speedTracker         Compile              1       2.4920s       2.4920s   2.4920s   2.4920s   2.4920s   
23:13:53.248 INFO speedTracker         Load Dictionary      1       0.2950s       0.2950s   0.2950s   0.2950s   0.2950s   
23:13:53.249 INFO speedTracker         Load LM              1       4.0040s   4.0040s   4.0040s   4.0040s   4.0040s   
------------------------------------
Speech Recognization Started.
-------------------------------------
Speak any sentence and exit to quit

1 个答案:

答案 0 :(得分:0)

您的while循环条件可能是您的程序无法正常工作的原因。尝试添加如下所示的条件以检查是否从识别器返回结果。

import java.io.IOException;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;

public class SphinxTest {

public static void main(String args[]){
      final String ACOUSTIC_MODEL =
            "resource:/edu/cmu/sphinx/models/en-us/en-us";
         final String DICTIONARY_PATH =
            "resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict";
         final String GRAMMAR_PATH =
            "resource:/edu/cmu/sphinx/demo/dialog/";
         final String LANGUAGE_MODEL =
            "resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin";

         LiveSpeechRecognizer jsgfRecognizer = null;

         Configuration configuration = new Configuration();
            configuration.setAcousticModelPath(ACOUSTIC_MODEL);
            configuration.setDictionaryPath(DICTIONARY_PATH);
            configuration.setLanguageModelPath(LANGUAGE_MODEL);
            configuration.setUseGrammar(false);
            try {
                 jsgfRecognizer =
                        new LiveSpeechRecognizer(configuration);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Cannot Fetch Microphone....");
            }
            jsgfRecognizer.startRecognition(true);
            while ((result = jsgfRecognizer.getResult()) != null) {
                System.out.println("------------------------------------");
                System.out.println("Speech Recognization Started.");
                System.out.println("-------------------------------------");
                System.out.println("Speak any sentence and exit to quit");

                String utterance = 
  jsgfRecognizer.getResult().getHypothesis();
                System.out.println("utterance {} "+utterance);
                if (utterance.startsWith("exit"))
                    {
                    System.out.println("----Bye Bye----");
                    break;
                    }

                if (utterance.endsWith("go")) {
                    jsgfRecognizer.stopRecognition();
                    System.out.println(utterance);
                    jsgfRecognizer.startRecognition(true);
                }


            }
            jsgfRecognizer.stopRecognition();
}

}