Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 ? #43

Open
Ruthvik47 opened this issue Sep 21, 2018 · 1 comment

Comments

@Ruthvik47
Copy link

Ruthvik47 commented Sep 21, 2018

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

@1Caxz
Copy link

1Caxz commented Sep 21, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants