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 –