问题
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() {
}
}
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.
回答1:
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);
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");
To start named search you point it's name, not keyword itselsf:
recognizer.startListening(KWS_SEARCH_NAME);
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);
}
}
来源:https://stackoverflow.com/questions/24463721/keyword-is-not-detected-using-pocketsphinx-on-android