0

我想在服务中使用pocketsphinx监听单词Hello不断无法启动服务? (语音识别测试)

我得到的错误。这里是full stack trace。这是它的一小部分。

Unable to create service curlybrace.ruchir.myApp.MyService: java.lang.RuntimeException: new_Decoder returned -1 

它是由这个原因引起:

  setupRecognizer(assetDir); //SETUP 

这:

   .getRecognizer(); 

在我onCreate

Log.v(TAG, "Voice recognition activated!"); 

     //Register voice recog listener :) 

     Assets assets = null; 
     try { 
      assets = new Assets(MyService.this); 
      File assetDir = assets.syncAssets(); 
      setupRecognizer(assetDir); //SETUP 

      Log.v(TAG, "Set up listener"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

这里是我的setupRecognizer方法:

private void setupRecognizer(File assetDir) throws IOException { 

     recognizer = defaultSetup() 
       .setAcousticModel(new File(assetDir, "hmm/en-us-semi")) 
       .setDictionary(new File(assetDir, "lm/cmu07a.dic")) 
       .setKeywordThreshold(1e-5f) 
       .getRecognizer(); 

     recognizer.addListener(this); 
     // recognizer.addKeywordSearch("Hello", assetDir); //I don't know what this does... 
    recognizer.startListening("Hello"); //Start listeneing 


    } 

这里的实现方法之一:

@Override 
    public void onPartialResult(Hypothesis hypothesis) { 

     String text = hypothesis.getHypstr(); 
     if (text.equals("Hello")) { 
      // do something 

      Log.v(TAG, "SPEECH RECOGNIZED HELLO!"); 
     } 

    } 

我将不胜感激任何反馈。积极,消极,甚至一个评论。在这之后,我绝望了,试了两天!

+0

就像一个想法:你是否在清单中注册了麦克风权限? – luxer

+0

@luxer不,我没有 –

+1

即使您使用了库,您的应用程序也需要获得听麦克风的权限。你有没有试过添加它? – luxer

回答

0

对于命令,下面的代码是我做的,它运行良好。如果您只做关键字检测,请查看Sphinx下载中的关键字spotting示例包并修改下面的代码。

确保assets --> sync文件夹只包含下列文件

folder en-us-ptm 
assets.lst 
cmudict-en-us.dict 
cmudict-en-us.dict.md5 
command.gram 
your_preferred_name.dict 

如果允许用户设置的命令,那么不需要命令和your_preferred_name.dict。您可以稍后将其添加到代码中,并将其保存在下面的相应目录中。对于关键字发现,用Sphinx示例中的任何名称替换command.gram。

assets --> sync文件夹中修改列出的文件以具有下面的内容。如果应用程序很难理解调整阈值参数即/ 1E-8,您可以编辑这些文件,用记事本++

assets.lst

cmudict-en-us.dict 
en-us-ptm/README 
en-us-ptm/feat.params 
en-us-ptm/mdef 
en-us-ptm/means 
en-us-ptm/noisedict 
en-us-ptm/sendump 
en-us-ptm/transition_matrices 
en-us-ptm/variances 

command.gram

hello /1/ 

/阈值越小,识别器越容易拾取该词,但也容易得到误报。对于关键字发现,请使用关键字替换Sphinx关键字示例。

your_prefered_name.dict
复制而且,在本例中command.gram字的cmudict烯us.dict整条生产线是字你好。我有一个单独的字典,以便文件更小,以便字典搜索有所改进。所以你的your_prefered_name。字典应该看起来像

hello HH AH L OW 
hello(2) HH EH L OW 

对于关键词识别我认为你可以串词放在一起(不知道你一定要试试,看看它是否会工作),所以例如你好世界将是

hello world HH AH L OW .... (the dot is for world) 

在您的应用程序的开始创建一个目录说“斯芬克斯”

String createSphinxDir() 
{ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    String sphinxDir = prefs.getString("sphinx", null); 
    if (sphinxDir == null) 
    { 
     Assets assets; 
     try 
     { 
      assets = new Assets(this); 
      File sphinxDirFile = assets.syncAssets(); 
      if (sphinxDirFile != null) 
      { 
       sphinxDir = sphinxDirFile.getAbsolutePath(); 
       Editor editor = prefs.edit(); 
       editor.putString("sphinx", sphinxDir); 
       editor.commit(); 
       // Also save the command.gram and your_preferred_name.dict 
       // to the sphinx dir here. Or save the them later to this 
       // dir if you allow user to set the command or keyword 
      } 
     } 
     catch (IOException e) 
     { 

     } 
    } 
    return sphinxDir; 
} 

那么无论你发起的语音识别

String sphinxDir = createSphinxDir(); 
     if (sphinxDir != null) 
     { 
      try 
      { 
       mSpeechRecognizer = defaultSetup() 
         .setAcousticModel(new File(sphinxDir, "en-us-ptm")) 
         .setDictionary(new File(sphinxDir, "your_preferred_name.dict")) 
         .setBoolean("-allphone_ci", true) 
         .getRecognizer(); 
       mSpeechRecognizer.addListener(your listener); 

// check if file exists here I have a util called FileIOUtils, you should create a method to check.     
if ((new File(sphinxDir + File.separator + "command.gram")).isFile()) 
       { 
        mSpeechRecognizer.addKeywordSearch("wakeup", 
          new File(sphinxDir + File.separator + "command.gram")); 
       } 

       // Or wherever appropriate 
       startListening("wakeup"); 
      } 
      catch (IOException e) 
      { 

      } 
     } 

对于关键字spotting,只需将上面的内容更改为Sphinx示例中的那个。

+0

Hey Hoan,我花了很多时间去理解它,但我仍然遇到一些错误。我已经创造了这个要点,并且评论了所有有错误的地方。请让我知道我应该如何解决这些问题,或者我做错了什么。非常感谢。 gist.github.com/anonymous/e67e876dc1a33df25b2c –

+0

gist.github.com/anonymous/e67e876dc1a33df25b2c –

+0

编辑后的命令现在应该可以工作。 –

1

你有这样的:

private void setupRecognizer(File assetDir) throws IOException { 
     recognizer = defaultSetup() 
       .setAcousticModel(new File(assetDir, "hmm/en-us-semi")) 
       .setDictionary(new File(assetDir, "lm/cmu07a.dic")) 
       .setKeywordThreshold(1e-5f) 
       .getRecognizer(); 
     recognizer.addListener(this); 
     // recognizer.addKeywordSearch("Hello", assetDir); //I don't know what this does... 
    recognizer.startListening("Hello"); //Start listeneing 
    } 

尝试将其更改为这样:

private void setupRecognizer(File assetDir) throws IOException { 
     recognizer = defaultSetup() 
       .setAcousticModel(new File(assetDir, "hmm/en-us-semi")) 
       .setDictionary(new File(assetDir, "lm/cmu07a.dic")) 
       .setKeywordThreshold(1e-5f) 
       .getRecognizer(); 
     recognizer.addListener(this); 

    //Add this: 
    File digitsGrammar = new File(modelsDir, "grammar/digits.gram"); 
    recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar); 
    } 

首先讲话侦察,从按钮调用它。当它工作,从服务调用它,让事情变得简单了:

recognizer.startListening("Hello"); //Start listeneing 

现在,创建一个名为digits.gram新的文件,并把它放在这里所说的文件夹内:/youProjectRootFolder/grammar/digits.gram 这个文件实际上是。 txt文件,但扩展名更改为.gram当您完成把这个文本中:

hello /1e-1/ 
hi /1e-1/ 
bye /1e-1/ 
goodbye /1e-1/ 
...etc. /1e-1/ 

在这里,你会发现类似的情况:Recognizing multiple keywords using PocketSphinx 祝您好运!

+0

非常感谢您的回答!我似乎无法找到我的根目录中的'grammar'文件夹...我是否需要创建它? [这是我的目录在项目视图中的截图。](http://i.snag.gy/Q3Rrn.jpg)我是否应该创建语法文件?另外,我对'setAcousticModel'和'setDictionary'方法有点困惑,以及为什么他们需要一个文件参数。为什么我们甚至需要'assetDir'文件?我刚刚从演示中获得了这一点。请让我知道:)非常感谢乔希! –

+0

是的,如果你的项目没有,你需要创建自己的“语法”文件夹,然后自己创建语法文件:只需复制粘贴我上面提到的文本,然后将扩展名从.txt更改为.gram, 。我不确定assetDir如何在下面深入工作,但我知道它允许您从他们的文件中加载词典和声学模型。 @RuchirBaronia – Josh

+0

嗯......我仍然在这行'.getRecognizer();'有同样的问题。同样的新解码器返回-1错误正在发生......我不知道为什么!也许我没有正确添加digits.gram文件,是这样吗? http://snag.gy/VCCBH.jpg –