I'm building application that listens for keyword in service for this I'm using sphinx, I noticed that when I speak keyword the onPartialResult method runs again and again and never able to run onresult so that I could do some work, I dnt know about how to configure assets etc, I just downloaded project, Here is my code:
private void runRecognizerSetup() {
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(VoiceService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception e) {
if (e != null) {
Log.i(LOG_TAG, "Failed to init recognizer ");
} else {
startListening(KWS_SEARCH);
}
}
}.execute();
}
private void startListening(String kwsSearch) {
recognizer.startListening(KWS_SEARCH);
Log.i(LOG_TAG, "startedlistening");
}
@Override
public void onDestroy() {
super.onDestroy();
if (recognizer != null) {
recognizer.cancel();
recognizer.shutdown();
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
if (text.contains(KEYPHRASE)) {
Toast.makeText(this, "onPartialResult text=" +text,Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
Log.i(LOG_TAG, "onPartialResult text=" +text);
}
/**
* This callback is called when we stop the recognizer.
*/
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.i(LOG_TAG, "onResult text=" +text);
Intent Intent = new Intent(this, ReceiverActivity.class);
Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent);
}
}
@Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
this.timer.schedule(new TimerTask() {
@Override
public void run() {
recognizer.stop();
Log.i("Runningafter", "runningafter2seco");
}
}, 5000);
this.timer.schedule(new TimerTask() {
@Override
public void run() {
recognizer.startListening(KWS_SEARCH);
Log.i("startedlisteningagain", "startedlisteningagain");
}
}, 5000);
}
@Override
public void onEndOfSpeech() {
if (!recognizer.getSearchName().contains(KWS_SEARCH))
// switchSearch(KWS_SEARCH);
Log.i(LOG_TAG, "onEndOfSpeech");
}
private void switchSearch(String searchName) {
// Log.i(LOG_TAG, "switchSearch searchName = " + searchName);
if (recognizer != null) {
Log.i("RecognizerNull ", "Null");
recognizer.stop();
recognizer.startListening(searchName);
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
}
},
2000
);
this.timer.schedule(new TimerTask() {
@Override
public void run() {
recognizer.startListening(KWS_SEARCH);
Log.i("Runningafter", "runningafter2seco");
}
}, 10000);
}
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
.setKeywordThreshold(1e-45f) // Threshold to tune for keyphrase to balance between false alarms and misses
.setBoolean("-allphone_ci", true) // Use context-independent phonetic search, context-dependent is too slow for mobile
.getRecognizer();
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
recognizer.addListener(this);
Log.i(LOG_TAG, "setupRecognizer");
}
@Override
public void onError(Exception error) {
Log.i(LOG_TAG, "onError " + error.getMessage());
}
@Override
public void onTimeout() {
switchSearch(KWS_SEARCH);
Log.i(LOG_TAG, "onTimeout");
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm building application that listens for keyword in service for this I'm using sphinx, I noticed that when I speak keyword the onPartialResult method runs again and again and never able to run onresult so that I could do some work, I dnt know about how to configure assets etc, I just downloaded project, Here is my code:
You need to stop listening with
recognizer.cancel()
immediately after recognizer detected a keyphrase to reset partial result.you mean in onbegining method?
in
onPartialResult
. Actually the original demo did it right. You modified switchSearch with a timer, you should have return that modification back.In onpartial method I first stop recognizer and then in the next line started recognizer but in this case it recognizes only once
Could you tell me what is threshold for keyword?