Menu

pocketsphinx on android not recognazing language model

Help
2017-02-28
2017-03-03
  • dasdas dsadas

    dasdas dsadas - 2017-02-28

    Hhello refer to my code and sphinx cant recognize even one word true makes mistakes all the time

    i have converted lm file into bin and added it to language model Ngramsearch but still lags and not true words

    regards

    package edu.cmu.pocketsphinx.demo;
    
    import android.Manifest;
    import android.app.Activity;
    import android.content.pm.PackageManager;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.os.Handler;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.view.WindowManager;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import org.w3c.dom.Text;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    
    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;
    
    import static android.widget.Toast.makeText;
    
    public class PocketSphinxActivity extends Activity implements
            RecognitionListener {
    
        /* Named searches allow to quickly reconfigure the decoder */
        private static final String KWS_SEARCH = "wakeup";
        private static final String FORECAST_SEARCH = "forecast";
        private static final String DIGITS_SEARCH = "digits";
        private static final String PHONE_SEARCH = "phones";
        private static final String MENU_SEARCH = "menu";
    
        /* Keyword we are looking for to activate menu */
        private static final String KEYPHRASE = "oh mighty computer";
    
        /* Used to handle permission request */
        private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
    
        private SpeechRecognizer recognizer;
        private HashMap<String, Integer> captions;
        TextView cro;
        @Override
        public void onCreate(Bundle state) {
            super.onCreate(state);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
            // 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);
            captions.put(DIGITS_SEARCH, R.string.digits_caption);
            captions.put(PHONE_SEARCH, R.string.phone_caption);
            captions.put(FORECAST_SEARCH, R.string.forecast_caption);
            setContentView(R.layout.main);
            ((TextView) findViewById(R.id.caption_text)).setText("Preparing....");
    
            // 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;
            }
            runRecognizerSetup();
    
    
     cro = (TextView)findViewById(R.id.cro);
            new CountDownTimer(300000000, 1000) {
    
                public void onTick(long millisUntilFinished) {
    
                    getActionBar().setTitle( String.valueOf(300000-millisUntilFinished/1000)+" seconds." );
                }
    
                public void onFinish() {
                    cro.setText("done!");
                }
            }.start();
    
        }
    
        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(PocketSphinxActivity.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.caption_text))
                                .setText("Failed to init recognizer " + result);
                    } else {
                       // switchSearch(KWS_SEARCH);
                        ((TextView) findViewById(R.id.caption_text)).setText("Listening....");
    
                        recognizer.startListening("forecast");
                    }
                }
            }.execute();
        }
    
    
        @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
            if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    runRecognizerSetup();
                } else {
                    finish();
                }
            }
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            if (recognizer != null) {
                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.
         */
        @Override
        public void onPartialResult(Hypothesis hypothesis) {
            if (hypothesis == null)
                return;
    
            String text = hypothesis.getHypstr();
    //        if (text.equals(KEYPHRASE))
    //            switchSearch(MENU_SEARCH);
    //        else if (text.equals(DIGITS_SEARCH))
    //            switchSearch(DIGITS_SEARCH);
    //        else if (text.equals(PHONE_SEARCH))
    //            switchSearch(PHONE_SEARCH);
    //        else if (text.equals(FORECAST_SEARCH))
    //            switchSearch(FORECAST_SEARCH);
    //        else
                ((TextView) findViewById(R.id.result_text)).setText(text);
        }
    
        /**
         * This callback is called when we stop the recognizer.
         */
        @Override
        public void onResult(Hypothesis hypothesis) {
            ((TextView) findViewById(R.id.result_text)).setText("");
            recognizer.cancel();
            if (hypothesis != null) {
                String text = hypothesis.getHypstr();
                ((TextView) findViewById(R.id.result_text)).setText(text);
    
                makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
                ((TextView) findViewById(R.id.result_text)).setText("");
    
                // FireRecognition();
                ((TextView) findViewById(R.id.caption_text)).setText("Listening....");
    
                recognizer.startListening("forecast");
    //Timer();
            }
        }
        public void Timer(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                       // Thread.sleep(1);
                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                recognizer.stop();
                               // recognizer.cancel();
    
    //                            ((TextView) findViewById(R.id.result_text)).setText("");
    //
    //                           // FireRecognition();
    //                            ((TextView) findViewById(R.id.caption_text)).setText("Listening....");
    //
    //                            recognizer.startListening("forecast");
    
                            }
                        });
                    } catch (Exception e) {
                    }
                }
    
            }).start();
        }
        @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);
           ((TextView) findViewById(R.id.result_text)).setText("");
    //recognizer.cancel();
    
            ////
            ((TextView) findViewById(R.id.result_text)).setText("");
    
            // FireRecognition();
            ((TextView) findViewById(R.id.caption_text)).setText("Listening....");
    
            //recognizer.startListening("forecast");
            ////
    
          //  recognizer.shutdown();
           // Timer();
        }
    
        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.caption_text)).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 = SpeechRecognizerSetup.defaultSetup()
                    .setAcousticModel(new File(assetsDir, "cmusphinx-en-us-8khz-5.2"))
                    .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
                    .setKeywordThreshold(1/100)
                    .setRawLogDir(assetsDir) // To disable logging of raw audio comment out this call (takes a lot of space on the device)
    
                    .getRecognizer();
            //File languageModel = new File(assetsDir, "model.lm.bin");
    
           // recognizer.addNgramSearch("forecast", languageModel);
            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
         //   File digitsGrammar = new File(assetsDir, "digits.gram");
            //recognizer.addGrammarSearch("digits", digitsGrammar);
    
            // Create language model search
            //File languageModel = new File(assetsDir, "weather.dmp");
    
           // recognizer.addKeyphraseSearch("forecast", languageModel);
    
    
            // Phonetic search
            //File phoneticModel = new File(assetsDir, "en-phone.dmp");
           // recognizer.addAllphoneSearch(PHONE_SEARCH, phoneticModel);
        }
    
        @Override
        public void onError(Exception error) {
            ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
        }
    
        @Override
        public void onTimeout() {
            //switchSearch(KWS_SEARCH);
    
        }
    }
    
     

    Last edit: dasdas dsadas 2017-02-28
    • Nickolay V. Shmyrev

      In your code you are using very slow acoustic model not suitable for mobile. You are also not configuring any langauge model. There is little chance your code will work.

       
      • dasdas dsadas

        dasdas dsadas - 2017-03-01

        so what is the solution? i have configured and bypassed but yes its very very slow and mistakes a lot

        how can i solve this on conituos recognization

        best regards

         
  • dasdas dsadas

    dasdas dsadas - 2017-03-01

    so what is the solution? i have configured and bypassed but yes its very very slow and mistakes a lot

    how can i solve this on conituos recognization

    best regards

     
    • Nickolay V. Shmyrev

      There is no easy solution yet. You can stream audio to a server and recognize on a server.

       
  • dasdas dsadas

    dasdas dsadas - 2017-03-02

    where is the server files

     

Log in to post a comment.