在android上使用pocketsphinx未检测到关键字

时间:2014-06-28 04:54:56

标签: android speech-recognition pocketsphinx-android

有人可以解释我如何使用pocketsphinx将我的演讲转换为文本吗?我试试这个:

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() {
    }

}

当我说"你好"时,这段代码没有错误,但onPartialResult调用。我的应用必须将每个语音转换为文本。请给我一个样品。

1 个答案:

答案 0 :(得分:8)

您的代码包含多个问题。尝试关键字阈值为1e-60,1e-40,1e-20,1e-10,当然不是200行:

       .setKeywordThreshold(200)

如果您只想查找关键字,则不需要这行语法:

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

这部分看起来也不合理。关键字搜索会使用一个单词列表来搜索每行一个,而不是menu.gram文件

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

如果您要搜索单个关键字,则无需添加关键字搜索,只需为该短语添加关键短语搜索

 recognizer.addKeyphraseSearch(KWS_SEARCH_NAME, "hello");

要开始命名搜索,请指出它的名称,而不是关键字itselsf:

 recognizer.startListening(KWS_SEARCH_NAME);

正确的代码应如下所示:

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);
   }
}