Ruchir Baronia
Ruchir Baronia

Reputation: 7561

File not found exception? (Voice recog)

Sorry for the long question, I have been stuck on this for a month, and I want to provide as much detail as possible...its just a file not found exception in a simple library... :)

I am getting a file not found exception on my variances file:

enter image description here

I do, however, have the variances file:

enter image description here

I am trying to simply implement voice recognition in my background service, so that I can detect when the user says the word hello (using pocketsphinx).

The problem happens in this method: createSphinxDir();

Here is my service:

 @Override
    public void onCreate() {
        super.onCreate();
       setupRecog();

        }
     private void setupRecog() {
    String sphinxDir = createSphinxDir();
    Log.v(TAG, "ABOUT TO CREATE SETUP");
    if (sphinxDir != null) {
        try {

            Log.v(TAG, "SETTING UP! :)");
            mSpeechRecognizer = defaultSetup()
                    .setAcousticModel(new File(sphinxDir, "en-us-ptm"))
                    .setDictionary(new File(sphinxDir, "hello.dict"))
                    .setBoolean("-allphone_ci", true) //WHAT IS THIS
                    .getRecognizer();
            mSpeechRecognizer.addListener(this);

            Log.v(TAG, "ADDED LISTENER");

            if ((new File(sphinxDir + File.separator + "command.gram")).isFile()) {
                mSpeechRecognizer.addKeywordSearch("hello",
                        new File(sphinxDir + File.separator + "command.gram"));

                Log.v(TAG, "ADDED KEYWORD SEARCH! :)");
            }

            // Or wherever appropriate
            mSpeechRecognizer.startListening("wakeup"); //Is this correct?
            Log.v(TAG, "STARTED LISTENING");

        } catch (IOException e) {

            Log.v("ERROR", TAG);

        }
    }
}


String createSphinxDir() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String sphinxDir = prefs.getString("sphinx", null);
    if (sphinxDir == null) {
        Assets assets;
        Log.v(TAG, "Assets are not synced, should sync now:");
        try {
            Log.v(TAG, "In try block!");
            assets = new Assets(this);
            File sphinxDirFile = assets.syncAssets();
            Log.v(TAG, "Syncing assets...should set up listener");
            if (sphinxDirFile != null) {
                sphinxDir = sphinxDirFile.getAbsolutePath();
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("sphinx", sphinxDir);
                editor.commit();
                Log.v(TAG, "Set up listener");

            }else{
                Log.v(TAG, "sphinxDirFile is null!");
            }
        } catch (IOException e) { //THIS IS THE PLACE WHERE I AM GETTING THE ERROR!
            e.printStackTrace();
            Log.d(TAG, e.toString());
        }
    }
    return sphinxDir;
}

I also have all the call back methods (onPartialResult, onResult, etc.) but they never get called.

Earlier I was getting an exception saying the variances .md5 file didn't exist, so I put a space in between the variances and the .md5, but now I am getting this error, and I don't know why...

Please let me know,

Ruchir

Upvotes: 0

Views: 303

Answers (1)

Nikolay Shmyrev
Nikolay Shmyrev

Reputation: 25210

Earlier I was getting an exception saying the variances .md5 file didn't exist, so I put a space in between the variances and the .md5, but now I am getting this error, and I don't know why...

You should not do such things, it causes problems, instead you need to follow the documentation:

The standard way to ship resource files with your application in Android is to put them in assets/ directory of your project. But in order to make them available for pocketsphinx files should have physical path, as long as they are within .apk they don't have one. Assets class from pocketsphinx-android provides a method to automatically copy asset files to external storage of the target device. edu.cmu.pocketsphinx.Assets#syncAssets synchronizes resources reading items from assets.lst file located on the top assets/. Before copying it matches MD5 checksums of an asset and a file on external storage with the same name if such exists. It only does actualy copying if there is incomplete information (no file on external storage, no any of two .md5 files) or there is hash mismatch. PocketSphinxAndroidDemo contains ant script that generates assets.lst as well as .md5 files, look for assets.xml.

Please note that if ant build script doesn't run properly in your build process, assets might be out of sync. Make sure that script runs during the build.

To integrate assets sync in your application do the following

Include app/asset.xml build file from the demo application into your application. Edit build.gradle build file to run assets.xml:

  ant.importBuild 'assets.xml'
  preBuild.dependsOn(list, checksum)
  clean.dependsOn(clean_assets)

Upvotes: 1

Related Questions