pocketsphinx-android icon indicating copy to clipboard operation
pocketsphinx-android copied to clipboard

The wake up word in Partial results are not accurate they are triggering continuously.They are even triggering for similar pronunciation .Is their any solution for that ?

Open Ruthvik47 opened this issue 6 years ago • 1 comments

package com.whatever.ruthvikreddy.pocketsphinx_android;

import android.Manifest; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView;

import java.io.File; import java.io.IOException; import java.lang.ref.WeakReference;

import edu.cmu.pocketsphinx.Assets; import edu.cmu.pocketsphinx.Hypothesis; import edu.cmu.pocketsphinx.RecognitionListener; import edu.cmu.pocketsphinx.SpeechRecognizer; import edu.cmu.pocketsphinx.SpeechRecognizerSetup;

public class MainActivity extends AppCompatActivity implements RecognitionListener {

/* We only need the keyphrase to start recognition, one menu with list of choices,
 and one word that is required for method switchSearch - it will bring recognizer
 back to listening for the keyphrase*/
private static final String KWS_SEARCH = "wakeup";
private static final String MENU_SEARCH = "menu";
/* Keyword we are looking for to activate recognition */
private static final String KEYPHRASE = "hey bob";
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
int i =0;
/* Recognition object */
private SpeechRecognizer recognizer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Check if user has given permission to record audio
    int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
        return;
    }
    new SetupTask(this).execute();
}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().equals(KWS_SEARCH))
        switchSearch(KWS_SEARCH);

}

@Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr().;
    Log.i("input_text",text);

    if (text.equals(KEYPHRASE)) {
        ((TextView)findViewById(R.id.output)).setText(text + i);
        i++;
        //switchSearch(KEYPHRASE);
    }

}

@Override
public void onResult(Hypothesis hypothesis) {
    if (hypothesis != null) {
        System.out.println(hypothesis.getHypstr());
    }


}

@Override
public void onError(Exception e) {
    System.out.println(e.getMessage());
   switchSearch(KEYPHRASE);

}

@Override
public void onTimeout() {
    switchSearch(KWS_SEARCH);

}


private static class SetupTask extends AsyncTask<Void, Void, Exception> {
    WeakReference<MainActivity> activityReference;
    SetupTask(MainActivity activity) {
        this.activityReference = new WeakReference<>(activity);
    }
    @Override
    protected Exception doInBackground(Void... params) {
        try {
            Assets assets = new Assets(activityReference.get());
            File assetDir = assets.syncAssets();
            activityReference.get().setupRecognizer(assetDir);
        } catch (IOException e) {
            return e;
        }
        return null;
    }
    @Override
    protected void onPostExecute(Exception result) {
        if (result != null) { ;
        } else {
            activityReference.get().switchSearch(KWS_SEARCH);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions, @NonNull  int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Recognizer initialization is a time-consuming and it involves IO,
            // so we execute it in async task
            new SetupTask(this).execute();
        } else {
            finish();
        }
    }
}
private void setupRecognizer(File assetsDir) throws IOException {
    recognizer = SpeechRecognizerSetup.defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            // Disable this line if you don't want recognizer to save raw
            // audio files to app's storage
            //.setRawLogDir(assetsDir)
            .getRecognizer();
    recognizer.addListener(this);
    // Create keyword-activation search.
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
    // Create your custom grammar-based search
    File menuGrammar = new File(assetsDir, "mymenu.gram");
    recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
}
private void switchSearch(String searchName) {
    recognizer.stop();
    if (searchName.equals(KWS_SEARCH))
        recognizer.startListening(searchName);
    else
        recognizer.startListening(searchName, 10000);
}
@Override
public void onStop() {
    super.onStop();
    if (recognizer != null) {
        recognizer.cancel();
        recognizer.shutdown();
    }
}

} capture

Ruthvik47 avatar Sep 21 '18 09:09 Ruthvik47

Try to adjust value of -vad_threshold, the max value is 4.0.

recognizer = SpeechRecognizerSetup.defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            .setFloat("-vad_threshold", 3.5)
            .getRecognizer();

1Caxz avatar Sep 21 '18 10:09 1Caxz