首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CMUsphinx现场演讲卡住了

CMUsphinx现场演讲卡住了
EN

Stack Overflow用户
提问于 2017-09-22 02:01:02
回答 1查看 366关注 0票数 0

我是新来的狮身人面像。我想做一个没有语法文件的类,它可以将实时语音转换为文本。我制作了一个java类,它使用CMUSphinx API收听现场语音,并将其转换为文本并打印出来。我的类输出卡住了,无法识别语音。

我的代码:

代码语言:javascript
复制
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();
}

}

来自输出的结束代码段:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-18 18:20:15

while循环条件可能是导致程序无法工作的原因。尝试添加类似下面的条件,以检查是否从识别器返回结果。

代码语言:javascript
复制
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();
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46350439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档