Menu

Listen for more words

Help
JuLoOr
2014-08-17
2014-08-19
  • JuLoOr

    JuLoOr - 2014-08-17

    Hello guys,

    i tried the ps-android demo and it works perfekt for me.
    Now I want to listen for more keywords. How can i do that?

    My present code:

    public class MainActivity extends Activity implements RecognitionListener {

    private SpeechRecognizer recognizer;
    
    private static final String KWS_SEARCH = "wakeup";
    private static final String KEYPHRASE = "test";
    TextView tv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        tv = (TextView) findViewById(R.id.tv);
    
        new AsyncTask<Void, Void, Exception>() {
            @Override
            protected Exception doInBackground(Void... params) {
                try {
                    Assets assets = new Assets(MainActivity.this);
                    File assetDir = assets.syncAssets();
    
                    setupRecognizer(assetDir);
                } catch (IOException e) {
                    return e;
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Exception result) {
                if (result != null) {
                    Log.d("Recognizer", result.toString());
                } else {
                    startSearch(KWS_SEARCH);
                }
            }
        }.execute();
    
    }
    
    private void setupRecognizer(File assetDir) {
        File modelsDir = new File(assetDir, "models");
        recognizer = defaultSetup()
                .setAcousticModel(new File(modelsDir, "hmm/en-us-semi"))
                .setDictionary(new File(modelsDir, "dict/cmu07a.dic"))
                .setRawLogDir(assetDir).setKeywordThreshold(1e-20f)
                .getRecognizer();
        recognizer.addListener(this);
    
        recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
    
    }
    
    @Override
    public void onPartialResult(Hypothesis hypothesis) {
        String text = hypothesis.getHypstr();
    
        tv.setText(text);
    
        recognizer.stop();
        recognizer.startListening(KWS_SEARCH);
    }
    
    private void startSearch(String searchName) {
        // recognizer.stop();
        recognizer.startListening(searchName);
    }
    

    Best regards Jul

     

    Last edit: JuLoOr 2014-08-17
  • Alexander Solovets

    SpeechRecognizer#addKeywordSearch(String name, File file). The file should contain keyphrases, one per line.

     
  • JuLoOr

    JuLoOr - 2014-08-18

    Nice, will try it later.
    Thanks for your good help!!
    Jul

     
  • JuLoOr

    JuLoOr - 2014-08-19

    How can I add my commands.txt file to assets/sync/.. ?

    I thought adding to models dir is enough, but i need a md5?

     
    • Nickolay V. Shmyrev

      How can I add my commands.txt file to assets/sync/.. ?

      Just put the file in the folder

      I thought adding to models dir is enough, but i need a md5?

      MD5 is created automatically with a custom builder. You need to have Java Development installed in Eclipse and make sure that custom builder is properly imported when you import the project.

      See for details

      http://cmusphinx.sourceforge.net/wiki/tutorialandroid

       
  • JuLoOr

    JuLoOr - 2014-08-19

    Does not work:

    recognizer.addKeywordSearch(KWS_SEARCH, new File(modelsDir, "commands.txt"));

    Log:
    08-19 18:46:38.577: E/cmusphinx(26396): ERROR: "kws_search.c", line 349: Failed to open keyword file '/storage/emulated/0/Android/data/com.example.keywordrecognition/files/sync/models/commands.txt': No such file or directory

    Just checked my sd-card, there is no commands.txt

     

    Last edit: JuLoOr 2014-08-19
  • JuLoOr

    JuLoOr - 2014-08-19

    Ok just found my mistake. You were right, I built the first time with the custom builder, but now it's gone. Can i add it afterwards?

     
  • JuLoOr

    JuLoOr - 2014-08-19

    Super nice !
    Thanks for helping and the new learned stuff ! :-)
    Jul

     

Log in to post a comment.