Menu

PocketSphinx Recognizes words from noise . Without anything is said

Help
2016-12-16
2017-01-22
  • Pratik Sanap

    Pratik Sanap - 2016-12-16

    How do i get the result after saying word.Not without saying anything.

    Here is the menu gram file.

    #JSGF V1.0;
    
    grammar menu;
    
    public <item> = MOBILE SILENT KARU | APP CHALU KARU;
    

    And this is the code

    package svara.alienart.in.svara;
    
    import static android.widget.Toast.makeText;
    import static edu.cmu.pocketsphinx.SpeechRecognizerSetup.defaultSetup;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Context;
    import android.content.Intent;
    import android.media.AudioManager;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.Toast;
    import edu.cmu.pocketsphinx.Assets;
    import edu.cmu.pocketsphinx.Hypothesis;
    import edu.cmu.pocketsphinx.RecognitionListener;
    import edu.cmu.pocketsphinx.SpeechRecognizer;
    
    public class MainActivitySVARA extends Activity implements
            RecognitionListener {
    
        /* Named searches allow to quickly reconfigure the decoder */
        private static final String KWS_SEARCH = "wakeup";
    
        private static final String MENU_SEARCH = "menu";
    
        /* Keyword we are looking for to activate menu */
        private static final String KEYPHRASE = "SHAREIT AHET";
    
        private SpeechRecognizer recognizer;
        private HashMap<String, Integer> captions;
    
    
        @Override
        public void onCreate(Bundle state) {
            super.onCreate(state);
    
            // Prepare the data for UI
            captions = new HashMap<String, Integer>();
            captions.put(KWS_SEARCH, R.string.kws_caption);
            captions.put(MENU_SEARCH, R.string.menu_caption);
    
            setContentView(R.layout.activity_main_svara);
            ((TextView) findViewById(R.id.textview1))
                    .setText("Preparing the recognizer");
    
            // 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(MainActivitySVARA.this);
                        File assetDir = assets.syncAssets();
                        setupRecognizer(assetDir);
                    } catch (IOException e) {
                        return e;
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(Exception result) {
                    if (result != null) {
                        ((TextView) findViewById(R.id.textview1))
                                .setText("Failed to init recognizer " + result);
                    } else {
                        switchSearch(KWS_SEARCH);
                    }
                }
            }.execute();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            recognizer.cancel();
            recognizer.shutdown();
        }
    
        /**
         * In partial result we get quick updates about current hypothesis. In
         * keyword spotting mode we can react here, in other modes we need to wait
         * for final result in onResult.
         */
        public void launchApp(String packageName) {
            Intent mIntent = getPackageManager().getLaunchIntentForPackage(
                    packageName);
            if (mIntent != null) {
                try {
                    startActivity(mIntent);
                } catch (ActivityNotFoundException err) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "not found", Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        }
        public void slientmode(){
            AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        @Override
        public void onPartialResult(Hypothesis hypothesis) {
            Toast t = Toast.makeText(getApplicationContext(),
                    "Activited", Toast.LENGTH_SHORT);
            if (hypothesis == null)
                return;
    
            String text = hypothesis.getHypstr();
            if (text.equals(KEYPHRASE))
    
                switchSearch(MENU_SEARCH);
    
            else if (text.equals("APP CHALU KARU"))
    
                launchApp("in.alienart.guessmarathimovie");
            else if (text.equals("MOBILE SILENT KARU"))
    
                slientmode();
    
            else
                ((TextView) findViewById(R.id.textview2)).setText(text);
        }
    
        /**
         * This callback is called when we stop the recognizer.
         */
        @Override
        public void onResult(Hypothesis hypothesis) {
            ((TextView) findViewById(R.id.textview2)).setText("");
            if (hypothesis != null) {
                String text = hypothesis.getHypstr();
                makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        }
    
        @Override
        public void onBeginningOfSpeech() {
        }
    
        /**
         * We stop recognizer here to get a final result
         */
        @Override
        public void onEndOfSpeech() {
            if (!recognizer.getSearchName().equals(KWS_SEARCH))
                switchSearch(KWS_SEARCH);
        }
    
        private void switchSearch(String searchName) {
            recognizer.stop();
    
            // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
            if (searchName.equals(KWS_SEARCH))
                recognizer.startListening(searchName);
            else
                recognizer.startListening(searchName, 10000);
    
            String caption = getResources().getString(captions.get(searchName));
    
            ((TextView) findViewById(R.id.textview1)).setText(caption);
        }
    
        private void setupRecognizer(File assetsDir) throws IOException {
            // The recognizer can be configured to perform multiple searches
            // of different kind and switch between them
    
            recognizer = defaultSetup()
                    .setAcousticModel(new File(assetsDir, "svara"))
                    .setDictionary(new File(assetsDir, "svara.dic"))
    
                    // To disable logging of raw audio comment out this call (takes a lot of space on the device)
                    .setRawLogDir(assetsDir)
    
                    // Threshold to tune for keyphrase to balance between false alarms and misses
                    .setKeywordThreshold(1e-45f)
    
                    // Use context-independent phonetic search, context-dependent is too slow for mobile
                    .setBoolean("-allphone_ci", true)
    
                    .getRecognizer();
            recognizer.addListener(this);
    
            /** In your application you might not need to add all those searches.
             * They are added here for demonstration. You can leave just one.
             */
    
            // Create keyword-activation search.
            recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
    
            // Create grammar-based search for selection between demos
            File menuGrammar = new File(assetsDir, "menu.gram");
            recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
    
            // Create grammar-based search for digit recognition
    
    
        }
    
        @Override
        public void onError(Exception error) {
            ((TextView) findViewById(R.id.textview1)).setText(error.getMessage());
        }
    
        @Override
        public void onTimeout() {
            switchSearch(KWS_SEARCH);
        }
    }
    
     

    Last edit: Nickolay V. Shmyrev 2016-12-23
  • Pratik Sanap

    Pratik Sanap - 2016-12-18

    plz anyone i need a help. plz

     

    Last edit: Pratik Sanap 2016-12-18
    • Nickolay V. Shmyrev

      It is not clear if you get false alarm for keyphrase or for grammar recognition mode.

       
      • Pratik Sanap

        Pratik Sanap - 2017-01-22

        now i am trying keyphrase mode. but pocketsphinx detects words without saying. how can get out of this problem.?

        this is how the keyword file look like

        MOBILE /1e-3/
        KARU /1e-2/
        APP /1e-1/
        CHALU /1e-3/

         

        Last edit: Pratik Sanap 2017-01-22
  • NewUser

    NewUser - 2017-02-10

    @Pratik Sanap, were you able to resolve this issue?
    I am also facing the problem, without speaking the keywords are getting recognised from the .gram file

     
    • Pratik Sanap

      Pratik Sanap - 2017-02-11

      Not yet Bro but i am using gram file to reduce the noise. But you can use the threshold to reduse the noise effect

       
  • NewUser

    NewUser - 2017-02-13

    Hi Pratik, As of now, i am using around 4 phrases, which have 3 words each like "Oh Mighty Computer"
    and I am using the same treshold as in the sample code used for "Oh Mighty Computer" and its working fine
    I need to find a way to train for using different sentences now

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.