本文介绍了在Android上使用pocketsphinx未检测关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释我怎么能转换我的演讲用pocketsphinx发送短信?我试试这个:

Can anybody explain me how I can convert my speech to text using pocketsphinx? I try this:

import com.example.speechtutor.SpeechRecognizerRecorder;
import com.example.speechtutor.SpeechRecognizerRecorderSetup;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import static edu.cmu.pocketsphinx.Assets.syncAssets;

public class SpeakActivity extends Activity implements RecognitionListener {


SpeechRecognizerRecorder recognizer;

private File appDir;

 String filePath;

 private static final String KWS_SEARCH_NAME = "wakeup";
 private static final String FORECAST_SEARCH = "forecast";
 private static final String DIGITS_SEARCH = "digits";
 private static final String MENU_SEARCH = "menu";
 private static final String KEYPHRASE = "hello";


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_speak);

try {
    Log.d("Tag","before trying to sync assets");
    appDir = syncAssets(getApplicationContext());
} catch (IOException e) {
    throw new RuntimeException("failed to synchronize assets", e);
}

Log.d("TAG","before recognizer instantiaiton");
recognizer = SpeechRecognizerRecorderSetup.defaultSetup()
           .setAcousticModel(new File(appDir, "models/hmm/en-us-semi"))
           .setDictionary(new File(appDir, "models/lm/cmu07a.dic"))
           .setRawLogDir(appDir)
           .setKeywordThreshold(200)
           .setAudioStorageDirectory("SpeechTutor")
           .getRecognizer();




filePath = recognizer.getAudioStorageFilePath();

    recognizer.addListener(this);
    // Create keyword-activation search.
    File fillers = new File(appDir, "models/grammar/menu.gram");
    recognizer.addKeywordSearch(KWS_SEARCH_NAME, fillers.getPath());
    // Create grammar-based searches.
    //File menuGrammar = new File(appDir, "models/grammar/menu.gram");
    //recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
    File digitsGrammar = new File(appDir, "models/grammar/digits.gram");
    recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);
    // Create language model search.
    //digitsGrammar.File languageModel = new File(appDir, "models/lm/weather.dmp");
    //recognizer.addNgramSearch(FORECAST_SEARCH, languageModel);

    recognizer.startListening(KEYPHRASE);


}

    @Override
public void onPartialResult(Hypothesis arg0) {
       String text = results.getHypstr();

    Log.d("Spoken text",text);  
   }

    @Override
public void onBeginningOfSpeech() {
    }

}

这code,没有错误,但 onPartialResult 通话效果,当我说你好。我的应用程序必须转换每个语音至文本。请给我一个样本。

This code works with no error but onPartialResult call when I say "hello". My app must convert every voice to text. Please give me a sample.

推荐答案

您code包含多个问题。尝试关键字的阈值1E-60 1E-40 1E-20 1E-10,在这一行肯定不是200:

Your code contains multiple issues. Try keyword threshold values as 1e-60, 1e-40, 1e-20, 1e-10, certainly not 200 in this line:

       .setKeywordThreshold(200)

如果你只打算寻找关键字,也没有必要与语法这一行:

If you only going to look for keyword, there is no need for this line with grammar:

File digitsGrammar = new File(appDir, "models/grammar/digits.gram");
recognizer.addGrammarSearch(DIGITS_SEARCH, digitsGrammar);

这部分看起来并不合理的为好。关键字搜索需要的单词列表搜索每行一个,不是 menu.gram 文件

This part doesn't look reasonable as well. Keyword search takes a list of words to search one per line, not menu.gram file

File fillers = new File(appDir, "models/grammar/menu.gram");
recognizer.addKeywordSearch(KWS_SEARCH_NAME, fillers.getPath());

如果你要搜索只是单个关键字,没有必要添加关键字搜索,你只需要添加关键词的搜索这句话

If you are going to search just for single keyword there is no need to add keyword search, you just add keyphrase search for that phrase

 recognizer.addKeyphraseSearch(KWS_SEARCH_NAME, "hello");

要开始你点它的名字命名的搜索,而不是关键字itselsf:

To start named search you point it's name, not keyword itselsf:

 recognizer.startListening(KWS_SEARCH_NAME);

正确code应该是这样的:

Correct code should look like this:

import com.example.speechtutor.SpeechRecognizerRecorder;
import com.example.speechtutor.SpeechRecognizerRecorderSetup;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import static edu.cmu.pocketsphinx.Assets.syncAssets;

public class SpeakActivity extends Activity implements RecognitionListener {

 SpeechRecognizerRecorder recognizer;

 private File appDir;

 private static final String KWS_SEARCH_NAME = "wakeup";
 private static final String KEYPHRASE = "hello";


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_speak);

    try {
       Log.d("Tag","before trying to sync assets");
         appDir = syncAssets(getApplicationContext());
    } catch (IOException e) {
         throw new RuntimeException("failed to synchronize assets", e);
    }

Log.d("TAG","before recognizer instantiaiton");
recognizer = SpeechRecognizerRecorderSetup.defaultSetup()
           .setAcousticModel(new File(appDir, "models/hmm/en-us-semi"))
           .setDictionary(new File(appDir, "models/lm/cmu07a.dic"))
           .setRawLogDir(appDir)
           .setKeywordThreshold(1e-40)
           .setAudioStorageDirectory("SpeechTutor")
           .getRecognizer();


    recognizer.addListener(this);
    recognizer.addKeyphraseSearch(KWS_SEARCH_NAME, KEYPHRASE);
    recognizer.startListening(KWS_SEARCH_NAME);
}

    @Override
    public void onPartialResult(Hypothesis hyp) {
        if (hyp == null)
             return;
        // Restart the recognition if keyword is found
        String text = hyp.getHypstr();
        Log.d("Spoken text",text);  
        recognizer.cancel();
        recognizer.startSearch(KWS_SEARCH_NAME);
   }
}

这篇关于在Android上使用pocketsphinx未检测关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:53